feat: Phase 1 Week 2 - Authentication Migration #168
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Date: 16 Jan 2025
Developer Name: @lakshayman
Description
This PR implements Phase 1, Week 2: Authentication Migration for the Feature Flag Backend. It enhances the authentication system to verify users against our internal database and provides user context throughout the request pipeline, moving away from external authentication dependency.
Key Features Implemented:
Enhanced JWT Middleware with User Verification
JWTMiddlewareWithUserVerification()that validates JWT tokens and verifies users exist in our databaseUserContextwith userId, role, and emailUser Context System
UserContextstruct to pass authenticated user information through the request pipelineDatabase Helper Function
GetUserById()function in database layer for user lookupsEndpoint Migration
createFeatureFlagendpoint to use new authentication middlewareuserIdrequirement from request body (now uses authenticated user)Backward Compatibility
JWTMiddleware()remains unchanged for gradual migrationSecurity Improvements:
Migration Strategy:
JWTMiddlewareWithUserVerification()for enhanced securityDocumentation Updated?
Under Feature Flag
Database Changes
Database Changes:
usertable created in Week 1Breaking Changes
Note: The
CreateFeatureFlagRequeststruct no longer requiresuserIdin the request body. The userId is automatically extracted from the authenticated user's token. This is a non-breaking change as the field is now optional and will be ignored if provided.Development Tested?
Testing:
Screenshots
Screenshot 1
Test Coverage
Test Coverage Details
Additional Notes
API Changes
Before:
POST /feature-flags/
{
"name": "new-feature",
"description": "Description",
"userId": "user-123" // Required, but could be spoofed
}After:
POST /feature-flags/
{
"name": "new-feature",
"description": "Description"
// userId automatically extracted from authenticated token
}### Usage Example
New Middleware:
jwtResponse, userContext, err := jwt.JWTMiddlewareWithUserVerification()(req)
if err != nil || jwtResponse.StatusCode != http.StatusOK {
return jwtResponse, err
}
// Use userContext.UserId, userContext.Role, userContext.EmailOld Middleware (still works):
jwtResponse, userId, err := jwt.JWTMiddleware()(req)
if err != nil || jwtResponse.StatusCode != http.StatusOK {
return jwtResponse, err
}### Next Steps (Week 3)
Security Considerations
Files Changed
layer/jwt/jwt.go(updated - added enhanced middleware)layer/database/dynamodb.go(updated - added GetUserById helper)layer/utils/UserContext.go(new)layer/utils/RequestResponse.go(updated - made userId optional)createFeatureFlag/main.go(updated - uses new middleware)Migration Path: