Skip to content

Conversation

@appleboy
Copy link
Owner

@appleboy appleboy commented Jul 17, 2025

  • Remove the MapClaims type alias and use jwt.MapClaims directly throughout the codebase
  • Update all function signatures and implementations to replace MapClaims with jwt.MapClaims
  • Refactor related tests to use jwt.MapClaims instead of MapClaims

Summary by CodeRabbit

  • Refactor
    • Unified the usage of JWT claims by replacing the custom type alias with the standard jwt.MapClaims type throughout the application and tests. No changes to logic or user-facing behavior.

- Remove the MapClaims type alias and use jwt.MapClaims directly throughout the codebase
- Update all function signatures and implementations to replace MapClaims with jwt.MapClaims
- Refactor related tests to use jwt.MapClaims instead of MapClaims

Signed-off-by: Bo-Yi Wu <[email protected]>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 17, 2025

Walkthrough

The changes remove a custom type alias for JWT claims and replace all references to it with the direct usage of jwt.MapClaims from the JWT library in both implementation and test files. Function signatures, struct fields, and type assertions are updated accordingly, with no modification to logic or control flow.

Changes

File(s) Change Summary
auth_jwt.go Replaced all uses of the custom MapClaims type alias with jwt.MapClaims in fields, functions, and return types.
auth_jwt_test.go Updated all references of MapClaims to jwt.MapClaims in function signatures, assertions, and test logic.

Poem

A hop and a skip, I tidy my claims,
No more aliases, just true JWT names.
With jwt.MapClaims, the code is now neat,
Simpler and clearer—oh, what a treat!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4fceb9a and 5cf4b0e.

📒 Files selected for processing (2)
  • auth_jwt.go (4 hunks)
  • auth_jwt_test.go (9 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
auth_jwt_test.go (1)
auth_jwt.go (1)
  • ExtractClaimsFromToken (825-836)
🔇 Additional comments (12)
auth_jwt_test.go (8)

238-240: LGTM: PayloadFunc signature updated correctly

The function signature and return statement have been properly updated to use jwt.MapClaims instead of the custom MapClaims alias, maintaining consistency with the refactoring objective.


702-728: LGTM: PayloadFunc implementation updated correctly

All references to MapClaims have been properly updated to use jwt.MapClaims directly, including:

  • Function signature
  • Type assertions
  • Return statements for both empty and populated claims

The changes maintain the same functionality while eliminating the custom type alias.


772-774: LGTM: TokenGenerator call updated correctly

The TokenGenerator method call has been properly updated to use jwt.MapClaims instead of the custom MapClaims alias.


823-825: LGTM: ConvertClaims function parameter updated correctly

The function parameter has been properly updated to use jwt.MapClaims instead of the custom MapClaims alias.


867-867: LGTM: Empty claims assertion updated correctly

The assertion has been properly updated to use jwt.MapClaims{} instead of the custom MapClaims{} alias.


1308-1314: LGTM: PayloadFunc in TestCheckTokenString updated correctly

The function signature and type assertion have been properly updated to use jwt.MapClaims instead of the custom MapClaims alias.


1321-1323: LGTM: TokenGenerator call updated correctly

The TokenGenerator method call has been properly updated to use jwt.MapClaims instead of the custom MapClaims alias.


1350-1350: LGTM: ExtractClaimsFromToken assertion updated correctly

The assertion has been properly updated to use jwt.MapClaims{} instead of the custom MapClaims{} alias.

auth_jwt.go (4)

63-63: LGTM: PayloadFunc field type updated correctly

The PayloadFunc field in the GinJWTMiddleware struct has been properly updated to use jwt.MapClaims instead of the custom MapClaims alias.


502-520: LGTM: GetClaimsFromJWT function updated correctly

The function signature and local variable declaration have been properly updated to use jwt.MapClaims instead of the custom MapClaims alias. The function logic remains unchanged while properly using the direct JWT library type.


815-822: LGTM: ExtractClaims function updated correctly

The function signature and return statements have been properly updated to use jwt.MapClaims instead of the custom MapClaims alias. The function maintains the same behavior while using the direct JWT library type.


825-836: LGTM: ExtractClaimsFromToken function updated correctly

The function signature, return statement, and local variable declaration have been properly updated to use jwt.MapClaims instead of the custom MapClaims alias. The function maintains the same functionality while using the direct JWT library type.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @appleboy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request undertakes a refactoring initiative to simplify the codebase by eliminating an unnecessary type alias. By directly utilizing jwt.MapClaims from the underlying JWT library, the changes enhance code clarity, reduce abstraction, and promote consistency with external dependencies across the main application logic and its associated test suite.

Highlights

  • Type Alias Removal: The custom MapClaims type alias, which was a direct alias for jwt.MapClaims, has been removed. The codebase now directly uses jwt.MapClaims from the github.com/dgrijalva/jwt-go library.
  • Function Signature Updates: All function signatures and struct fields that previously used the MapClaims alias, such as PayloadFunc, GetClaimsFromJWT, ExtractClaims, and ExtractClaimsFromToken, have been updated to directly reference jwt.MapClaims.
  • Codebase Consistency: Internal variable declarations, type assertions, and initializations (e.g., claims := MapClaims{} becoming claims := jwt.MapClaims{}) have been refactored to align with the direct usage of jwt.MapClaims.
  • Test Suite Alignment: Corresponding test cases in auth_jwt_test.go have been updated to reflect the changes, ensuring that the tests continue to function correctly with the direct jwt.MapClaims type.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@appleboy appleboy merged commit de350c8 into master Jul 17, 2025
7 checks passed
@appleboy appleboy deleted the map branch July 17, 2025 05:44
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a nice refactoring that removes the local MapClaims alias in favor of using jwt.MapClaims directly, which improves code clarity. The changes look correct.

I've added a few suggestions to improve efficiency and robustness in areas touched by this refactoring. Specifically:

  • Replacing manual claim-copying loops with direct, safe type assertions. This avoids unnecessary allocations and prevents potential panics.
  • Pointing out a potentially useless test helper function that could be removed to clean up the test suite.

Overall, a good change. Addressing the comments will make the code even more robust.

}

return claims.(MapClaims)
return claims.(jwt.MapClaims)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This type assertion can panic if the value stored in JWT_PAYLOAD is not of type jwt.MapClaims. This could happen due to a programming error elsewhere. It's safer to use the value, ok form of type assertion to handle this case gracefully.

I'd suggest replacing this line with a safe check:

if mapClaims, ok := claims.(jwt.MapClaims); ok {
    return mapClaims
}
return make(jwt.MapClaims)

Comment on lines +514 to 517
claims := jwt.MapClaims{}
for key, value := range token.Claims.(jwt.MapClaims) {
claims[key] = value
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code creates a new map and copies all claims from the token. This is inefficient as it allocates a new map and iterates over all claims. Additionally, the type assertion token.Claims.(jwt.MapClaims) will cause a panic if the assertion fails.

A better approach is to perform a safe type assertion and return the claims map directly. This improves both performance and robustness.

Consider refactoring this block (lines 514-519) to:

	claims, ok := token.Claims.(jwt.MapClaims)
	if !ok {
		return nil, errors.New("invalid token claims type")
	}

	return claims, nil

Comment on lines +830 to 833
claims := jwt.MapClaims{}
for key, value := range token.Claims.(jwt.MapClaims) {
claims[key] = value
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to GetClaimsFromJWT, this block of code is inefficiently copying claims and uses a panic-prone type assertion.

You can refactor this section to directly return the claims after a safe type assertion, which is more performant and robust. Here's a suggested implementation for lines 830 through 835:

if claims, ok := token.Claims.(jwt.MapClaims); ok {
    return claims
}
return make(jwt.MapClaims)

Comment on lines +823 to 825
func ConvertClaims(claims jwt.MapClaims) map[string]interface{} {
return map[string]interface{}{}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This test helper function ConvertClaims is currently a stub that always returns an empty map. Its only usage is in TestEmptyClaims on a value that is already asserted to be empty, making this call redundant. The function as it stands serves no purpose.

Please consider either implementing it fully if it's a placeholder, or removing the function and its call from TestEmptyClaims to improve code clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants