mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-08 09:01:00 +08:00
Add sms and email providers to app.
This commit is contained in:
@ -38,8 +38,8 @@ func (c *ApiController) SendVerificationCode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isHuman := false
|
isHuman := false
|
||||||
provider := object.GetDefaultHumanCheckProvider()
|
captchaProvider := object.GetDefaultHumanCheckProvider()
|
||||||
if provider == nil {
|
if captchaProvider == nil {
|
||||||
isHuman = object.VerifyCaptcha(checkId, checkKey)
|
isHuman = object.VerifyCaptcha(checkId, checkKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +48,9 @@ func (c *ApiController) SendVerificationCode() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
organization := object.GetOrganization(orgId)
|
||||||
|
application := object.GetApplicationByOrganizationName(organization.Name)
|
||||||
|
|
||||||
msg := "Invalid dest type."
|
msg := "Invalid dest type."
|
||||||
switch destType {
|
switch destType {
|
||||||
case "email":
|
case "email":
|
||||||
@ -55,7 +58,9 @@ func (c *ApiController) SendVerificationCode() {
|
|||||||
c.ResponseError("Invalid Email address")
|
c.ResponseError("Invalid Email address")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
msg = object.SendVerificationCodeToEmail(remoteAddr, dest)
|
|
||||||
|
provider := application.GetEmailProvider()
|
||||||
|
msg = object.SendVerificationCodeToEmail(provider, remoteAddr, dest)
|
||||||
case "phone":
|
case "phone":
|
||||||
if !util.IsPhoneCnValid(dest) {
|
if !util.IsPhoneCnValid(dest) {
|
||||||
c.ResponseError("Invalid phone number")
|
c.ResponseError("Invalid phone number")
|
||||||
@ -66,8 +71,10 @@ func (c *ApiController) SendVerificationCode() {
|
|||||||
c.ResponseError("Missing parameter.")
|
c.ResponseError("Missing parameter.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dest = fmt.Sprintf("+%s%s", org.PhonePrefix, dest)
|
dest = fmt.Sprintf("+%s%s", org.PhonePrefix, dest)
|
||||||
msg = object.SendVerificationCodeToPhone(remoteAddr, dest)
|
provider := application.GetSmsProvider()
|
||||||
|
msg = object.SendVerificationCodeToPhone(provider, remoteAddr, dest)
|
||||||
}
|
}
|
||||||
|
|
||||||
status := "ok"
|
status := "ok"
|
||||||
|
@ -68,7 +68,9 @@ func extendApplicationWithProviders(application *Application) {
|
|||||||
|
|
||||||
application.ProviderObjs = []*Provider{}
|
application.ProviderObjs = []*Provider{}
|
||||||
for _, providerName := range application.Providers {
|
for _, providerName := range application.Providers {
|
||||||
application.ProviderObjs = append(application.ProviderObjs, m[providerName])
|
if provider, ok := m[providerName]; ok {
|
||||||
|
application.ProviderObjs = append(application.ProviderObjs, provider)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +95,7 @@ func getApplication(owner string, name string) *Application {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getApplicationByOrganization(organization string) *Application {
|
func GetApplicationByOrganizationName(organization string) *Application {
|
||||||
application := Application{}
|
application := Application{}
|
||||||
existed, err := adapter.Engine.Where("organization=?", organization).Get(&application)
|
existed, err := adapter.Engine.Where("organization=?", organization).Get(&application)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -110,7 +112,7 @@ func getApplicationByOrganization(organization string) *Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetApplicationByUser(user *User) *Application {
|
func GetApplicationByUser(user *User) *Application {
|
||||||
return getApplicationByOrganization(user.Owner)
|
return GetApplicationByOrganizationName(user.Owner)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getApplicationByClientId(clientId string) *Application {
|
func getApplicationByClientId(clientId string) *Application {
|
||||||
@ -168,3 +170,31 @@ func DeleteApplication(application *Application) bool {
|
|||||||
|
|
||||||
return affected != 0
|
return affected != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (application *Application) getProviderByCategory(category string) *Provider {
|
||||||
|
providers := GetProviders(application.Owner)
|
||||||
|
m := map[string]*Provider{}
|
||||||
|
for _, provider := range providers {
|
||||||
|
if provider.Category != category {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
m[provider.Name] = provider
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, providerName := range application.Providers {
|
||||||
|
if provider, ok := m[providerName]; ok {
|
||||||
|
return provider
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (application *Application) GetEmailProvider() *Provider {
|
||||||
|
return application.getProviderByCategory("Email")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (application *Application) GetSmsProvider() *Provider {
|
||||||
|
return application.getProviderByCategory("SMS")
|
||||||
|
}
|
||||||
|
@ -18,11 +18,7 @@ package object
|
|||||||
|
|
||||||
import "github.com/go-gomail/gomail"
|
import "github.com/go-gomail/gomail"
|
||||||
|
|
||||||
func SendEmail(title, content, dest, sender string) (string, error) {
|
func SendEmail(provider *Provider, title, content, dest, sender string) (string, error) {
|
||||||
provider := getDefaultEmailProvider()
|
|
||||||
if provider == nil {
|
|
||||||
return "Please set an Email provider first", nil
|
|
||||||
}
|
|
||||||
dialer := gomail.NewDialer(provider.Host, provider.Port, provider.ClientId, provider.ClientSecret)
|
dialer := gomail.NewDialer(provider.Host, provider.Port, provider.ClientId, provider.ClientSecret)
|
||||||
|
|
||||||
message := gomail.NewMessage()
|
message := gomail.NewMessage()
|
||||||
|
@ -70,32 +70,6 @@ func GetProvider(id string) *Provider {
|
|||||||
return getProvider(owner, name)
|
return getProvider(owner, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDefaultEmailProvider() *Provider {
|
|
||||||
provider := Provider{Owner: "admin", Category: "Email"}
|
|
||||||
existed, err := adapter.Engine.Get(&provider)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !existed {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return &provider
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDefaultPhoneProvider() *Provider {
|
|
||||||
provider := Provider{Owner: "admin", Category: "SMS"}
|
|
||||||
existed, err := adapter.Engine.Get(&provider)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !existed {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return &provider
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetDefaultHumanCheckProvider() *Provider {
|
func GetDefaultHumanCheckProvider() *Provider {
|
||||||
provider := Provider{Owner: "admin", Category: "HumanCheck"}
|
provider := Provider{Owner: "admin", Category: "HumanCheck"}
|
||||||
existed, err := adapter.Engine.Get(&provider)
|
existed, err := adapter.Engine.Get(&provider)
|
||||||
|
@ -20,11 +20,7 @@ import (
|
|||||||
"github.com/casdoor/go-sms-sender"
|
"github.com/casdoor/go-sms-sender"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SendCodeToPhone(phone, code string) string {
|
func SendCodeToPhone(provider *Provider, phone, code string) string {
|
||||||
provider := getDefaultPhoneProvider()
|
|
||||||
if provider == nil {
|
|
||||||
return "Please set an phone provider first"
|
|
||||||
}
|
|
||||||
client := go_sms_sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.RegionId, provider.TemplateCode, provider.AppId)
|
client := go_sms_sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.RegionId, provider.TemplateCode, provider.AppId)
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return fmt.Sprintf("Unsupported provide type: %s", provider.Type)
|
return fmt.Sprintf("Unsupported provide type: %s", provider.Type)
|
||||||
|
@ -26,23 +26,28 @@ import (
|
|||||||
type VerificationRecord struct {
|
type VerificationRecord struct {
|
||||||
RemoteAddr string `xorm:"varchar(100) notnull pk"`
|
RemoteAddr string `xorm:"varchar(100) notnull pk"`
|
||||||
Type string `xorm:"varchar(10) notnull pk"`
|
Type string `xorm:"varchar(10) notnull pk"`
|
||||||
|
Provider string `xorm:"varchar(100) notnull"`
|
||||||
Receiver string `xorm:"varchar(100) notnull"`
|
Receiver string `xorm:"varchar(100) notnull"`
|
||||||
Code string `xorm:"varchar(10) notnull"`
|
Code string `xorm:"varchar(10) notnull"`
|
||||||
Time int64 `xorm:"notnull"`
|
Time int64 `xorm:"notnull"`
|
||||||
IsUsed bool
|
IsUsed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendVerificationCodeToEmail(remoteAddr, dest string) string {
|
func SendVerificationCodeToEmail(provider *Provider, remoteAddr string, dest string) string {
|
||||||
|
if provider == nil {
|
||||||
|
return "Please set an Email provider first"
|
||||||
|
}
|
||||||
|
|
||||||
title := "Casdoor Verification Code"
|
title := "Casdoor Verification Code"
|
||||||
sender := "Casdoor"
|
sender := "Casdoor"
|
||||||
code := getRandomCode(5)
|
code := getRandomCode(5)
|
||||||
content := fmt.Sprintf("You have requested a verification code at Casdoor. Here is your code: %s, please enter in 5 minutes.", code)
|
content := fmt.Sprintf("You have requested a verification code at Casdoor. Here is your code: %s, please enter in 5 minutes.", code)
|
||||||
|
|
||||||
if result := AddToVerificationRecord(remoteAddr, "email", dest, code); len(result) != 0 {
|
if result := AddToVerificationRecord(provider.Name, remoteAddr, "Email", dest, code); len(result) != 0 {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
msg, err := SendEmail(title, content, dest, sender)
|
msg, err := SendEmail(provider, title, content, dest, sender)
|
||||||
if msg != "" {
|
if msg != "" {
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
@ -53,19 +58,24 @@ func SendVerificationCodeToEmail(remoteAddr, dest string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendVerificationCodeToPhone(remoteAddr, dest string) string {
|
func SendVerificationCodeToPhone(provider *Provider, remoteAddr string, dest string) string {
|
||||||
|
if provider == nil {
|
||||||
|
return "Please set a SMS provider first"
|
||||||
|
}
|
||||||
|
|
||||||
code := getRandomCode(5)
|
code := getRandomCode(5)
|
||||||
if result := AddToVerificationRecord(remoteAddr, "phone", dest, code); len(result) != 0 {
|
if result := AddToVerificationRecord(provider.Name, remoteAddr, "SMS", dest, code); len(result) != 0 {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
return SendCodeToPhone(dest, code)
|
return SendCodeToPhone(provider, dest, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddToVerificationRecord(remoteAddr, recordType, dest, code string) string {
|
func AddToVerificationRecord(providerName, remoteAddr, recordType, dest, code string) string {
|
||||||
var record VerificationRecord
|
var record VerificationRecord
|
||||||
record.RemoteAddr = remoteAddr
|
record.RemoteAddr = remoteAddr
|
||||||
record.Type = recordType
|
record.Type = recordType
|
||||||
|
record.Provider = providerName
|
||||||
has, err := adapter.Engine.Get(&record)
|
has, err := adapter.Engine.Get(&record)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -73,7 +83,7 @@ func AddToVerificationRecord(remoteAddr, recordType, dest, code string) string {
|
|||||||
|
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
|
||||||
if has && now - record.Time < 60 {
|
if has && now-record.Time < 60 {
|
||||||
return "You can only send one code in 60s."
|
return "You can only send one code in 60s."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user