Skip to content

Commit 8289d85

Browse files
pilcrowonpaperhurby24m4rvrmastermakrelagithub-actions[bot]
authored
Release v3.2.2 (#263)
Co-authored-by: Hurby <[email protected]> Co-authored-by: Marvin <[email protected]> Co-authored-by: Christoph <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 8326780 commit 8289d85

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

.RELEASE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Entra ID: Add `Origin` header to requests ([#260](https://github.com/pilcrowonpaper/arctic/issues/260)).

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.2.1",
4+
"version": "3.2.2",
55
"description": "OAuth 2.0 clients for popular providers",
66
"main": "dist/index.js",
77
"types": "dist/index.d.ts",
Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { CodeChallengeMethod, OAuth2Client } from "../client.js";
1+
import { createS256CodeChallenge } from "../oauth2.js";
2+
import {
3+
createOAuth2Request,
4+
encodeBasicCredentials,
5+
joinURIAndPath,
6+
sendTokenRequest
7+
} from "../request.js";
28

39
import type { OAuth2Tokens } from "../oauth2.js";
4-
import { joinURIAndPath } from "../request.js";
510

611
export class MicrosoftEntraId {
712
private authorizationEndpoint: string;
813
private tokenEndpoint: string;
9-
10-
private client: OAuth2Client;
14+
private clientId: string;
15+
private clientSecret: string | null;
16+
private redirectURI: string;
1117

1218
constructor(tenant: string, clientId: string, clientSecret: string | null, redirectURI: string) {
1319
this.authorizationEndpoint = joinURIAndPath(
@@ -20,34 +26,67 @@ export class MicrosoftEntraId {
2026
tenant,
2127
"/oauth2/v2.0/token"
2228
);
23-
this.client = new OAuth2Client(clientId, clientSecret, redirectURI);
29+
this.clientId = clientId;
30+
this.clientSecret = clientSecret;
31+
this.redirectURI = redirectURI;
2432
}
2533

2634
public createAuthorizationURL(state: string, codeVerifier: string, scopes: string[]): URL {
27-
const url = this.client.createAuthorizationURLWithPKCE(
28-
this.authorizationEndpoint,
29-
state,
30-
CodeChallengeMethod.S256,
31-
codeVerifier,
32-
scopes
33-
);
35+
const url = new URL(this.authorizationEndpoint);
36+
url.searchParams.set("response_type", "code");
37+
url.searchParams.set("client_id", this.clientId);
38+
url.searchParams.set("redirect_uri", this.redirectURI);
39+
url.searchParams.set("state", state);
40+
const codeChallenge = createS256CodeChallenge(codeVerifier);
41+
url.searchParams.set("code_challenge_method", "S256");
42+
url.searchParams.set("code_challenge", codeChallenge);
43+
if (scopes.length > 0) {
44+
url.searchParams.set("scope", scopes.join(" "));
45+
}
3446
return url;
3547
}
3648

3749
public async validateAuthorizationCode(
3850
code: string,
3951
codeVerifier: string
4052
): Promise<OAuth2Tokens> {
41-
const tokens = await this.client.validateAuthorizationCode(
42-
this.tokenEndpoint,
43-
code,
44-
codeVerifier
45-
);
53+
const body = new URLSearchParams();
54+
body.set("grant_type", "authorization_code");
55+
body.set("code", code);
56+
body.set("redirect_uri", this.redirectURI);
57+
body.set("code_verifier", codeVerifier);
58+
if (this.clientSecret === null) {
59+
body.set("client_id", this.clientId);
60+
}
61+
const request = createOAuth2Request(this.tokenEndpoint, body);
62+
// Origin header required for public clients. Value can be anything.
63+
request.headers.set("Origin", "arctic");
64+
if (this.clientSecret !== null) {
65+
const encodedCredentials = encodeBasicCredentials(this.clientId, this.clientId);
66+
request.headers.set("Authorization", `Basic ${encodedCredentials}`);
67+
}
68+
const tokens = await sendTokenRequest(request);
4669
return tokens;
4770
}
4871

4972
public async refreshAccessToken(refreshToken: string, scopes: string[]): Promise<OAuth2Tokens> {
50-
const tokens = await this.client.refreshAccessToken(this.tokenEndpoint, refreshToken, scopes);
73+
const body = new URLSearchParams();
74+
body.set("grant_type", "refresh_token");
75+
body.set("refresh_token", refreshToken);
76+
if (this.clientSecret === null) {
77+
body.set("client_id", this.clientId);
78+
}
79+
if (scopes.length > 0) {
80+
body.set("scope", scopes.join(" "));
81+
}
82+
const request = createOAuth2Request(this.tokenEndpoint, body);
83+
// Origin header required for public clients. Value can be anything.
84+
request.headers.set("Origin", "arctic");
85+
if (this.clientSecret !== null) {
86+
const encodedCredentials = encodeBasicCredentials(this.clientId, this.clientSecret);
87+
request.headers.set("Authorization", `Basic ${encodedCredentials}`);
88+
}
89+
const tokens = await sendTokenRequest(request);
5190
return tokens;
5291
}
5392
}

0 commit comments

Comments
 (0)