mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	refactor: oauth2登录重构
This commit is contained in:
		@@ -1,20 +0,0 @@
 | 
			
		||||
package entity
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"mayfly-go/pkg/model"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type OAuthAccount struct {
 | 
			
		||||
	model.DeletedModel
 | 
			
		||||
 | 
			
		||||
	AccountId uint64 `json:"accountId" gorm:"column:account_id;index:account_id,unique"`
 | 
			
		||||
	Identity  string `json:"identity" gorm:"column:identity;index:identity,unique"`
 | 
			
		||||
 | 
			
		||||
	CreateTime *time.Time `json:"createTime"`
 | 
			
		||||
	UpdateTime *time.Time `json:"updateTime"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (OAuthAccount) TableName() string {
 | 
			
		||||
	return "t_oauth_account"
 | 
			
		||||
}
 | 
			
		||||
@@ -8,8 +8,7 @@ import (
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	ConfigKeyAccountLoginSecurity string = "AccountLoginSecurity" // 账号登录安全配置
 | 
			
		||||
	ConfigKeyUseLoginCaptcha      string = "UseLoginCaptcha"      // 是否使用登录验证码
 | 
			
		||||
	ConfigKeyUseLoginOtp          string = "UseLoginOtp"          // 是否开启otp双因素校验
 | 
			
		||||
	ConfigKeyOauth2Login          string = "Oauth2Login"          // oauth2认证登录配置
 | 
			
		||||
	ConfigKeyDbQueryMaxCount      string = "DbQueryMaxCount"      // 数据库查询的最大数量
 | 
			
		||||
	ConfigKeyDbSaveQuerySQL       string = "DbSaveQuerySQL"       // 数据库是否记录查询相关sql
 | 
			
		||||
	ConfigUseWartermark           string = "UseWartermark"        // 是否使用水印
 | 
			
		||||
@@ -19,8 +18,8 @@ type Config struct {
 | 
			
		||||
	model.Model
 | 
			
		||||
	Name   string `json:"name"` // 配置名
 | 
			
		||||
	Key    string `json:"key"`  // 配置key
 | 
			
		||||
	Params string `json:"params" gorm:"column:params;type:varchar(1000)"`
 | 
			
		||||
	Value  string `json:"value" gorm:"column:value;type:varchar(1000)"`
 | 
			
		||||
	Params string `json:"params" gorm:"column:params;type:varchar(1500)"`
 | 
			
		||||
	Value  string `json:"value" gorm:"column:value;type:varchar(1500)"`
 | 
			
		||||
	Remark string `json:"remark"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -80,6 +79,36 @@ func (c *Config) ToAccountLoginSecurity() *AccountLoginSecurity {
 | 
			
		||||
	return als
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ConfigOauth2Login struct {
 | 
			
		||||
	Enable           bool   // 是否启用
 | 
			
		||||
	ClientId         string `json:"clientId"`
 | 
			
		||||
	ClientSecret     string `json:"clientSecret"`
 | 
			
		||||
	AuthorizationURL string `json:"authorizationURL"`
 | 
			
		||||
	AccessTokenURL   string `json:"accessTokenURL"`
 | 
			
		||||
	RedirectURL      string `json:"redirectURL"`
 | 
			
		||||
	Scopes           string `json:"scopes"`
 | 
			
		||||
	ResourceURL      string `json:"resourceURL"`
 | 
			
		||||
	UserIdentifier   string `json:"userIdentifier"`
 | 
			
		||||
	AutoRegister     bool   `json:"autoRegister"` // 是否自动注册
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 转换为Oauth2Login结构体
 | 
			
		||||
func (c *Config) ToOauth2Login() *ConfigOauth2Login {
 | 
			
		||||
	jm := c.GetJsonMap()
 | 
			
		||||
	ol := new(ConfigOauth2Login)
 | 
			
		||||
	ol.Enable = convertBool(jm["enable"], false)
 | 
			
		||||
	ol.ClientId = jm["clientId"]
 | 
			
		||||
	ol.ClientSecret = jm["clientSecret"]
 | 
			
		||||
	ol.AuthorizationURL = jm["authorizationURL"]
 | 
			
		||||
	ol.AccessTokenURL = jm["accessTokenURL"]
 | 
			
		||||
	ol.RedirectURL = jm["redirectURL"]
 | 
			
		||||
	ol.Scopes = jm["scopes"]
 | 
			
		||||
	ol.ResourceURL = jm["resourceURL"]
 | 
			
		||||
	ol.UserIdentifier = jm["userIdentifier"]
 | 
			
		||||
	ol.AutoRegister = convertBool(jm["autoRegister"], true)
 | 
			
		||||
	return ol
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 转换配置中的值为bool类型(默认"1"或"true"为true,其他为false)
 | 
			
		||||
func convertBool(value string, defaultValue bool) bool {
 | 
			
		||||
	if value == "" {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +0,0 @@
 | 
			
		||||
package repository
 | 
			
		||||
 | 
			
		||||
import "mayfly-go/internal/sys/domain/entity"
 | 
			
		||||
 | 
			
		||||
type OAuthAccount interface {
 | 
			
		||||
	// GetOAuthAccount 根据identity获取第三方账号信息
 | 
			
		||||
	GetOAuthAccount(condition *entity.OAuthAccount, cols ...string) error
 | 
			
		||||
 | 
			
		||||
	SaveOAuthAccount(e *entity.OAuthAccount) error
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user