Skip to content

Commit 691cf1d

Browse files
Next: v3.1.0 (#243)
Co-authored-by: Hurby <[email protected]>
1 parent b6526d2 commit 691cf1d

File tree

7 files changed

+110
-1
lines changed

7 files changed

+110
-1
lines changed

.RELEASE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add Battle.net provider ([#241](https://github.com/pilcrowonpaper/arctic/pull/241)).

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Arctic does not strictly follow semantic versioning. While we aim to only introd
3838
- Atlassian
3939
- Auth0
4040
- Authentik
41+
- Battle.net
4142
- Bitbucket
4243
- Box
4344
- Bungie

docs/malta.config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
["Atlassian", "/providers/atlassian"],
2525
["Auth0", "/providers/auth0"],
2626
["Authentik", "/providers/authentik"],
27+
["Battle.net", "/providers/battlenet"],
2728
["Bitbucket", "/providers/bitbucket"],
2829
["Box", "/providers/box"],
2930
["Bungie", "/providers/bungie"],

docs/pages/providers/battlenet.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: "Battle.net"
3+
---
4+
5+
# Battle.net
6+
7+
OAuth 2.0 provider for Battle.net.
8+
9+
Also see the [OAuth 2.0](/guides/oauth2) guide.
10+
11+
## Initialization
12+
13+
```ts
14+
import * as arctic from "arctic";
15+
16+
const battlenet = new arctic.BattleNet(clientId, clientSecret, redirectURI);
17+
```
18+
19+
## Create authorization URL
20+
21+
```ts
22+
import * as arctic from "arctic";
23+
24+
const state = arctic.generateState();
25+
const scopes = ["openid", "wow.profile"];
26+
const url = battlenet.createAuthorizationURL(state, scopes);
27+
```
28+
29+
## Validate authorization code
30+
31+
`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.
32+
33+
```ts
34+
import * as arctic from "arctic";
35+
36+
try {
37+
const tokens = await battlenet.validateAuthorizationCode(code);
38+
const accessToken = tokens.accessToken();
39+
} catch (e) {
40+
if (e instanceof arctic.OAuth2RequestError) {
41+
// Invalid authorization code, credentials, or redirect URI
42+
const code = e.code;
43+
// ...
44+
}
45+
if (e instanceof arctic.ArcticFetchError) {
46+
// Failed to call `fetch()`
47+
const cause = e.cause;
48+
// ...
49+
}
50+
// Parse error
51+
}
52+
```
53+
54+
## Get user profile
55+
56+
Use the [`User Info` endpoint](https://develop.battle.net/documentation/battle-net/oauth-apis).
57+
58+
```ts
59+
const response = await fetch("https://oauth.battle.net/userinfo", {
60+
headers: {
61+
Authorization: `Bearer ${accessToken}`
62+
}
63+
});
64+
const user = await response.json();
65+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "arctic",
33
"type": "module",
4-
"version": "3.0.0",
4+
"version": "3.1.0",
55
"description": "OAuth 2.0 clients for popular providers",
66
"main": "dist/index.js",
77
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export { Apple } from "./providers/apple.js";
44
export { Atlassian } from "./providers/atlassian.js";
55
export { Auth0 } from "./providers/auth0.js";
66
export { Authentik } from "./providers/authentik.js";
7+
export { BattleNet } from "./providers/battlenet.js";
78
export { Bitbucket } from "./providers/bitbucket.js";
89
export { Box } from "./providers/box.js";
910
export { Bungie } from "./providers/bungie.js";

src/providers/battlenet.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { createOAuth2Request, sendTokenRequest } from "../request.js";
2+
3+
import type { OAuth2Tokens } from "../oauth2.js";
4+
5+
const authorizationEndpoint = "https://oauth.battle.net/authorize";
6+
const tokenEndpoint = "https://oauth.battle.net/token";
7+
8+
export class BattleNet {
9+
private clientId: string;
10+
private clientSecret: string;
11+
private redirectURI: string;
12+
13+
constructor(clientId: string, clientSecret: string, redirectURI: string) {
14+
this.clientId = clientId;
15+
this.clientSecret = clientSecret;
16+
this.redirectURI = redirectURI;
17+
}
18+
19+
public createAuthorizationURL(state: string, scopes: string[]): URL {
20+
const url = new URL(authorizationEndpoint);
21+
url.searchParams.set("response_type", "code");
22+
url.searchParams.set("client_id", this.clientId);
23+
url.searchParams.set("state", state);
24+
url.searchParams.set("scope", scopes.join(" "));
25+
url.searchParams.set("redirect_uri", this.redirectURI);
26+
return url;
27+
}
28+
29+
public async validateAuthorizationCode(code: string): Promise<OAuth2Tokens> {
30+
const body = new URLSearchParams();
31+
body.set("grant_type", "authorization_code");
32+
body.set("code", code);
33+
body.set("redirect_uri", this.redirectURI);
34+
body.set("client_id", this.clientId);
35+
body.set("client_secret", this.clientSecret);
36+
const request = createOAuth2Request(tokenEndpoint, body);
37+
const tokens = await sendTokenRequest(request);
38+
return tokens;
39+
}
40+
}

0 commit comments

Comments
 (0)