-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: implement shikimori provider * docs: add docs for shikimori provider * ShikimoriTokens -> TokenResponseBody and remove export, ShikimoriTokens interface for validateAuthorizationCode * ShikimoriTokens for refreshAccessToken * update readme and docs
- Loading branch information
Showing
6 changed files
with
88 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 Shikimori 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
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,21 @@ | ||
--- | ||
title: "Shikimori" | ||
--- | ||
|
||
# Shikimori | ||
|
||
For usage, see [OAuth 2.0 provider](/guides/oauth2). | ||
|
||
```ts | ||
import { Shikimori } from "arctic"; | ||
|
||
const shikimori = new Shikimori(clientId, clientSecret, redirectURI); | ||
``` | ||
|
||
```ts | ||
import type { ShikimoriTokens } from "arctic"; | ||
|
||
const url: URL = await shikimori.createAuthorizationURL(state); | ||
const tokens: ShikimoriTokens = await shikimori.validateAuthorizationCode(code); | ||
const refreshedTokens: ShikimoriTokens = await shikimori.refreshAccessToken(refreshToken); | ||
``` |
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,62 @@ | ||
import { OAuth2Client } from "oslo/oauth2"; | ||
import type { OAuth2Provider } from "../index.js"; | ||
|
||
const authorizeEndpoint = "https://shikimori.one/oauth/authorize"; | ||
const tokenEndpoint = "https://shikimori.one/oauth/token"; | ||
|
||
export class Shikimori 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; | ||
} | ||
|
||
createAuthorizationURL(state: string, scopes?: string[]): Promise<URL> { | ||
return this.client.createAuthorizationURL({ state, scopes }); | ||
} | ||
|
||
async validateAuthorizationCode(code: string): Promise<ShikimoriTokens> { | ||
const result = await this.client.validateAuthorizationCode<TokenResponseBody>(code, { | ||
authenticateWith: "request_body", | ||
credentials: this.clientSecret | ||
}); | ||
|
||
return { | ||
accessToken: result.access_token, | ||
refreshToken: result.refresh_token, | ||
accessTokenExpiresAt: new Date((result.created_at + result.expires_in) * 1000), | ||
}; | ||
} | ||
|
||
async refreshAccessToken(refreshToken: string): Promise<ShikimoriTokens> { | ||
const result = await this.client.refreshAccessToken<TokenResponseBody>(refreshToken, { | ||
authenticateWith: "request_body", | ||
credentials: this.clientSecret | ||
}); | ||
|
||
return { | ||
accessToken: result.access_token, | ||
refreshToken: result.refresh_token, | ||
accessTokenExpiresAt: new Date((result.created_at + result.expires_in) * 1000), | ||
}; | ||
} | ||
} | ||
|
||
interface TokenResponseBody { | ||
access_token: string; | ||
token_type: string; | ||
expires_in: number; | ||
refresh_token: string; | ||
scope: string; | ||
created_at: number; | ||
} | ||
|
||
export interface ShikimoriTokens { | ||
accessToken: string; | ||
refreshToken: string; | ||
accessTokenExpiresAt: Date; | ||
} |