From d78aa07f7e5cfb78139919a6689ca56467d83fb6 Mon Sep 17 00:00:00 2001 From: Wang Sijie Date: Fri, 21 Jan 2022 20:47:14 +0800 Subject: [PATCH] feat(github): getUserInfo (#188) * feat(github): getUserInfo * fix: pr --- .../core/src/connectors/github/constant.ts | 1 + .../core/src/connectors/github/index.test.ts | 22 +++++++++- packages/core/src/connectors/github/index.ts | 43 ++++++++++++++++--- packages/core/src/connectors/types.ts | 11 +++++ 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/packages/core/src/connectors/github/constant.ts b/packages/core/src/connectors/github/constant.ts index 14d517a53..6e467cfd0 100644 --- a/packages/core/src/connectors/github/constant.ts +++ b/packages/core/src/connectors/github/constant.ts @@ -1,3 +1,4 @@ export const authorizationEndpoint = 'https://github.com/login/oauth/authorize'; export const scope = 'read:user'; export const accessTokenEndpoint = 'https://github.com/login/oauth/access_token'; +export const userInfoEndpoint = 'https://api.github.com/user'; diff --git a/packages/core/src/connectors/github/index.test.ts b/packages/core/src/connectors/github/index.test.ts index f714f04af..eb52a62d6 100644 --- a/packages/core/src/connectors/github/index.test.ts +++ b/packages/core/src/connectors/github/index.test.ts @@ -1,8 +1,8 @@ import nock from 'nock'; -import { getAccessToken, getAuthorizationUri, validateConfig } from '.'; +import { getAccessToken, getAuthorizationUri, validateConfig, getUserInfo } from '.'; import { getConnectorConfig } from '../utilities'; -import { accessTokenEndpoint, authorizationEndpoint } from './constant'; +import { accessTokenEndpoint, authorizationEndpoint, userInfoEndpoint } from './constant'; jest.mock('../utilities'); @@ -51,3 +51,21 @@ describe('validateConfig', () => { await expect(validateConfig({ clientId: 'clientId' })).rejects.toThrowError(); }); }); + +describe('getUserInfo', () => { + it('shoud get valid SocialUserInfo', async () => { + nock(userInfoEndpoint).get('').reply(200, { + id: 1, + avatar_url: 'https://github.com/images/error/octocat_happy.gif', + name: 'monalisa octocat', + email: 'octocat@github.com', + }); + const socialUserInfo = await getUserInfo('code'); + expect(socialUserInfo).toMatchObject({ + id: '1', + avatar: 'https://github.com/images/error/octocat_happy.gif', + name: 'monalisa octocat', + email: 'octocat@github.com', + }); + }); +}); diff --git a/packages/core/src/connectors/github/index.ts b/packages/core/src/connectors/github/index.ts index d9fdf151c..fbd5a83d7 100644 --- a/packages/core/src/connectors/github/index.ts +++ b/packages/core/src/connectors/github/index.ts @@ -9,9 +9,10 @@ import { GetAccessToken, GetAuthorizationUri, ValidateConfig, + GetUserInfo, } from '../types'; import { getConnectorConfig } from '../utilities'; -import { authorizationEndpoint, accessTokenEndpoint, scope } from './constant'; +import { authorizationEndpoint, accessTokenEndpoint, scope, userInfoEndpoint } from './constant'; export const metadata: ConnectorMetadata = { id: 'github', @@ -56,6 +57,12 @@ export const getAuthorizationUri: GetAuthorizationUri = async (redirectUri, stat }; export const getAccessToken: GetAccessToken = async (code) => { + type AccessTokenResponse = { + access_token: string; + scope: string; + token_type: string; + }; + const { clientId: client_id, clientSecret: client_secret } = await getConnectorConfig(metadata.id, metadata.type); const { access_token: accessToken } = await got @@ -67,10 +74,34 @@ export const getAccessToken: GetAccessToken = async (code) => { code, }, }) - .json<{ - access_token: string; - scope: string; - token_type: string; - }>(); + .json(); return accessToken; }; + +export const getUserInfo: GetUserInfo = async (accessToken: string) => { + type UserInfoResponse = { + id: number; + avatar_url: string; + email: string; + name: string; + }; + + const { + id, + avatar_url: avatar, + email, + name, + } = await got + .get(userInfoEndpoint, { + headers: { + authorization: `token ${accessToken}`, + }, + }) + .json(); + return { + id: String(id), + avatar, + email, + name, + }; +}; diff --git a/packages/core/src/connectors/types.ts b/packages/core/src/connectors/types.ts index 16d565042..8dd3e42d0 100644 --- a/packages/core/src/connectors/types.ts +++ b/packages/core/src/connectors/types.ts @@ -24,6 +24,7 @@ export interface EmailConector extends BaseConnector { export interface SocialConector extends BaseConnector { getAuthorizationUri: GetAuthorizationUri; getAccessToken: GetAccessToken; + getUserInfo: GetUserInfo; } export interface EmailMessageTypes { @@ -56,3 +57,13 @@ export type ValidateConfig = ( export type GetAuthorizationUri = (redirectUri: string, state: string) => Promise; export type GetAccessToken = (code: string) => Promise; + +export type GetUserInfo = (accessToken: string) => Promise; + +export interface SocialUserInfo { + id: string; + email?: string; + phone?: string; + name?: string; + avatar?: string; +}