From 1187dc9452208625f1135d86bf9862a2ae90e0e2 Mon Sep 17 00:00:00 2001 From: garrettladley Date: Fri, 24 May 2024 21:07:49 -0400 Subject: [PATCH] soc --- backend/auth/jwt.go | 46 ------------------------------------- backend/auth/locals.go | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 46 deletions(-) create mode 100644 backend/auth/locals.go diff --git a/backend/auth/jwt.go b/backend/auth/jwt.go index 0aec5e2d..5aec8d5e 100644 --- a/backend/auth/jwt.go +++ b/backend/auth/jwt.go @@ -6,7 +6,6 @@ import ( "github.com/GenerateNU/sac/backend/config" "github.com/GenerateNU/sac/backend/constants" - "github.com/google/uuid" "github.com/GenerateNU/sac/backend/utilities" m "github.com/garrettladley/mattress" @@ -19,51 +18,6 @@ type CustomClaims struct { Role string `json:"role"` } -type localsKey byte - -const ( - claimsKey localsKey = 0 - userIDKey localsKey = 1 -) - -// CustomClaimsFrom extracts the CustomClaims from the fiber context -// Returns nil if the claims are not present -func CustomClaimsFrom(c *fiber.Ctx) (*CustomClaims, error) { - rawClaims := c.Locals(claimsKey) - if rawClaims == nil { - return nil, utilities.Forbidden() - } - - claims, ok := rawClaims.(*CustomClaims) - if !ok { - return nil, fmt.Errorf("claims are not of type CustomClaims. got: %T", rawClaims) - } - - return claims, nil -} - -func SetClaims(c *fiber.Ctx, claims *CustomClaims) { - c.Locals(claimsKey, claims) -} - -func UserIDFrom(c *fiber.Ctx) (*uuid.UUID, error) { - userID := c.Locals(userIDKey) - if userID == nil { - return nil, utilities.Forbidden() - } - - id, ok := userID.(*uuid.UUID) - if !ok { - return nil, fmt.Errorf("userID is not of type uuid.UUID. got: %T", userID) - } - - return id, nil -} - -func SetUserID(c *fiber.Ctx, id *uuid.UUID) { - c.Locals(userIDKey, id) -} - type JWTType string const ( diff --git a/backend/auth/locals.go b/backend/auth/locals.go new file mode 100644 index 00000000..8c195565 --- /dev/null +++ b/backend/auth/locals.go @@ -0,0 +1,52 @@ +package auth + +import ( + "fmt" + + "github.com/GenerateNU/sac/backend/utilities" + "github.com/gofiber/fiber/v2" + "github.com/google/uuid" +) + +type localsKey byte + +const ( + claimsKey localsKey = 0 + userIDKey localsKey = 1 +) + +func CustomClaimsFrom(c *fiber.Ctx) (*CustomClaims, error) { + rawClaims := c.Locals(claimsKey) + if rawClaims == nil { + return nil, utilities.Forbidden() + } + + claims, ok := rawClaims.(*CustomClaims) + if !ok { + return nil, fmt.Errorf("claims are not of type CustomClaims. got: %T", rawClaims) + } + + return claims, nil +} + +func SetClaims(c *fiber.Ctx, claims *CustomClaims) { + c.Locals(claimsKey, claims) +} + +func UserIDFrom(c *fiber.Ctx) (*uuid.UUID, error) { + userID := c.Locals(userIDKey) + if userID == nil { + return nil, utilities.Forbidden() + } + + id, ok := userID.(*uuid.UUID) + if !ok { + return nil, fmt.Errorf("userID is not of type uuid.UUID. got: %T", userID) + } + + return id, nil +} + +func SetUserID(c *fiber.Ctx, id *uuid.UUID) { + c.Locals(userIDKey, id) +}