-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29e49c1
commit 1187dc9
Showing
2 changed files
with
52 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |