Skip to content

Commit

Permalink
🧹 chore: fiber locals reorg (#905)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored May 25, 2024
1 parent d2b8078 commit a9661bf
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 16 deletions.
9 changes: 5 additions & 4 deletions backend/entities/oauth/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"errors"
"net/http"

"github.com/GenerateNU/sac/backend/auth"
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/locals"

"github.com/gofiber/fiber/v2"
)

Expand All @@ -25,7 +26,7 @@ func (oc *OAuthController) Authorize(c *fiber.Ctx) error {
}

// Extract the user making the call:
userID, err := auth.UserIDFrom(c)
userID, err := locals.UserID(c)
if err != nil {
return err
}
Expand Down Expand Up @@ -53,7 +54,7 @@ func (oc *OAuthController) Token(c *fiber.Ctx) error {
}

// Extract the user making the call:
userID, err := auth.UserIDFrom(c)
userID, err := locals.UserID(c)
if err != nil {
return err
}
Expand All @@ -75,7 +76,7 @@ func (oc *OAuthController) Revoke(c *fiber.Ctx) error {
}

// Extract the user making the call:
userID, err := auth.UserIDFrom(c)
userID, err := locals.UserID(c)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions backend/entities/users/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package base
import (
"net/http"

"github.com/GenerateNU/sac/backend/auth"
authEntities "github.com/GenerateNU/sac/backend/entities/auth"
"github.com/GenerateNU/sac/backend/locals"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"

Expand Down Expand Up @@ -63,7 +63,7 @@ func (u *UserController) GetUsers(c *fiber.Ctx) error {
// @Failure 500 {object} error
// @Router /auth/me [get]
func (u *UserController) GetMe(c *fiber.Ctx) error {
userID, err := auth.UserIDFrom(c)
userID, err := locals.UserID(c)
if err != nil {
return err
}
Expand Down
27 changes: 27 additions & 0 deletions backend/locals/claims.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package locals

import (
"fmt"

"github.com/GenerateNU/sac/backend/auth"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/gofiber/fiber/v2"
)

func CustomClaims(c *fiber.Ctx) (*auth.CustomClaims, error) {
rawClaims := c.Locals(claimsKey)
if rawClaims == nil {
return nil, utilities.Forbidden()
}

claims, ok := rawClaims.(*auth.CustomClaims)
if !ok {
return nil, fmt.Errorf("claims are not of type auth.CustomClaims. got: %T", rawClaims)
}

return claims, nil
}

func SetCustomClaims(c *fiber.Ctx, claims *auth.CustomClaims) {
c.Locals(claimsKey, claims)
}
8 changes: 8 additions & 0 deletions backend/locals/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package locals

type localsKey byte

const (
claimsKey localsKey = 0
userIDKey localsKey = 1
)
27 changes: 27 additions & 0 deletions backend/locals/user_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package locals

import (
"fmt"

"github.com/GenerateNU/sac/backend/utilities"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)

func UserID(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)
}
9 changes: 5 additions & 4 deletions backend/middleware/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/GenerateNU/sac/backend/auth"
"github.com/GenerateNU/sac/backend/locals"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/google/uuid"

Expand All @@ -17,7 +18,7 @@ import (
)

func (m *AuthMiddlewareService) IsSuper(c *fiber.Ctx) bool {
claims, err := auth.CustomClaimsFrom(c)
claims, err := locals.CustomClaims(c)
if err != nil {
_ = err
return false
Expand Down Expand Up @@ -67,15 +68,15 @@ func (m *AuthMiddlewareService) Authenticate(c *fiber.Ctx) error {
// return errors.Unauthorized.FiberError(c)
// }

auth.SetClaims(c, claims)
locals.SetCustomClaims(c, claims)

rawUserID := claims.Issuer
userID, err := uuid.Parse(rawUserID)
if err != nil {
return fmt.Errorf("invalid user id: %s", rawUserID)
}

auth.SetUserID(c, &userID)
locals.SetUserID(c, &userID)

return nil
}(c)
Expand All @@ -88,7 +89,7 @@ func (m *AuthMiddlewareService) Authorize(requiredPermissions ...auth.Permission
return utilities.Unauthorized()
}

claims, err := auth.CustomClaimsFrom(c)
claims, err := locals.CustomClaims(c)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions backend/middleware/auth/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package auth
import (
"slices"

"github.com/GenerateNU/sac/backend/auth"
"github.com/GenerateNU/sac/backend/entities/clubs"
"github.com/GenerateNU/sac/backend/locals"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/gofiber/fiber/v2"
)
Expand All @@ -25,7 +25,7 @@ func (m *AuthMiddlewareService) ClubAuthorizeById(c *fiber.Ctx, extractor Extrac
return err
}

userID, err := auth.UserIDFrom(c)
userID, err := locals.UserID(c)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions backend/middleware/auth/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package auth
import (
"slices"

"github.com/GenerateNU/sac/backend/auth"
"github.com/GenerateNU/sac/backend/entities/events"
"github.com/GenerateNU/sac/backend/locals"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/gofiber/fiber/v2"
)
Expand All @@ -25,7 +25,7 @@ func (m *AuthMiddlewareService) EventAuthorizeById(c *fiber.Ctx, extractor Extra
return err
}

userID, err := auth.UserIDFrom(c)
userID, err := locals.UserID(c)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions backend/middleware/auth/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package auth

import (
"github.com/GenerateNU/sac/backend/auth"
"github.com/GenerateNU/sac/backend/locals"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/gofiber/fiber/v2"
)
Expand All @@ -21,7 +21,7 @@ func (m *AuthMiddlewareService) UserAuthorizeById(c *fiber.Ctx) error {
return err
}

userID, err := auth.UserIDFrom(c)
userID, err := locals.UserID(c)
if err != nil {
return err
}
Expand Down

0 comments on commit a9661bf

Please sign in to comment.