-
-
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.
Showing
5 changed files
with
98 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 VK 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
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,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(); | ||
``` |
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,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; | ||
} |