-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d059cb
commit 6896dfa
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add Yandex provider. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: "Yandex" | ||
--- | ||
|
||
# Yandex | ||
|
||
For usage, see [OAuth 2.0 provider](/guides/oauth2). | ||
|
||
```ts | ||
import { Yandex } from "arctic"; | ||
|
||
const yandex = new Yandex(clientId, clientSecret, { | ||
// optional | ||
redirectURI | ||
}); | ||
``` | ||
|
||
```ts | ||
const url: URL = await yandex.createAuthorizationURL(state, { | ||
// optional | ||
scopes | ||
}); | ||
const tokens: YandexTokens = await yandex.validateAuthorizationCode(code); | ||
const tokens: YandexTokens = await yandex.refreshAccessToken(refreshToken); | ||
``` | ||
|
||
## Get user profile | ||
|
||
Use the [`/myself` endpoint](https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-the-authenticated-user). | ||
|
||
```ts | ||
const response = await fetch("https://api.tracker.yandex.net/v2/myself", { | ||
headers: { | ||
Authorization: `OAuth ${tokens.accessToken}`, | ||
"X-Org-ID": ORGANIZATION_ID | ||
} | ||
}); | ||
const user = await response.json(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { OAuth2Client } from "oslo/oauth2"; | ||
import { TimeSpan, createDate } from "oslo"; | ||
|
||
import type { OAuth2Provider } from "../index.js"; | ||
|
||
const authorizeEndpoint = "https://oauth.yandex.com/authorize"; | ||
const tokenEndpoint = "https://oauth.yandex.com/token"; | ||
|
||
export class Yandex implements OAuth2Provider { | ||
private client: OAuth2Client; | ||
private clientSecret: string; | ||
|
||
constructor(clientId: string, clientSecret: string, options?: { | ||
redirectURI: string | ||
}) { | ||
this.client = new OAuth2Client(clientId, authorizeEndpoint, tokenEndpoint, { | ||
redirectURI: options?.redirectURI | ||
}); | ||
this.clientSecret = clientSecret; | ||
} | ||
|
||
public async createAuthorizationURL( | ||
state: string, | ||
options?: { | ||
scopes?: string[]; | ||
} | ||
): Promise<URL> { | ||
return await this.client.createAuthorizationURL({ | ||
state, | ||
scopes: options?.scopes ?? [], | ||
}); | ||
} | ||
|
||
public async validateAuthorizationCode(code: string): Promise<YandexTokens> { | ||
const result = await this.client.validateAuthorizationCode<TokenResponseBody>(code, { | ||
credentials: this.clientSecret | ||
}); | ||
const tokens: YandexTokens = { | ||
accessToken: result.access_token, | ||
refreshToken: result.refresh_token, | ||
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")) | ||
}; | ||
return tokens; | ||
} | ||
|
||
public async refreshAccessToken(refreshToken: string): Promise<YandexTokens> { | ||
const result = await this.client.refreshAccessToken<TokenResponseBody>(refreshToken, { | ||
credentials: this.clientSecret | ||
}); | ||
const tokens: YandexTokens = { | ||
accessToken: result.access_token, | ||
refreshToken: result.refresh_token, | ||
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")) | ||
}; | ||
return tokens; | ||
} | ||
} | ||
|
||
interface TokenResponseBody { | ||
access_token: string; | ||
expires_in: number; | ||
refresh_token: string; | ||
} | ||
|
||
export interface YandexTokens { | ||
accessToken: string; | ||
refreshToken: string; | ||
accessTokenExpiresAt: Date; | ||
} |