UNPKG

@adonisjs/ally

Version:

Social authentication provider for AdonisJS

173 lines (172 loc) 4.9 kB
import { Oauth2Driver } from "../../chunk-GWAQFMNS.js"; import "../../chunk-N72DEJC2.js"; import "../../chunk-PZ5AY32C.js"; // src/drivers/github.ts var GithubDriver = class extends Oauth2Driver { constructor(ctx, config) { super(ctx, config); this.config = config; this.loadState(); } accessTokenUrl = "https://github.com/login/oauth/access_token"; authorizeUrl = "https://github.com/login/oauth/authorize"; userInfoUrl = "https://api.github.com/user"; userEmailUrl = "https://api.github.com/user/emails"; /** * The param name for the authorization code */ codeParamName = "code"; /** * The param name for the error */ errorParamName = "error"; /** * Cookie name for storing the "gh_oauth_state" */ stateCookieName = "gh_oauth_state"; /** * Parameter name to be used for sending and receiving the state * from Github */ 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 || ["user"]); if (this.config.allowSignup !== void 0) { request.param("allow_signup", this.config.allowSignup); } if (this.config.login) { request.param("login", this.config.login); } } /** * Configuring the access token API request to send extra fields */ configureAccessTokenRequest(request) { if (!this.isStateless) { request.field("state", this.stateCookieValue); } request.clearField("grant_type"); } /** * Returns the HTTP request with the authorization header set */ getAuthenticatedRequest(url, token) { const request = this.httpClient(url); request.header("Authorization", `token ${token}`); request.header("Accept", "application/json"); request.parseAs("json"); return request; } /** * Fetches the user info from the Github API * https://docs.github.com/en/rest/reference/users#get-the-authenticated-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, nickName: body.name, email: body.email, // May not always be there emailVerificationState: body.email ? "verified" : "unsupported", name: body.name ?? body.login, avatarUrl: body.avatar_url, original: body }; } /** * Fetches the user email from the Github API. * https://docs.github.com/en/rest/reference/users#list-email-addresses-for-the-authenticated-user */ async getUserEmail(token, callback) { const request = this.getAuthenticatedRequest( this.config.userEmailUrl || this.userEmailUrl, token ); if (typeof callback === "function") { callback(request); } try { let emails = await request.get(); emails = emails.sort((email) => email.primary ? -1 : 1); let mainEmail = emails.find((email) => email.verified); if (!mainEmail) { mainEmail = emails[0]; } return mainEmail; } catch (error) { if (error && error.response && error.response.statusCode === 404) { return; } throw error; } } /** * 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); if (!user.email) { this.ctx.logger.trace("Fetching github user email separately"); const emailResponse = await this.getUserEmail(token.token, callback); if (emailResponse) { user.email = emailResponse.email; user.emailVerificationState = emailResponse.verified ? "verified" : "unverified"; } } return { ...user, token }; } /** * Finds the user by the access token */ async userFromToken(token, callback) { const user = await this.getUserInfo(token, callback); if (!user.email) { this.ctx.logger.trace("Fetching github user email separately"); const emailResponse = await this.getUserEmail(token, callback); if (emailResponse) { user.email = emailResponse.email; user.emailVerificationState = emailResponse.verified ? "verified" : "unverified"; } } return { ...user, token: { token, type: "bearer" } }; } }; export { GithubDriver }; //# sourceMappingURL=github.js.map