File size: 6,623 Bytes
f1dd159 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | package adminauth
import (
"errors"
"io"
"net"
"net/http"
"strings"
"time"
adminapp "github.com/chenyme/grok2api/backend/internal/application/adminauth"
admindomain "github.com/chenyme/grok2api/backend/internal/domain/admin"
"github.com/chenyme/grok2api/backend/internal/shared/response"
"github.com/chenyme/grok2api/backend/internal/transport/http/middleware"
"github.com/gin-gonic/gin"
)
const refreshCookieName = "grok2api_admin_refresh"
type Handler struct {
service *adminapp.Service
secureCookies bool
}
func NewHandler(service *adminapp.Service, secureCookies bool) *Handler {
return &Handler{service: service, secureCookies: secureCookies}
}
func (h *Handler) RegisterPublic(router *gin.RouterGroup) {
router.POST("/auth/login", h.login)
router.POST("/auth/refresh", h.refresh)
router.POST("/auth/logout", h.logout)
}
func (h *Handler) RegisterAuthenticated(router *gin.RouterGroup) {
router.GET("/me", h.me)
router.PUT("/me/password", h.changePassword)
}
type loginRequest struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}
type refreshRequest struct {
RefreshToken string `json:"refreshToken"`
}
type changePasswordRequest struct {
CurrentPassword string `json:"currentPassword" binding:"required"`
NewPassword string `json:"newPassword" binding:"required"`
}
type tokenResponse struct {
AccessToken string `json:"accessToken"`
AccessTokenExpiresAt string `json:"accessTokenExpiresAt"`
RefreshTokenExpiresAt string `json:"refreshTokenExpiresAt"`
}
type adminResponse struct {
ID uint64 `json:"id,string"`
Username string `json:"username"`
}
func (h *Handler) login(c *gin.Context) {
var request loginRequest
if err := c.ShouldBindJSON(&request); err != nil {
response.Error(c, http.StatusBadRequest, "invalidRequest", "请求参数无效")
return
}
adminValue, tokens, err := h.service.Login(c.Request.Context(), request.Username, request.Password, remoteAddress(c.Request))
if err != nil {
if errors.Is(err, adminapp.ErrLoginRateLimited) {
response.Error(c, http.StatusTooManyRequests, "loginRateLimited", "登录尝试过于频繁,请稍后重试")
return
}
if errors.Is(err, adminapp.ErrRuntimeUnavailable) {
response.Error(c, http.StatusServiceUnavailable, "authRuntimeUnavailable", "管理员认证服务暂不可用")
return
}
response.Error(c, http.StatusUnauthorized, "invalidCredentials", "管理员账号或密码错误")
return
}
h.setRefreshCookie(c, tokens)
response.Success(c, http.StatusOK, gin.H{"admin": newAdminResponse(adminValue), "tokens": newTokenResponse(tokens)})
}
func remoteAddress(request *http.Request) string {
value := strings.TrimSpace(request.RemoteAddr)
host, _, err := net.SplitHostPort(value)
if err == nil && host != "" {
return host
}
return value
}
func (h *Handler) refresh(c *gin.Context) {
var request refreshRequest
if err := c.ShouldBindJSON(&request); err != nil && !errors.Is(err, io.EOF) {
response.Error(c, http.StatusBadRequest, "invalidRequest", "请求参数无效")
return
}
if request.RefreshToken == "" {
request.RefreshToken, _ = c.Cookie(refreshCookieName)
}
if request.RefreshToken == "" {
response.Error(c, http.StatusUnauthorized, "invalidRefreshToken", "刷新会话无效")
return
}
tokens, err := h.service.Refresh(c.Request.Context(), request.RefreshToken)
if err != nil {
if errors.Is(err, adminapp.ErrRuntimeUnavailable) {
response.Error(c, http.StatusServiceUnavailable, "authRuntimeUnavailable", "管理员认证服务暂不可用")
return
}
response.Error(c, http.StatusUnauthorized, "invalidRefreshToken", "刷新会话无效")
return
}
h.setRefreshCookie(c, tokens)
response.Success(c, http.StatusOK, newTokenResponse(tokens))
}
func (h *Handler) logout(c *gin.Context) {
var request refreshRequest
if err := c.ShouldBindJSON(&request); err != nil && !errors.Is(err, io.EOF) {
response.Error(c, http.StatusBadRequest, "invalidRequest", "请求参数无效")
return
}
if request.RefreshToken == "" {
request.RefreshToken, _ = c.Cookie(refreshCookieName)
}
if err := h.service.Logout(c.Request.Context(), request.RefreshToken); err != nil {
response.Error(c, http.StatusServiceUnavailable, "authRuntimeUnavailable", "管理员认证服务暂不可用")
return
}
c.SetSameSite(http.SameSiteStrictMode)
c.SetCookie(refreshCookieName, "", -1, "/api/admin/v1/auth", "", h.secureCookies || c.Request.TLS != nil, true)
response.Success(c, http.StatusOK, gin.H{"loggedOut": true})
}
func (h *Handler) me(c *gin.Context) {
value, ok := c.Get(middleware.AdminKey)
adminValue, valid := value.(admindomain.Admin)
if !ok || !valid {
response.Error(c, http.StatusUnauthorized, "adminUnauthorized", "管理员登录已失效")
return
}
response.Success(c, http.StatusOK, newAdminResponse(adminValue))
}
func (h *Handler) changePassword(c *gin.Context) {
var request changePasswordRequest
if err := c.ShouldBindJSON(&request); err != nil {
response.Error(c, http.StatusBadRequest, "invalidRequest", "请求参数无效")
return
}
value, ok := c.Get(middleware.AdminKey)
adminValue, valid := value.(admindomain.Admin)
if !ok || !valid {
response.Error(c, http.StatusUnauthorized, "adminUnauthorized", "管理员登录已失效")
return
}
if err := h.service.ChangePassword(c.Request.Context(), adminValue.ID, request.CurrentPassword, request.NewPassword); err != nil {
if errors.Is(err, adminapp.ErrInvalidCredentials) || errors.Is(err, adminapp.ErrInvalidPassword) {
response.Error(c, http.StatusBadRequest, "passwordChangeFailed", err.Error())
return
}
response.Error(c, http.StatusInternalServerError, "passwordChangeFailed", "修改管理员密码失败")
return
}
response.Success(c, http.StatusOK, gin.H{"passwordChanged": true})
}
func newAdminResponse(value admindomain.Admin) adminResponse {
return adminResponse{ID: value.ID, Username: value.Username}
}
func newTokenResponse(value adminapp.Tokens) tokenResponse {
return tokenResponse{AccessToken: value.AccessToken, AccessTokenExpiresAt: value.AccessTokenExpiresAt.Format(time.RFC3339), RefreshTokenExpiresAt: value.RefreshTokenExpiresAt.Format(time.RFC3339)}
}
func (h *Handler) setRefreshCookie(c *gin.Context, value adminapp.Tokens) {
maxAge := int(time.Until(value.RefreshTokenExpiresAt).Seconds())
if maxAge < 0 {
maxAge = 0
}
c.SetSameSite(http.SameSiteStrictMode)
c.SetCookie(refreshCookieName, value.RefreshToken, maxAge, "/api/admin/v1/auth", "", h.secureCookies || c.Request.TLS != nil, true)
}
|