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,
|
||||
EmailMessageTypes,
|
||||
EmailSendMessageFunction,
|
||||
EmailSendTestMessageFunction,
|
||||
ValidateConfig,
|
||||
EmailConnector,
|
||||
GetConnectorConfig,
|
||||
|
@ -40,11 +41,7 @@ export default class AliyunDmConnector implements EmailConnector {
|
|||
return this.sendMessageBy(emailConfig, address, type, data);
|
||||
};
|
||||
|
||||
public sendTestMessage: EmailSendMessageFunction = async (address, type, data, config) => {
|
||||
if (!config) {
|
||||
throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters);
|
||||
}
|
||||
|
||||
public sendTestMessage: EmailSendTestMessageFunction = async (address, type, data, config) => {
|
||||
await this.validateConfig(config);
|
||||
|
||||
return this.sendMessageBy(config as AliyunDmConfig, address, type, data);
|
||||
|
|
|
@ -4,6 +4,7 @@ import {
|
|||
ConnectorMetadata,
|
||||
SmsMessageTypes,
|
||||
SmsSendMessageFunction,
|
||||
SmsSendTestMessageFunction,
|
||||
ValidateConfig,
|
||||
SmsConnector,
|
||||
GetConnectorConfig,
|
||||
|
@ -35,14 +36,10 @@ export default class AliyunSmsConnector implements SmsConnector {
|
|||
return this.sendMessageBy(smsConfig, phone, type, { code });
|
||||
};
|
||||
|
||||
public sendTestMessage: SmsSendMessageFunction = async (phone, type, { code }, config) => {
|
||||
if (!config) {
|
||||
throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters);
|
||||
}
|
||||
|
||||
public sendTestMessage: SmsSendTestMessageFunction = async (phone, type, data, 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 (
|
||||
|
|
|
@ -4,6 +4,7 @@ import {
|
|||
ConnectorMetadata,
|
||||
EmailMessageTypes,
|
||||
EmailSendMessageFunction,
|
||||
EmailSendTestMessageFunction,
|
||||
ValidateConfig,
|
||||
EmailConnector,
|
||||
GetConnectorConfig,
|
||||
|
@ -41,11 +42,7 @@ export default class SendGridMailConnector implements EmailConnector {
|
|||
return this.sendMessageBy(emailConfig, address, type, data);
|
||||
};
|
||||
|
||||
public sendTestMessage: EmailSendMessageFunction = async (address, type, data, config) => {
|
||||
if (!config) {
|
||||
throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters);
|
||||
}
|
||||
|
||||
public sendTestMessage: EmailSendTestMessageFunction = async (address, type, data, config) => {
|
||||
await this.validateConfig(config);
|
||||
|
||||
return this.sendMessageBy(config as SendGridMailConfig, address, type, data);
|
||||
|
|
|
@ -4,6 +4,7 @@ import {
|
|||
ConnectorMetadata,
|
||||
EmailMessageTypes,
|
||||
EmailSendMessageFunction,
|
||||
EmailSendTestMessageFunction,
|
||||
ValidateConfig,
|
||||
EmailConnector,
|
||||
GetConnectorConfig,
|
||||
|
@ -35,11 +36,7 @@ export default class SmtpConnector implements EmailConnector {
|
|||
return this.sendMessageBy(emailConfig, address, type, data);
|
||||
};
|
||||
|
||||
public sendTestMessage: EmailSendMessageFunction = async (address, type, data, config) => {
|
||||
if (!config) {
|
||||
throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters);
|
||||
}
|
||||
|
||||
public sendTestMessage: EmailSendTestMessageFunction = async (address, type, data, config) => {
|
||||
await this.validateConfig(config);
|
||||
|
||||
return this.sendMessageBy(config as SmtpConfig, address, type, data);
|
||||
|
|
|
@ -4,6 +4,7 @@ import {
|
|||
ConnectorMetadata,
|
||||
SmsMessageTypes,
|
||||
SmsSendMessageFunction,
|
||||
SmsSendTestMessageFunction,
|
||||
ValidateConfig,
|
||||
SmsConnector,
|
||||
GetConnectorConfig,
|
||||
|
@ -34,14 +35,10 @@ export default class TwilioSmsConnector implements SmsConnector {
|
|||
return this.sendMessageBy(smsConfig, address, type, data);
|
||||
};
|
||||
|
||||
public sendTestMessage: SmsSendMessageFunction = async (address, type, data, config) => {
|
||||
if (!config) {
|
||||
throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters);
|
||||
}
|
||||
|
||||
public sendTestMessage: SmsSendTestMessageFunction = async (phone, type, data, 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 (
|
||||
|
|
|
@ -33,6 +33,7 @@ export enum ConnectorErrorCodes {
|
|||
InvalidConfig,
|
||||
InvalidResponse,
|
||||
TemplateNotFound,
|
||||
NotImplemented,
|
||||
SocialAuthCodeInvalid,
|
||||
SocialAccessTokenInvalid,
|
||||
SocialIdTokenInvalid,
|
||||
|
@ -67,17 +68,29 @@ export type EmailMessageTypes = {
|
|||
export type SmsMessageTypes = EmailMessageTypes;
|
||||
|
||||
export type EmailSendMessageFunction<T = unknown> = (
|
||||
address: string,
|
||||
type: keyof EmailMessageTypes,
|
||||
payload: EmailMessageTypes[typeof type]
|
||||
) => Promise<T>;
|
||||
|
||||
export type EmailSendTestMessageFunction<T = unknown> = (
|
||||
address: string,
|
||||
type: keyof EmailMessageTypes,
|
||||
payload: EmailMessageTypes[typeof type],
|
||||
config?: Record<string, unknown>
|
||||
config: Record<string, unknown>
|
||||
) => Promise<T>;
|
||||
|
||||
export type SmsSendMessageFunction<T = unknown> = (
|
||||
phone: string,
|
||||
type: keyof SmsMessageTypes,
|
||||
payload: SmsMessageTypes[typeof type]
|
||||
) => Promise<T>;
|
||||
|
||||
export type SmsSendTestMessageFunction<T = unknown> = (
|
||||
phone: string,
|
||||
type: keyof SmsMessageTypes,
|
||||
payload: SmsMessageTypes[typeof type],
|
||||
config?: Record<string, unknown>
|
||||
config: Record<string, unknown>
|
||||
) => Promise<T>;
|
||||
|
||||
export interface BaseConnector {
|
||||
|
@ -88,12 +101,12 @@ export interface BaseConnector {
|
|||
|
||||
export interface SmsConnector extends BaseConnector {
|
||||
sendMessage: SmsSendMessageFunction;
|
||||
sendTestMessage: SmsSendMessageFunction;
|
||||
sendTestMessage?: SmsSendTestMessageFunction;
|
||||
}
|
||||
|
||||
export interface EmailConnector extends BaseConnector {
|
||||
sendMessage: EmailSendMessageFunction;
|
||||
sendTestMessage: EmailSendMessageFunction;
|
||||
sendTestMessage?: EmailSendTestMessageFunction;
|
||||
}
|
||||
|
||||
export interface SocialConnector extends BaseConnector {
|
||||
|
|
|
@ -53,6 +53,14 @@ export default function koaConnectorErrorHandler<StateT, ContextT>(): Middleware
|
|||
},
|
||||
data
|
||||
);
|
||||
case ConnectorErrorCodes.NotImplemented:
|
||||
throw new RequestError(
|
||||
{
|
||||
code: 'connector.not_implemented',
|
||||
status: 500,
|
||||
},
|
||||
data
|
||||
);
|
||||
case ConnectorErrorCodes.SocialAuthCodeInvalid:
|
||||
throw new RequestError(
|
||||
{
|
||||
|
|
|
@ -187,17 +187,16 @@ export default function connectorRoutes<T extends AuthedRouter>(router: T) {
|
|||
})
|
||||
);
|
||||
|
||||
if (config) {
|
||||
await connector.validateConfig(config);
|
||||
}
|
||||
const { sendTestMessage } = connector;
|
||||
assertThat(sendTestMessage, new RequestError('connector.not_implemented'));
|
||||
|
||||
await connector.sendTestMessage(
|
||||
await sendTestMessage(
|
||||
subject,
|
||||
'Test',
|
||||
{
|
||||
code: phone ? '123456' : 'email-test',
|
||||
},
|
||||
config
|
||||
config as Record<string, unknown>
|
||||
);
|
||||
|
||||
ctx.status = 204;
|
||||
|
|
|
@ -606,6 +606,7 @@ const errors = {
|
|||
invalid_config: "The connector's config is invalid.",
|
||||
invalid_response: "The connector's response is invalid.",
|
||||
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_auth_code: "The connector's auth code is invalid.",
|
||||
invalid_id_token: "The connector's id token is invalid.",
|
||||
|
|
|
@ -584,6 +584,7 @@ const errors = {
|
|||
invalid_config: '连接器配置错误',
|
||||
invalid_response: '连接器错误响应',
|
||||
template_not_found: '无法从连接器配置中找到对应的模板',
|
||||
not_implemented: '该方法尚未实现',
|
||||
invalid_access_token: '当前连接器的 access_token 无效',
|
||||
invalid_auth_code: '当前连接器的授权码无效',
|
||||
invalid_id_token: '当前连接器的 id_token 无效',
|
||||
|
|
Loading…
Reference in a new issue