UNPKG

@adonisjs/ally

Version:

Social authentication provider for AdonisJS

115 lines (114 loc) 3.14 kB
import { Oauth2Driver } from "../../chunk-GWAQFMNS.js"; import "../../chunk-N72DEJC2.js"; import "../../chunk-PZ5AY32C.js"; // src/drivers/linked_in_openid_connect.ts var LinkedInOpenidConnectDriver = class extends Oauth2Driver { constructor(ctx, config) { super(ctx, config); this.config = config; this.loadState(); } authorizeUrl = "https://www.linkedin.com/oauth/v2/authorization"; accessTokenUrl = "https://www.linkedin.com/oauth/v2/accessToken"; userInfoUrl = "https://api.linkedin.com/v2/userinfo"; /** * The param name for the authorization code */ codeParamName = "code"; /** * The param name for the error */ errorParamName = "error"; /** * Cookie name for storing the "linkedin_openid_connect_oauth_state" */ stateCookieName = "linkedin_openid_connect_oauth_state"; /** * Parameter name to be used for sending and receiving the state * from linkedin */ 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 || ["openid", "profile", "email"]); request.param("response_type", "code"); } /** * 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 LinkedIn API */ async getUserInfo(token, callback) { let url = this.config.userInfoUrl || this.userInfoUrl; const request = this.getAuthenticatedRequest(url, token); if (typeof callback === "function") { callback(request); } const body = await request.get(); const emailVerificationState = body.email_verified ? "verified" : "unverified"; return { id: body.sub, nickName: body.given_name, name: body.family_name, avatarUrl: body.picture, email: body.email, emailVerificationState, original: body }; } /** * Find if the current error code is for access denied */ accessDenied() { const error = this.getError(); if (!error) { return false; } return error === "user_cancelled_login" || error === "user_cancelled_authorize"; } /** * Returns details for the authorized user */ async user(callback) { const accessToken = await this.accessToken(callback); const userInfo = await this.getUserInfo(accessToken.token, callback); return { ...userInfo, token: { ...accessToken } }; } /** * 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 { LinkedInOpenidConnectDriver }; //# sourceMappingURL=linked_in_openid_connect.js.map