feat: Phase 1 Week 2 - Authentication Migration#168
Draft
lakshayman wants to merge 1 commit intophase1-week1-user-service-foundationfrom
Draft
feat: Phase 1 Week 2 - Authentication Migration#168lakshayman wants to merge 1 commit intophase1-week1-user-service-foundationfrom
lakshayman wants to merge 1 commit intophase1-week1-user-service-foundationfrom
Conversation
- Enhance JWT middleware with user verification from database - Add UserContext struct to pass authenticated user information - Create JWTMiddlewareWithUserVerification for internal auth - Add GetUserById database helper function - Update createFeatureFlag endpoint to use authenticated user context - Remove userId requirement from request body (use authenticated user) - Verify user exists and is active before allowing operations - Extract role from token and database for authorization - Maintain backward compatibility with existing JWTMiddleware
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: