refactor: oauth2登录优化

This commit is contained in:
meilin.huang
2023-07-26 10:24:32 +08:00
parent 5083b2bdfe
commit 8f37b71d7f
13 changed files with 56 additions and 56 deletions

View File

@@ -33,7 +33,7 @@ func (c *Config) BoolValue(defaultValue bool) bool {
if c.Id == 0 {
return defaultValue
}
return convertBool(c.Value, defaultValue)
return c.ConvBool(c.Value, defaultValue)
}
// 值返回json map
@@ -52,7 +52,7 @@ func (c *Config) IntValue(defaultValue int) int {
if c.Id == 0 {
return defaultValue
}
return convertInt(c.Value, defaultValue)
return c.ConvInt(c.Value, defaultValue)
}
type AccountLoginSecurity struct {
@@ -67,10 +67,10 @@ type AccountLoginSecurity struct {
func (c *Config) ToAccountLoginSecurity() *AccountLoginSecurity {
jm := c.GetJsonMap()
als := new(AccountLoginSecurity)
als.UseCaptcha = convertBool(jm["useCaptcha"], true)
als.UseOtp = convertBool(jm["useOtp"], false)
als.LoginFailCount = convertInt(jm["loginFailCount"], 5)
als.LoginFailMin = convertInt(jm["loginFailMin"], 10)
als.UseCaptcha = c.ConvBool(jm["useCaptcha"], true)
als.UseOtp = c.ConvBool(jm["useOtp"], false)
als.LoginFailCount = c.ConvInt(jm["loginFailCount"], 5)
als.LoginFailMin = c.ConvInt(jm["loginFailMin"], 10)
otpIssuer := jm["otpIssuer"]
if otpIssuer == "" {
otpIssuer = "mayfly-go"
@@ -80,7 +80,8 @@ func (c *Config) ToAccountLoginSecurity() *AccountLoginSecurity {
}
type ConfigOauth2Login struct {
Enable bool // 是否启用
Enable bool // 是否启用
Name string
ClientId string `json:"clientId"`
ClientSecret string `json:"clientSecret"`
AuthorizationURL string `json:"authorizationURL"`
@@ -96,7 +97,8 @@ type ConfigOauth2Login struct {
func (c *Config) ToOauth2Login() *ConfigOauth2Login {
jm := c.GetJsonMap()
ol := new(ConfigOauth2Login)
ol.Enable = convertBool(jm["enable"], false)
ol.Enable = c.ConvBool(jm["enable"], false)
ol.Name = jm["name"]
ol.ClientId = jm["clientId"]
ol.ClientSecret = jm["clientSecret"]
ol.AuthorizationURL = jm["authorizationURL"]
@@ -105,12 +107,12 @@ func (c *Config) ToOauth2Login() *ConfigOauth2Login {
ol.Scopes = jm["scopes"]
ol.ResourceURL = jm["resourceURL"]
ol.UserIdentifier = jm["userIdentifier"]
ol.AutoRegister = convertBool(jm["autoRegister"], true)
ol.AutoRegister = c.ConvBool(jm["autoRegister"], true)
return ol
}
// 转换配置中的值为bool类型默认"1"或"true"为true其他为false
func convertBool(value string, defaultValue bool) bool {
func (c *Config) ConvBool(value string, defaultValue bool) bool {
if value == "" {
return defaultValue
}
@@ -118,7 +120,7 @@ func convertBool(value string, defaultValue bool) bool {
}
// 转换配置值中的值为int
func convertInt(value string, defaultValue int) int {
func (c *Config) ConvInt(value string, defaultValue int) int {
if value == "" {
return defaultValue
}