@@ -51,7 +51,7 @@ func (a *AuthMiddleware) HTTPContextFunc(next func(ctx context.Context, r *http.
51
51
if ! strings .HasPrefix (authHeader , "Bearer " ) {
52
52
a .logger .Warn ("Missing or invalid authorization header from %s" , r .RemoteAddr )
53
53
// Set authentication error in context instead of failing the request
54
- ctx = context .WithValue (ctx , "auth_error" , "missing_token" )
54
+ ctx = context .WithValue (ctx , authErrorKey , "missing_token" )
55
55
return next (ctx , r )
56
56
}
57
57
@@ -61,24 +61,24 @@ func (a *AuthMiddleware) HTTPContextFunc(next func(ctx context.Context, r *http.
61
61
claims , err := a .validateJWT (token )
62
62
if err != nil {
63
63
a .logger .Warn ("Invalid token from %s: %v" , r .RemoteAddr , err )
64
- ctx = context .WithValue (ctx , "auth_error" , "invalid_token" )
64
+ ctx = context .WithValue (ctx , authErrorKey , "invalid_token" )
65
65
return next (ctx , r )
66
66
}
67
67
68
68
// Check if token is expired
69
69
if time .Now ().Unix () > claims .ExpiresAt {
70
70
a .logger .Warn ("Expired token from %s" , r .RemoteAddr )
71
- ctx = context .WithValue (ctx , "auth_error" , "expired_token" )
71
+ ctx = context .WithValue (ctx , authErrorKey , "expired_token" )
72
72
return next (ctx , r )
73
73
}
74
74
75
75
a .logger .Info ("Authenticated user %s (%s) from %s" , claims .Username , claims .Role , r .RemoteAddr )
76
76
77
77
// Add user to request context
78
- ctx = context .WithValue (ctx , "authenticated" , true )
79
- ctx = context .WithValue (ctx , "user_id" , claims .UserID )
80
- ctx = context .WithValue (ctx , "username" , claims .Username )
81
- ctx = context .WithValue (ctx , "user_role" , claims .Role )
78
+ ctx = context .WithValue (ctx , authenticatedKey , true )
79
+ ctx = context .WithValue (ctx , userIDKey , claims .UserID )
80
+ ctx = context .WithValue (ctx , usernameKey , claims .Username )
81
+ ctx = context .WithValue (ctx , userRoleKey , claims .Role )
82
82
83
83
return next (ctx , r )
84
84
}
@@ -185,25 +185,25 @@ func (a *AuthMiddleware) GenerateToken(userID, username, role string, expiration
185
185
186
186
// isAuthenticated checks if the request context contains valid authentication
187
187
func isAuthenticated (ctx context.Context ) bool {
188
- if auth , ok := ctx .Value ("authenticated" ).(bool ); ok && auth {
188
+ if auth , ok := ctx .Value (authenticatedKey ).(bool ); ok && auth {
189
189
return true
190
190
}
191
191
return false
192
192
}
193
193
194
194
// getAuthError returns any authentication error from the context
195
195
func getAuthError (ctx context.Context ) string {
196
- if err , ok := ctx .Value ("auth_error" ).(string ); ok {
196
+ if err , ok := ctx .Value (authErrorKey ).(string ); ok {
197
197
return err
198
198
}
199
199
return ""
200
200
}
201
201
202
202
// getUserInfo extracts user information from the authenticated context
203
203
func getUserInfo (ctx context.Context ) (userID , username , role string ) {
204
- if userID , ok := ctx .Value ("user_id" ).(string ); ok {
205
- if username , ok := ctx .Value ("username" ).(string ); ok {
206
- if role , ok := ctx .Value ("user_role" ).(string ); ok {
204
+ if userID , ok := ctx .Value (userIDKey ).(string ); ok {
205
+ if username , ok := ctx .Value (usernameKey ).(string ); ok {
206
+ if role , ok := ctx .Value (userRoleKey ).(string ); ok {
207
207
return userID , username , role
208
208
}
209
209
}
0 commit comments