更新 Go 版本至 1.23.0,添加管理员功能,包括管理员登录、用户和客户端管理,新增相应的模板和中间件,优化数据库模型以支持管理员管理。

This commit is contained in:
2025-04-17 01:47:10 +08:00
parent a3f3cc17cf
commit 83c82f7135
18 changed files with 686 additions and 21 deletions

30
main.go
View File

@@ -10,8 +10,10 @@ import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"html/template"
"oidc-oauth2-server/config"
"oidc-oauth2-server/handlers"
"oidc-oauth2-server/middleware"
"oidc-oauth2-server/models"
"oidc-oauth2-server/services"
)
@@ -41,11 +43,20 @@ func main() {
}
clientService := services.NewClientService(db)
tokenService := services.NewTokenService(db, oauthService.GetKeyManager())
adminService := services.NewAdminService(db)
// 设置 Gin 路由
r := gin.Default()
// 设置模板目录
r.SetFuncMap(template.FuncMap{
"subtract": func(a, b int) int {
return a - b
},
"add": func(a, b int) int {
return a + b
},
})
r.LoadHTMLGlob("templates/*")
// 设置 session 中间件
@@ -64,10 +75,13 @@ func main() {
oidcHandler := handlers.NewOIDCHandler(config.GlobalConfig.OAuth.IssuerURL, oauthService, authService)
registrationHandler := handlers.NewRegistrationHandler(clientService)
tokenHandler := handlers.NewTokenHandler(tokenService)
adminHandler := handlers.NewAdminHandler(adminService)
// 认证路由
r.GET("/login", authHandler.ShowLogin)
r.POST("/login", authHandler.HandleLogin)
r.GET("/signup", authHandler.ShowSignup)
r.POST("/signup", authHandler.HandleSignup)
// OIDC 端点
r.GET("/.well-known/openid-configuration", oidcHandler.OpenIDConfiguration)
@@ -86,6 +100,22 @@ func main() {
r.POST("/revoke", tokenHandler.Revoke)
r.POST("/introspect", tokenHandler.Introspect)
// 管理后台路由
admin := r.Group("/admin")
{
admin.GET("/login", adminHandler.ShowAdminLogin)
admin.POST("/login", adminHandler.HandleAdminLogin)
// 需要管理员认证的路由
authorized := admin.Group("/")
authorized.Use(middleware.AdminAuthRequired())
{
authorized.GET("/dashboard", adminHandler.Dashboard)
authorized.GET("/users", adminHandler.ListUsers)
authorized.GET("/clients", adminHandler.ListClients)
}
}
// 启动服务器
addr := fmt.Sprintf("%s:%d", config.GlobalConfig.Server.Host, config.GlobalConfig.Server.Port)
log.Printf("Starting server on %s", addr)