Add GetClaims method to Token struct - #16
Conversation
WalkthroughA new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
jwt/jwt_test.go (1)
42-71: Good test coverage, but consider adding edge case.The test effectively covers the two main scenarios:
- Nil parsed token returning empty map
- Valid MapClaims returning expected claims
However, consider adding a test case for when
parsed.Claimsis not of typegolangjwt.MapClaimsto achieve complete code coverage of the third return path inGetClaims().Add this test case to complete coverage:
t.Run("returns empty map when claims are not MapClaims", func(t *testing.T) { token := &Token{ processing: tokenProcessing{ parsed: &golangjwt.Token{ Claims: &golangjwt.RegisteredClaims{ Subject: "test_subject", }, }, }, } claims := token.GetClaims() assert.NotNil(t, claims) assert.Equal(t, 0, len(claims)) })
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
jwt/jwt.go(1 hunks)jwt/jwt_test.go(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
jwt/jwt_test.go (1)
jwt/jwt.go (1)
Token(21-26)
🔇 Additional comments (1)
jwt/jwt.go (1)
146-154: LGTM! Well-implemented safe claims accessor.The implementation correctly handles edge cases by:
- Checking for nil parsed token
- Using type assertion with ok pattern
- Returning empty maps as safe defaults
The method integrates well with existing Token methods and follows consistent naming conventions.
Introduce the GetClaims method to retrieve claims from the JWT token, along with tests to validate its behavior in different scenarios.