Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

feat: aliyun sms connector add custom endpoint feature #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions packages/connector-aliyun-sms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ Go to the [Aliyun website](https://cn.aliyun.com/) and register your Aliyun acco
- You can add multiple SMS connector templates for different cases. Here is an example of adding a single template:
- Fill the `templateCode` field, which is how you can control SMS context, with "Template Code" (模板 CODE) from step 2.
- Fill out `usageType` field with either `Register`, `SignIn`, `ForgotPassword`, `Generic` or `Test` for different use cases. (`usageType` is a Logto property to identify the proper use case.) In order to enable full user flows, templates with usageType `Register`, `SignIn` and `ForgotPassword` are required.
- If you need to send international SMS please set `endpoint` to the access point of the corresponding region

Here is an example of Aliyun SMS connector config JSON.

```json
{
"endpoint": "<sms-endpoint-url>",
"accessKeyId": "<your-access-key-id>",
"accessKeySecret": "<your-access-key-secret>",
"signName": "<Aliyun>",
Expand All @@ -90,7 +92,7 @@ Here is an example of Aliyun SMS connector config JSON.
{
"templateCode": "<SMS_567890>",
"usageType": "Test"
},
}
]
}
```
Expand All @@ -103,12 +105,13 @@ That's it. Don't forget to [Enable connector in sign-in experience](https://docs

### Config types

| Name | Type |
|-----------------|------------|
| accessKeyId | string |
| accessKeySecret | string |
| signName | string |
| templates | Template[] |
| Name | Type | Description |
|-----------------|-------------|----------------------------------------|
| endpoint | string null | Use mainland China endpoint when empty |
| accessKeyId | string | |
| accessKeySecret | string | |
| signName | string | |
| templates | Template[] | |

| Template Properties | Type | Enum values |
|---------------------|-------------|------------------------------------------------------|
Expand Down Expand Up @@ -160,11 +163,13 @@ That's it. Don't forget to [Enable connector in sign-in experience](https://docs
- 你可以添加多个短信服务模板以应对不同的用户场景。这里展示填写单个模板的例子:
- `templateCode` 栏是你可以用来控制所发送短信内容的属性。它们的值从步骤 2 中的「模板 CODE」获取。
- `usageType` 栏填写 `Register`,`SignIn`,`ForgotPassword`,`Generic` 或者 `Test` 其中之一以分别对应 _注册_,_登录_,_忘记密码_,_用户档案补全_ 和 _测试_ 的不同场景。(`usageType` 是 Logto 的属性,用来确定使用场景。)为了能够使用完成的流程,需要配置 `usageType` 为 `Register`,`SignIn` 以及 `ForgotPassword` 的模板。
- 如果你需要发送国际短息请设置 `endpoint` 为对应地区的接入点

这是一个阿里云短信服务连接器 JSON 配置的样例。

```json
{
"endpoint": "<sms-endpoint-url>",
"accessKeyId": "<your-access-key-id>",
"accessKeySecret": "<your-access-key-secret>",
"signName": "<Aliyun>",
Expand All @@ -188,7 +193,7 @@ That's it. Don't forget to [Enable connector in sign-in experience](https://docs
{
"templateCode": "<SMS_567890>",
"usageType": "Test"
},
}
]
}
```
Expand All @@ -201,12 +206,13 @@ That's it. Don't forget to [Enable connector in sign-in experience](https://docs

### 配置类型

| 名称 | 类型 |
|-----------------|------------|
| accessKeyId | string |
| accessKeySecret | string |
| signName | string |
| templates | Template[] |
| 名称 | 类型 | 描述 |
|------------------|-------------|--------------|
| endpoint | string null | 为空时使用中国大陆接入点 |
| accessKeyId | string | |
| accessKeySecret | string | |
| signName | string | |
| templates | Template[] | |

| 模板属性 | 类型 | 枚举值 |
|--------------|-------------|------------------------------------------------------|
Expand Down
1 change: 1 addition & 0 deletions packages/connector-aliyun-sms/docs/config-template.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"endpoint": "<sms-endpoint-url>",
"accessKeyId": "<access-key-id>",
"accessKeySecret": "<access-key-secret>",
"signName": "<sign-name>",
Expand Down
2 changes: 0 additions & 2 deletions packages/connector-aliyun-sms/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { ConnectorMetadata } from '@logto/connector-kit';

export const endpoint = 'https://dysmsapi.aliyuncs.com/';

export const staticConfigs = {
Format: 'json',
RegionId: 'cn-hangzhou',
Expand Down
5 changes: 4 additions & 1 deletion packages/connector-aliyun-sms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ const sendMessage =
const { to, type, payload } = data;
const config = inputConfig ?? (await getConfig(defaultMetadata.id));
validateConfig<AliyunSmsConfig>(config, aliyunSmsConfigGuard);
const { accessKeyId, accessKeySecret, signName, templates } = config;
const { endpoint, accessKeyId, accessKeySecret, signName, templates } = config;
const template = templates.find(({ usageType }) => usageType === type);

assert(
template,
new ConnectorError(ConnectorErrorCodes.TemplateNotFound, `Cannot find template!`)
);

const _endpoint = endpoint ?? 'https://dysmsapi.aliyuncs.com/';

try {
const httpResponse = await sendSms(
_endpoint,
{
AccessKeyId: accessKeyId,
PhoneNumbers: to,
Expand Down
1 change: 1 addition & 0 deletions packages/connector-aliyun-sms/src/mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const mockedConnectorConfig = {
endpoint: 'https://dysmsapi.aliyuncs.com/',
accessKeyId: 'accessKeyId',
accessKeySecret: 'accessKeySecret',
signName: 'signName',
Expand Down
1 change: 1 addition & 0 deletions packages/connector-aliyun-sms/src/single-send-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('sendSms', () => {
const code = mockedRandomCode;

await sendSms(
'https://dysmsapi.aliyuncs.com',
{
AccessKeyId: '<access-key-id>',
PhoneNumbers: '13912345678',
Expand Down
8 changes: 6 additions & 2 deletions packages/connector-aliyun-sms/src/single-send-text.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { endpoint, staticConfigs } from './constant.js';
import { staticConfigs } from './constant.js';
import type { PublicParameters, SendSms } from './types.js';
import { request } from './utils.js';

/**
* @doc https://help.aliyun.com/document_detail/101414.html
*/
export const sendSms = async (parameters: PublicParameters & SendSms, accessKeySecret: string) => {
export const sendSms = async (
endpoint: string,
parameters: PublicParameters & SendSms,
accessKeySecret: string
) => {
return request(endpoint, { Action: 'SendSms', ...staticConfigs, ...parameters }, accessKeySecret);
};
2 changes: 2 additions & 0 deletions packages/connector-aliyun-sms/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const templateGuard = z.object({
});

export const aliyunSmsConfigGuard = z.object({
// https://help.aliyun.com/document_detail/419270.html
endpoint: z.string().nullable().default('https://dysmsapi.aliyuncs.com/'),
accessKeyId: z.string(),
accessKeySecret: z.string(),
signName: z.string(),
Expand Down