mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -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:
parent
004fe65d5a
commit
2a153c2b6d
6 changed files with 74 additions and 2 deletions
|
@ -47,6 +47,12 @@ const wechatConnector = {
|
||||||
config: {},
|
config: {},
|
||||||
createdAt: 1_646_382_233_000,
|
createdAt: 1_646_382_233_000,
|
||||||
};
|
};
|
||||||
|
const wechatNativeConnector = {
|
||||||
|
id: 'wechat-native',
|
||||||
|
enabled: false,
|
||||||
|
config: {},
|
||||||
|
createdAt: 1_646_382_233_000,
|
||||||
|
};
|
||||||
|
|
||||||
const connectors = [
|
const connectors = [
|
||||||
aliyunDmConnector,
|
aliyunDmConnector,
|
||||||
|
@ -55,6 +61,7 @@ const connectors = [
|
||||||
githubConnector,
|
githubConnector,
|
||||||
googleConnector,
|
googleConnector,
|
||||||
wechatConnector,
|
wechatConnector,
|
||||||
|
wechatNativeConnector,
|
||||||
];
|
];
|
||||||
const connectorMap = new Map(connectors.map((connector) => [connector.id, connector]));
|
const connectorMap = new Map(connectors.map((connector) => [connector.id, connector]));
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,17 @@ import * as GitHub from './github';
|
||||||
import * as Google from './google';
|
import * as Google from './google';
|
||||||
import { ConnectorInstance, ConnectorType, IConnector, SocialConnectorInstance } from './types';
|
import { ConnectorInstance, ConnectorType, IConnector, SocialConnectorInstance } from './types';
|
||||||
import * as WeChat from './wechat';
|
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[]> => {
|
export const getConnectorInstances = async (): Promise<ConnectorInstance[]> => {
|
||||||
const connectors = await findAllConnectors();
|
const connectors = await findAllConnectors();
|
||||||
|
|
2
packages/core/src/connectors/wechat-native/constant.ts
Normal file
2
packages/core/src/connectors/wechat-native/constant.ts
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export const authorizationEndpoint = 'https://wechat.native/';
|
||||||
|
export const scope = 'snsapi_userinfo';
|
24
packages/core/src/connectors/wechat-native/index.test.ts
Normal file
24
packages/core/src/connectors/wechat-native/index.test.ts
Normal 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`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
30
packages/core/src/connectors/wechat-native/index.ts
Normal file
30
packages/core/src/connectors/wechat-native/index.ts
Normal 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,
|
||||||
|
})}`;
|
||||||
|
};
|
|
@ -50,7 +50,7 @@ export const metadata: ConnectorMetadata = {
|
||||||
|
|
||||||
const weChatConfigGuard = z.object({ appId: z.string(), appSecret: z.string() });
|
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) => {
|
export const validateConfig: ValidateConfig = async (config: unknown) => {
|
||||||
const result = weChatConfigGuard.safeParse(config);
|
const result = weChatConfigGuard.safeParse(config);
|
||||||
|
|
Loading…
Reference in a new issue