UNPKG

@adonisjs/ally

Version:

Social authentication provider for AdonisJS

137 lines (136 loc) 3.98 kB
import { Oauth2Driver } from "../../chunk-GWAQFMNS.js"; import "../../chunk-N72DEJC2.js"; import "../../chunk-PZ5AY32C.js"; // src/drivers/discord.ts var DiscordDriver = class extends Oauth2Driver { constructor(ctx, config) { super(ctx, config); this.config = config; this.loadState(); } accessTokenUrl = "https://discord.com/api/oauth2/token"; authorizeUrl = "https://discord.com/oauth2/authorize"; userInfoUrl = "https://discord.com/api/users/@me"; /** * The param name for the authorization code */ codeParamName = "code"; /** * The param name for the error */ errorParamName = "error"; /** * Cookie name for storing the "discord_oauth_state" */ stateCookieName = "discord_oauth_state"; /** * Parameter name to be used for sending and receiving the state * from Discord */ stateParamName = "state"; /** * Parameter name for defining the scopes */ scopeParamName = "scope"; /** * Scopes separator */ scopesSeparator = " "; /** * Configuring the redirect request with defaults */ configureRedirectRequest(request) { request.scopes(this.config.scopes || ["identify", "email"]); request.param("response_type", "code"); request.param("grant_type", "authorization_code"); request.param("integration_type", 1); if (this.config.prompt) { request.param("prompt", this.config.prompt); } if (this.config.guildId) { request.param("guild_id", this.config.guildId); } if (this.config.disableGuildSelect !== void 0) { request.param("disable_guild_select", this.config.disableGuildSelect); } if (this.config.permissions !== void 0) { request.param("permissions", this.config.permissions); } } /** * Configuring the access token API request to send extra fields */ configureAccessTokenRequest(request) { if (!this.isStateless) { request.field("state", this.stateCookieValue); } } /** * Returns the HTTP request with the authorization header set */ getAuthenticatedRequest(url, token) { const request = this.httpClient(url); request.header("Authorization", `Bearer ${token}`); request.header("Accept", "application/json"); request.parseAs("json"); return request; } /** * Fetches the user info from the Discord API * https://discord.com/developers/docs/resources/user#get-current-user */ async getUserInfo(token, callback) { const request = this.getAuthenticatedRequest(this.config.userInfoUrl || this.userInfoUrl, token); if (typeof callback === "function") { callback(request); } const body = await request.get(); return { id: body.id, name: `${body.username}#${body.discriminator}`, nickName: body.username, avatarUrl: body.avatar ? `https://cdn.discordapp.com/avatars/${body.id}/${body.avatar}.${body.avatar.startsWith("a_") ? "gif" : "png"}` : `https://cdn.discordapp.com/embed/avatars/${body.discriminator % 5}.png`, email: body.email, // May not always be there (requires email scope) emailVerificationState: "verified" in body ? body.verified ? "verified" : "unverified" : "unsupported", original: body }; } /** * Find if the current error code is for access denied */ accessDenied() { const error = this.getError(); if (!error) { return false; } return error === "access_denied"; } /** * Returns details for the authorized user */ async user(callback) { const token = await this.accessToken(callback); const user = await this.getUserInfo(token.token, callback); return { ...user, token }; } /** * Finds the user by the access token */ async userFromToken(token, callback) { const user = await this.getUserInfo(token, callback); return { ...user, token: { token, type: "bearer" } }; } }; export { DiscordDriver }; //# sourceMappingURL=discord.js.map