|
| 1 | +import { Oauth2Driver } from '../abstract_drivers/oauth2.js' |
| 2 | +import type { HttpContext } from '@adonisjs/core/http' |
| 3 | +import type { |
| 4 | + ApiRequestContract, |
| 5 | + LinkedInOpenidConnectAccessToken, |
| 6 | + LinkedInOpenidConnectDriverConfig, |
| 7 | + LinkedInOpenidConnectScopes, |
| 8 | + RedirectRequestContract, |
| 9 | +} from '@adonisjs/ally/types' |
| 10 | +import type { HttpClient } from '@poppinss/oauth-client' |
| 11 | + |
| 12 | +/** |
| 13 | + * LinkedIn openid connect driver to login user via LinkedIn using openid connect requirements |
| 14 | + */ |
| 15 | +export class LinkedInOpenidConnectDriver extends Oauth2Driver< |
| 16 | + LinkedInOpenidConnectAccessToken, |
| 17 | + LinkedInOpenidConnectScopes |
| 18 | +> { |
| 19 | + protected authorizeUrl = 'https://www.linkedin.com/oauth/v2/authorization' |
| 20 | + protected accessTokenUrl = 'https://www.linkedin.com/oauth/v2/accessToken' |
| 21 | + protected userInfoUrl = 'https://api.linkedin.com/v2/userinfo' |
| 22 | + |
| 23 | + /** |
| 24 | + * The param name for the authorization code |
| 25 | + */ |
| 26 | + protected codeParamName = 'code' |
| 27 | + |
| 28 | + /** |
| 29 | + * The param name for the error |
| 30 | + */ |
| 31 | + protected errorParamName = 'error' |
| 32 | + |
| 33 | + /** |
| 34 | + * Cookie name for storing the "linkedin_openid_connect_oauth_state" |
| 35 | + */ |
| 36 | + protected stateCookieName = 'linkedin_openid_connect_oauth_state' |
| 37 | + |
| 38 | + /** |
| 39 | + * Parameter name to be used for sending and receiving the state |
| 40 | + * from linkedin |
| 41 | + */ |
| 42 | + protected stateParamName = 'state' |
| 43 | + |
| 44 | + /** |
| 45 | + * Parameter name for defining the scopes |
| 46 | + */ |
| 47 | + protected scopeParamName = 'scope' |
| 48 | + |
| 49 | + /** |
| 50 | + * Scopes separator |
| 51 | + */ |
| 52 | + protected scopesSeparator = ' ' |
| 53 | + |
| 54 | + constructor( |
| 55 | + ctx: HttpContext, |
| 56 | + public config: LinkedInOpenidConnectDriverConfig |
| 57 | + ) { |
| 58 | + super(ctx, config) |
| 59 | + /** |
| 60 | + * Extremely important to call the following method to clear the |
| 61 | + * state set by the redirect request. |
| 62 | + * |
| 63 | + * DO NOT REMOVE THE FOLLOWING LINE |
| 64 | + */ |
| 65 | + this.loadState() |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Configuring the redirect request with defaults |
| 70 | + */ |
| 71 | + protected configureRedirectRequest( |
| 72 | + request: RedirectRequestContract<LinkedInOpenidConnectScopes> |
| 73 | + ) { |
| 74 | + /** |
| 75 | + * Define user defined scopes or the default one's |
| 76 | + */ |
| 77 | + request.scopes(this.config.scopes || ['openid', 'profile', 'email']) |
| 78 | + |
| 79 | + /** |
| 80 | + * Set "response_type" param |
| 81 | + */ |
| 82 | + request.param('response_type', 'code') |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns the HTTP request with the authorization header set |
| 87 | + */ |
| 88 | + protected getAuthenticatedRequest(url: string, token: string): HttpClient { |
| 89 | + const request = this.httpClient(url) |
| 90 | + request.header('Authorization', `Bearer ${token}`) |
| 91 | + request.header('Accept', 'application/json') |
| 92 | + request.parseAs('json') |
| 93 | + return request |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Fetches the user info from the LinkedIn API |
| 98 | + */ |
| 99 | + protected async getUserInfo(token: string, callback?: (request: ApiRequestContract) => void) { |
| 100 | + let url = this.config.userInfoUrl || this.userInfoUrl |
| 101 | + const request = this.getAuthenticatedRequest(url, token) |
| 102 | + |
| 103 | + if (typeof callback === 'function') { |
| 104 | + callback(request) |
| 105 | + } |
| 106 | + |
| 107 | + const body = await request.get() |
| 108 | + const emailVerificationState: 'verified' | 'unverified' = body.email_verified |
| 109 | + ? 'verified' |
| 110 | + : 'unverified' |
| 111 | + |
| 112 | + return { |
| 113 | + id: body.sub, |
| 114 | + nickName: body.given_name, |
| 115 | + name: body.family_name, |
| 116 | + avatarUrl: body.picture, |
| 117 | + email: body.email, |
| 118 | + emailVerificationState, |
| 119 | + original: body, |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Find if the current error code is for access denied |
| 125 | + */ |
| 126 | + accessDenied(): boolean { |
| 127 | + const error = this.getError() |
| 128 | + if (!error) { |
| 129 | + return false |
| 130 | + } |
| 131 | + |
| 132 | + return error === 'user_cancelled_login' || error === 'user_cancelled_authorize' |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns details for the authorized user |
| 137 | + */ |
| 138 | + async user(callback?: (request: ApiRequestContract) => void) { |
| 139 | + const accessToken = await this.accessToken(callback) |
| 140 | + const userInfo = await this.getUserInfo(accessToken.token, callback) |
| 141 | + |
| 142 | + return { |
| 143 | + ...userInfo, |
| 144 | + token: { ...accessToken }, |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Finds the user by the access token |
| 150 | + */ |
| 151 | + async userFromToken(token: string, callback?: (request: ApiRequestContract) => void) { |
| 152 | + const user = await this.getUserInfo(token, callback) |
| 153 | + |
| 154 | + return { |
| 155 | + ...user, |
| 156 | + token: { token, type: 'bearer' as const }, |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments