Skip to content

Commit 7640821

Browse files
authored
Merge pull request #15 from zgsm-plugin/main
feat(sms): Update SMS provider
2 parents c3b7c4b + 835b20e commit 7640821

File tree

9 files changed

+135
-244
lines changed

9 files changed

+135
-244
lines changed

charts/oidc-auth/templates/configmap.yaml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ data:
2626
providers:
2727
casdoor:
2828
clientID: {{ .Values.providers.casdoor.clientID | quote }}
29-
clientSecret: "PROVIDERS_CASDOOR__CLIENTSECRET"
29+
clientSecret: {{ .Values.providers.casdoor.clientSecret | quote }}
3030
baseURL: {{ .Values.providers.casdoor.baseURL | quote }}
3131
internalURL: {{ .Values.providers.casdoor.internalURL }}
3232
syncStar:
3333
enabled: {{ .Values.syncStar.enabled | quote }}
34-
personalToken: "SYNCSTAR_PERSONALTOKEN"
34+
personalToken: {{ .Values.syncStar.personalToken | quote }}
3535
owner: {{ .Values.syncStar.owner | quote }}
3636
repo: {{ .Values.syncStar.repo | quote }}
3737
interval: {{ .Values.syncStar.interval }} # minute
@@ -40,12 +40,12 @@ data:
4040
host: {{ .Values.database.host | quote }}
4141
port: {{ .Values.database.port }}
4242
username: {{ .Values.database.username | quote }}
43-
password: "DATABASE_PASSWORD"
43+
password: {{ .Values.database.password | quote }}
4444
dbname: {{ .Values.database.dbname | quote }}
4545
maxIdleConns: {{ .Values.database.maxIdleConns }}
4646
maxOpenConns: {{ .Values.database.maxOpenConns }}
4747
encrypt:
48-
aesKey: "ENCRYPT_AESKEY"
48+
aesKey: {{ .Values.encrypt.aesKey | quote }}
4949
enableRsa: {{ .Values.encrypt.enableRsa | quote }}
5050
privateKey: {{ .Values.encrypt.privateKey | quote }}
5151
publicKey: {{ .Values.encrypt.publicKey | quote }}
@@ -58,7 +58,8 @@ data:
5858
compress: {{ .Values.log.compress }}
5959
sms:
6060
enabledTest: {{ .Values.sms.enabledTest | quote }}
61-
clientID: {{ .Values.sms.clientID | quote }}
62-
clientSecret: "SMS_CLIENTSECRET"
63-
tokenURL: {{ .Values.sms.tokenURL | quote }}
61+
appID: {{ .Values.sms.appID | quote }}
62+
apiKey: {{ .Values.sms.apiKey | quote }}
63+
mchID: {{ .Values.sms.mchID | quote }}
64+
templateID: {{ .Values.sms.templateID | quote }}
6465
sendURL: {{ .Values.sms.sendURL | quote }}

charts/oidc-auth/templates/deployments.yaml

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,6 @@ spec:
8787
{{- end }}
8888
resources:
8989
{{ toYaml .Values.resources | nindent 12 }}
90-
env:
91-
- name: PROVIDERS_CASDOOR_CLIENTSECRET
92-
valueFrom:
93-
secretKeyRef:
94-
name: {{ .Release.Name }}-secrets
95-
key: casdoor-client-secret
96-
- name: SYNCSTAR_PERSONALTOKEN
97-
valueFrom:
98-
secretKeyRef:
99-
name: {{ .Release.Name }}-secrets
100-
key: sync-star-personal-token
101-
- name: DATABASE_PASSWORD
102-
valueFrom:
103-
secretKeyRef:
104-
name: {{ .Release.Name }}-secrets
105-
key: database-password
106-
- name: ENCRYPT_AESKEY
107-
valueFrom:
108-
secretKeyRef:
109-
name: {{ .Release.Name }}-secrets
110-
key: aes-key
111-
- name: SMS_CLIENTSECRET
112-
valueFrom:
113-
secretKeyRef:
114-
name: {{ .Release.Name }}-secrets
115-
key: sms-sercret
11690
{{- if not (empty .Values.env) }}
11791
env:
11892
{{ toYaml .Values.env | nindent 12 }}

charts/oidc-auth/templates/secret.yaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

charts/oidc-auth/values.yaml

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,23 @@ providers:
5656

5757
# SMS service configuration for verification codes
5858
sms:
59-
# Enable test mode for SMS service. "true" = test mode, "false" = production mode
60-
enabledTest: "true" # true / false
59+
# Enable test mode. If "true", SMS won't be sent to real users.
60+
enabledTest: "true"
6161

62-
# Client ID for SMS service authentication
63-
clientID: ""
62+
# Application ID from the SMS provider.
63+
appID: ""
6464

65-
# Client secret for SMS service authentication
66-
clientSecret: ""
65+
# API Key from the SMS provider, used for signing requests.
66+
apiKey: ""
6767

68-
# URL endpoint for obtaining JWT token for SMS service
69-
tokenURL: "http://<host>/api/api-auth/oauth/get/jwttoken"
68+
# Merchant ID, if required by the provider.
69+
mchID: ""
7070

71-
# URL endpoint for sending SMS messages
72-
sendURL: "http://<host>/sms/sendSms"
71+
# Pre-approved message template ID for the verification code.
72+
templateID: ""
73+
74+
# API endpoint for the send SMS action.
75+
sendURL: "https://apis.shlianlu.com/sms/trade/template/send"
7376

7477
# GitHub star synchronization configuration
7578
syncStar:
@@ -199,12 +202,6 @@ resources:
199202
memory: 128Mi
200203

201204
env: []
202-
# - name: MY_ENV
203-
# MY_SECRET_VAR:
204-
# valueFrom:
205-
# secretKeyRef:
206-
# name: my-secret
207-
# key: my-secret-key
208205

209206
autoscaling:
210207
enabled: false
@@ -217,7 +214,6 @@ rollingUpdate:
217214
maxUnavailable: "25%"
218215
maxSurge: "25%"
219216

220-
# Affinity
221217
affinity:
222218
podAntiAffinity:
223219
preferredDuringSchedulingIgnoredDuringExecution:

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ var serveCmd = &cobra.Command{
111111
if smsc == nil {
112112
log.Fatal(nil, "Failed to initialize SMS service")
113113
}
114+
smsc.HTTPClient = httpClient
114115
providerCfg := make(map[string]*providers.ProviderConfig)
115116
for name, p := range globalConfig.Providers {
116117
providerCfg[name] = &providers.ProviderConfig{

config/config.yaml

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ server:
44
serverPort: "8080"
55

66
# Base URL for the application, used for generating callbacks and redirects
7-
baseURL: "https://<host>"
7+
baseURL: ""
88

99
# Intranet/Extranet access
1010
isPrivate: "false"
@@ -46,32 +46,35 @@ providers:
4646
clientSecret: ""
4747

4848
# Casdoor server base URL, for callback address
49-
baseURL: "https://<host>"
49+
baseURL: ""
5050

5151
# Used to obtain tokens, etc. If provided, it is used, if not provided, the baseURL is used
52-
internalURL: "http://test-svc.test-ns.svc.cluster.local:8000"
52+
internalURL: ""
5353

5454
# SMS service configuration for verification codes
5555
sms:
56-
# Enable test mode for SMS service. "true" = test mode, "false" = production mode
57-
enabledTest: "true" # true / false
56+
# Enable test mode. If "true", SMS won't be sent to real users.
57+
enabledTest: "false"
5858

59-
# Client ID for SMS service authentication
60-
clientID: ""
59+
# Application ID from the SMS provider.
60+
appID: ""
6161

62-
# Client secret for SMS service authentication
63-
clientSecret: ""
62+
# API Key from the SMS provider, used for signing requests.
63+
apiKey: ""
6464

65-
# URL endpoint for obtaining JWT token for SMS service
66-
tokenURL: "http://<host>/api/api-auth/oauth/get/jwttoken"
65+
# Merchant ID, if required by the provider.
66+
mchID: ""
6767

68-
# URL endpoint for sending SMS messages
69-
sendURL: "http://<host>/sms/sendSms"
68+
# Pre-approved message template ID for the verification code.
69+
templateID: ""
70+
71+
# API endpoint for the send SMS action.
72+
sendURL: ""
7073

7174
# GitHub star synchronization configuration
7275
syncStar:
7376
# Enable or disable star synchronization feature
74-
enabled: "true" # true / false
77+
enabled: "false" # true / false
7578

7679
# GitHub personal access token for API authentication
7780
personalToken: ""
@@ -80,7 +83,7 @@ syncStar:
8083
owner: "zgsm-ai"
8184

8285
# GitHub repository name to sync stars from
83-
repo: "zgsm"
86+
repo: "costrict"
8487

8588
# Synchronization interval in minutes
8689
interval: 1 # minute
@@ -91,16 +94,16 @@ database:
9194
type: "postgres" # mysql or postgres
9295

9396
# Database server hostname or IP address
94-
host: "<host>"
97+
host: ""
9598

9699
# Database server port number
97100
port: 5432
98101

99102
# Database username for authentication
100-
username: "postgres"
103+
username: ""
101104

102105
# Database password for authentication
103-
password: "password"
106+
password: ""
104107

105108
# Database name to connect to
106109
dbname: "auth"

internal/config/config.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ type EncryptConfig struct {
7777
}
7878

7979
type SMSConfig struct {
80-
EnabledTest bool `json:"enabledTest" mapstructure:"enabledTest" validate:"required"`
81-
ClientID string `json:"clientID" mapstructure:"clientID" validate:"required"`
82-
ClientSecret string `json:"clientSecret" mapstructure:"clientSecret" validate:"required"`
83-
TokenURL string `json:"tokenURL" mapstructure:"tokenURL" validate:"required,url"`
84-
SendURL string `json:"sendURL" mapstructure:"sendURL" validate:"required"`
80+
EnabledTest bool `json:"enabledTest" mapstructure:"enabledTest" validate:"required"`
81+
AppID string `json:"appID" mapstructure:"appID"`
82+
MchID string `json:"mchID" mapstructure:"mchID"`
83+
TemplateID string `json:"templateID" mapstructure:"templateID"`
84+
ApiKey string `json:"apiKey" mapstructure:"apiKey"`
85+
SendURL string `json:"sendURL" mapstructure:"sendURL"`
86+
HTTPClient *http.Client
8587
}
8688

8789
type ProviderConfig struct {

internal/handler/sms_handler.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"fmt"
54
"net/http"
65
"strings"
76

@@ -57,29 +56,13 @@ func (s *Server) SMSHandler(c *gin.Context) {
5756
log.Info(c, "processed request to send SMS to: %s, content: %s", phoneNumber, messageContent)
5857
log.Info(c, "simulating SMS sent to %s with content: %s", phoneNumber, messageContent)
5958
} else {
60-
code, err := service.GetLoginCode(SMSCfg.ClientID, SMSCfg.ClientSecret)
59+
err := service.SendSMS(phoneNumber, messageContent)
6160
if err != nil {
62-
errmsg := fmt.Sprintf("Error getting sms code: %v", err)
63-
log.Error(c, "Error: %s received data: %v", errmsg)
64-
response.JSONError(c, http.StatusBadRequest, "", errmsg)
61+
log.Error(c, "failed to send SMS to %s, error: %v", phoneNumber, err)
62+
response.JSONError(c, http.StatusInternalServerError, "", "failed to send sms")
6563
return
6664
}
67-
token, err := service.GetJWTToken(code, SMSCfg.ClientID, s.HTTPClient)
68-
if err != nil {
69-
errmsg := fmt.Sprintf("Error getting sms token: %v", err)
70-
log.Error(c, "Error: %s Received data: %v", errmsg)
71-
response.JSONError(c, http.StatusBadRequest, "", errmsg)
72-
return
73-
}
74-
messageContent = fmt.Sprintf("验证码:%s,5分钟内有效,请妥善保管!", messageContent)
75-
_, err = service.SendSMS(s.HTTPClient, token, phoneNumber, messageContent)
76-
if err != nil {
77-
errmsg := fmt.Sprintf("Error getting sms token: %v", err)
78-
log.Error(c, "Error: %s Received data: %v", errmsg)
79-
response.JSONError(c, http.StatusBadRequest, "", errmsg)
80-
return
81-
}
82-
log.Info(c, "simulating SMS sent to %s with content: %s", phoneNumber, messageContent)
65+
log.Info(c, "successfully sent SMS to %s for verification", phoneNumber)
8366
}
8467
c.JSON(http.StatusOK, ResponseBody{Status: "ok", Msg: "simulated SMS sent successfully"})
8568
}

0 commit comments

Comments
 (0)