feat(auth): improve email and mobile config unmarshalling with default values
This commit is contained in:
parent
0e8e2d442e
commit
6034a32e85
@ -17,13 +17,11 @@ func Email(ctx *svc.ServiceContext) {
|
||||
logger.Debug("Email config initialization")
|
||||
method, err := ctx.AuthModel.FindOneByMethod(context.Background(), "email")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to find email auth method: %v", err.Error()))
|
||||
panic(fmt.Sprintf("[Error] Initialization Failed to find email auth method: %v", err.Error()))
|
||||
}
|
||||
var cfg config.EmailConfig
|
||||
var emailConfig = new(auth.EmailAuthConfig)
|
||||
if err := emailConfig.Unmarshal(method.Config); err != nil {
|
||||
panic(fmt.Sprintf("failed to unmarshal email auth config: %v", err.Error()))
|
||||
}
|
||||
emailConfig.Unmarshal(method.Config)
|
||||
tool.DeepCopy(&cfg, emailConfig)
|
||||
cfg.Enable = *method.Enabled
|
||||
value, _ := json.Marshal(emailConfig.PlatformConfig)
|
||||
|
||||
@ -3,7 +3,6 @@ package initialize
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
|
||||
@ -21,9 +20,7 @@ func Mobile(ctx *svc.ServiceContext) {
|
||||
}
|
||||
var cfg config.MobileConfig
|
||||
var mobileConfig auth.MobileAuthConfig
|
||||
if err := mobileConfig.Unmarshal(method.Config); err != nil {
|
||||
panic(fmt.Sprintf("failed to unmarshal mobile auth config: %v", err.Error()))
|
||||
}
|
||||
mobileConfig.Unmarshal(method.Config)
|
||||
tool.DeepCopy(&cfg, mobileConfig)
|
||||
cfg.Enable = *method.Enabled
|
||||
value, _ := json.Marshal(mobileConfig.PlatformConfig)
|
||||
|
||||
@ -18,7 +18,7 @@ type GetAuthMethodConfigLogic struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// Get auth method config
|
||||
// NewGetAuthMethodConfigLogic Get auth method config
|
||||
func NewGetAuthMethodConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAuthMethodConfigLogic {
|
||||
return &GetAuthMethodConfigLogic{
|
||||
Logger: logger.WithContext(ctx),
|
||||
|
||||
@ -40,34 +40,32 @@ func (l *UpdateAuthMethodConfigLogic) UpdateAuthMethodConfig(req *types.UpdateAu
|
||||
|
||||
tool.DeepCopy(method, req)
|
||||
if req.Config != nil {
|
||||
if value, ok := req.Config.(map[string]interface{}); ok {
|
||||
if req.Method == "email" && value["verify_email_template"] == "" {
|
||||
value["verify_email_template"] = email.DefaultEmailVerifyTemplate
|
||||
}
|
||||
if req.Method == "email" && value["expiration_email_template"] == "" {
|
||||
value["expiration_email_template"] = email.DefaultExpirationEmailTemplate
|
||||
}
|
||||
if req.Method == "email" && value["maintenance_email_template"] == "" {
|
||||
value["maintenance_email_template"] = email.DefaultMaintenanceEmailTemplate
|
||||
}
|
||||
if req.Method == "email" && value["traffic_exceed_email_template"] == "" {
|
||||
value["traffic_exceed_email_template"] = email.DefaultTrafficExceedEmailTemplate
|
||||
}
|
||||
|
||||
if value["platform_config"] != nil {
|
||||
platformConfig, err := validatePlatformConfig(value["platform"].(string), value["platform_config"].(map[string]interface{}))
|
||||
if err != nil {
|
||||
l.Errorw("validate platform config failed", logger.Field("config", req.Config), logger.Field("error", err.Error()))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "validate platform config failed: %v", err.Error())
|
||||
}
|
||||
req.Config.(map[string]interface{})["platform_config"] = platformConfig
|
||||
}
|
||||
_, exist := req.Config.(map[string]interface{})
|
||||
if !exist {
|
||||
req.Config = initializePlatformConfig(req.Method).(string)
|
||||
}
|
||||
if req.Method == "email" {
|
||||
configs, _ := json.Marshal(req.Config)
|
||||
emailConfig := new(auth.EmailAuthConfig)
|
||||
emailConfig.Unmarshal(string(configs))
|
||||
req.Config = emailConfig
|
||||
}
|
||||
|
||||
if req.Method == "mobile" {
|
||||
configs, _ := json.Marshal(req.Config)
|
||||
mobileConfig := new(auth.MobileAuthConfig)
|
||||
mobileConfig.Unmarshal(string(configs))
|
||||
req.Config = mobileConfig
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(req.Config)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "marshal config failed: %v", err.Error())
|
||||
}
|
||||
method.Config = string(bytes)
|
||||
} else {
|
||||
// initialize platform config
|
||||
method.Config = initializePlatformConfig(req.Method).(string)
|
||||
}
|
||||
err = l.svcCtx.AuthModel.Update(l.ctx, method)
|
||||
if err != nil {
|
||||
@ -124,3 +122,26 @@ func validatePlatformConfig(platform string, cfg map[string]interface{}) (interf
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func initializePlatformConfig(platform string) interface{} {
|
||||
var result interface{}
|
||||
switch platform {
|
||||
case "email":
|
||||
result = new(auth.EmailAuthConfig).Marshal()
|
||||
case "mobile":
|
||||
result = new(auth.MobileAuthConfig).Marshal()
|
||||
case "apple":
|
||||
result = new(auth.AppleAuthConfig).Marshal()
|
||||
case "google":
|
||||
result = new(auth.GoogleAuthConfig).Marshal()
|
||||
case "github":
|
||||
result = new(auth.GithubAuthConfig).Marshal()
|
||||
case "facebook":
|
||||
result = new(auth.FacebookAuthConfig).Marshal()
|
||||
case "telegram":
|
||||
result = new(auth.TelegramAuthConfig).Marshal()
|
||||
case "device":
|
||||
result = new(auth.DeviceConfig).Marshal()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@ -52,8 +52,10 @@ func (l *BindOAuthCallbackLogic) BindOAuthCallback(req *types.BindOAuthCallbackR
|
||||
err = l.google(req)
|
||||
case "apple":
|
||||
err = l.apple(req)
|
||||
case "telegram":
|
||||
err = l.telegram(req)
|
||||
default:
|
||||
l.Errorw("oauth login method not support: %v", logger.Field("method", req.Method))
|
||||
l.Errorw("oauth login method not support", logger.Field("method", req.Method))
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "oauth login method not support: %v", req.Method)
|
||||
}
|
||||
if err != nil {
|
||||
@ -212,3 +214,7 @@ func (l *BindOAuthCallbackLogic) apple(req *types.BindOAuthCallbackRequest) erro
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *BindOAuthCallbackLogic) telegram(req *types.BindOAuthCallbackRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -3,6 +3,8 @@ package auth
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/pkg/email"
|
||||
)
|
||||
|
||||
type Auth struct {
|
||||
@ -124,15 +126,55 @@ type EmailAuthConfig struct {
|
||||
}
|
||||
|
||||
func (l *EmailAuthConfig) Marshal() string {
|
||||
if l.ExpirationEmailTemplate == "" {
|
||||
l.ExpirationEmailTemplate = email.DefaultExpirationEmailTemplate
|
||||
}
|
||||
if l.ExpirationEmailTemplate == "" {
|
||||
l.MaintenanceEmailTemplate = email.DefaultMaintenanceEmailTemplate
|
||||
}
|
||||
if l.TrafficExceedEmailTemplate == "" {
|
||||
l.TrafficExceedEmailTemplate = email.DefaultTrafficExceedEmailTemplate
|
||||
}
|
||||
if l.VerifyEmailTemplate == "" {
|
||||
l.VerifyEmailTemplate = email.DefaultEmailVerifyTemplate
|
||||
}
|
||||
bytes, err := json.Marshal(l)
|
||||
if err != nil {
|
||||
bytes, _ = json.Marshal(new(EmailAuthConfig))
|
||||
config := &EmailAuthConfig{
|
||||
Platform: "smtp",
|
||||
PlatformConfig: new(SMTPConfig),
|
||||
EnableVerify: true,
|
||||
EnableNotify: true,
|
||||
EnableDomainSuffix: false,
|
||||
DomainSuffixList: "",
|
||||
VerifyEmailTemplate: email.DefaultEmailVerifyTemplate,
|
||||
ExpirationEmailTemplate: email.DefaultExpirationEmailTemplate,
|
||||
MaintenanceEmailTemplate: email.DefaultMaintenanceEmailTemplate,
|
||||
TrafficExceedEmailTemplate: email.DefaultTrafficExceedEmailTemplate,
|
||||
}
|
||||
|
||||
bytes, _ = json.Marshal(config)
|
||||
}
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
func (l *EmailAuthConfig) Unmarshal(data string) error {
|
||||
return json.Unmarshal([]byte(data), &l)
|
||||
func (l *EmailAuthConfig) Unmarshal(data string) {
|
||||
err := json.Unmarshal([]byte(data), &l)
|
||||
if err != nil {
|
||||
config := &EmailAuthConfig{
|
||||
Platform: "smtp",
|
||||
PlatformConfig: new(SMTPConfig),
|
||||
EnableVerify: true,
|
||||
EnableNotify: true,
|
||||
EnableDomainSuffix: false,
|
||||
DomainSuffixList: "",
|
||||
VerifyEmailTemplate: email.DefaultEmailVerifyTemplate,
|
||||
ExpirationEmailTemplate: email.DefaultExpirationEmailTemplate,
|
||||
MaintenanceEmailTemplate: email.DefaultMaintenanceEmailTemplate,
|
||||
TrafficExceedEmailTemplate: email.DefaultTrafficExceedEmailTemplate,
|
||||
}
|
||||
_ = json.Unmarshal([]byte(config.Marshal()), &l)
|
||||
}
|
||||
}
|
||||
|
||||
// SMTPConfig Email SMTP configuration
|
||||
@ -167,13 +209,28 @@ type MobileAuthConfig struct {
|
||||
func (l *MobileAuthConfig) Marshal() string {
|
||||
bytes, err := json.Marshal(l)
|
||||
if err != nil {
|
||||
bytes, _ = json.Marshal(new(MobileAuthConfig))
|
||||
config := &MobileAuthConfig{
|
||||
Platform: "alibaba_cloud",
|
||||
PlatformConfig: new(AlibabaCloudConfig),
|
||||
EnableWhitelist: false,
|
||||
Whitelist: []string{},
|
||||
}
|
||||
bytes, _ = json.Marshal(config)
|
||||
}
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
func (l *MobileAuthConfig) Unmarshal(data string) error {
|
||||
return json.Unmarshal([]byte(data), &l)
|
||||
func (l *MobileAuthConfig) Unmarshal(data string) {
|
||||
err := json.Unmarshal([]byte(data), &l)
|
||||
if err != nil {
|
||||
config := &MobileAuthConfig{
|
||||
Platform: "alibaba_cloud",
|
||||
PlatformConfig: new(AlibabaCloudConfig),
|
||||
EnableWhitelist: false,
|
||||
Whitelist: []string{},
|
||||
}
|
||||
_ = json.Unmarshal([]byte(config.Marshal()), &l)
|
||||
}
|
||||
}
|
||||
|
||||
type AlibabaCloudConfig struct {
|
||||
|
||||
@ -282,7 +282,6 @@ const (
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
DefaultTrafficExceedEmailTemplate = `<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user