添加 OIDC 和 OAuth2 服务器的基础结构,包括配置、数据库模型、服务、处理器和路由。新增登录页面模板,支持用户认证和授权流程。

This commit is contained in:
2025-04-17 01:08:15 +08:00
commit 0368547137
17 changed files with 1049 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
package models
import (
"time"
"gorm.io/gorm"
)
type AuthorizationCode struct {
gorm.Model
Code string `gorm:"uniqueIndex;not null"`
ClientID string `gorm:"not null"`
UserID uint `gorm:"not null"`
RedirectURI string `gorm:"not null"`
Scope string `gorm:"not null"`
ExpiresAt time.Time `gorm:"not null"`
Used bool `gorm:"default:false"`
}
func (ac *AuthorizationCode) TableName() string {
return "oauth_authorization_codes"
}

23
models/client.go Normal file
View File

@@ -0,0 +1,23 @@
package models
import (
"time"
"gorm.io/gorm"
)
type Client struct {
gorm.Model
ClientID string `gorm:"uniqueIndex;not null"`
ClientSecret string `gorm:"not null"`
RedirectURIs []string `gorm:"type:json"`
GrantTypes []string `gorm:"type:json"`
Scopes []string `gorm:"type:json"`
IsActive bool `gorm:"default:true"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (c *Client) TableName() string {
return "oauth_clients"
}

14
models/migration.go Normal file
View File

@@ -0,0 +1,14 @@
package models
import (
"gorm.io/gorm"
)
// AutoMigrate 自动迁移数据库表结构
func AutoMigrate(db *gorm.DB) error {
return db.AutoMigrate(
&User{},
&Client{},
&AuthorizationCode{},
)
}

22
models/user.go Normal file
View File

@@ -0,0 +1,22 @@
package models
import (
"time"
"gorm.io/gorm"
)
type User struct {
gorm.Model
Username string `gorm:"uniqueIndex;not null"`
Password string `gorm:"not null"`
Email string `gorm:"uniqueIndex"`
LastLogin time.Time
IsActive bool `gorm:"default:true"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (u *User) TableName() string {
return "users"
}