2022-08-20 18:06:12 +08:00
|
|
|
import path from 'path';
|
|
|
|
|
2022-09-29 21:05:31 +08:00
|
|
|
import { Context } from '@nocobase/actions';
|
2022-08-20 18:06:12 +08:00
|
|
|
import { Op } from '@nocobase/database';
|
|
|
|
import { HandlerType } from '@nocobase/resourcer';
|
2022-09-29 21:05:31 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
|
|
|
import { Registry } from '@nocobase/utils';
|
2022-08-20 18:06:12 +08:00
|
|
|
|
2022-09-29 21:05:31 +08:00
|
|
|
import { namespace } from '.';
|
2022-08-20 18:06:12 +08:00
|
|
|
import initActions from './actions';
|
|
|
|
import { CODE_STATUS_UNUSED, CODE_STATUS_USED, PROVIDER_TYPE_SMS_ALIYUN } from './constants';
|
|
|
|
import { zhCN } from './locale';
|
2022-09-29 21:05:31 +08:00
|
|
|
import initProviders, { Provider } from './providers';
|
2022-08-20 18:06:12 +08:00
|
|
|
|
|
|
|
export interface Interceptor {
|
|
|
|
manual?: boolean;
|
|
|
|
expiresIn?: number;
|
2023-01-08 12:45:02 +08:00
|
|
|
|
2022-08-20 18:06:12 +08:00
|
|
|
getReceiver(ctx): string;
|
2023-01-08 12:45:02 +08:00
|
|
|
|
2022-08-20 18:06:12 +08:00
|
|
|
getCode?(ctx): string;
|
2023-01-08 12:45:02 +08:00
|
|
|
|
2022-08-20 18:06:12 +08:00
|
|
|
validate?(ctx: Context, receiver: string): boolean | Promise<boolean>;
|
2022-10-16 19:16:14 +08:00
|
|
|
}
|
2022-08-20 18:06:12 +08:00
|
|
|
|
|
|
|
export default class VerificationPlugin extends Plugin {
|
|
|
|
providers: Registry<typeof Provider> = new Registry();
|
|
|
|
interceptors: Registry<Interceptor> = new Registry();
|
|
|
|
|
|
|
|
intercept: HandlerType = async (context, next) => {
|
|
|
|
const { resourceName, actionName, values } = context.action.params;
|
|
|
|
const key = `${resourceName}:${actionName}`;
|
|
|
|
const interceptor = this.interceptors.get(key);
|
|
|
|
|
|
|
|
if (!interceptor) {
|
|
|
|
return context.throw(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
const receiver = interceptor.getReceiver(context);
|
|
|
|
const content = interceptor.getCode ? interceptor.getCode(context) : values.code;
|
|
|
|
if (!receiver || !content) {
|
|
|
|
return context.throw(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if code match, then call next
|
|
|
|
// find the code based on action params
|
|
|
|
const VerificationRepo = this.db.getRepository('verifications');
|
|
|
|
const item = await VerificationRepo.findOne({
|
|
|
|
filter: {
|
|
|
|
receiver,
|
|
|
|
type: key,
|
|
|
|
content,
|
|
|
|
expiresAt: {
|
2022-10-16 19:16:14 +08:00
|
|
|
[Op.gt]: new Date(),
|
2022-08-20 18:06:12 +08:00
|
|
|
},
|
2022-10-16 19:16:14 +08:00
|
|
|
status: CODE_STATUS_UNUSED,
|
|
|
|
},
|
2022-08-20 18:06:12 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!item) {
|
2023-01-08 12:45:02 +08:00
|
|
|
return context.throw(400, {
|
|
|
|
code: 'InvalidVerificationCode',
|
|
|
|
message: context.t('Verification code is invalid', { ns: namespace }),
|
|
|
|
});
|
2022-08-20 18:06:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: code should be removed if exists in values
|
|
|
|
// context.action.mergeParams({
|
|
|
|
// values: {
|
|
|
|
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
|
|
|
|
await next();
|
|
|
|
|
|
|
|
// or delete
|
|
|
|
await item.update({
|
2022-10-16 19:16:14 +08:00
|
|
|
status: CODE_STATUS_USED,
|
2022-08-20 18:06:12 +08:00
|
|
|
});
|
2022-10-16 19:16:14 +08:00
|
|
|
};
|
2022-08-20 18:06:12 +08:00
|
|
|
|
|
|
|
async install() {
|
|
|
|
const {
|
|
|
|
DEFAULT_SMS_VERIFY_CODE_PROVIDER,
|
|
|
|
INIT_ALI_SMS_ACCESS_KEY,
|
|
|
|
INIT_ALI_SMS_ACCESS_KEY_SECRET,
|
|
|
|
INIT_ALI_SMS_ENDPOINT = 'dysmsapi.aliyuncs.com',
|
|
|
|
INIT_ALI_SMS_VERIFY_CODE_TEMPLATE,
|
2022-10-16 19:16:14 +08:00
|
|
|
INIT_ALI_SMS_VERIFY_CODE_SIGN,
|
2022-08-20 18:06:12 +08:00
|
|
|
} = process.env;
|
|
|
|
|
2022-10-16 19:16:14 +08:00
|
|
|
if (
|
2022-12-02 10:19:40 +08:00
|
|
|
DEFAULT_SMS_VERIFY_CODE_PROVIDER &&
|
2022-10-16 19:16:14 +08:00
|
|
|
INIT_ALI_SMS_ACCESS_KEY &&
|
|
|
|
INIT_ALI_SMS_ACCESS_KEY_SECRET &&
|
|
|
|
INIT_ALI_SMS_VERIFY_CODE_TEMPLATE &&
|
|
|
|
INIT_ALI_SMS_VERIFY_CODE_SIGN
|
2022-08-20 18:06:12 +08:00
|
|
|
) {
|
|
|
|
const ProviderRepo = this.db.getRepository('verifications_providers');
|
|
|
|
await ProviderRepo.create({
|
|
|
|
values: {
|
|
|
|
id: DEFAULT_SMS_VERIFY_CODE_PROVIDER,
|
|
|
|
type: PROVIDER_TYPE_SMS_ALIYUN,
|
|
|
|
title: 'Default SMS sender',
|
|
|
|
options: {
|
|
|
|
accessKeyId: INIT_ALI_SMS_ACCESS_KEY,
|
|
|
|
accessKeySecret: INIT_ALI_SMS_ACCESS_KEY_SECRET,
|
|
|
|
endpoint: INIT_ALI_SMS_ENDPOINT,
|
|
|
|
sign: INIT_ALI_SMS_VERIFY_CODE_SIGN,
|
2022-10-16 19:16:14 +08:00
|
|
|
template: INIT_ALI_SMS_VERIFY_CODE_TEMPLATE,
|
|
|
|
},
|
2023-01-08 12:45:02 +08:00
|
|
|
default: true,
|
2022-10-16 19:16:14 +08:00
|
|
|
},
|
2022-08-20 18:06:12 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
const { app, db, options } = this;
|
|
|
|
|
|
|
|
app.i18n.addResources('zh-CN', namespace, zhCN);
|
|
|
|
|
2023-01-08 12:45:02 +08:00
|
|
|
await this.importCollections(path.resolve(__dirname, 'collections'));
|
2022-08-20 18:06:12 +08:00
|
|
|
|
|
|
|
initProviders(this);
|
|
|
|
initActions(this);
|
|
|
|
|
|
|
|
// add middleware to action
|
|
|
|
app.resourcer.use(async (context, next) => {
|
|
|
|
const { resourceName, actionName, values } = context.action.params;
|
|
|
|
const key = `${resourceName}:${actionName}`;
|
|
|
|
const interceptor = this.interceptors.get(key);
|
|
|
|
if (!interceptor || interceptor.manual) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.intercept(context, next);
|
|
|
|
});
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
app.acl.allow('verifications', 'create', 'public');
|
|
|
|
this.app.acl.registerSnippet({
|
|
|
|
name: `pm.${this.name}.providers`,
|
|
|
|
actions: ['verifications_providers:*'],
|
|
|
|
});
|
2022-11-28 16:41:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async getDefault() {
|
|
|
|
const providerRepo = this.db.getRepository('verifications_providers');
|
|
|
|
return providerRepo.findOne({
|
|
|
|
filter: {
|
|
|
|
default: true,
|
2023-01-08 12:45:02 +08:00
|
|
|
},
|
2022-11-28 16:41:58 +08:00
|
|
|
});
|
2022-08-20 18:06:12 +08:00
|
|
|
}
|
|
|
|
}
|