Skip to content

Commit

Permalink
add yandex provider
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper committed Mar 28, 2024
1 parent 5d059cb commit 6896dfa
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions .changesets/o0uyh.minor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Yandex provider.
39 changes: 39 additions & 0 deletions docs/pages/providers/yandex.md
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();
```
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export { Twitch } from "./providers/twitch.js";
export { Twitter } from "./providers/twitter.js";
export { WorkOS } from "./providers/workos.js";
export { Yahoo } from "./providers/yahoo.js";
export { Yandex } from "./providers/yandex.js";
export { Zoom } from "./providers/zoom.js";

export type {
Expand Down Expand Up @@ -75,6 +76,7 @@ export type { TwitchTokens } from "./providers/twitch.js";
export type { TwitterTokens } from "./providers/twitter.js";
export type { WorkOSTokens } from "./providers/workos.js";
export type { YahooTokens } from "./providers/yahoo.js";
export type { YandexTokens } from "./providers/yandex.js";
export type { ZoomTokens } from "./providers/zoom.js";

export { generateCodeVerifier, generateState, OAuth2RequestError } from "oslo/oauth2";
Expand Down
69 changes: 69 additions & 0 deletions src/providers/yandex.ts
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;
}

0 comments on commit 6896dfa

Please sign in to comment.