Files
pocket-id-pocket-id-1/backend/internal/middleware/jwt_auth.go

60 lines
1.5 KiB
Go
Raw Normal View History

2024-08-12 11:00:25 +02:00
package middleware
import (
"strings"
"github.com/gin-gonic/gin"
"github.com/pocket-id/pocket-id/backend/internal/common"
"github.com/pocket-id/pocket-id/backend/internal/service"
"github.com/pocket-id/pocket-id/backend/internal/utils/cookie"
2024-08-12 11:00:25 +02:00
)
type JwtAuthMiddleware struct {
jwtService *service.JwtService
}
func NewJwtAuthMiddleware(jwtService *service.JwtService) *JwtAuthMiddleware {
return &JwtAuthMiddleware{jwtService: jwtService}
}
2024-08-12 11:00:25 +02:00
func (m *JwtAuthMiddleware) Add(adminRequired bool) gin.HandlerFunc {
return func(c *gin.Context) {
2024-08-12 11:00:25 +02:00
userID, isAdmin, err := m.Verify(c, adminRequired)
if err != nil {
2024-08-12 11:00:25 +02:00
c.Abort()
c.Error(err)
2024-08-12 11:00:25 +02:00
return
}
c.Set("userID", userID)
c.Set("userIsAdmin", isAdmin)
c.Next()
}
}
func (m *JwtAuthMiddleware) Verify(c *gin.Context, adminRequired bool) (userID string, isAdmin bool, err error) {
// Extract the token from the cookie
token, err := c.Cookie(cookie.AccessTokenCookieName)
if err != nil {
// Try to extract the token from the Authorization header if it's not in the cookie
authorizationHeaderSplit := strings.Split(c.GetHeader("Authorization"), " ")
if len(authorizationHeaderSplit) != 2 {
return "", false, &common.NotSignedInError{}
2024-08-12 11:00:25 +02:00
}
token = authorizationHeaderSplit[1]
}
2024-08-12 11:00:25 +02:00
claims, err := m.jwtService.VerifyAccessToken(token)
if err != nil {
return "", false, &common.NotSignedInError{}
2024-08-12 11:00:25 +02:00
}
// Check if the user is an admin
if adminRequired && !claims.IsAdmin {
return "", false, &common.MissingPermissionError{}
}
return claims.Subject, claims.IsAdmin, nil
2024-08-12 11:00:25 +02:00
}