Skip to content

Commit

Permalink
feat(core): update connector db schema (#732)
Browse files Browse the repository at this point in the history
* feat(core): update connector db schema

* feat(core): eliminate code redundancy for UTs

* feat(core): delete insertConnector on conflict logic and fix UTs

* feat(core): fix UI according to new connector type implementation

* feat(core): removed unused getConnectorByTargetAndPlatform methods

* feat(core): deprecate the function that updateConnector by giving target and platform

* feat(core): keep SSOT on combination of connector and corresponding metadata

* feat(core): rename index name in db
  • Loading branch information
darcyYe authored May 12, 2022
1 parent bafd094 commit 8e1533a
Show file tree
Hide file tree
Showing 45 changed files with 1,302 additions and 1,778 deletions.
5 changes: 3 additions & 2 deletions packages/connector-alipay/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { ConnectorType, ConnectorMetadata } from '@logto/connector-types';
import { ConnectorType, ConnectorMetadata, ConnectorPlatform } from '@logto/connector-types';
import { getFileContents } from '@logto/shared';

export const authorizationEndpoint = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm';
Expand All @@ -23,8 +23,9 @@ const readmeContentFallback = 'Please check README.md file directory.';
const configTemplateFallback = 'Please check config-template.md file directory.';

export const defaultMetadata: ConnectorMetadata = {
id: 'alipay',
target: 'alipay',
type: ConnectorType.Social,
platform: ConnectorPlatform.Web,
name: {
en: 'Sign In with Alipay',
'zh-CN': '支付宝登录',
Expand Down
6 changes: 3 additions & 3 deletions packages/connector-alipay/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class AlipayConnector implements SocialConnector {
};

public getAuthorizationUri: GetAuthorizationUri = async (redirectUri, state) => {
const { appId: app_id } = await this.getConfig(this.metadata.id);
const { appId: app_id } = await this.getConfig(this.metadata.target, this.metadata.platform);

const redirect_uri = encodeURI(redirectUri);

Expand All @@ -71,7 +71,7 @@ export class AlipayConnector implements SocialConnector {
};

public getAccessToken: GetAccessToken = async (code): Promise<AccessTokenObject> => {
const config = await this.getConfig(this.metadata.id);
const config = await this.getConfig(this.metadata.target, this.metadata.platform);
const initSearchParameters = {
method: methodForAccessToken,
format: 'JSON',
Expand Down Expand Up @@ -104,7 +104,7 @@ export class AlipayConnector implements SocialConnector {
};

public getUserInfo: GetUserInfo = async (accessTokenObject) => {
const config = await this.getConfig(this.metadata.id);
const config = await this.getConfig(this.metadata.target, this.metadata.platform);
const { accessToken } = accessTokenObject;
assert(
accessToken && config,
Expand Down
3 changes: 2 additions & 1 deletion packages/connector-aliyun-dm/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ const readmeContentFallback = 'Please check README.md file directory.';
const configTemplateFallback = 'Please check config-template.md file directory.';

export const defaultMetadata: ConnectorMetadata = {
id: 'aliyun-dm',
target: 'aliyun-dm',
type: ConnectorType.Email,
platform: null,
name: {
en: 'Aliyun Direct Mail',
'zh-CN': '阿里云邮件推送',
Expand Down
2 changes: 1 addition & 1 deletion packages/connector-aliyun-dm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class AliyunDmConnector implements EmailConnector {
type,
data
) => {
const config = await this.getConfig(this.metadata.id);
const config = await this.getConfig(this.metadata.target, this.metadata.platform);
await this.validateConfig(config);
const { accessKeyId, accessKeySecret, accountName, fromAlias, templates } = config;
const template = templates.find((template) => template.usageType === type);
Expand Down
3 changes: 2 additions & 1 deletion packages/connector-aliyun-sms/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const readmeContentFallback = 'Please check README.md file directory.';
const configTemplateFallback = 'Please check config-template.md file directory.';

export const defaultMetadata: ConnectorMetadata = {
id: 'aliyun-sms',
target: 'aliyun-sms',
type: ConnectorType.SMS,
platform: null,
name: {
en: 'Aliyun Short Message Service',
'zh-CN': '阿里云短信服务',
Expand Down
2 changes: 1 addition & 1 deletion packages/connector-aliyun-sms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class AliyunSmsConnector implements SmsConnector {
type,
{ code }
) => {
const config = await this.getConfig(this.metadata.id);
const config = await this.getConfig(this.metadata.target, this.metadata.platform);
await this.validateConfig(config);
const { accessKeyId, accessKeySecret, signName, templates } = config;
const template = templates.find(({ usageType }) => usageType === type);
Expand Down
5 changes: 3 additions & 2 deletions packages/connector-facebook/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { ConnectorMetadata, ConnectorType } from '@logto/connector-types';
import { ConnectorMetadata, ConnectorType, ConnectorPlatform } from '@logto/connector-types';
import { getFileContents } from '@logto/shared';
import { z } from 'zod';

Expand Down Expand Up @@ -33,8 +33,9 @@ const readmeContentFallback = 'Please check README.md file directory.';
const configTemplateFallback = 'Please check config-template.md file directory.';

export const defaultMetadata: ConnectorMetadata = {
id: 'facebook',
target: 'facebook',
type: ConnectorType.Social,
platform: ConnectorPlatform.Web,
name: {
en: 'Sign In with Facebook',
'zh-CN': 'Facebook 登录',
Expand Down
5 changes: 3 additions & 2 deletions packages/connector-facebook/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class FacebookConnector implements SocialConnector {
};

public getAuthorizationUri: GetAuthorizationUri = async (redirectUri, state) => {
const config = await this.getConfig(this.metadata.id);
const config = await this.getConfig(this.metadata.target, this.metadata.platform);

const queryParameters = new URLSearchParams({
client_id: config.clientId,
Expand All @@ -67,7 +67,8 @@ export class FacebookConnector implements SocialConnector {
};

const { clientId: client_id, clientSecret: client_secret } = await this.getConfig(
this.metadata.id
this.metadata.target,
this.metadata.platform
);

const { access_token: accessToken } = await got
Expand Down
5 changes: 3 additions & 2 deletions packages/connector-github/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { ConnectorMetadata, ConnectorType } from '@logto/connector-types';
import { ConnectorMetadata, ConnectorType, ConnectorPlatform } from '@logto/connector-types';
import { getFileContents } from '@logto/shared';
import { z } from 'zod';

Expand All @@ -24,8 +24,9 @@ const readmeContentFallback = 'Please check README.md file directory.';
const configTemplateFallback = 'Please check config-template.md file directory.';

export const defaultMetadata: ConnectorMetadata = {
id: 'github',
target: 'github',
type: ConnectorType.Social,
platform: ConnectorPlatform.Web,
name: {
en: 'Sign In with GitHub',
'zh-CN': 'GitHub登录',
Expand Down
5 changes: 3 additions & 2 deletions packages/connector-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class GithubConnector implements SocialConnector {
};

public getAuthorizationUri: GetAuthorizationUri = async (redirectUri, state) => {
const config = await this.getConfig(this.metadata.id);
const config = await this.getConfig(this.metadata.target, this.metadata.platform);

const queryParameters = new URLSearchParams({
client_id: config.clientId,
Expand All @@ -61,7 +61,8 @@ export class GithubConnector implements SocialConnector {
};

const { clientId: client_id, clientSecret: client_secret } = await this.getConfig(
this.metadata.id
this.metadata.target,
this.metadata.platform
);

const { access_token: accessToken } = await got
Expand Down
5 changes: 3 additions & 2 deletions packages/connector-google/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { ConnectorMetadata, ConnectorType } from '@logto/connector-types';
import { ConnectorMetadata, ConnectorType, ConnectorPlatform } from '@logto/connector-types';
import { getFileContents } from '@logto/shared';
import { z } from 'zod';

Expand All @@ -24,8 +24,9 @@ const readmeContentFallback = 'Please check README.md file directory.';
const configTemplateFallback = 'Please check config-template.md file directory.';

export const defaultMetadata: ConnectorMetadata = {
id: 'google',
target: 'google',
type: ConnectorType.Social,
platform: ConnectorPlatform.Web,
name: {
en: 'Sign In with Google',
'zh-CN': 'Google登录',
Expand Down
7 changes: 5 additions & 2 deletions packages/connector-google/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class GoogleConnector implements SocialConnector {
};

public getAuthorizationUri: GetAuthorizationUri = async (redirectUri, state) => {
const config = await this.getConfig(this.metadata.id);
const config = await this.getConfig(this.metadata.target, this.metadata.platform);

const queryParameters = new URLSearchParams({
client_id: config.clientId,
Expand All @@ -65,7 +65,10 @@ export class GoogleConnector implements SocialConnector {
token_type: string;
};

const { clientId, clientSecret } = await this.getConfig(this.metadata.id);
const { clientId, clientSecret } = await this.getConfig(
this.metadata.target,
this.metadata.platform
);

// Note:Need to decodeURIComponent on code
// https://stackoverflow.com/questions/51058256/google-api-node-js-invalid-grant-malformed-auth-code
Expand Down
21 changes: 16 additions & 5 deletions packages/connector-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import type { Languages } from '@logto/phrases';
import { Language } from '@logto/phrases';
import { Nullable } from '@silverhand/essentials';

export enum ConnectorType {
Email = 'Email',
SMS = 'SMS',
Social = 'Social',
}

export enum ConnectorPlatform {
Native = 'Native',
Universal = 'Universal',
Web = 'Web',
}

export interface ConnectorMetadata {
id: string;
target: string;
type: ConnectorType;
name: Record<Languages, string>;
platform: Nullable<ConnectorPlatform>;
name: Record<Language, string>;
logo: string;
description: Record<Languages, string>;
description: Record<Language, string>;
readme: string;
configTemplate: string;
}
Expand Down Expand Up @@ -94,4 +102,7 @@ export type GetUserInfo = (
accessTokenObject: AccessTokenObject
) => Promise<{ id: string } & Record<string, string | undefined>>;

export type GetConnectorConfig<T = Record<string, unknown>> = (id: string) => Promise<T>;
export type GetConnectorConfig<T = Record<string, unknown>> = (
target: string,
platform: Nullable<ConnectorPlatform>
) => Promise<T>;
5 changes: 3 additions & 2 deletions packages/connector-wechat-native/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { ConnectorMetadata, ConnectorType } from '@logto/connector-types';
import { ConnectorMetadata, ConnectorType, ConnectorPlatform } from '@logto/connector-types';
import { getFileContents } from '@logto/shared';
import { z } from 'zod';

Expand All @@ -21,8 +21,9 @@ const readmeContentFallback = 'Please check README.md file directory.';
const configTemplateFallback = 'Please check config-template.md file directory.';

export const defaultMetadata: ConnectorMetadata = {
id: 'wechat-native',
target: 'wechat-native',
type: ConnectorType.Social,
platform: ConnectorPlatform.Native,
name: {
en: 'Sign In with WeChat',
'zh-CN': '微信登录',
Expand Down
7 changes: 5 additions & 2 deletions packages/connector-wechat-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class WeChatNativeConnector implements SocialConnector {
};

public getAuthorizationUri: GetAuthorizationUri = async (redirectUri, state) => {
const { appId } = await this.getConfig(this.metadata.id);
const { appId } = await this.getConfig(this.metadata.target, this.metadata.platform);

const queryParameters = new URLSearchParams({
appid: appId,
Expand All @@ -71,7 +71,10 @@ export class WeChatNativeConnector implements SocialConnector {
errcode?: number;
};

const { appId: appid, appSecret: secret } = await this.getConfig(this.metadata.id);
const { appId: appid, appSecret: secret } = await this.getConfig(
this.metadata.target,
this.metadata.platform
);

const {
access_token: accessToken,
Expand Down
5 changes: 3 additions & 2 deletions packages/connector-wechat/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { ConnectorMetadata, ConnectorType } from '@logto/connector-types';
import { ConnectorMetadata, ConnectorType, ConnectorPlatform } from '@logto/connector-types';
import { getFileContents } from '@logto/shared';
import { z } from 'zod';

Expand All @@ -21,8 +21,9 @@ const readmeContentFallback = 'Please check README.md file directory.';
const configTemplateFallback = 'Please check config-template.md file directory.';

export const defaultMetadata: ConnectorMetadata = {
id: 'wechat',
target: 'wechat',
type: ConnectorType.Social,
platform: ConnectorPlatform.Web,
name: {
en: 'Sign In with WeChat',
'zh-CN': '微信登录',
Expand Down
7 changes: 5 additions & 2 deletions packages/connector-wechat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class WeChatConnector implements SocialConnector {
};

public getAuthorizationUri: GetAuthorizationUri = async (redirectUri, state) => {
const { appId } = await this.getConfig(this.metadata.id);
const { appId } = await this.getConfig(this.metadata.target, this.metadata.platform);

const queryParameters = new URLSearchParams({
appid: appId,
Expand All @@ -72,7 +72,10 @@ export class WeChatConnector implements SocialConnector {
errcode?: number;
};

const { appId: appid, appSecret: secret } = await this.getConfig(this.metadata.id);
const { appId: appid, appSecret: secret } = await this.getConfig(
this.metadata.target,
this.metadata.platform
);

const {
access_token: accessToken,
Expand Down
3 changes: 1 addition & 2 deletions packages/console/src/components/UnnamedTrans/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Languages } from '@logto/phrases';
import React from 'react';
import { useTranslation } from 'react-i18next';

type Props = {
resource: Record<Languages, string>;
resource: Record<string, string>;
className?: string;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ const GuideModal = ({ connector, isOpen, onClose }: Props) => {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const {
id: connectorId,
type: connectorType,
metadata: { name, configTemplate, readme },
metadata: { type: connectorType, name, configTemplate, readme },
} = connector;

const locale = i18next.language;
const connectorName = name[locale] ?? name.en;
// TODO: LOG-2393 should fix name[locale] syntax error
const foundName = Object.entries(name).find(([lang]) => lang === locale);
const connectorName = foundName ? foundName[1] : name.en;
const isSocialConnector =
connectorType !== ConnectorType.SMS && connectorType !== ConnectorType.Email;
const [activeStepIndex, setActiveStepIndex] = useState<number>(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Languages } from '@logto/phrases';
import { Language } from '@logto/phrases';
import { ConnectorDTO, Identities } from '@logto/schemas';
import { Optional } from '@silverhand/essentials';
import React, { useMemo, useState } from 'react';
Expand All @@ -23,7 +23,7 @@ type DisplayConnector = {
id: string;
userId?: string;
logo: string;
name: Record<Languages, string>;
name: Record<Language, string>;
};

const UserConnectors = ({ userId, connectors, onDelete }: Props) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@logto/connector-wechat-native": "^0.1.0",
"@logto/phrases": "^0.1.0",
"@logto/schemas": "^0.1.0",
"@silverhand/essentials": "^1.1.0",
"@silverhand/essentials": "^1.1.6",
"argon2": "^0.28.5",
"chalk": "^4",
"dayjs": "^1.10.5",
Expand Down
Loading

0 comments on commit 8e1533a

Please sign in to comment.