-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from pilcrowOnPaper/osu-provider
Add osu! provider
- Loading branch information
Showing
6 changed files
with
125 additions
and
1 deletion.
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
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,45 @@ | ||
--- | ||
title: "osu!" | ||
--- | ||
|
||
# osu! | ||
|
||
For usage, see [OAuth 2.0 provider](/guides/oauth2). | ||
|
||
```ts | ||
import { Osu } from "arctic"; | ||
|
||
const osu = new Osu(clientId, clientSecret, { | ||
// optional | ||
redirectURI // required when multiple redirect URIs are defined | ||
}); | ||
``` | ||
|
||
```ts | ||
const url: URL = await osu.createAuthorizationURL(state, { | ||
// optional | ||
scopes | ||
}); | ||
const tokens: OsuTokens = await osu.validateAuthorizationCode(code); | ||
const tokens: OsuTokens = await osu.refreshAccessToken(refreshToken); | ||
``` | ||
|
||
## Get user profile | ||
|
||
Use the [`/me` endpoint`](https://osu.ppy.sh/docs/index.html#get-own-data). | ||
|
||
```ts | ||
const url = await osu.createAuthorizationURL(state, { | ||
scopes: ["identify"] // implicitly granted by osu! | ||
}); | ||
``` | ||
|
||
```ts | ||
const tokens = await osu.validateAuthorizationCode(code); | ||
const response = await fetch("https://osu.ppy.sh/api/v2/me", { | ||
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,75 @@ | ||
import { OAuth2Client } from "oslo/oauth2"; | ||
import { TimeSpan, createDate } from "oslo"; | ||
|
||
import type { OAuth2Provider } from "../index.js"; | ||
|
||
const authorizeEndpoint = "https://osu.ppy.sh/oauth/authorize"; | ||
const tokenEndpoint = "https://osu.ppy.sh/oauth/token"; | ||
|
||
export class Osu 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<OsuTokens> { | ||
const result = await this.client.validateAuthorizationCode<TokenResponseBody>(code, { | ||
authenticateWith: "request_body", | ||
credentials: this.clientSecret | ||
}); | ||
const tokens: OsuTokens = { | ||
accessToken: result.access_token, | ||
refreshToken: result.refresh_token, | ||
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")) | ||
}; | ||
return tokens; | ||
} | ||
|
||
public async refreshAccessToken(refreshToken: string): Promise<OsuTokens> { | ||
const result = await this.client.refreshAccessToken<TokenResponseBody>(refreshToken, { | ||
authenticateWith: "request_body", | ||
credentials: this.clientSecret | ||
}); | ||
const tokens: OsuTokens = { | ||
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 OsuTokens { | ||
accessToken: string; | ||
refreshToken: string; | ||
accessTokenExpiresAt: Date; | ||
} |