Skip to content

Commit 5096a07

Browse files
committed
fix role type issues, make sure to create debug user in debug mode
1 parent 36ec90a commit 5096a07

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/auth/auth.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package auth
22

33
import (
44
"OPP/auth/api"
5+
"OPP/auth/dao"
56
opp_jwt "OPP/auth/jwt"
67
"context"
78
"errors"
@@ -38,10 +39,24 @@ func AuthenticationWrapperFunc(ctx context.Context, input *openapi3filter.Authen
3839
// AuthenticationFunc can be used for endpoints that aren't marked as requiring authentication
3940
// but still need to check auth tokens when provided.
4041
// Returns (username, role, error) where error is nil if authentication succeeded
41-
func AuthenticationFunc(authHeader string) (string, api.UserRequestRole, error) {
42+
func AuthenticationFunc(authHeader string) (string, string, error) {
4243
// Debug mode: override username and role
4344
if DEBUG_MODE == "true" {
44-
return "admin_debug", api.UserRequestRoleAdmin, nil
45+
// make sure to create a debug user if it doesn't exist
46+
role := api.UserRequestRoleAdmin
47+
debug_user := api.UserRequest{
48+
Username: "admin_debug",
49+
Password: "admin_debug",
50+
Role: &role,
51+
Email: "admin.debug@debug.com",
52+
Name: "Admin",
53+
Surname: "Debug",
54+
}
55+
_, err := dao.NewUserDao().AddUser(context.Background(), debug_user)
56+
if err != nil && !errors.Is(err, dao.ErrUserAlreadyExists) {
57+
return "", "", errors.New("failed to create debug user: " + err.Error())
58+
}
59+
return "admin_debug", "admin", nil
4560
}
4661

4762
if authHeader == "" {
@@ -83,6 +98,5 @@ func AuthenticationFunc(authHeader string) (string, api.UserRequestRole, error)
8398
return "", "", errors.New("missing role in token claims")
8499
}
85100

86-
role := api.UserRequestRole(roleStr)
87-
return username, role, nil
101+
return username, roleStr, nil
88102
}

src/handlers/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (h *SessionHandlers) Register(c *gin.Context) {
8686
}
8787

8888
// Check if the user has admin privileges
89-
if role != api.UserRequestRoleAdmin {
89+
if role != "admin" {
9090
c.JSON(http.StatusForbidden, gin.H{"error": "Admin privileges required to register " + string(*newUser.Role) + " accounts"})
9191
return
9292
}

0 commit comments

Comments
 (0)