Files
SeeyonFileSystem/server/model/sso.go
2026-07-18 10:52:32 +08:00

107 lines
4.3 KiB
Go

package model
import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
)
// SSOProvider SSO提供商配置
// 支持 OAuth2.0 / CAS / LDAP / OIDC 等协议
type SSOProvider struct {
ID uint64 `json:"id" gorm:"primaryKey"`
Name string `json:"name" gorm:"size:64;not null"` // 提供商名称: 企业微信/钉钉/飞书
Type string `json:"type" gorm:"size:32;not null"` // 协议类型: oauth2/cas/oidc/ldap
DisplayName string `json:"display_name" gorm:"size:64;not null"` // 登录页显示名称
Icon string `json:"icon" gorm:"size:512"` // 图标URL
Config SSOConfig `json:"config" gorm:"type:text"` // 协议配置
AutoCreate int8 `json:"auto_create" gorm:"default:1"` // 自动创建用户: 1=是 0=否
DefaultRole string `json:"default_role" gorm:"size:32;default:viewer"` // 自动创建时的默认角色
Status int8 `json:"status" gorm:"default:1"` // 1=启用 0=禁用
SortOrder int `json:"sort_order" gorm:"default:0"` // 排序
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (SSOProvider) TableName() string {
return "fs_sso_provider"
}
// SSOConfig SSO协议配置
// 不同协议使用不同的配置字段
type SSOConfig struct {
// OAuth2.0 / OIDC 通用配置
ClientID string `json:"client_id,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
AuthURL string `json:"auth_url,omitempty"` // 授权端点
TokenURL string `json:"token_url,omitempty"` // Token端点
UserInfoURL string `json:"user_info_url,omitempty"` // 用户信息端点
RedirectURI string `json:"redirect_uri,omitempty"` // 回调地址
Scopes string `json:"scopes,omitempty"` // 授权范围
ResponseType string `json:"response_type,omitempty"` // response_type
// OIDC 特有
IssuerURL string `json:"issuer_url,omitempty"` // Issuer URL
// CAS 特有
CasServerURL string `json:"cas_server_url,omitempty"` // CAS服务器地址
// LDAP 特有
LDAPServer string `json:"ldap_server,omitempty"` // LDAP服务器地址
LDAPPort int `json:"ldap_port,omitempty"` // LDAP端口
LDAPBaseDN string `json:"ldap_base_dn,omitempty"` // Base DN
LDAPBindUser string `json:"ldap_bind_user,omitempty"` // 绑定用户
LDAPBindPass string `json:"ldap_bind_pass,omitempty"` // 绑定密码
LDAPFilter string `json:"ldap_filter,omitempty"` // 用户过滤器
// 字段映射 (从SSO用户信息到本地用户的字段映射)
MappingUsername string `json:"mapping_username,omitempty"` // 用户名字段映射
MappingEmail string `json:"mapping_email,omitempty"` // 邮箱字段映射
MappingNickname string `json:"mapping_nickname,omitempty"` // 昵称字段映射
}
// Value 实现 driver.Valuer
func (c SSOConfig) Value() (driver.Value, error) {
b, err := json.Marshal(c)
if err != nil {
return nil, err
}
return string(b), nil
}
// Scan 实现 sql.Scanner
func (c *SSOConfig) Scan(value interface{}) error {
if value == nil {
return nil
}
var bytes []byte
switch v := value.(type) {
case []byte:
bytes = v
case string:
bytes = []byte(v)
default:
return fmt.Errorf("SSOConfig.Scan: 无法将 %T 转换为 []byte", value)
}
return json.Unmarshal(bytes, c)
}
// SSOSession SSO会话记录
// 记录SSO登录的来源和关联信息
type SSOSession struct {
ID uint64 `json:"id" gorm:"primaryKey"`
UserID uint64 `json:"user_id" gorm:"index;not null"` // 关联的本地用户ID
ProviderID uint64 `json:"provider_id" gorm:"not null"` // SSO提供商ID
ExternalID string `json:"external_id" gorm:"size:256;not null"` // 外部系统用户ID
AccessToken string `json:"-" gorm:"size:1024"` // SSO Access Token
RefreshToken string `json:"-" gorm:"size:1024"` // SSO Refresh Token
ExpiresAt time.Time `json:"expires_at"` // Token过期时间
LastLoginAt time.Time `json:"last_login_at"` // 最近登录时间
CreatedAt time.Time `json:"created_at"`
}
func (SSOSession) TableName() string {
return "fs_sso_session"
}