Skip to content

Commit

Permalink
Next: v3.1.0 (#243)
Browse files Browse the repository at this point in the history
Co-authored-by: Hurby <[email protected]>
  • Loading branch information
pilcrowonpaper and hurby24 authored Jan 6, 2025
1 parent b6526d2 commit 691cf1d
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 1 deletion.
1 change: 1 addition & 0 deletions .RELEASE.md
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)).
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Arctic does not strictly follow semantic versioning. While we aim to only introd
- Atlassian
- Auth0
- Authentik
- Battle.net
- Bitbucket
- Box
- Bungie
Expand Down
1 change: 1 addition & 0 deletions docs/malta.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
["Atlassian", "/providers/atlassian"],
["Auth0", "/providers/auth0"],
["Authentik", "/providers/authentik"],
["Battle.net", "/providers/battlenet"],
["Bitbucket", "/providers/bitbucket"],
["Box", "/providers/box"],
["Bungie", "/providers/bungie"],
Expand Down
65 changes: 65 additions & 0 deletions docs/pages/providers/battlenet.md
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();
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "arctic",
"type": "module",
"version": "3.0.0",
"version": "3.1.0",
"description": "OAuth 2.0 clients for popular providers",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { Apple } from "./providers/apple.js";
export { Atlassian } from "./providers/atlassian.js";
export { Auth0 } from "./providers/auth0.js";
export { Authentik } from "./providers/authentik.js";
export { BattleNet } from "./providers/battlenet.js";
export { Bitbucket } from "./providers/bitbucket.js";
export { Box } from "./providers/box.js";
export { Bungie } from "./providers/bungie.js";
Expand Down
40 changes: 40 additions & 0 deletions src/providers/battlenet.ts
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;
}
}

0 comments on commit 691cf1d

Please sign in to comment.