fixed roleID
This commit is contained in:
+3
-3
@@ -19,7 +19,7 @@ const (
|
||||
claimsKey models.ContextKey = "claims"
|
||||
userIDKey models.ContextKey = "user_id"
|
||||
usernameKey models.ContextKey = "username"
|
||||
roleKey models.ContextKey = "role"
|
||||
roleIDKey models.ContextKey = "role_id"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -178,7 +178,7 @@ func buildContext(parent context.Context, claims *models.Claims) context.Context
|
||||
ctx := context.WithValue(parent, claimsKey, claims)
|
||||
ctx = context.WithValue(ctx, userIDKey, claims.UserID)
|
||||
ctx = context.WithValue(ctx, usernameKey, claims.Username)
|
||||
ctx = context.WithValue(ctx, roleKey, claims.Role)
|
||||
ctx = context.WithValue(ctx, roleIDKey, claims.RoleID)
|
||||
return ctx
|
||||
}
|
||||
|
||||
@@ -202,6 +202,6 @@ func GetUsername(r *http.Request) (string, bool) {
|
||||
|
||||
// GetRole retrieves the role from the request context
|
||||
func GetRole(r *http.Request) (string, bool) {
|
||||
role, ok := r.Context().Value(roleKey).(string)
|
||||
role, ok := r.Context().Value(roleIDKey).(string)
|
||||
return role, ok
|
||||
}
|
||||
|
||||
+15
-15
@@ -132,7 +132,7 @@ func TestParseAndValidateToken(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
Role: "admin",
|
||||
RoleID: "admin",
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
||||
},
|
||||
@@ -164,7 +164,7 @@ func TestParseAndValidateToken(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
Role: "admin",
|
||||
RoleID: "admin",
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(-1 * time.Hour)),
|
||||
},
|
||||
@@ -184,7 +184,7 @@ func TestBuildContext(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
Role: "admin",
|
||||
RoleID: "admin",
|
||||
}
|
||||
|
||||
parent := context.Background()
|
||||
@@ -206,7 +206,7 @@ func TestBuildContext(t *testing.T) {
|
||||
}
|
||||
|
||||
// Check role
|
||||
if val, ok := ctx.Value(roleKey).(string); !ok || val != "admin" {
|
||||
if val, ok := ctx.Value(roleIDKey).(string); !ok || val != "admin" {
|
||||
t.Error("Role not properly set in context")
|
||||
}
|
||||
}
|
||||
@@ -215,7 +215,7 @@ func TestGetClaims(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
Role: "admin",
|
||||
RoleID: "admin",
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
@@ -261,7 +261,7 @@ func TestGetUsername(t *testing.T) {
|
||||
|
||||
func TestGetRole(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
ctx := context.WithValue(req.Context(), roleKey, "admin")
|
||||
ctx := context.WithValue(req.Context(), roleIDKey, "admin")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
role, ok := GetRole(req)
|
||||
@@ -318,7 +318,7 @@ func TestJWTAuthValidToken(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
Role: "admin",
|
||||
RoleID: "admin",
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
||||
},
|
||||
@@ -413,7 +413,7 @@ func TestBuildContextWithDifferentRoles(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
Role: role,
|
||||
RoleID: role,
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
@@ -424,8 +424,8 @@ func TestBuildContextWithDifferentRoles(t *testing.T) {
|
||||
if !ok {
|
||||
t.Error("Claims not found in context")
|
||||
}
|
||||
if retrievedClaims.Role != role {
|
||||
t.Errorf("Role = %q, want %q", retrievedClaims.Role, role)
|
||||
if retrievedClaims.RoleID != role {
|
||||
t.Errorf("Role = %q, want %q", retrievedClaims.RoleID, role)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -528,7 +528,7 @@ func TestJWTAuthExpiredToken(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
Role: "admin",
|
||||
RoleID: "admin",
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(-1 * time.Hour)),
|
||||
},
|
||||
@@ -568,7 +568,7 @@ func TestJWTAuthTokenWithMissingClaims(t *testing.T) {
|
||||
"Missing UserID",
|
||||
&models.Claims{
|
||||
Username: "testuser",
|
||||
Role: "admin",
|
||||
RoleID: "admin",
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
||||
},
|
||||
@@ -578,7 +578,7 @@ func TestJWTAuthTokenWithMissingClaims(t *testing.T) {
|
||||
"Missing Username",
|
||||
&models.Claims{
|
||||
UserID: "user123",
|
||||
Role: "admin",
|
||||
RoleID: "admin",
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
||||
},
|
||||
@@ -636,7 +636,7 @@ func TestJWTAuthConcurrentRequests(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
Role: "admin",
|
||||
RoleID: "admin",
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
||||
},
|
||||
@@ -687,7 +687,7 @@ func TestJWTAuthTokenSignedWithWrongKey(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
Role: "admin",
|
||||
RoleID: "admin",
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user