Skip to content

Commit

Permalink
DAVID GOATED | drill down AuthSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 3, 2024
1 parent 124faac commit 307dc76
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 29 deletions.
28 changes: 12 additions & 16 deletions backend/src/auth/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ func ExpireCookie(name string) *fiber.Cookie {
}

// RefreshAccessToken refreshes the access token
func RefreshAccessToken(refreshCookie string, role string, accessExpiresAfter uint, accessTokenSecret string) (*string, *errors.Error) {
func RefreshAccessToken(refreshCookie string, role string, refreshTokenSecret string, accessExpiresAfter uint, accessTokenSecret string) (*string, *errors.Error) {
// Parse the refresh token
refreshToken, err := ParseRefreshToken(refreshCookie)
refreshToken, err := ParseRefreshToken(refreshCookie, refreshTokenSecret)
if err != nil {
return nil, &errors.FailedToParseRefreshToken
}
Expand All @@ -126,26 +126,22 @@ func RefreshAccessToken(refreshCookie string, role string, accessExpiresAfter ui
}

// ParseAccessToken parses the access token
func ParseAccessToken(cookie string) (*jwt.Token, error) {
var settings config.Settings

func ParseAccessToken(cookie string, accessTokenSecret string) (*jwt.Token, error) {
return jwt.ParseWithClaims(cookie, &types.CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(settings.Auth.AccessToken), nil
return []byte(accessTokenSecret), nil
})
}

// ParseRefreshToken parses the refresh token
func ParseRefreshToken(cookie string) (*jwt.Token, error) {
var settings config.Settings

func ParseRefreshToken(cookie string, refreshTokenSecret string) (*jwt.Token, error) {
return jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(settings.Auth.RefreshToken), nil
return []byte(refreshTokenSecret), nil
})
}

// GetRoleFromToken gets the role from the custom claims
func GetRoleFromToken(tokenString string) (*string, error) {
token, err := ParseAccessToken(tokenString)
func GetRoleFromToken(tokenString string, accessTokenSecret string) (*string, error) {
token, err := ParseAccessToken(tokenString, accessTokenSecret)
if err != nil {
return nil, err
}
Expand All @@ -159,8 +155,8 @@ func GetRoleFromToken(tokenString string) (*string, error) {
}

// ExtractClaims extracts the claims from the token
func ExtractAccessClaims(tokenString string) (*types.CustomClaims, *errors.Error) {
token, err := ParseAccessToken(tokenString)
func ExtractAccessClaims(tokenString string, accessTokenSecret string) (*types.CustomClaims, *errors.Error) {
token, err := ParseAccessToken(tokenString, accessTokenSecret)
if err != nil {
return nil, &errors.FailedToParseAccessToken
}
Expand All @@ -174,8 +170,8 @@ func ExtractAccessClaims(tokenString string) (*types.CustomClaims, *errors.Error
}

// ExtractClaims extracts the claims from the token
func ExtractRefreshClaims(tokenString string) (*jwt.StandardClaims, *errors.Error) {
token, err := ParseRefreshToken(tokenString)
func ExtractRefreshClaims(tokenString string, refreshTokenSecret string) (*jwt.StandardClaims, *errors.Error) {
token, err := ParseRefreshToken(tokenString, refreshTokenSecret)
if err != nil {
return nil, &errors.FailedToParseRefreshToken
}
Expand Down
6 changes: 3 additions & 3 deletions backend/src/controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (a *AuthController) Me(c *fiber.Ctx) error {
// Extract token values from cookies
accessTokenValue := c.Cookies("access_token")

claims, err := auth.ExtractAccessClaims(accessTokenValue)
claims, err := auth.ExtractAccessClaims(accessTokenValue, a.AuthSettings.AccessToken)
if err != nil {
return err.FiberError(c)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (a *AuthController) Refresh(c *fiber.Ctx) error {
refreshTokenValue := c.Cookies("refresh_token")

// Extract id from refresh token
claims, err := auth.ExtractRefreshClaims(refreshTokenValue)
claims, err := auth.ExtractRefreshClaims(refreshTokenValue, a.AuthSettings.RefreshToken)
if err != nil {
return err.FiberError(c)
}
Expand All @@ -112,7 +112,7 @@ func (a *AuthController) Refresh(c *fiber.Ctx) error {
return err.FiberError(c)
}

accessToken, err := auth.RefreshAccessToken(refreshTokenValue, string(*role), a.AuthSettings.AccessTokenExpiry, a.AuthSettings.AccessToken)
accessToken, err := auth.RefreshAccessToken(refreshTokenValue, string(*role), a.AuthSettings.RefreshToken, a.AuthSettings.AccessTokenExpiry, a.AuthSettings.AccessToken)
if err != nil {
return err.FiberError(c)
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (m *MiddlewareService) Authenticate(c *fiber.Ctx) error {
return c.Next()
}

token, err := auth.ParseAccessToken(c.Cookies("access_token"))
token, err := auth.ParseAccessToken(c.Cookies("access_token"), m.AuthSettings.AccessToken)
if err != nil {
return errors.FailedToParseAccessToken.FiberError(c)
}
Expand All @@ -42,7 +42,7 @@ func (m *MiddlewareService) Authenticate(c *fiber.Ctx) error {

func (m *MiddlewareService) Authorize(requiredPermissions ...types.Permission) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
role, err := auth.GetRoleFromToken(c.Cookies("access_token"))
role, err := auth.GetRoleFromToken(c.Cookies("access_token"), m.AuthSettings.AccessToken)
if err != nil {
return errors.FailedToParseAccessToken.FiberError(c)
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/middleware/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (m *MiddlewareService) ClubAuthorizeById(c *fiber.Ctx) error {
return errors.FailedToParseUUID.FiberError(c)
}

token, tokenErr := auth.ParseAccessToken(c.Cookies("access_token"))
token, tokenErr := auth.ParseAccessToken(c.Cookies("access_token"), m.AuthSettings.AccessToken)
if tokenErr != nil {
return errors.FailedToParseAccessToken.FiberError(c)
}
Expand Down
13 changes: 8 additions & 5 deletions backend/src/middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middleware

import (
"github.com/GenerateNU/sac/backend/src/config"
"github.com/GenerateNU/sac/backend/src/types"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
Expand All @@ -15,13 +16,15 @@ type MiddlewareInterface interface {
}

type MiddlewareService struct {
DB *gorm.DB
Validate *validator.Validate
DB *gorm.DB
Validate *validator.Validate
AuthSettings config.AuthSettings
}

func NewMiddlewareService(db *gorm.DB, validate *validator.Validate) *MiddlewareService {
func NewMiddlewareService(db *gorm.DB, validate *validator.Validate, authSettings config.AuthSettings) *MiddlewareService {
return &MiddlewareService{
DB: db,
Validate: validate,
DB: db,
Validate: validate,
AuthSettings: authSettings,
}
}
2 changes: 1 addition & 1 deletion backend/src/middleware/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (m *MiddlewareService) UserAuthorizeById(c *fiber.Ctx) error {
return errors.FailedToParseUUID.FiberError(c)
}

token, tokenErr := auth.ParseAccessToken(c.Cookies("access_token"))
token, tokenErr := auth.ParseAccessToken(c.Cookies("access_token"), m.AuthSettings.AccessToken)
if tokenErr != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Init(db *gorm.DB, settings config.Settings) *fiber.App {
app := newFiberApp()

validate := utilities.RegisterCustomValidators()
middlewareService := middleware.NewMiddlewareService(db, validate)
middlewareService := middleware.NewMiddlewareService(db, validate, settings.Auth)

apiv1 := app.Group("/api/v1")
apiv1.Use(middlewareService.Authenticate)
Expand Down

0 comments on commit 307dc76

Please sign in to comment.