|
|
|
@ -9,13 +9,13 @@ package oauth
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/veypi/OneAuth/models"
|
|
|
|
"github.com/vyes-ai/vigo"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// User 用户表
|
|
|
|
// User 用户表
|
|
|
|
type User struct {
|
|
|
|
type User struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
Username string `json:"username" gorm:"uniqueIndex;not null;size:50;comment:用户名""`
|
|
|
|
Username string `json:"username" gorm:"uniqueIndex;not null;size:50;comment:用户名""`
|
|
|
|
Email string `json:"email" gorm:"uniqueIndex;size:100;comment:邮箱地址"`
|
|
|
|
Email string `json:"email" gorm:"uniqueIndex;size:100;comment:邮箱地址"`
|
|
|
|
Phone string `json:"phone" gorm:"uniqueIndex;size:20;comment:手机号码"`
|
|
|
|
Phone string `json:"phone" gorm:"uniqueIndex;size:20;comment:手机号码"`
|
|
|
|
@ -42,7 +42,7 @@ type User struct {
|
|
|
|
|
|
|
|
|
|
|
|
// Role 角色表
|
|
|
|
// Role 角色表
|
|
|
|
type Role struct {
|
|
|
|
type Role struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
Name string `json:"name" gorm:"uniqueIndex;not null;size:50;comment:角色名称" validate:"required"`
|
|
|
|
Name string `json:"name" gorm:"uniqueIndex;not null;size:50;comment:角色名称" validate:"required"`
|
|
|
|
DisplayName string `json:"display_name" gorm:"size:100;comment:显示名称"`
|
|
|
|
DisplayName string `json:"display_name" gorm:"size:100;comment:显示名称"`
|
|
|
|
Description string `json:"description" gorm:"type:text;comment:角色描述"`
|
|
|
|
Description string `json:"description" gorm:"type:text;comment:角色描述"`
|
|
|
|
@ -57,7 +57,7 @@ type Role struct {
|
|
|
|
|
|
|
|
|
|
|
|
// Permission 权限表
|
|
|
|
// Permission 权限表
|
|
|
|
type Permission struct {
|
|
|
|
type Permission struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
Name string `json:"name" gorm:"uniqueIndex;not null;size:100;comment:权限名称" validate:"required"`
|
|
|
|
Name string `json:"name" gorm:"uniqueIndex;not null;size:100;comment:权限名称" validate:"required"`
|
|
|
|
DisplayName string `json:"display_name" gorm:"size:100;comment:显示名称"`
|
|
|
|
DisplayName string `json:"display_name" gorm:"size:100;comment:显示名称"`
|
|
|
|
Description string `json:"description" gorm:"type:text;comment:权限描述"`
|
|
|
|
Description string `json:"description" gorm:"type:text;comment:权限描述"`
|
|
|
|
@ -98,7 +98,7 @@ type RolePermission struct {
|
|
|
|
|
|
|
|
|
|
|
|
// UserLoginLog 用户登录日志表
|
|
|
|
// UserLoginLog 用户登录日志表
|
|
|
|
type UserLoginLog struct {
|
|
|
|
type UserLoginLog struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
IPAddress string `json:"ip_address" gorm:"size:45;comment:IP地址"`
|
|
|
|
IPAddress string `json:"ip_address" gorm:"size:45;comment:IP地址"`
|
|
|
|
UserAgent string `json:"user_agent" gorm:"type:text;comment:用户代理"`
|
|
|
|
UserAgent string `json:"user_agent" gorm:"type:text;comment:用户代理"`
|
|
|
|
@ -113,9 +113,6 @@ type UserLoginLog struct {
|
|
|
|
|
|
|
|
|
|
|
|
// GORM Hooks
|
|
|
|
// GORM Hooks
|
|
|
|
func (u *User) BeforeCreate(tx *gorm.DB) error {
|
|
|
|
func (u *User) BeforeCreate(tx *gorm.DB) error {
|
|
|
|
if err := u.BaseModel.BeforeCreate(tx); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Locale == "" {
|
|
|
|
if u.Locale == "" {
|
|
|
|
u.Locale = "zh-CN"
|
|
|
|
u.Locale = "zh-CN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -175,7 +172,7 @@ func (r *Role) HasPermission(resource, action string) bool {
|
|
|
|
|
|
|
|
|
|
|
|
// OAuthClient OAuth客户端表
|
|
|
|
// OAuthClient OAuth客户端表
|
|
|
|
type OAuthClient struct {
|
|
|
|
type OAuthClient struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
ClientID string `json:"client_id" gorm:"uniqueIndex;not null;size:255;comment:客户端ID"`
|
|
|
|
ClientID string `json:"client_id" gorm:"uniqueIndex;not null;size:255;comment:客户端ID"`
|
|
|
|
ClientSecret string `json:"-" gorm:"not null;size:255;comment:客户端密钥"`
|
|
|
|
ClientSecret string `json:"-" gorm:"not null;size:255;comment:客户端密钥"`
|
|
|
|
ClientName string `json:"client_name" gorm:"not null;size:255;comment:客户端名称"`
|
|
|
|
ClientName string `json:"client_name" gorm:"not null;size:255;comment:客户端名称"`
|
|
|
|
@ -201,7 +198,7 @@ type OAuthClient struct {
|
|
|
|
|
|
|
|
|
|
|
|
// OAuthAuthorizationCode 授权码表
|
|
|
|
// OAuthAuthorizationCode 授权码表
|
|
|
|
type OAuthAuthorizationCode struct {
|
|
|
|
type OAuthAuthorizationCode struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
Code string `json:"code" gorm:"uniqueIndex;not null;size:255;comment:授权码"`
|
|
|
|
Code string `json:"code" gorm:"uniqueIndex;not null;size:255;comment:授权码"`
|
|
|
|
ClientID string `json:"client_id" gorm:"not null;type:varchar(32);index;comment:客户端ID"`
|
|
|
|
ClientID string `json:"client_id" gorm:"not null;type:varchar(32);index;comment:客户端ID"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
@ -219,7 +216,7 @@ type OAuthAuthorizationCode struct {
|
|
|
|
|
|
|
|
|
|
|
|
// OAuthAccessToken 访问令牌表
|
|
|
|
// OAuthAccessToken 访问令牌表
|
|
|
|
type OAuthAccessToken struct {
|
|
|
|
type OAuthAccessToken struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
Token string `json:"token" gorm:"uniqueIndex;not null;size:500;comment:访问令牌"`
|
|
|
|
Token string `json:"token" gorm:"uniqueIndex;not null;size:500;comment:访问令牌"`
|
|
|
|
ClientID string `json:"client_id" gorm:"not null;type:varchar(32);index;comment:客户端ID"`
|
|
|
|
ClientID string `json:"client_id" gorm:"not null;type:varchar(32);index;comment:客户端ID"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
@ -235,7 +232,7 @@ type OAuthAccessToken struct {
|
|
|
|
|
|
|
|
|
|
|
|
// OAuthRefreshToken 刷新令牌表
|
|
|
|
// OAuthRefreshToken 刷新令牌表
|
|
|
|
type OAuthRefreshToken struct {
|
|
|
|
type OAuthRefreshToken struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
Token string `json:"token" gorm:"uniqueIndex;not null;size:500;comment:刷新令牌"`
|
|
|
|
Token string `json:"token" gorm:"uniqueIndex;not null;size:500;comment:刷新令牌"`
|
|
|
|
AccessTokenID string `json:"access_token_id" gorm:"type:varchar(32);uniqueIndex;comment:访问令牌ID"`
|
|
|
|
AccessTokenID string `json:"access_token_id" gorm:"type:varchar(32);uniqueIndex;comment:访问令牌ID"`
|
|
|
|
ClientID string `json:"client_id" gorm:"not null;type:varchar(32);index;comment:客户端ID"`
|
|
|
|
ClientID string `json:"client_id" gorm:"not null;type:varchar(32);index;comment:客户端ID"`
|
|
|
|
@ -252,7 +249,7 @@ type OAuthRefreshToken struct {
|
|
|
|
|
|
|
|
|
|
|
|
// OAuthScope OAuth授权范围表
|
|
|
|
// OAuthScope OAuth授权范围表
|
|
|
|
type OAuthScope struct {
|
|
|
|
type OAuthScope struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
Name string `json:"name" gorm:"uniqueIndex;not null;size:100;comment:范围名称"`
|
|
|
|
Name string `json:"name" gorm:"uniqueIndex;not null;size:100;comment:范围名称"`
|
|
|
|
DisplayName string `json:"display_name" gorm:"size:100;comment:显示名称"`
|
|
|
|
DisplayName string `json:"display_name" gorm:"size:100;comment:显示名称"`
|
|
|
|
Description string `json:"description" gorm:"type:text;comment:范围描述"`
|
|
|
|
Description string `json:"description" gorm:"type:text;comment:范围描述"`
|
|
|
|
@ -272,7 +269,7 @@ type OAuthClientScope struct {
|
|
|
|
|
|
|
|
|
|
|
|
// OAuthProvider 第三方OAuth提供商表(用于OAuth客户端模式)
|
|
|
|
// OAuthProvider 第三方OAuth提供商表(用于OAuth客户端模式)
|
|
|
|
type OAuthProvider struct {
|
|
|
|
type OAuthProvider struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
Name string `json:"name" gorm:"uniqueIndex;not null;size:100;comment:提供商名称"`
|
|
|
|
Name string `json:"name" gorm:"uniqueIndex;not null;size:100;comment:提供商名称"`
|
|
|
|
DisplayName string `json:"display_name" gorm:"size:100;comment:显示名称"`
|
|
|
|
DisplayName string `json:"display_name" gorm:"size:100;comment:显示名称"`
|
|
|
|
ClientID string `json:"client_id" gorm:"not null;size:255;comment:客户端ID"`
|
|
|
|
ClientID string `json:"client_id" gorm:"not null;size:255;comment:客户端ID"`
|
|
|
|
@ -289,7 +286,7 @@ type OAuthProvider struct {
|
|
|
|
|
|
|
|
|
|
|
|
// OAuthAccount 用户OAuth账户表(第三方登录)
|
|
|
|
// OAuthAccount 用户OAuth账户表(第三方登录)
|
|
|
|
type OAuthAccount struct {
|
|
|
|
type OAuthAccount struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
ProviderID string `json:"provider_id" gorm:"not null;type:varchar(32);index;comment:提供商ID"`
|
|
|
|
ProviderID string `json:"provider_id" gorm:"not null;type:varchar(32);index;comment:提供商ID"`
|
|
|
|
ProviderUserID string `json:"provider_user_id" gorm:"not null;size:255;comment:提供商用户ID"`
|
|
|
|
ProviderUserID string `json:"provider_user_id" gorm:"not null;size:255;comment:提供商用户ID"`
|
|
|
|
@ -308,7 +305,7 @@ type OAuthAccount struct {
|
|
|
|
|
|
|
|
|
|
|
|
// UserToken 用户令牌表(API令牌等)
|
|
|
|
// UserToken 用户令牌表(API令牌等)
|
|
|
|
type UserToken struct {
|
|
|
|
type UserToken struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
TokenType string `json:"token_type" gorm:"not null;size:50;comment:令牌类型"` // api, session, etc.
|
|
|
|
TokenType string `json:"token_type" gorm:"not null;size:50;comment:令牌类型"` // api, session, etc.
|
|
|
|
Token string `json:"token" gorm:"uniqueIndex;not null;size:500;comment:令牌值"`
|
|
|
|
Token string `json:"token" gorm:"uniqueIndex;not null;size:500;comment:令牌值"`
|
|
|
|
@ -325,7 +322,7 @@ type UserToken struct {
|
|
|
|
|
|
|
|
|
|
|
|
// UserSession 用户会话表
|
|
|
|
// UserSession 用户会话表
|
|
|
|
type UserSession struct {
|
|
|
|
type UserSession struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
SessionID string `json:"session_id" gorm:"uniqueIndex;not null;size:255;comment:会话ID"`
|
|
|
|
SessionID string `json:"session_id" gorm:"uniqueIndex;not null;size:255;comment:会话ID"`
|
|
|
|
IPAddress string `json:"ip_address" gorm:"size:45;comment:IP地址"`
|
|
|
|
IPAddress string `json:"ip_address" gorm:"size:45;comment:IP地址"`
|
|
|
|
@ -340,7 +337,7 @@ type UserSession struct {
|
|
|
|
|
|
|
|
|
|
|
|
// OAuthUserConsent 用户授权同意表
|
|
|
|
// OAuthUserConsent 用户授权同意表
|
|
|
|
type OAuthUserConsent struct {
|
|
|
|
type OAuthUserConsent struct {
|
|
|
|
models.BaseModel
|
|
|
|
vigo.Model
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null;type:varchar(32);index;comment:用户ID"`
|
|
|
|
ClientID string `json:"client_id" gorm:"not null;type:varchar(32);index;comment:客户端ID"`
|
|
|
|
ClientID string `json:"client_id" gorm:"not null;type:varchar(32);index;comment:客户端ID"`
|
|
|
|
Scope string `json:"scope" gorm:"type:text;comment:授权范围"`
|
|
|
|
Scope string `json:"scope" gorm:"type:text;comment:授权范围"`
|
|
|
|
|