mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 05:56:38 +08:00
76e9d5b0e7
Result: {"status":"keep","total_issues":8,"golint_canonicalheader":0,"golint_errname":0,"golint_errorlint":1,"golint_forcetypeassert":0,"golint_gosec":0,"golint_intrange":0,"golint_modernize":3,"golint_nilnil":3,"golint_perfsprint":0,"golint_prealloc":0,"golint_recvcheck":1,"golint_usestdlibvars":0,"golint_wastedassign":0,"golint_total":8,"eslint_problems":0,"eslint_errors":0,"eslint_warnings":0,"tsc_errors":0,"measure_s":90,"golint_test_testifylint":0,"golint_test_thelper":0,"golint_test_usetesting":0,"golint_test_total":0,"golint_exhaustive":0,"golint_vetx_total":0,"vitest_failed":0,"vitest_total":126}
100 lines
2.3 KiB
Go
100 lines
2.3 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package oauth
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/infra/config"
|
|
"github.com/Rain-kl/Wavelet/internal/model"
|
|
"github.com/Rain-kl/Wavelet/internal/repository"
|
|
"github.com/gin-contrib/sessions"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
gsessions "github.com/gorilla/sessions"
|
|
)
|
|
|
|
// GetUserIDFromSession 从 Session 中提取用户 ID
|
|
func GetUserIDFromSession(s sessions.Session) uint64 {
|
|
userID, ok := s.Get(UserIDKey).(uint64)
|
|
if !ok {
|
|
return 0
|
|
}
|
|
return userID
|
|
}
|
|
|
|
// GetUserIDFromContext 从 Gin Context 的 Session 中提取用户 ID
|
|
func GetUserIDFromContext(c *gin.Context) uint64 {
|
|
session := sessions.Default(c)
|
|
return GetUserIDFromSession(session)
|
|
}
|
|
|
|
func ensureSessionToken(s sessions.Session) (string, bool) {
|
|
token, ok := s.Get(SessionTokenKey).(string)
|
|
if !ok || token == "" {
|
|
token = uuid.NewString()
|
|
s.Set(SessionTokenKey, token)
|
|
return token, true
|
|
}
|
|
return token, false
|
|
}
|
|
|
|
func hashSessionToken(token string) string {
|
|
h := sha256.New()
|
|
h.Write([]byte(token))
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
func rotateSessionID(s sessions.Session) {
|
|
if inner, ok := s.(interface{ Session() *gsessions.Session }); ok {
|
|
if sess := inner.Session(); sess != nil {
|
|
sess.ID = ""
|
|
}
|
|
}
|
|
}
|
|
|
|
// SetLoginSession writes the authenticated user into a freshly rotated session.
|
|
func SetLoginSession(ctx context.Context, c *gin.Context, user *model.User, extras ...map[string]any) error {
|
|
session := sessions.Default(c)
|
|
session.Clear()
|
|
rotateSessionID(session)
|
|
|
|
session.Set(UserIDKey, user.ID)
|
|
session.Set(UserNameKey, user.Username)
|
|
session.Set(PasswordHashKey, user.Password)
|
|
if len(extras) > 0 {
|
|
for key, value := range extras[0] {
|
|
session.Set(key, value)
|
|
}
|
|
}
|
|
|
|
maxAge := config.Config.App.SessionAge
|
|
isSessionCookie := false
|
|
|
|
ttlHours, err := repository.GetIntByKey(ctx, model.ConfigKeyLoginSessionTTLHours)
|
|
if err == nil {
|
|
switch {
|
|
case ttlHours == -1:
|
|
maxAge = 10 * 365 * 24 * 3600
|
|
case ttlHours > 0:
|
|
maxAge = ttlHours * 3600
|
|
case ttlHours == 0:
|
|
isSessionCookie = true
|
|
}
|
|
}
|
|
session.Options(GetSessionOptions(maxAge))
|
|
|
|
if err := session.Save(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if isSessionCookie {
|
|
StripCookieMaxAgeAndExpires(c.Writer.Header(), config.Config.App.SessionCookieName)
|
|
}
|
|
|
|
return nil
|
|
}
|