Changeset 0.28.4 (#190)
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/auth"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/config"
|
||||
@@ -19,7 +18,7 @@ import (
|
||||
// session_id, channel_id, and auth_type="session".
|
||||
//
|
||||
// Session auth is only valid for workflow channels with allow_anonymous=true.
|
||||
func AuthOrSession(cfg *config.Config, stores store.Stores) gin.HandlerFunc {
|
||||
func AuthOrSession(cfg *config.Config, stores store.Stores, cache *UserStatusCache) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// Skip auth when running without a database
|
||||
if !database.IsConnected() {
|
||||
@@ -30,17 +29,14 @@ func AuthOrSession(cfg *config.Config, stores store.Stores) gin.HandlerFunc {
|
||||
// ── Try normal JWT auth first ──
|
||||
tokenString := extractBearerToken(c)
|
||||
if tokenString != "" {
|
||||
claims := &Claims{}
|
||||
token, err := jwt.ParseWithClaims(tokenString, claims, func(t *jwt.Token) (interface{}, error) {
|
||||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, jwt.ErrSignatureInvalid
|
||||
if claims, ok := parseAndValidateJWT(tokenString, cfg.JWTSecret); ok {
|
||||
role, valid := verifyUser(c, claims, stores.Users, cache)
|
||||
if !valid {
|
||||
return
|
||||
}
|
||||
return []byte(cfg.JWTSecret), nil
|
||||
})
|
||||
if err == nil && token.Valid {
|
||||
c.Set("user_id", claims.UserID)
|
||||
c.Set("email", claims.Email)
|
||||
c.Set("role", claims.Role)
|
||||
c.Set("role", role)
|
||||
c.Set("auth_type", "user")
|
||||
c.Next()
|
||||
return
|
||||
@@ -49,17 +45,14 @@ func AuthOrSession(cfg *config.Config, stores store.Stores) gin.HandlerFunc {
|
||||
|
||||
// ── Try sb_token cookie (page auth pattern) ──
|
||||
if cookie, err := c.Cookie("sb_token"); err == nil && cookie != "" {
|
||||
claims := &Claims{}
|
||||
token, err := jwt.ParseWithClaims(cookie, claims, func(t *jwt.Token) (interface{}, error) {
|
||||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, jwt.ErrSignatureInvalid
|
||||
if claims, ok := parseAndValidateJWT(cookie, cfg.JWTSecret); ok {
|
||||
role, valid := verifyUser(c, claims, stores.Users, cache)
|
||||
if !valid {
|
||||
return
|
||||
}
|
||||
return []byte(cfg.JWTSecret), nil
|
||||
})
|
||||
if err == nil && token.Valid {
|
||||
c.Set("user_id", claims.UserID)
|
||||
c.Set("email", claims.Email)
|
||||
c.Set("role", claims.Role)
|
||||
c.Set("role", role)
|
||||
c.Set("auth_type", "user")
|
||||
c.Next()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user