mirror of
https://github.com/logto-io/logto.git
synced 2024-12-30 20:33:54 -05:00
refactor(connector): refactor passwordless connector
This commit is contained in:
parent
d42c12b747
commit
b1631e87ea
10 changed files with 43 additions and 36 deletions
|
@ -4,6 +4,7 @@ import {
|
||||||
ConnectorMetadata,
|
ConnectorMetadata,
|
||||||
EmailMessageTypes,
|
EmailMessageTypes,
|
||||||
EmailSendMessageFunction,
|
EmailSendMessageFunction,
|
||||||
|
EmailSendTestMessageFunction,
|
||||||
ValidateConfig,
|
ValidateConfig,
|
||||||
EmailConnector,
|
EmailConnector,
|
||||||
GetConnectorConfig,
|
GetConnectorConfig,
|
||||||
|
@ -40,11 +41,7 @@ export default class AliyunDmConnector implements EmailConnector {
|
||||||
return this.sendMessageBy(emailConfig, address, type, data);
|
return this.sendMessageBy(emailConfig, address, type, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
public sendTestMessage: EmailSendMessageFunction = async (address, type, data, config) => {
|
public sendTestMessage: EmailSendTestMessageFunction = async (address, type, data, config) => {
|
||||||
if (!config) {
|
|
||||||
throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.validateConfig(config);
|
await this.validateConfig(config);
|
||||||
|
|
||||||
return this.sendMessageBy(config as AliyunDmConfig, address, type, data);
|
return this.sendMessageBy(config as AliyunDmConfig, address, type, data);
|
||||||
|
|
|
@ -4,6 +4,7 @@ import {
|
||||||
ConnectorMetadata,
|
ConnectorMetadata,
|
||||||
SmsMessageTypes,
|
SmsMessageTypes,
|
||||||
SmsSendMessageFunction,
|
SmsSendMessageFunction,
|
||||||
|
SmsSendTestMessageFunction,
|
||||||
ValidateConfig,
|
ValidateConfig,
|
||||||
SmsConnector,
|
SmsConnector,
|
||||||
GetConnectorConfig,
|
GetConnectorConfig,
|
||||||
|
@ -35,14 +36,10 @@ export default class AliyunSmsConnector implements SmsConnector {
|
||||||
return this.sendMessageBy(smsConfig, phone, type, { code });
|
return this.sendMessageBy(smsConfig, phone, type, { code });
|
||||||
};
|
};
|
||||||
|
|
||||||
public sendTestMessage: SmsSendMessageFunction = async (phone, type, { code }, config) => {
|
public sendTestMessage: SmsSendTestMessageFunction = async (phone, type, data, config) => {
|
||||||
if (!config) {
|
|
||||||
throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.validateConfig(config);
|
await this.validateConfig(config);
|
||||||
|
|
||||||
return this.sendMessageBy(config as AliyunSmsConfig, phone, type, { code });
|
return this.sendMessageBy(config as AliyunSmsConfig, phone, type, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
private readonly sendMessageBy = async (
|
private readonly sendMessageBy = async (
|
||||||
|
|
|
@ -4,6 +4,7 @@ import {
|
||||||
ConnectorMetadata,
|
ConnectorMetadata,
|
||||||
EmailMessageTypes,
|
EmailMessageTypes,
|
||||||
EmailSendMessageFunction,
|
EmailSendMessageFunction,
|
||||||
|
EmailSendTestMessageFunction,
|
||||||
ValidateConfig,
|
ValidateConfig,
|
||||||
EmailConnector,
|
EmailConnector,
|
||||||
GetConnectorConfig,
|
GetConnectorConfig,
|
||||||
|
@ -41,11 +42,7 @@ export default class SendGridMailConnector implements EmailConnector {
|
||||||
return this.sendMessageBy(emailConfig, address, type, data);
|
return this.sendMessageBy(emailConfig, address, type, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
public sendTestMessage: EmailSendMessageFunction = async (address, type, data, config) => {
|
public sendTestMessage: EmailSendTestMessageFunction = async (address, type, data, config) => {
|
||||||
if (!config) {
|
|
||||||
throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.validateConfig(config);
|
await this.validateConfig(config);
|
||||||
|
|
||||||
return this.sendMessageBy(config as SendGridMailConfig, address, type, data);
|
return this.sendMessageBy(config as SendGridMailConfig, address, type, data);
|
||||||
|
|
|
@ -4,6 +4,7 @@ import {
|
||||||
ConnectorMetadata,
|
ConnectorMetadata,
|
||||||
EmailMessageTypes,
|
EmailMessageTypes,
|
||||||
EmailSendMessageFunction,
|
EmailSendMessageFunction,
|
||||||
|
EmailSendTestMessageFunction,
|
||||||
ValidateConfig,
|
ValidateConfig,
|
||||||
EmailConnector,
|
EmailConnector,
|
||||||
GetConnectorConfig,
|
GetConnectorConfig,
|
||||||
|
@ -35,11 +36,7 @@ export default class SmtpConnector implements EmailConnector {
|
||||||
return this.sendMessageBy(emailConfig, address, type, data);
|
return this.sendMessageBy(emailConfig, address, type, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
public sendTestMessage: EmailSendMessageFunction = async (address, type, data, config) => {
|
public sendTestMessage: EmailSendTestMessageFunction = async (address, type, data, config) => {
|
||||||
if (!config) {
|
|
||||||
throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.validateConfig(config);
|
await this.validateConfig(config);
|
||||||
|
|
||||||
return this.sendMessageBy(config as SmtpConfig, address, type, data);
|
return this.sendMessageBy(config as SmtpConfig, address, type, data);
|
||||||
|
|
|
@ -4,6 +4,7 @@ import {
|
||||||
ConnectorMetadata,
|
ConnectorMetadata,
|
||||||
SmsMessageTypes,
|
SmsMessageTypes,
|
||||||
SmsSendMessageFunction,
|
SmsSendMessageFunction,
|
||||||
|
SmsSendTestMessageFunction,
|
||||||
ValidateConfig,
|
ValidateConfig,
|
||||||
SmsConnector,
|
SmsConnector,
|
||||||
GetConnectorConfig,
|
GetConnectorConfig,
|
||||||
|
@ -34,14 +35,10 @@ export default class TwilioSmsConnector implements SmsConnector {
|
||||||
return this.sendMessageBy(smsConfig, address, type, data);
|
return this.sendMessageBy(smsConfig, address, type, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
public sendTestMessage: SmsSendMessageFunction = async (address, type, data, config) => {
|
public sendTestMessage: SmsSendTestMessageFunction = async (phone, type, data, config) => {
|
||||||
if (!config) {
|
|
||||||
throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.validateConfig(config);
|
await this.validateConfig(config);
|
||||||
|
|
||||||
return this.sendMessageBy(config as TwilioSmsConfig, address, type, data);
|
return this.sendMessageBy(config as TwilioSmsConfig, phone, type, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
private readonly sendMessageBy = async (
|
private readonly sendMessageBy = async (
|
||||||
|
|
|
@ -33,6 +33,7 @@ export enum ConnectorErrorCodes {
|
||||||
InvalidConfig,
|
InvalidConfig,
|
||||||
InvalidResponse,
|
InvalidResponse,
|
||||||
TemplateNotFound,
|
TemplateNotFound,
|
||||||
|
NotImplemented,
|
||||||
SocialAuthCodeInvalid,
|
SocialAuthCodeInvalid,
|
||||||
SocialAccessTokenInvalid,
|
SocialAccessTokenInvalid,
|
||||||
SocialIdTokenInvalid,
|
SocialIdTokenInvalid,
|
||||||
|
@ -67,17 +68,29 @@ export type EmailMessageTypes = {
|
||||||
export type SmsMessageTypes = EmailMessageTypes;
|
export type SmsMessageTypes = EmailMessageTypes;
|
||||||
|
|
||||||
export type EmailSendMessageFunction<T = unknown> = (
|
export type EmailSendMessageFunction<T = unknown> = (
|
||||||
|
address: string,
|
||||||
|
type: keyof EmailMessageTypes,
|
||||||
|
payload: EmailMessageTypes[typeof type]
|
||||||
|
) => Promise<T>;
|
||||||
|
|
||||||
|
export type EmailSendTestMessageFunction<T = unknown> = (
|
||||||
address: string,
|
address: string,
|
||||||
type: keyof EmailMessageTypes,
|
type: keyof EmailMessageTypes,
|
||||||
payload: EmailMessageTypes[typeof type],
|
payload: EmailMessageTypes[typeof type],
|
||||||
config?: Record<string, unknown>
|
config: Record<string, unknown>
|
||||||
) => Promise<T>;
|
) => Promise<T>;
|
||||||
|
|
||||||
export type SmsSendMessageFunction<T = unknown> = (
|
export type SmsSendMessageFunction<T = unknown> = (
|
||||||
|
phone: string,
|
||||||
|
type: keyof SmsMessageTypes,
|
||||||
|
payload: SmsMessageTypes[typeof type]
|
||||||
|
) => Promise<T>;
|
||||||
|
|
||||||
|
export type SmsSendTestMessageFunction<T = unknown> = (
|
||||||
phone: string,
|
phone: string,
|
||||||
type: keyof SmsMessageTypes,
|
type: keyof SmsMessageTypes,
|
||||||
payload: SmsMessageTypes[typeof type],
|
payload: SmsMessageTypes[typeof type],
|
||||||
config?: Record<string, unknown>
|
config: Record<string, unknown>
|
||||||
) => Promise<T>;
|
) => Promise<T>;
|
||||||
|
|
||||||
export interface BaseConnector {
|
export interface BaseConnector {
|
||||||
|
@ -88,12 +101,12 @@ export interface BaseConnector {
|
||||||
|
|
||||||
export interface SmsConnector extends BaseConnector {
|
export interface SmsConnector extends BaseConnector {
|
||||||
sendMessage: SmsSendMessageFunction;
|
sendMessage: SmsSendMessageFunction;
|
||||||
sendTestMessage: SmsSendMessageFunction;
|
sendTestMessage?: SmsSendTestMessageFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EmailConnector extends BaseConnector {
|
export interface EmailConnector extends BaseConnector {
|
||||||
sendMessage: EmailSendMessageFunction;
|
sendMessage: EmailSendMessageFunction;
|
||||||
sendTestMessage: EmailSendMessageFunction;
|
sendTestMessage?: EmailSendTestMessageFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SocialConnector extends BaseConnector {
|
export interface SocialConnector extends BaseConnector {
|
||||||
|
|
|
@ -53,6 +53,14 @@ export default function koaConnectorErrorHandler<StateT, ContextT>(): Middleware
|
||||||
},
|
},
|
||||||
data
|
data
|
||||||
);
|
);
|
||||||
|
case ConnectorErrorCodes.NotImplemented:
|
||||||
|
throw new RequestError(
|
||||||
|
{
|
||||||
|
code: 'connector.not_implemented',
|
||||||
|
status: 500,
|
||||||
|
},
|
||||||
|
data
|
||||||
|
);
|
||||||
case ConnectorErrorCodes.SocialAuthCodeInvalid:
|
case ConnectorErrorCodes.SocialAuthCodeInvalid:
|
||||||
throw new RequestError(
|
throw new RequestError(
|
||||||
{
|
{
|
||||||
|
|
|
@ -187,17 +187,16 @@ export default function connectorRoutes<T extends AuthedRouter>(router: T) {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
if (config) {
|
const { sendTestMessage } = connector;
|
||||||
await connector.validateConfig(config);
|
assertThat(sendTestMessage, new RequestError('connector.not_implemented'));
|
||||||
}
|
|
||||||
|
|
||||||
await connector.sendTestMessage(
|
await sendTestMessage(
|
||||||
subject,
|
subject,
|
||||||
'Test',
|
'Test',
|
||||||
{
|
{
|
||||||
code: phone ? '123456' : 'email-test',
|
code: phone ? '123456' : 'email-test',
|
||||||
},
|
},
|
||||||
config
|
config as Record<string, unknown>
|
||||||
);
|
);
|
||||||
|
|
||||||
ctx.status = 204;
|
ctx.status = 204;
|
||||||
|
|
|
@ -606,6 +606,7 @@ const errors = {
|
||||||
invalid_config: "The connector's config is invalid.",
|
invalid_config: "The connector's config is invalid.",
|
||||||
invalid_response: "The connector's response is invalid.",
|
invalid_response: "The connector's response is invalid.",
|
||||||
template_not_found: 'Unable to find correct template in connector config.',
|
template_not_found: 'Unable to find correct template in connector config.',
|
||||||
|
not_implemented: 'The method has not been implemented.',
|
||||||
invalid_access_token: "The connector's access token is invalid.",
|
invalid_access_token: "The connector's access token is invalid.",
|
||||||
invalid_auth_code: "The connector's auth code is invalid.",
|
invalid_auth_code: "The connector's auth code is invalid.",
|
||||||
invalid_id_token: "The connector's id token is invalid.",
|
invalid_id_token: "The connector's id token is invalid.",
|
||||||
|
|
|
@ -584,6 +584,7 @@ const errors = {
|
||||||
invalid_config: '连接器配置错误',
|
invalid_config: '连接器配置错误',
|
||||||
invalid_response: '连接器错误响应',
|
invalid_response: '连接器错误响应',
|
||||||
template_not_found: '无法从连接器配置中找到对应的模板',
|
template_not_found: '无法从连接器配置中找到对应的模板',
|
||||||
|
not_implemented: '该方法尚未实现',
|
||||||
invalid_access_token: '当前连接器的 access_token 无效',
|
invalid_access_token: '当前连接器的 access_token 无效',
|
||||||
invalid_auth_code: '当前连接器的授权码无效',
|
invalid_auth_code: '当前连接器的授权码无效',
|
||||||
invalid_id_token: '当前连接器的 id_token 无效',
|
invalid_id_token: '当前连接器的 id_token 无效',
|
||||||
|
|
Loading…
Reference in a new issue