-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Hurby <[email protected]>
- Loading branch information
1 parent
b6526d2
commit 691cf1d
Showing
7 changed files
with
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Add Battle.net provider ([#241](https://github.com/pilcrowonpaper/arctic/pull/241)). |
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,65 @@ | ||
--- | ||
title: "Battle.net" | ||
--- | ||
|
||
# Battle.net | ||
|
||
OAuth 2.0 provider for Battle.net. | ||
|
||
Also see the [OAuth 2.0](/guides/oauth2) guide. | ||
|
||
## Initialization | ||
|
||
```ts | ||
import * as arctic from "arctic"; | ||
|
||
const battlenet = new arctic.BattleNet(clientId, clientSecret, redirectURI); | ||
``` | ||
|
||
## Create authorization URL | ||
|
||
```ts | ||
import * as arctic from "arctic"; | ||
|
||
const state = arctic.generateState(); | ||
const scopes = ["openid", "wow.profile"]; | ||
const url = battlenet.createAuthorizationURL(state, scopes); | ||
``` | ||
|
||
## Validate authorization code | ||
|
||
`validateAuthorizationCode()` will either return an [`OAuth2Tokens`](/reference/main/OAuth2Tokens), or throw one of [`OAuth2RequestError`](/reference/main/OAuth2RequestError), [`ArcticFetchError`](/reference/main/ArcticFetchError), [`UnexpectedResponseError`](/reference/main/UnexpectedResponseError), or [`UnexpectedErrorResponseBodyError`](/reference/main/UnexpectedErrorResponseBodyError). Battle.net returns an access token and the access token expiration. | ||
|
||
```ts | ||
import * as arctic from "arctic"; | ||
|
||
try { | ||
const tokens = await battlenet.validateAuthorizationCode(code); | ||
const accessToken = tokens.accessToken(); | ||
} catch (e) { | ||
if (e instanceof arctic.OAuth2RequestError) { | ||
// Invalid authorization code, credentials, or redirect URI | ||
const code = e.code; | ||
// ... | ||
} | ||
if (e instanceof arctic.ArcticFetchError) { | ||
// Failed to call `fetch()` | ||
const cause = e.cause; | ||
// ... | ||
} | ||
// Parse error | ||
} | ||
``` | ||
|
||
## Get user profile | ||
|
||
Use the [`User Info` endpoint](https://develop.battle.net/documentation/battle-net/oauth-apis). | ||
|
||
```ts | ||
const response = await fetch("https://oauth.battle.net/userinfo", { | ||
headers: { | ||
Authorization: `Bearer ${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
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,40 @@ | ||
import { createOAuth2Request, sendTokenRequest } from "../request.js"; | ||
|
||
import type { OAuth2Tokens } from "../oauth2.js"; | ||
|
||
const authorizationEndpoint = "https://oauth.battle.net/authorize"; | ||
const tokenEndpoint = "https://oauth.battle.net/token"; | ||
|
||
export class BattleNet { | ||
private clientId: string; | ||
private clientSecret: string; | ||
private redirectURI: string; | ||
|
||
constructor(clientId: string, clientSecret: string, redirectURI: string) { | ||
this.clientId = clientId; | ||
this.clientSecret = clientSecret; | ||
this.redirectURI = redirectURI; | ||
} | ||
|
||
public createAuthorizationURL(state: string, scopes: string[]): URL { | ||
const url = new URL(authorizationEndpoint); | ||
url.searchParams.set("response_type", "code"); | ||
url.searchParams.set("client_id", this.clientId); | ||
url.searchParams.set("state", state); | ||
url.searchParams.set("scope", scopes.join(" ")); | ||
url.searchParams.set("redirect_uri", this.redirectURI); | ||
return url; | ||
} | ||
|
||
public async validateAuthorizationCode(code: string): Promise<OAuth2Tokens> { | ||
const body = new URLSearchParams(); | ||
body.set("grant_type", "authorization_code"); | ||
body.set("code", code); | ||
body.set("redirect_uri", this.redirectURI); | ||
body.set("client_id", this.clientId); | ||
body.set("client_secret", this.clientSecret); | ||
const request = createOAuth2Request(tokenEndpoint, body); | ||
const tokens = await sendTokenRequest(request); | ||
return tokens; | ||
} | ||
} |