Skip to content

Commit

Permalink
ran gofumpt -w .
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye committed Feb 2, 2024
1 parent fe324f7 commit edcea7f
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 80 deletions.
2 changes: 1 addition & 1 deletion backend/src/auth/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func GetRoleFromToken(tokenString string) (*string, error) {
if !ok || !token.Valid {
return nil, &errors.FailedToValidateAccessToken
}

return &claims.Role, nil
}

Expand Down
16 changes: 8 additions & 8 deletions backend/src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Settings struct {
Application ApplicationSettings `yaml:"application"`
Database DatabaseSettings `yaml:"database"`
SuperUser SuperUserSettings `yaml:"superuser"`
Auth AuthSettings `yaml:"authsecret"`
Auth AuthSettings `yaml:"authsecret"`
}

type ProductionSettings struct {
Expand Down Expand Up @@ -65,10 +65,10 @@ type SuperUserSettings struct {
}

type AuthSettings struct {
AccessToken string `yaml:"accesstoken"`
RefreshToken string `yaml:"refreshtoken"`
AcessTokenExpiry uint `yaml:"accesstokenexpiry"`
RefreshTokenExpiry uint `yaml:"refreshtokenexpiry"`
AccessToken string `yaml:"accesstoken"`
RefreshToken string `yaml:"refreshtoken"`
AcessTokenExpiry uint `yaml:"accesstokenexpiry"`
RefreshTokenExpiry uint `yaml:"refreshtokenexpiry"`
}

type Environment string
Expand Down Expand Up @@ -160,9 +160,9 @@ func GetConfiguration(path string) (Settings, error) {
Password: os.Getenv(fmt.Sprintf("%sPASSWORD", superUserPrefix)),
},
Auth: AuthSettings{
AccessToken: os.Getenv(fmt.Sprintf("%sACCESS_TOKEN", authSecretPrefix)),
RefreshToken: os.Getenv(fmt.Sprintf("%sREFRESH_TOKEN", authSecretPrefix)),
AcessTokenExpiry: uint(authAccessExpiryInt),
AccessToken: os.Getenv(fmt.Sprintf("%sACCESS_TOKEN", authSecretPrefix)),
RefreshToken: os.Getenv(fmt.Sprintf("%sREFRESH_TOKEN", authSecretPrefix)),
AcessTokenExpiry: uint(authAccessExpiryInt),
RefreshTokenExpiry: uint(authRefreshExpiryInt),
},
}, nil
Expand Down
8 changes: 4 additions & 4 deletions backend/src/controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (a *AuthController) Me(c *fiber.Ctx) error {
if err != nil {
return err.FiberError(c)
}

user, err := a.authService.Me(claims.Issuer)
if err != nil {
return err.FiberError(c)
Expand Down Expand Up @@ -91,7 +91,7 @@ func (a *AuthController) Login(c *fiber.Ctx) error {
// @ID refresh-user
// @Tags user
// @Accept json
// @Produce json
// @Produce json
// @Success 200 {object} string "success"
// @Failure 401 {string} string "failed to refresh access token"
// @Router /api/v1/auth/refresh [get]
Expand Down Expand Up @@ -140,10 +140,10 @@ func (a *AuthController) Logout(c *fiber.Ctx) error {
// TODO: Redis
a.blacklist = append(a.blacklist, accessTokenValue)
a.blacklist = append(a.blacklist, refreshTokenValue)

// Expire and clear the cookies
c.Cookie(auth.ExpireCookie("access_token"))
c.Cookie(auth.ExpireCookie("refresh_token"))

return utilities.FiberMessage(c, fiber.StatusOK, "success")
}
}
1 change: 0 additions & 1 deletion backend/src/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func (u *UserController) DeleteUser(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusNoContent)
}


func (u *UserController) GetUserTags(c *fiber.Ctx) error {
tags, err := u.userService.GetUserTags(c.Params("uid"))
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions backend/src/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func createSuperUser(settings config.Settings, db *gorm.DB) error {
if err := tx.Create(&superUser).Error; err != nil {
tx.Rollback()
return err
}
}

superClub := models.Club{
Name: "SAC",
Expand All @@ -126,9 +126,9 @@ func createSuperUser(settings config.Settings, db *gorm.DB) error {
}

membership := models.Membership{
ClubID: superClub.ID,
UserID: superUser.ID,
MembershipType: models.MembershipTypeAdmin,
ClubID: superClub.ID,
UserID: superUser.ID,
MembershipType: models.MembershipTypeAdmin,
}

if err := tx.Create(&membership).Error; err != nil {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ func (m *MiddlewareService) Authorize(requiredPermissions ...types.Permission) f

return c.Next()
}
}
}
2 changes: 1 addition & 1 deletion backend/src/middleware/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ func (m *MiddlewareService) ClubAuthorizeById(c *fiber.Ctx) error {
}

return errors.Unauthorized.FiberError(c)
}
}
4 changes: 2 additions & 2 deletions backend/src/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type MiddlewareInterface interface {
ClubAuthorizeById(c *fiber.Ctx) error
ClubAuthorizeById(c *fiber.Ctx) error
UserAuthorizeById(c *fiber.Ctx) error
Authenticate(c *fiber.Ctx) error
Authorize(requiredPermissions ...types.Permission) func(c *fiber.Ctx) error
Expand All @@ -24,4 +24,4 @@ func NewMiddlewareService(db *gorm.DB, validate *validator.Validate) *Middleware
DB: db,
Validate: validate,
}
}
}
7 changes: 3 additions & 4 deletions backend/src/middleware/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@ func (m *MiddlewareService) UserAuthorizeById(c *fiber.Ctx) error {
return err
}


claims, ok := token.Claims.(*types.CustomClaims)
if !ok || !token.Valid {
return errors.FailedToValidateAccessToken.FiberError(c)
}

issuerIDAsUUID, err := utilities.ValidateID(claims.Issuer)
if err != nil {
return errors.FailedToParseUUID.FiberError(c)
}

if issuerIDAsUUID.String() == idAsUUID.String() {
return c.Next()
}

return errors.Unauthorized.FiberError(c)
}
}
2 changes: 1 addition & 1 deletion backend/src/models/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ func (c *Club) AfterCreate(tx *gorm.DB) (err error) {
func (c *Club) AfterDelete(tx *gorm.DB) (err error) {
tx.Model(&c).Update("num_members", c.NumMembers-1)
return
}
}
10 changes: 5 additions & 5 deletions backend/src/models/membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func (Membership) TableName() string {
type Membership struct {
Model

UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id" validate:"required,uuid4"`
ClubID uuid.UUID `gorm:"type:uuid;not null" json:"club_id" validate:"required,uuid4"`
UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id" validate:"required,uuid4"`
ClubID uuid.UUID `gorm:"type:uuid;not null" json:"club_id" validate:"required,uuid4"`

Club *Club `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
User *User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`

MembershipType MembershipType `gorm:"type:varchar(255);not null;default:member" json:"membership_type" validate:"required,oneof=member admin"`
}
}
4 changes: 2 additions & 2 deletions backend/src/services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (a *AuthService) Me(id string) (*models.User, *errors.Error) {
if idErr != nil {
return nil, idErr
}

user, err := transactions.GetUser(a.DB, *idAsUint)
if err != nil {
return nil, &errors.UserNotFound
Expand Down Expand Up @@ -78,4 +78,4 @@ func (a *AuthService) GetRole(id string) (*models.UserRole, *errors.Error) {
role := models.UserRole(user.Role)

return &role, nil
}
}
70 changes: 35 additions & 35 deletions backend/src/types/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,57 @@ type Permission string

const (
UserReadAll Permission = "user:readAll"
UserRead Permission = "user:read"
UserWrite Permission = "user:write"
UserDelete Permission = "user:delete"
UserRead Permission = "user:read"
UserWrite Permission = "user:write"
UserDelete Permission = "user:delete"

TagReadAll Permission = "tag:readAll"
TagRead Permission = "tag:read"
TagWrite Permission = "tag:write"
TagCreate Permission = "tag:create"
TagDelete Permission = "tag:delete"
TagRead Permission = "tag:read"
TagWrite Permission = "tag:write"
TagCreate Permission = "tag:create"
TagDelete Permission = "tag:delete"

ClubReadAll Permission = "club:readAll"
ClubRead Permission = "club:read"
ClubWrite Permission = "club:write"
ClubCreate Permission = "club:create"
ClubDelete Permission = "club:delete"
ClubRead Permission = "club:read"
ClubWrite Permission = "club:write"
ClubCreate Permission = "club:create"
ClubDelete Permission = "club:delete"

PointOfContactReadAll Permission = "pointOfContact:readAll"
PointOfContactRead Permission = "pointOfContact:read"
PointOfContactCreate Permission = "pointOfContact:create"
PointOfContactWrite Permission = "pointOfContact:write"
PointOfContactDelete Permission = "pointOfContact:delete"
PointOfContactRead Permission = "pointOfContact:read"
PointOfContactCreate Permission = "pointOfContact:create"
PointOfContactWrite Permission = "pointOfContact:write"
PointOfContactDelete Permission = "pointOfContact:delete"

CommentReadAll Permission = "comment:readAll"
CommentRead Permission = "comment:read"
CommentCreate Permission = "comment:create"
CommentWrite Permission = "comment:write"
CommentDelete Permission = "comment:delete"
CommentRead Permission = "comment:read"
CommentCreate Permission = "comment:create"
CommentWrite Permission = "comment:write"
CommentDelete Permission = "comment:delete"

EventReadAll Permission = "event:readAll"
EventRead Permission = "event:read"
EventWrite Permission = "event:write"
EventCreate Permission = "event:create"
EventDelete Permission = "event:delete"
EventRead Permission = "event:read"
EventWrite Permission = "event:write"
EventCreate Permission = "event:create"
EventDelete Permission = "event:delete"

ContactReadAll Permission = "contact:readAll"
ContactRead Permission = "contact:read"
ContactWrite Permission = "contact:write"
ContactCreate Permission = "contact:create"
ContactDelete Permission = "contact:delete"
ContactRead Permission = "contact:read"
ContactWrite Permission = "contact:write"
ContactCreate Permission = "contact:create"
ContactDelete Permission = "contact:delete"

CategoryReadAll Permission = "category:readAll"
CategoryRead Permission = "category:read"
CategoryWrite Permission = "category:write"
CategoryCreate Permission = "category:create"
CategoryDelete Permission = "category:delete"
CategoryRead Permission = "category:read"
CategoryWrite Permission = "category:write"
CategoryCreate Permission = "category:create"
CategoryDelete Permission = "category:delete"

NotificationReadAll Permission = "notification:readAll"
NotificationRead Permission = "notification:read"
NotificationWrite Permission = "notification:write"
NotificationCreate Permission = "notification:create"
NotificationDelete Permission = "notification:delete"
NotificationRead Permission = "notification:read"
NotificationWrite Permission = "notification:write"
NotificationCreate Permission = "notification:create"
NotificationDelete Permission = "notification:delete"
)

var rolePermissions = map[models.UserRole][]Permission{
Expand Down
1 change: 0 additions & 1 deletion backend/src/utilities/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utilities

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


func FiberMessage(c *fiber.Ctx, statusCode int, response string) error {
return c.Status(statusCode).JSON(fiber.Map{"message": response})
}
20 changes: 10 additions & 10 deletions backend/tests/api/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
)

func TestCreateTokenPairSuccess(t *testing.T) {
id := "user123"
role := "admin"
id := "user123"
role := "admin"

accessToken, refreshToken, err := auth.CreateTokenPair(id, role)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
accessToken, refreshToken, err := auth.CreateTokenPair(id, role)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

if accessToken == nil || refreshToken == nil {
t.Errorf("Expected both tokens to be non-nil, got: %v, %v", accessToken, refreshToken)
}
if accessToken == nil || refreshToken == nil {
t.Errorf("Expected both tokens to be non-nil, got: %v, %v", accessToken, refreshToken)
}
}

func TestCreateTokenPairFailure(t *testing.T) {
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestCreateRefreshTokenFailure(t *testing.T) {
}
}

func TestSignTokenSuccess(t *testing.T) {
func TestSignTokenSuccess(t *testing.T) {
tokenString := &jwt.Token{
Header: map[string]interface{}{
"alg": "HS256",
Expand Down

0 comments on commit edcea7f

Please sign in to comment.