|
| 1 | +import { conditional } from '@silverhand/essentials'; |
| 2 | + |
| 3 | +import { |
| 4 | + ConnectorError, |
| 5 | + ConnectorErrorCodes, |
| 6 | + validateConfig, |
| 7 | + ConnectorType, |
| 8 | + jsonGuard, |
| 9 | +} from '@logto/connector-kit'; |
| 10 | +import type { |
| 11 | + GetAuthorizationUri, |
| 12 | + GetUserInfo, |
| 13 | + SocialConnector, |
| 14 | + CreateConnector, |
| 15 | + GetConnectorConfig, |
| 16 | +} from '@logto/connector-kit'; |
| 17 | +import ky, { HTTPError } from 'ky'; |
| 18 | + |
| 19 | +import { |
| 20 | + authorizationEndpoint, |
| 21 | + accessTokenEndpoint, |
| 22 | + defaultMetadata, |
| 23 | + defaultTimeout, |
| 24 | + defaultScope, |
| 25 | + userInfoEndpoint, |
| 26 | +} from './constant.js'; |
| 27 | +import type { LinkedInConfig } from './types.js'; |
| 28 | +import { |
| 29 | + linkedInConfigGuard, |
| 30 | + userInfoResponseGuard, |
| 31 | + authResponseGuard, |
| 32 | + accessTokenResponseGuard, |
| 33 | +} from './types.js'; |
| 34 | + |
| 35 | +const getAuthorizationUri = |
| 36 | + (getConfig: GetConnectorConfig): GetAuthorizationUri => |
| 37 | + async ({ state, redirectUri }, setSession) => { |
| 38 | + const config = await getConfig(defaultMetadata.id); |
| 39 | + validateConfig(config, linkedInConfigGuard); |
| 40 | + |
| 41 | + await setSession({ redirectUri }); |
| 42 | + |
| 43 | + const queryParams = new URLSearchParams({ |
| 44 | + response_type: 'code', |
| 45 | + client_id: config.clientId, |
| 46 | + redirect_uri: redirectUri, |
| 47 | + scope: config.scope ?? defaultScope, |
| 48 | + state, |
| 49 | + }); |
| 50 | + |
| 51 | + return `${authorizationEndpoint}?${queryParams.toString()}`; |
| 52 | + }; |
| 53 | + |
| 54 | +export const getAccessToken = async (config: LinkedInConfig, code: string, redirectUri: string) => { |
| 55 | + const response = await ky |
| 56 | + .post(accessTokenEndpoint, { |
| 57 | + headers: { |
| 58 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 59 | + }, |
| 60 | + body: new URLSearchParams({ |
| 61 | + grant_type: 'authorization_code', |
| 62 | + code, |
| 63 | + redirect_uri: redirectUri, |
| 64 | + client_id: config.clientId, |
| 65 | + client_secret: config.clientSecret, |
| 66 | + }).toString(), |
| 67 | + timeout: defaultTimeout, |
| 68 | + }) |
| 69 | + .json(); |
| 70 | + |
| 71 | + return accessTokenResponseGuard.parse(response); |
| 72 | +}; |
| 73 | + |
| 74 | +const getUserInfo = |
| 75 | + (getConfig: GetConnectorConfig): GetUserInfo => |
| 76 | + async (data, getSession) => { |
| 77 | + const config = await getConfig(defaultMetadata.id); |
| 78 | + validateConfig(config, linkedInConfigGuard); |
| 79 | + |
| 80 | + const authResponseResult = authResponseGuard.safeParse(data); |
| 81 | + |
| 82 | + if (!authResponseResult.success) { |
| 83 | + throw new ConnectorError(ConnectorErrorCodes.General, JSON.stringify(data)); |
| 84 | + } |
| 85 | + |
| 86 | + const { code } = authResponseResult.data; |
| 87 | + const { redirectUri } = await getSession(); |
| 88 | + |
| 89 | + if (!redirectUri) { |
| 90 | + throw new ConnectorError(ConnectorErrorCodes.General, { |
| 91 | + message: 'Cannot find `redirectUri` from connector session.', |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + try { |
| 96 | + const { access_token } = await getAccessToken(config, code, redirectUri); |
| 97 | + |
| 98 | + const userInfo = await ky |
| 99 | + .get(userInfoEndpoint, { |
| 100 | + headers: { |
| 101 | + Authorization: `Bearer ${access_token}`, |
| 102 | + }, |
| 103 | + timeout: defaultTimeout, |
| 104 | + }) |
| 105 | + .json(); |
| 106 | + const userInfoResult = userInfoResponseGuard.safeParse(userInfo); |
| 107 | + |
| 108 | + if (!userInfoResult.success) { |
| 109 | + throw new ConnectorError(ConnectorErrorCodes.InvalidResponse, userInfoResult.error); |
| 110 | + } |
| 111 | + |
| 112 | + const { sub, name, email, picture } = userInfoResult.data; |
| 113 | + |
| 114 | + return { |
| 115 | + id: sub, |
| 116 | + name: conditional(name), |
| 117 | + email: conditional(email), |
| 118 | + picture: conditional(picture), |
| 119 | + rawData: jsonGuard.parse(userInfo), |
| 120 | + }; |
| 121 | + } catch (error: unknown) { |
| 122 | + if (error instanceof HTTPError) { |
| 123 | + const { status, body: rawBody } = error.response; |
| 124 | + |
| 125 | + if (status === 401) { |
| 126 | + throw new ConnectorError(ConnectorErrorCodes.SocialAccessTokenInvalid); |
| 127 | + } |
| 128 | + |
| 129 | + throw new ConnectorError(ConnectorErrorCodes.General, JSON.stringify(rawBody)); |
| 130 | + } |
| 131 | + |
| 132 | + throw error; |
| 133 | + } |
| 134 | + }; |
| 135 | + |
| 136 | +const createLinkedInConnector: CreateConnector<SocialConnector> = async ({ getConfig }) => { |
| 137 | + return { |
| 138 | + metadata: defaultMetadata, |
| 139 | + type: ConnectorType.Social, |
| 140 | + configGuard: linkedInConfigGuard, |
| 141 | + getAuthorizationUri: getAuthorizationUri(getConfig), |
| 142 | + getUserInfo: getUserInfo(getConfig), |
| 143 | + }; |
| 144 | +}; |
| 145 | + |
| 146 | +export default createLinkedInConnector; |
0 commit comments