UNPKG

ally-auth0

Version:

An Auth0 driver for AdonisJS's Ally authentication module

158 lines (157 loc) 6.04 kB
"use strict"; /* |-------------------------------------------------------------------------- | Auth0 Ally Oauth driver |-------------------------------------------------------------------------- */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Auth0Driver = void 0; const standalone_1 = require("@adonisjs/ally/build/standalone"); /** * Driver implementation. It is mostly configuration driven except the user calls * * ------------------------------------------------ */ class Auth0Driver extends standalone_1.Oauth2Driver { constructor(ctx, config) { super(ctx, config); this.config = config; /** * The URL for the redirect request. The user will be redirected on this page * to authorize the request. * * Do not define query strings in this URL. */ this.authorizeUrl = this.config.authorizeUrl || `https://${this.config.domain}/authorize`; /** * The URL to hit to exchange the authorization code for the access token * * Do not define query strings in this URL. */ this.accessTokenUrl = this.config.accessTokenUrl || `https://${this.config.domain}/oauth/token`; /** * The URL to hit to get the user details * * Do not define query strings in this URL. */ this.userInfoUrl = this.config.userInfoUrl || `https://${this.config.domain}/userinfo`; /** * The param name for the authorization code. Read the documentation of your oauth * provider and update the param name to match the query string field name in * which the oauth provider sends the authorization_code post redirect. */ this.codeParamName = 'code'; /** * The param name for the error. Read the documentation of your oauth provider and update * the param name to match the query string field name in which the oauth provider sends * the error post redirect */ this.errorParamName = 'error'; /** * Cookie name for storing the CSRF token. Make sure it is always unique. So a better * approach is to prefix the oauth provider name to `oauth_state` value. For example: * For example: "facebook_oauth_state" */ this.stateCookieName = 'auth0_oauth_state'; /** * Parameter name to be used for sending and receiving the state from. * Read the documentation of your oauth provider and update the param * name to match the query string used by the provider for exchanging * the state. */ this.stateParamName = 'state'; /** * Parameter name for sending the scopes to the oauth provider. */ this.scopeParamName = 'scope'; /** * The separator indentifier for defining multiple scopes */ this.scopesSeparator = ' '; /** * Extremely important to call the following method to clear the * state set by the redirect request. * * DO NOT REMOVE THE FOLLOWING LINE */ this.loadState(); } /** * Optionally configure the authorization redirect request. The actual request * is made by the base implementation of "Oauth2" driver and this is a * hook to pre-configure the request. */ configureRedirectRequest(request) { request.param('response_type', 'code'); if (this.config.scopes?.length) { request.scopes(this.config.scopes); } if (this.config.audience?.length) { request.param('audience', this.config.audience); } } /** * Optionally configure the access token request. The actual request is made by * the base implementation of "Oauth2" driver and this is a hook to pre-configure * the request */ // protected configureAccessTokenRequest(request: ApiRequest) {} /** * Update the implementation to tell if the error received during redirect * means "ACCESS DENIED". * * For Auth0, see https://auth0.com/docs/api/authentication?http#standard-error-responses */ accessDenied() { return this.ctx.request.input('error') === 'access_denied'; } /** * Get the user details by querying the provider API. This method must return * the access token and the user details both. Checkout the google * implementation for same. * * https://github.com/adonisjs/ally/blob/develop/src/Drivers/Google/index.ts#L191-L199 */ async user(callback) { const token = await this.accessToken(callback); const user = await this.getUserInfo(token.token, callback); return { ...user, token: token, }; } async userFromToken(accessToken, callback) { const user = await this.getUserInfo(accessToken, callback); return { ...user, token: { token: accessToken, type: 'bearer' }, }; } /** * 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; } 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.nickname, name: body.name, email: body.email, avatarUrl: body.picture, emailVerificationState: body.email_verified ? 'verified' : 'unverified', original: body, }; } } exports.Auth0Driver = Auth0Driver;