Add custom HTTP SMS provider

This commit is contained in:
Yang Luo 2023-08-12 12:52:53 +08:00
parent 9f65053d04
commit 33a922f026
5 changed files with 214 additions and 73 deletions

View File

@ -140,10 +140,12 @@ func (c *ApiController) SendSms() {
return return
} }
invalidReceivers := getInvalidSmsReceivers(smsForm) if provider.Type != "Custom HTTP SMS" {
if len(invalidReceivers) != 0 { invalidReceivers := getInvalidSmsReceivers(smsForm)
c.ResponseError(fmt.Sprintf(c.T("service:Invalid phone receivers: %s"), strings.Join(invalidReceivers, ", "))) if len(invalidReceivers) != 0 {
return c.ResponseError(fmt.Sprintf(c.T("service:Invalid phone receivers: %s"), strings.Join(invalidReceivers, ", ")))
return
}
} }
err = object.SendSms(provider, smsForm.Content, smsForm.Receivers...) err = object.SendSms(provider, smsForm.Content, smsForm.Receivers...)

View File

@ -26,6 +26,8 @@ func getSmsClient(provider *Provider) (sender.SmsClient, error) {
if provider.Type == sender.HuaweiCloud || provider.Type == sender.AzureACS { if provider.Type == sender.HuaweiCloud || provider.Type == sender.AzureACS {
client, err = sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.TemplateCode, provider.ProviderUrl, provider.AppId) client, err = sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.TemplateCode, provider.ProviderUrl, provider.AppId)
} else if provider.Type == "Custom HTTP SMS" {
client, err = newHttpSmsClient(provider.Endpoint, provider.Method, provider.ClientId, provider.Title)
} else { } else {
client, err = sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.TemplateCode, provider.AppId) client, err = sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.TemplateCode, provider.AppId)
} }

75
object/sms_custom.go Normal file
View File

@ -0,0 +1,75 @@
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"bytes"
"fmt"
"net/http"
"github.com/casdoor/casdoor/proxy"
)
type HttpSmsClient struct {
endpoint string
method string
paramName string
text string
}
func newHttpSmsClient(endpoint string, method string, paramName string, text string) (*HttpSmsClient, error) {
client := &HttpSmsClient{
endpoint: endpoint,
method: method,
paramName: paramName,
text: text,
}
return client, nil
}
func (c *HttpSmsClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
var err error
content := param["code"]
httpClient := proxy.DefaultHttpClient
req, err := http.NewRequest(c.method, c.endpoint, bytes.NewBufferString(content))
if err != nil {
return err
}
if c.method == "POST" {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.PostForm = map[string][]string{
c.paramName: {content},
}
} else if c.method == "GET" {
q := req.URL.Query()
q.Add(c.paramName, content)
req.URL.RawQuery = q.Encode()
}
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("SendMessage() error, custom HTTP SMS request failed with status: %s", resp.Status)
}
return err
}

View File

@ -356,7 +356,7 @@ class ProviderEditPage extends React.Component {
<Select virtual={false} style={{width: "100%"}} value={this.state.provider.category} onChange={(value => { <Select virtual={false} style={{width: "100%"}} value={this.state.provider.category} onChange={(value => {
this.updateProviderField("category", value); this.updateProviderField("category", value);
if (value === "OAuth") { if (value === "OAuth") {
this.updateProviderField("type", "GitHub"); this.updateProviderField("type", "Google");
} else if (value === "Email") { } else if (value === "Email") {
this.updateProviderField("type", "Default"); this.updateProviderField("type", "Default");
this.updateProviderField("host", "smtp.example.com"); this.updateProviderField("host", "smtp.example.com");
@ -366,12 +366,12 @@ class ProviderEditPage extends React.Component {
this.updateProviderField("content", "You have requested a verification code at Casdoor. Here is your code: %s, please enter in 5 minutes."); this.updateProviderField("content", "You have requested a verification code at Casdoor. Here is your code: %s, please enter in 5 minutes.");
this.updateProviderField("receiver", this.props.account.email); this.updateProviderField("receiver", this.props.account.email);
} else if (value === "SMS") { } else if (value === "SMS") {
this.updateProviderField("type", "Aliyun SMS"); this.updateProviderField("type", "Twilio SMS");
} else if (value === "Storage") { } else if (value === "Storage") {
this.updateProviderField("type", "Local File System"); this.updateProviderField("type", "AWS S3");
this.updateProviderField("domain", Setting.getFullServerUrl()); this.updateProviderField("domain", Setting.getFullServerUrl());
} else if (value === "SAML") { } else if (value === "SAML") {
this.updateProviderField("type", "Aliyun IDaaS"); this.updateProviderField("type", "Keycloak");
} else if (value === "Payment") { } else if (value === "Payment") {
this.updateProviderField("type", "PayPal"); this.updateProviderField("type", "PayPal");
} else if (value === "Captcha") { } else if (value === "Captcha") {
@ -406,12 +406,16 @@ class ProviderEditPage extends React.Component {
this.updateProviderField("type", value); this.updateProviderField("type", value);
if (value === "Local File System") { if (value === "Local File System") {
this.updateProviderField("domain", Setting.getFullServerUrl()); this.updateProviderField("domain", Setting.getFullServerUrl());
} } else if (value === "Custom") {
if (value === "Custom") {
this.updateProviderField("customAuthUrl", "https://door.casdoor.com/login/oauth/authorize"); this.updateProviderField("customAuthUrl", "https://door.casdoor.com/login/oauth/authorize");
this.updateProviderField("scopes", "openid profile email"); this.updateProviderField("scopes", "openid profile email");
this.updateProviderField("customTokenUrl", "https://door.casdoor.com/api/login/oauth/access_token"); this.updateProviderField("customTokenUrl", "https://door.casdoor.com/api/login/oauth/access_token");
this.updateProviderField("customUserInfoUrl", "https://door.casdoor.com/api/userinfo"); this.updateProviderField("customUserInfoUrl", "https://door.casdoor.com/api/userinfo");
} else if (value === "Custom HTTP SMS") {
this.updateProviderField("endpoint", "https://door.casdoor.com/api/get-account");
this.updateProviderField("method", "GET");
this.updateProviderField("clientId", "param1");
this.updateProviderField("title", "");
} }
})}> })}>
{ {
@ -547,30 +551,33 @@ class ProviderEditPage extends React.Component {
) )
} }
{ {
(this.state.provider.category === "Captcha" && this.state.provider.type === "Default") || (this.state.provider.category === "Web3") || (this.state.provider.category === "Storage" && this.state.provider.type === "Local File System") ? null : ( (this.state.provider.category === "Captcha" && this.state.provider.type === "Default") ||
<React.Fragment> (this.state.provider.category === "SMS" && this.state.provider.type === "Custom HTTP SMS") ||
<Row style={{marginTop: "20px"}} > (this.state.provider.category === "Web3") ||
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}> (this.state.provider.category === "Storage" && this.state.provider.type === "Local File System") ? null : (
{this.getClientIdLabel(this.state.provider)} : <React.Fragment>
</Col> <Row style={{marginTop: "20px"}} >
<Col span={22} > <Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
<Input value={this.state.provider.clientId} onChange={e => { {this.getClientIdLabel(this.state.provider)} :
this.updateProviderField("clientId", e.target.value); </Col>
}} /> <Col span={22} >
</Col> <Input value={this.state.provider.clientId} onChange={e => {
</Row> this.updateProviderField("clientId", e.target.value);
<Row style={{marginTop: "20px"}} > }} />
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}> </Col>
{this.getClientSecretLabel(this.state.provider)} : </Row>
</Col> <Row style={{marginTop: "20px"}} >
<Col span={22} > <Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
<Input value={this.state.provider.clientSecret} onChange={e => { {this.getClientSecretLabel(this.state.provider)} :
this.updateProviderField("clientSecret", e.target.value); </Col>
}} /> <Col span={22} >
</Col> <Input value={this.state.provider.clientSecret} onChange={e => {
</Row> this.updateProviderField("clientSecret", e.target.value);
</React.Fragment> }} />
) </Col>
</Row>
</React.Fragment>
)
} }
{ {
this.state.provider.category !== "Email" && this.state.provider.type !== "WeChat" && this.state.provider.type !== "Aliyun Captcha" && this.state.provider.type !== "WeChat Pay" ? null : ( this.state.provider.category !== "Email" && this.state.provider.type !== "WeChat" && this.state.provider.type !== "Aliyun Captcha" && this.state.provider.type !== "WeChat Pay" ? null : (
@ -623,14 +630,14 @@ class ProviderEditPage extends React.Component {
{Setting.getLabel(i18next.t("provider:Domain"), i18next.t("provider:Domain - Tooltip"))} : {Setting.getLabel(i18next.t("provider:Domain"), i18next.t("provider:Domain - Tooltip"))} :
</Col> </Col>
<Col span={22} > <Col span={22} >
<Input value={this.state.provider.domain} onChange={e => { <Input prefix={<LinkOutlined />} value={this.state.provider.domain} onChange={e => {
this.updateProviderField("domain", e.target.value); this.updateProviderField("domain", e.target.value);
}} /> }} />
</Col> </Col>
</Row> </Row>
) )
} }
{this.state.provider.category === "Storage" ? ( {this.state.provider.category === "Storage" || this.state.provider.type === "Custom HTTP SMS" ? (
<div> <div>
{["Local File System"].includes(this.state.provider.type) ? null : ( {["Local File System"].includes(this.state.provider.type) ? null : (
<Row style={{marginTop: "20px"}} > <Row style={{marginTop: "20px"}} >
@ -638,25 +645,25 @@ class ProviderEditPage extends React.Component {
{Setting.getLabel(i18next.t("provider:Endpoint"), i18next.t("provider:Region endpoint for Internet"))} : {Setting.getLabel(i18next.t("provider:Endpoint"), i18next.t("provider:Region endpoint for Internet"))} :
</Col> </Col>
<Col span={22} > <Col span={22} >
<Input value={this.state.provider.endpoint} onChange={e => { <Input prefix={<LinkOutlined />} value={this.state.provider.endpoint} onChange={e => {
this.updateProviderField("endpoint", e.target.value); this.updateProviderField("endpoint", e.target.value);
}} /> }} />
</Col> </Col>
</Row> </Row>
)} )}
{["Local File System", "MinIO", "Tencent Cloud COS", "Google Cloud Storage", "Qiniu Cloud Kodo"].includes(this.state.provider.type) ? null : ( {["Custom HTTP SMS", "Local File System", "MinIO", "Tencent Cloud COS", "Google Cloud Storage", "Qiniu Cloud Kodo"].includes(this.state.provider.type) ? null : (
<Row style={{marginTop: "20px"}} > <Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={2}> <Col style={{marginTop: "5px"}} span={2}>
{Setting.getLabel(i18next.t("provider:Endpoint (Intranet)"), i18next.t("provider:Region endpoint for Intranet"))} : {Setting.getLabel(i18next.t("provider:Endpoint (Intranet)"), i18next.t("provider:Region endpoint for Intranet"))} :
</Col> </Col>
<Col span={22} > <Col span={22} >
<Input value={this.state.provider.intranetEndpoint} onChange={e => { <Input prefix={<LinkOutlined />} value={this.state.provider.intranetEndpoint} onChange={e => {
this.updateProviderField("intranetEndpoint", e.target.value); this.updateProviderField("intranetEndpoint", e.target.value);
}} /> }} />
</Col> </Col>
</Row> </Row>
)} )}
{["Local File System"].includes(this.state.provider.type) ? null : ( {["Custom HTTP SMS", "Local File System"].includes(this.state.provider.type) ? null : (
<Row style={{marginTop: "20px"}} > <Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={2}> <Col style={{marginTop: "5px"}} span={2}>
{Setting.getLabel(i18next.t("provider:Bucket"), i18next.t("provider:Bucket - Tooltip"))} : {Setting.getLabel(i18next.t("provider:Bucket"), i18next.t("provider:Bucket - Tooltip"))} :
@ -668,23 +675,25 @@ class ProviderEditPage extends React.Component {
</Col> </Col>
</Row> </Row>
)} )}
<Row style={{marginTop: "20px"}} > {["Custom HTTP SMS"].includes(this.state.provider.type) ? null : (
<Col style={{marginTop: "5px"}} span={2}> <Row style={{marginTop: "20px"}} >
{Setting.getLabel(i18next.t("provider:Path prefix"), i18next.t("provider:Path prefix - Tooltip"))} : <Col style={{marginTop: "5px"}} span={2}>
</Col> {Setting.getLabel(i18next.t("provider:Path prefix"), i18next.t("provider:Path prefix - Tooltip"))} :
<Col span={22} > </Col>
<Input value={this.state.provider.pathPrefix} onChange={e => { <Col span={22} >
this.updateProviderField("pathPrefix", e.target.value); <Input value={this.state.provider.pathPrefix} onChange={e => {
}} /> this.updateProviderField("pathPrefix", e.target.value);
</Col> }} />
</Row> </Col>
{["MinIO", "Google Cloud Storage", "Qiniu Cloud Kodo"].includes(this.state.provider.type) ? null : ( </Row>
)}
{["Custom HTTP SMS", "MinIO", "Google Cloud Storage", "Qiniu Cloud Kodo"].includes(this.state.provider.type) ? null : (
<Row style={{marginTop: "20px"}} > <Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={2}> <Col style={{marginTop: "5px"}} span={2}>
{Setting.getLabel(i18next.t("provider:Domain"), i18next.t("provider:Domain - Tooltip"))} : {Setting.getLabel(i18next.t("provider:Domain"), i18next.t("provider:Domain - Tooltip"))} :
</Col> </Col>
<Col span={22} > <Col span={22} >
<Input value={this.state.provider.domain} disabled={this.state.provider.type === "Local File System"} onChange={e => { <Input prefix={<LinkOutlined />} value={this.state.provider.domain} disabled={this.state.provider.type === "Local File System"} onChange={e => {
this.updateProviderField("domain", e.target.value); this.updateProviderField("domain", e.target.value);
}} /> }} />
</Col> </Col>
@ -704,6 +713,49 @@ class ProviderEditPage extends React.Component {
) : null} ) : null}
</div> </div>
) : null} ) : null}
{
this.state.provider.type !== "Custom HTTP SMS" ? null : (
<React.Fragment>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={2}>
{Setting.getLabel(i18next.t("general:Method"), i18next.t("provider:Method - Tooltip"))} :
</Col>
<Col span={22} >
<Select virtual={false} style={{width: "100%"}} value={this.state.provider.method} onChange={value => {
this.updateProviderField("method", value);
}}>
{
[
{id: "GET", name: "GET"},
{id: "POST", name: "POST"},
].map((method, index) => <Option key={index} value={method.id}>{method.name}</Option>)
}
</Select>
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("provider:Parameter name"), i18next.t("provider:Parameter name - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.provider.clientId} onChange={e => {
this.updateProviderField("clientId", e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("provider:Content"), i18next.t("provider:Content - Tooltip"))} :
</Col>
<Col span={22} >
<TextArea autoSize={{minRows: 3, maxRows: 100}} value={this.state.provider.title} onChange={e => {
this.updateProviderField("title", e.target.value);
}} />
</Col>
</Row>
</React.Fragment>
)
}
{this.getAppIdRow(this.state.provider)} {this.getAppIdRow(this.state.provider)}
{ {
this.state.provider.category === "Email" ? ( this.state.provider.category === "Email" ? (
@ -779,7 +831,7 @@ class ProviderEditPage extends React.Component {
</React.Fragment> </React.Fragment>
) : this.state.provider.category === "SMS" ? ( ) : this.state.provider.category === "SMS" ? (
<React.Fragment> <React.Fragment>
{["Twilio SMS", "Amazon SNS", "Azure ACS", "Msg91 SMS", "Infobip SMS"].includes(this.state.provider.type) ? {["Custom HTTP SMS", "Twilio SMS", "Amazon SNS", "Azure ACS", "Msg91 SMS", "Infobip SMS"].includes(this.state.provider.type) ?
null : null :
(<Row style={{marginTop: "20px"}} > (<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}> <Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
@ -793,7 +845,7 @@ class ProviderEditPage extends React.Component {
</Row> </Row>
) )
} }
{["Infobip SMS"].includes(this.state.provider.type) ? {["Custom HTTP SMS", "Infobip SMS"].includes(this.state.provider.type) ?
null : null :
(<Row style={{marginTop: "20px"}} > (<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}> <Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
@ -811,27 +863,32 @@ class ProviderEditPage extends React.Component {
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}> <Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("provider:SMS Test"), i18next.t("provider:SMS Test - Tooltip"))} : {Setting.getLabel(i18next.t("provider:SMS Test"), i18next.t("provider:SMS Test - Tooltip"))} :
</Col> </Col>
<Col span={4} > {["Custom HTTP SMS"].includes(this.state.provider.type) ?
<Input.Group compact> null :
<CountryCodeSelect (
style={{width: "30%"}} <Col span={4} >
value={this.state.provider.content} <Input.Group compact>
onChange={(value) => { <CountryCodeSelect
this.updateProviderField("content", value); style={{width: "30%"}}
}} value={this.state.provider.content}
countryCodes={this.props.account.organization.countryCodes} onChange={(value) => {
/> this.updateProviderField("content", value);
<Input value={this.state.provider.receiver} }}
style={{width: "70%"}} countryCodes={this.props.account.organization.countryCodes}
placeholder = {i18next.t("user:Input your phone number")} />
onChange={e => { <Input value={this.state.provider.receiver}
this.updateProviderField("receiver", e.target.value); style={{width: "70%"}}
}} /> placeholder = {i18next.t("user:Input your phone number")}
</Input.Group> onChange={e => {
</Col> this.updateProviderField("receiver", e.target.value);
}} />
</Input.Group>
</Col>
)
}
<Col span={2} > <Col span={2} >
<Button style={{marginLeft: "10px", marginBottom: "5px"}} type="primary" <Button style={{marginLeft: "10px", marginBottom: "5px"}} type="primary"
disabled={!Setting.isValidPhone(this.state.provider.receiver)} disabled={!Setting.isValidPhone(this.state.provider.receiver) && (this.state.provider.type !== "Custom HTTP SMS" || this.state.provider.endpoint === "")}
onClick={() => ProviderEditTestSms.sendTestSms(this.state.provider, "+" + Setting.getCountryCode(this.state.provider.content) + this.state.provider.receiver)} > onClick={() => ProviderEditTestSms.sendTestSms(this.state.provider, "+" + Setting.getCountryCode(this.state.provider.content) + this.state.provider.receiver)} >
{i18next.t("provider:Send Testing SMS")} {i18next.t("provider:Send Testing SMS")}
</Button> </Button>

View File

@ -93,6 +93,10 @@ export const OtherProviderInfo = {
logo: `${StaticBaseUrl}/img/social_azure.png`, logo: `${StaticBaseUrl}/img/social_azure.png`,
url: "https://azure.microsoft.com/en-us/products/communication-services", url: "https://azure.microsoft.com/en-us/products/communication-services",
}, },
"Custom HTTP SMS": {
logo: `${StaticBaseUrl}/img/email_default.png`,
url: "https://casdoor.org/docs/provider/sms/overview",
},
"Infobip SMS": { "Infobip SMS": {
logo: `${StaticBaseUrl}/img/social_infobip.png`, logo: `${StaticBaseUrl}/img/social_infobip.png`,
url: "https://portal.infobip.com/homepage/", url: "https://portal.infobip.com/homepage/",
@ -888,6 +892,7 @@ export function getProviderTypeOptions(category) {
{id: "Aliyun SMS", name: "Alibaba Cloud SMS"}, {id: "Aliyun SMS", name: "Alibaba Cloud SMS"},
{id: "Amazon SNS", name: "Amazon SNS"}, {id: "Amazon SNS", name: "Amazon SNS"},
{id: "Azure ACS", name: "Azure ACS"}, {id: "Azure ACS", name: "Azure ACS"},
{id: "Custom HTTP SMS", name: "Custom HTTP SMS"},
{id: "Infobip SMS", name: "Infobip SMS"}, {id: "Infobip SMS", name: "Infobip SMS"},
{id: "Tencent Cloud SMS", name: "Tencent Cloud SMS"}, {id: "Tencent Cloud SMS", name: "Tencent Cloud SMS"},
{id: "Baidu Cloud SMS", name: "Baidu Cloud SMS"}, {id: "Baidu Cloud SMS", name: "Baidu Cloud SMS"},