Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support scope as string in DiscordStrategy #100

Merged
merged 1 commit into from
Dec 20, 2021
Merged
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
53 changes: 39 additions & 14 deletions src/strategies/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,34 @@ import {
OAuth2StrategyVerifyCallback,
} from "./oauth2";


/**
* @see https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes
*/
export type DiscordScope = "activities.read" | "activities.write" | "applications.builds.read" | "applications.builds.upload" |
"applications.commands" | "applications.commands.update" | "applications.entitlements" | "applications.store.update" |
"bot" | "connections" | "email" | "gdm.join" | "guilds" | "guilds.join" | "guilds.members.read" | "identify" |
"messages.read" | "relationships.read" | "rpc" | "rpc.activities.write" | "rpc.notifications.read" |
"rpc.voice.read" | "rpc.voice.write" | "webhook.incoming";
export type DiscordScope =
| "activities.read"
| "activities.write"
| "applications.builds.read"
| "applications.builds.upload"
| "applications.commands"
| "applications.commands.update"
| "applications.entitlements"
| "applications.store.update"
| "bot"
| "connections"
| "email"
| "gdm.join"
| "guilds"
| "guilds.join"
| "guilds.members.read"
| "identify"
| "messages.read"
| "relationships.read"
| "rpc"
| "rpc.activities.write"
| "rpc.notifications.read"
| "rpc.voice.read"
| "rpc.voice.write"
| "webhook.incoming";

export interface DiscordStrategyOptions {
clientID: string;
Expand All @@ -24,7 +43,7 @@ export interface DiscordStrategyOptions {
* See all the possible scopes:
* @see https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes
*/
scope?: Array<DiscordScope>;
scope?: Array<DiscordScope> | string;
prompt?: "none" | "consent";
}

Expand Down Expand Up @@ -105,7 +124,8 @@ export interface DiscordProfile extends OAuth2Profile {
};
}

export interface DiscordExtraParams extends Record<string, Array<DiscordScope> | string | number> {
export interface DiscordExtraParams
extends Record<string, Array<DiscordScope> | string | number> {
expires_in: 604_800;
token_type: "Bearer";
scope: Array<DiscordScope>;
Expand Down Expand Up @@ -146,13 +166,17 @@ export class DiscordStrategy<User> extends OAuth2Strategy<
},
verify
);
this.scope = scope ?? ["identify", "email"];

if (!scope) this.scope = ["identify", "email"];
else if (Array.isArray(scope)) this.scope = scope;
else this.scope = scope.split(" ") as Array<DiscordScope>;

this.prompt = prompt;
}

protected authorizationParams() {
let params = new URLSearchParams({
scope: this.scope.join(' '),
scope: this.scope.join(" "),
});
if (this.prompt) params.set("prompt", this.prompt);
return params;
Expand Down Expand Up @@ -183,11 +207,12 @@ export class DiscordStrategy<User> extends OAuth2Strategy<
refreshToken: string;
extraParams: DiscordExtraParams;
}> {
let { access_token, refresh_token, scope, ...extraParams } = await response.json();
let { access_token, refresh_token, scope, ...extraParams } =
await response.json();
return {
accessToken: access_token,
refreshToken: refresh_token,
extraParams: {...extraParams, scope: scope.split(' ')},
accessToken: access_token,
refreshToken: refresh_token,
extraParams: { ...extraParams, scope: scope.split(" ") },
};
}
}