0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-23 20:33:16 -05:00

feat(core): add wechat-native connector (#470)

* feat(core): add wechat-native connector

* feat(core): add wechat native getAuthorizationUri UT
This commit is contained in:
Darcy Ye 2022-03-31 18:03:51 +08:00 committed by GitHub
parent 004fe65d5a
commit 2a153c2b6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 2 deletions

View file

@ -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]));

View file

@ -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<ConnectorInstance[]> => {
const connectors = await findAllConnectors();

View file

@ -0,0 +1,2 @@
export const authorizationEndpoint = 'https://wechat.native/';
export const scope = 'snsapi_userinfo';

View file

@ -0,0 +1,24 @@
import { getAuthorizationUri } from '.';
import { getConnectorConfig } from '../utilities';
import { authorizationEndpoint } from './constant';
jest.mock('../utilities');
beforeAll(() => {
(getConnectorConfig as jest.MockedFunction<typeof getConnectorConfig>).mockResolvedValue({
appId: '<app-id>',
appSecret: '<app-secret>',
});
});
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`
);
});
});

View file

@ -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<WeChatConfig>(metadata.id);
return `${authorizationEndpoint}?${stringify({
appid: appId,
redirect_uri: encodeURI(redirectUri), // The variable `redirectUri` should match {appId, appSecret}
scope,
state,
})}`;
};

View file

@ -50,7 +50,7 @@ export const metadata: ConnectorMetadata = {
const weChatConfigGuard = z.object({ appId: z.string(), appSecret: z.string() });
type WeChatConfig = z.infer<typeof weChatConfigGuard>;
export type WeChatConfig = z.infer<typeof weChatConfigGuard>;
export const validateConfig: ValidateConfig = async (config: unknown) => {
const result = weChatConfigGuard.safeParse(config);