UNPKG

@adonisjs/ally

Version:

Social authentication provider for AdonisJS

184 lines (183 loc) 4.65 kB
import { Oauth2Driver } from "../../chunk-GWAQFMNS.js"; import "../../chunk-N72DEJC2.js"; import "../../chunk-PZ5AY32C.js"; // src/drivers/google.ts var SCOPE_PREFIXES = { "https://www.googleapis.com/auth": [ "userinfo.email", "userinfo.profile", "contacts", "contacts.other.readonly", "contacts.readonly", "directory.readonly", "user.addresses.read", "user.birthday.read", "user.emails.read", "user.gender.read", "user.organization.read", "user.phonenumbers.read", "analytics", "analytics.readonly", "documents", "documents.readonly", "forms", "forms.currentonly", "groups", "spreadsheets", "calendar", "calendar.events", "calendar.events.readonly", "calendar.readonly", "calendar.settings.readonly", "drive", "drive.appdata", "drive.file", "drive.metadata", "drive.metadata.readonly", "drive.photos.readonly", "drive.readonly", "drive.scripts" ] }; var GoogleDriver = class extends Oauth2Driver { constructor(ctx, config) { super(ctx, config); this.config = config; this.loadState(); } accessTokenUrl = "https://oauth2.googleapis.com/token"; authorizeUrl = "https://accounts.google.com/o/oauth2/v2/auth"; userInfoUrl = "https://www.googleapis.com/oauth2/v3/userinfo"; /** * The param name for the authorization code */ codeParamName = "code"; /** * The param name for the error */ errorParamName = "error"; /** * Cookie name for storing the "google_oauth_state" */ stateCookieName = "google_oauth_state"; /** * Parameter name to be used for sending and receiving the state * from google */ stateParamName = "state"; /** * Parameter name for defining the scopes */ scopeParamName = "scope"; /** * Scopes separator */ scopesSeparator = " "; /** * Configuring the redirect request with defaults */ configureRedirectRequest(request) { request.transformScopes((scopes) => this.buildScopes(scopes)); request.scopes(this.config.scopes || ["openid", "userinfo.email", "userinfo.profile"]); request.param("response_type", "code"); if (this.config.accessType) { request.param("access_type", this.config.accessType); } if (this.config.prompt) { request.param("prompt", this.config.prompt); } if (this.config.display) { request.param("display", this.config.display); } if (this.config.hostedDomain) { request.param("hd", this.config.hostedDomain); } } /** * 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 Google API */ 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.sub, nickName: body.name, name: body.name, email: body.email, avatarUrl: body.picture, emailVerificationState: body.email_verified ? "verified" : "unverified", 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"; } /** * Get access token */ async accessToken(callback) { const token = await super.accessToken(callback); return { ...token, idToken: token.id_token }; } /** * 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" } }; } /** * Prefixes google scopes with the url */ buildScopes(scopes) { return scopes.map((name) => { const prefix = Object.keys(SCOPE_PREFIXES).find( (one) => SCOPE_PREFIXES[one].includes(name) ); return prefix ? `${prefix}/${name}` : name; }); } }; export { GoogleDriver }; //# sourceMappingURL=google.js.map