Skip to content

Commit

Permalink
Add VK provider (#86)
Browse files Browse the repository at this point in the history
* add vk provider

* fix exports
  • Loading branch information
pilcrowonpaper authored Mar 28, 2024
1 parent d4090cb commit 383aa2d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions .changesets/hu3zn.minor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add VK provider.
1 change: 1 addition & 0 deletions docs/malta.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
["Tumblr", "/providers/tumblr"],
["Twitch", "/providers/twitch"],
["Twitter", "/providers/twitter"],
["VK", "/providers/vk"],
["WorkOS", "/providers/workos"],
["Yahoo", "/providers/yahoo"],
["Yandex", "/providers/yandex"],
Expand Down
35 changes: 35 additions & 0 deletions docs/pages/providers/vk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: "VK"
---

# VK

For usage, see [OAuth 2.0 provider](/guides/oauth2).

```ts
import { VK } from "arctic";

const vk = new VK(clientId, clientSecret, redirectURI);
```

```ts
const url: URL = await vk.createAuthorizationURL(state, {
// optional
scopes
});
const tokens: VKTokens = await vk.validateAuthorizationCode(code);
```

## Get user profile

Use the [`users.get` endpoint`](https://dev.vk.com/en/method/users.get).

```ts
const tokens = await vk.validateAuthorizationCode(code);
const response = await fetch("https://api.vk.com/method/users.get", {
headers: {
Authorization: `Bearer ${tokens.accessToken}`
}
});
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 @@ -32,6 +32,7 @@ export { Strava } from "./providers/strava.js";
export { Tumblr } from "./providers/tumblr.js";
export { Twitch } from "./providers/twitch.js";
export { Twitter } from "./providers/twitter.js";
export { VK } from "./providers/vk.js";
export { WorkOS } from "./providers/workos.js";
export { Yahoo } from "./providers/yahoo.js";
export { Yandex } from "./providers/yandex.js";
Expand Down Expand Up @@ -74,6 +75,7 @@ export type { StravaTokens } from "./providers/strava.js";
export type { TumblrTokens } from "./providers/tumblr.js";
export type { TwitchTokens } from "./providers/twitch.js";
export type { TwitterTokens } from "./providers/twitter.js";
export type { VKTokens } from "./providers/vk.js";
export type { WorkOSTokens } from "./providers/workos.js";
export type { YahooTokens } from "./providers/yahoo.js";
export type { YandexTokens } from "./providers/yandex.js";
Expand Down
59 changes: 59 additions & 0 deletions src/providers/vk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { OAuth2Client } from "oslo/oauth2";
import { TimeSpan, createDate } from "oslo";

import type { OAuth2Provider } from "../index.js";

const authorizeEndpoint = "https://oauth.vk.com/authorize";
const tokenEndpoint = "https://oauth.vk.com/access_token";

export class VK implements OAuth2Provider {
private client: OAuth2Client;
private clientSecret: string;

constructor(clientId: string, clientSecret: string, redirectURI: string) {
this.client = new OAuth2Client(clientId, authorizeEndpoint, tokenEndpoint, {
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<VKTokens> {
const result = await this.client.validateAuthorizationCode<TokenResponseBody>(code, {
authenticateWith: "request_body",
credentials: this.clientSecret
});
const tokens: VKTokens = {
accessToken: result.access_token,
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")),
userId: result.user_id,
email: result.email ?? null
};
return tokens;
}
}

interface TokenResponseBody {
access_token: string;
expires_in: number;
user_id: string;
email?: string;
}

export interface VKTokens {
accessToken: string;
accessTokenExpiresAt: Date;
userId: string;
email: string | null;
}

0 comments on commit 383aa2d

Please sign in to comment.