From 2a153c2b6d59664cf2fe22e1d5ddf589f93860fa Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Thu, 31 Mar 2022 18:03:51 +0800 Subject: [PATCH] feat(core): add wechat-native connector (#470) * feat(core): add wechat-native connector * feat(core): add wechat native getAuthorizationUri UT --- packages/core/src/connectors/index.test.ts | 7 +++++ packages/core/src/connectors/index.ts | 11 ++++++- .../src/connectors/wechat-native/constant.ts | 2 ++ .../connectors/wechat-native/index.test.ts | 24 +++++++++++++++ .../src/connectors/wechat-native/index.ts | 30 +++++++++++++++++++ packages/core/src/connectors/wechat/index.ts | 2 +- 6 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 packages/core/src/connectors/wechat-native/constant.ts create mode 100644 packages/core/src/connectors/wechat-native/index.test.ts create mode 100644 packages/core/src/connectors/wechat-native/index.ts diff --git a/packages/core/src/connectors/index.test.ts b/packages/core/src/connectors/index.test.ts index 9f8de5d11..534169430 100644 --- a/packages/core/src/connectors/index.test.ts +++ b/packages/core/src/connectors/index.test.ts @@ -47,6 +47,12 @@ const wechatConnector = { config: {}, createdAt: 1_646_382_233_000, }; +const wechatNativeConnector = { + id: 'wechat-native', + enabled: false, + config: {}, + createdAt: 1_646_382_233_000, +}; const connectors = [ aliyunDmConnector, @@ -55,6 +61,7 @@ const connectors = [ githubConnector, googleConnector, wechatConnector, + wechatNativeConnector, ]; const connectorMap = new Map(connectors.map((connector) => [connector.id, connector])); diff --git a/packages/core/src/connectors/index.ts b/packages/core/src/connectors/index.ts index daca92265..cf798b0ce 100644 --- a/packages/core/src/connectors/index.ts +++ b/packages/core/src/connectors/index.ts @@ -8,8 +8,17 @@ import * as GitHub from './github'; import * as Google from './google'; import { ConnectorInstance, ConnectorType, IConnector, SocialConnectorInstance } from './types'; import * as WeChat from './wechat'; +import * as WeChatNative from './wechat-native'; -const allConnectors: IConnector[] = [AliyunDM, AliyunSMS, Facebook, GitHub, Google, WeChat]; +const allConnectors: IConnector[] = [ + AliyunDM, + AliyunSMS, + Facebook, + GitHub, + Google, + WeChat, + WeChatNative, +]; export const getConnectorInstances = async (): Promise => { const connectors = await findAllConnectors(); diff --git a/packages/core/src/connectors/wechat-native/constant.ts b/packages/core/src/connectors/wechat-native/constant.ts new file mode 100644 index 000000000..ededc3985 --- /dev/null +++ b/packages/core/src/connectors/wechat-native/constant.ts @@ -0,0 +1,2 @@ +export const authorizationEndpoint = 'https://wechat.native/'; +export const scope = 'snsapi_userinfo'; diff --git a/packages/core/src/connectors/wechat-native/index.test.ts b/packages/core/src/connectors/wechat-native/index.test.ts new file mode 100644 index 000000000..93e4b9897 --- /dev/null +++ b/packages/core/src/connectors/wechat-native/index.test.ts @@ -0,0 +1,24 @@ +import { getAuthorizationUri } from '.'; +import { getConnectorConfig } from '../utilities'; +import { authorizationEndpoint } from './constant'; + +jest.mock('../utilities'); + +beforeAll(() => { + (getConnectorConfig as jest.MockedFunction).mockResolvedValue({ + appId: '', + appSecret: '', + }); +}); + +describe('getAuthorizationUri', () => { + it('should get a valid uri by redirectUri and state', async () => { + const authorizationUri = await getAuthorizationUri( + 'http://localhost:3001/callback', + 'some_state' + ); + expect(authorizationUri).toEqual( + `${authorizationEndpoint}?appid=%3Capp-id%3E&redirect_uri=http%3A%2F%2Flocalhost%3A3001%2Fcallback&scope=snsapi_userinfo&state=some_state` + ); + }); +}); diff --git a/packages/core/src/connectors/wechat-native/index.ts b/packages/core/src/connectors/wechat-native/index.ts new file mode 100644 index 000000000..c4d916a12 --- /dev/null +++ b/packages/core/src/connectors/wechat-native/index.ts @@ -0,0 +1,30 @@ +/** + * The Implementation of OpenID Connect of WeChat Web Open Platform. + * https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Development_Guide.html + */ + +import { stringify } from 'query-string'; + +import { metadata as weChatWebMetadata, WeChatConfig } from '@/connectors/wechat'; + +import { ConnectorMetadata, GetAuthorizationUri } from '../types'; +import { getConnectorConfig } from '../utilities'; +import { authorizationEndpoint, scope } from './constant'; + +export { validateConfig, getAccessToken, getUserInfo } from '@/connectors/wechat'; + +export const metadata: ConnectorMetadata = { + ...weChatWebMetadata, + id: 'wechat-native', +}; + +export const getAuthorizationUri: GetAuthorizationUri = async (redirectUri, state) => { + const { appId } = await getConnectorConfig(metadata.id); + + return `${authorizationEndpoint}?${stringify({ + appid: appId, + redirect_uri: encodeURI(redirectUri), // The variable `redirectUri` should match {appId, appSecret} + scope, + state, + })}`; +}; diff --git a/packages/core/src/connectors/wechat/index.ts b/packages/core/src/connectors/wechat/index.ts index 47107b167..44f7c7ef4 100644 --- a/packages/core/src/connectors/wechat/index.ts +++ b/packages/core/src/connectors/wechat/index.ts @@ -50,7 +50,7 @@ export const metadata: ConnectorMetadata = { const weChatConfigGuard = z.object({ appId: z.string(), appSecret: z.string() }); -type WeChatConfig = z.infer; +export type WeChatConfig = z.infer; export const validateConfig: ValidateConfig = async (config: unknown) => { const result = weChatConfigGuard.safeParse(config);