Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ const oauth2Client = new OAuth2Client({
authorizationEndpointUri: "https://github.com/login/oauth/authorize",
tokenUri: "https://github.com/login/oauth/access_token",
redirectUri: "http://localhost:8000/oauth2/callback",
defaults: {
scope: "read:user",
},
scope: "read:user",
});

type AppState = {
Expand Down
4 changes: 1 addition & 3 deletions examples/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ const oauth2Client = new OAuth2Client({
authorizationEndpointUri: "https://github.com/login/oauth/authorize",
tokenUri: "https://github.com/login/oauth/access_token",
redirectUri: "http://localhost:8000/oauth2/callback",
defaults: {
scope: "read:user",
},
scope: "read:user",
});

/** This is where we'll store our state and PKCE codeVerifiers */
Expand Down
4 changes: 1 addition & 3 deletions examples/oak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ const oauth2Client = new OAuth2Client({
authorizationEndpointUri: "https://github.com/login/oauth/authorize",
tokenUri: "https://github.com/login/oauth/access_token",
redirectUri: "http://localhost:8000/oauth2/callback",
defaults: {
scope: "read:user",
},
scope: "read:user",
});

type AppState = {
Expand Down
4 changes: 2 additions & 2 deletions src/authorization_code_grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class AuthorizationCodeGrant extends OAuth2GrantBase {
if (typeof this.client.config.redirectUri === "string") {
params.set("redirect_uri", this.client.config.redirectUri);
}
const scope = options.scope ?? this.client.config.defaults?.scope;
const scope = options.scope ?? this.client.config.scope;
if (scope) {
params.set("scope", Array.isArray(scope) ? scope.join(" ") : scope);
}
Expand Down Expand Up @@ -191,7 +191,7 @@ export class AuthorizationCodeGrant extends OAuth2GrantBase {
const state = params.get("state");
const stateValidator = options.stateValidator ||
(options.state && ((s) => s === options.state)) ||
this.client.config.defaults?.stateValidator;
this.client.config.stateValidator;

if (stateValidator && !await stateValidator(state)) {
if (state === null) {
Expand Down
76 changes: 33 additions & 43 deletions src/authorization_code_grant_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Deno.test("AuthorizationCodeGrant.getAuthorizationUri works with redirectUri and

Deno.test("AuthorizationCodeGrant.getAuthorizationUri uses default scopes if no scope was specified", async () => {
const { uri, codeVerifier } = await getOAuth2Client({
defaults: { scope: ["default", "scopes"] },
scope: ["default", "scopes"],
}).code.getAuthorizationUri();

const codeChallenge = uri.searchParams.get("code_challenge");
Expand All @@ -164,7 +164,7 @@ Deno.test("AuthorizationCodeGrant.getAuthorizationUri uses default scopes if no

Deno.test("AuthorizationCodeGrant.getAuthorizationUri uses specified scopes over default scopes", async () => {
const { uri, codeVerifier } = await getOAuth2Client({
defaults: { scope: ["default", "scopes"] },
scope: ["default", "scopes"],
}).code.getAuthorizationUri({
scope: "notDefault",
});
Expand Down Expand Up @@ -257,21 +257,19 @@ Deno.test("AuthorizationCodeGrant.getAuthorizationUri works with redirectUri and

Deno.test("AuthorizationCodeGrant.getAuthorizationUri uses default scopes if no scope was specified with PKCE disabled", async () => {
assertMatchesUrl(
(await getOAuth2Client({
defaults: { scope: ["default", "scopes"] },
}).code.getAuthorizationUri({ disablePkce: true })).uri,
(await getOAuth2Client({ scope: ["default", "scopes"] }).code
.getAuthorizationUri({ disablePkce: true })).uri,
"https://auth.server/auth?response_type=code&client_id=clientId&scope=default+scopes",
);
});

Deno.test("AuthorizationCodeGrant.getAuthorizationUri uses specified scopes over default scopes with PKCE disabled", async () => {
assertMatchesUrl(
(await getOAuth2Client({
defaults: { scope: ["default", "scopes"] },
}).code.getAuthorizationUri({
scope: "notDefault",
disablePkce: true,
})).uri,
(await getOAuth2Client({ scope: ["default", "scopes"] }).code
.getAuthorizationUri({
scope: "notDefault",
disablePkce: true,
})).uri,
"https://auth.server/auth?response_type=code&client_id=clientId&scope=notDefault",
);
});
Expand Down Expand Up @@ -756,15 +754,13 @@ Deno.test("AuthorizationCodeGrant.getToken uses the default request options", as
const { request } = await mockATResponse(
() =>
getOAuth2Client({
defaults: {
requestOptions: {
headers: {
"User-Agent": "Custom User Agent",
"Content-Type": "application/json",
},
urlParams: { "custom-url-param": "value" },
body: { "custom-body-param": "value" },
requestOptions: {
headers: {
"User-Agent": "Custom User Agent",
"Content-Type": "application/json",
},
urlParams: { "custom-url-param": "value" },
body: { "custom-body-param": "value" },
},
}).code.getToken(buildAccessTokenCallback({
params: { code: "authCode" },
Expand All @@ -781,15 +777,13 @@ Deno.test("AuthorizationCodeGrant.getToken uses the passed request options over
const { request } = await mockATResponse(
() =>
getOAuth2Client({
defaults: {
requestOptions: {
headers: {
"User-Agent": "Custom User Agent",
"Content-Type": "application/json",
},
urlParams: { "custom-url-param": "value" },
body: { "custom-body-param": "value" },
requestOptions: {
headers: {
"User-Agent": "Custom User Agent",
"Content-Type": "application/json",
},
urlParams: { "custom-url-param": "value" },
body: { "custom-body-param": "value" },
},
}).code.getToken(
buildAccessTokenCallback({
Expand Down Expand Up @@ -819,11 +813,11 @@ Deno.test("AuthorizationCodeGrant.getToken uses the default state validator if n

await mockATResponse(
() =>
getOAuth2Client({
defaults: { stateValidator: defaultValidator },
}).code.getToken(buildAccessTokenCallback({
params: { code: "authCode", state: "some_state" },
})),
getOAuth2Client({ stateValidator: defaultValidator }).code.getToken(
buildAccessTokenCallback({
params: { code: "authCode", state: "some_state" },
}),
),
);

assertSpyCall(defaultValidator, 0, { args: ["some_state"], returned: true });
Expand All @@ -835,11 +829,11 @@ Deno.test("AuthorizationCodeGrant.getToken supports async default state validato

await mockATResponse(
() =>
getOAuth2Client({
defaults: { stateValidator: defaultValidator },
}).code.getToken(buildAccessTokenCallback({
params: { code: "authCode", state: "some_state" },
})),
getOAuth2Client({ stateValidator: defaultValidator }).code.getToken(
buildAccessTokenCallback({
params: { code: "authCode", state: "some_state" },
}),
),
);

assertSpyCallAsync(defaultValidator, 0, {
Expand All @@ -855,9 +849,7 @@ Deno.test("AuthorizationCodeGrant.getToken uses the passed state validator over

await mockATResponse(
() =>
getOAuth2Client({
defaults: { stateValidator: defaultValidator },
}).code.getToken(
getOAuth2Client({ stateValidator: defaultValidator }).code.getToken(
buildAccessTokenCallback({
params: { code: "authCode", state: "some_state" },
}),
Expand All @@ -876,9 +868,7 @@ Deno.test("AuthorizationCodeGrant.getToken uses the passed state validator over

await mockATResponse(
() =>
getOAuth2Client({
defaults: { stateValidator: defaultValidator },
}).code.getToken(
getOAuth2Client({ stateValidator: defaultValidator }).code.getToken(
buildAccessTokenCallback({
params: { code: "authCode", state: "some_state" },
}),
Expand Down
2 changes: 1 addition & 1 deletion src/client_credentials_grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class ClientCredentialsGrant extends OAuth2GrantBase {
"Authorization": `Basic ${btoa(`${clientId}:${clientSecret}`)}`,
};

const scope = options.scope ?? this.client.config.defaults?.scope;
const scope = options.scope ?? this.client.config.scope;
if (scope) {
if (Array.isArray(scope)) {
body.scope = scope.join(" ");
Expand Down
33 changes: 15 additions & 18 deletions src/client_credentials_grant_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ Deno.test("ClientCredentialsGrant.getToken includes default scopes in the token
() =>
getOAuth2Client({
clientSecret: "secret",
defaults: { scope: ["default", "scopes"] },
scope: ["default", "scopes"],
}).clientCredentials.getToken(),
);

Expand All @@ -319,7 +319,7 @@ Deno.test("ClientCredentialsGrant.getToken does not include default scopes in th
() =>
getOAuth2Client({
clientSecret: "secret",
defaults: { scope: ["default", "scopes"] },
scope: ["default", "scopes"],
}).clientCredentials.getToken({ scope: "notDefault" }),
);

Expand All @@ -344,15 +344,14 @@ Deno.test("ClientCredentialsGrant.getToken uses the default request options", as
() =>
getOAuth2Client({
clientSecret: "secret",
defaults: {
requestOptions: {
headers: {
"User-Agent": "Custom User Agent",
"Content-Type": "application/json",
},
urlParams: { "custom-url-param": "value" },
body: { "custom-body-param": "value" },

requestOptions: {
headers: {
"User-Agent": "Custom User Agent",
"Content-Type": "application/json",
},
urlParams: { "custom-url-param": "value" },
body: { "custom-body-param": "value" },
},
}).clientCredentials.getToken(),
);
Expand All @@ -368,15 +367,13 @@ Deno.test("ClientCredentialsGrant.getToken uses the passed request options over
() =>
getOAuth2Client({
clientSecret: "secret",
defaults: {
requestOptions: {
headers: {
"User-Agent": "Custom User Agent",
"Content-Type": "application/json",
},
urlParams: { "custom-url-param": "value" },
body: { "custom-body-param": "value" },
requestOptions: {
headers: {
"User-Agent": "Custom User Agent",
"Content-Type": "application/json",
},
urlParams: { "custom-url-param": "value" },
body: { "custom-body-param": "value" },
},
}).clientCredentials.getToken({
requestOptions: {
Expand Down
4 changes: 2 additions & 2 deletions src/grant_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export abstract class OAuth2GrantBase {
): Request {
const url = this.toUrl(baseUrl);

const clientDefaults = this.client.config.defaults?.requestOptions;
const clientDefaults = this.client.config.requestOptions;

const urlParams: Record<string, string> = {
...(clientDefaults?.urlParams),
Expand Down Expand Up @@ -61,7 +61,7 @@ export abstract class OAuth2GrantBase {
}

protected toUrl(url: string | URL): URL {
if (typeof (url) === "string") {
if (typeof url === "string") {
return new URL(url, "http://example.com");
}
return url;
Expand Down
32 changes: 15 additions & 17 deletions src/grant_base_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,21 @@ Deno.test("OAuth2GrantBase.buildRequest works without optional parameters", asyn

Deno.test("OAuth2GrantBase.buildRequest works with overrideOptions set", async () => {
const req = getGrantBase({
defaults: {
requestOptions: {
body: {
default1: "default",
default2: "default",
default3: "default",
},
headers: {
"default-header1": "default",
"default-header2": "default",
"default-header3": "default",
},
urlParams: {
"defaultParam1": "default",
"defaultParam2": "default",
"defaultParam3": "default",
},
requestOptions: {
body: {
default1: "default",
default2: "default",
default3: "default",
},
headers: {
"default-header1": "default",
"default-header2": "default",
"default-header3": "default",
},
urlParams: {
"defaultParam1": "default",
"defaultParam2": "default",
"defaultParam3": "default",
},
},
}).buildRequest("https://auth.server/req", {
Expand Down
4 changes: 2 additions & 2 deletions src/implicit_grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class ImplicitGrant extends OAuth2GrantBase {
if (typeof this.client.config.redirectUri === "string") {
params.set("redirect_uri", this.client.config.redirectUri);
}
const scope = options.scope ?? this.client.config.defaults?.scope;
const scope = options.scope ?? this.client.config.scope;
if (scope) {
params.set("scope", Array.isArray(scope) ? scope.join(" ") : scope);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ export class ImplicitGrant extends OAuth2GrantBase {
const state = params.get("state");
const stateValidator = options.stateValidator ||
(options.state && ((s) => s === options.state)) ||
this.client.config.defaults?.stateValidator;
this.client.config.stateValidator;

const tokens: Tokens = {
accessToken,
Expand Down
Loading