Skip to content

feat: added get userdata from jwt helper#24

Merged
LucaSchmitz2003 merged 1 commit intomainfrom
team-10-ASE767-userdata
Oct 20, 2025
Merged

feat: added get userdata from jwt helper#24
LucaSchmitz2003 merged 1 commit intomainfrom
team-10-ASE767-userdata

Conversation

@Julius257
Copy link
Copy Markdown
Contributor

@Julius257 Julius257 commented Oct 16, 2025

UserInformationJWT - Helper Class

The backend provides a convenient helper class UserInformationJWT to extract user information from JWT tokens, similar to the frontend's useUser hook.

Example

@RestController
public class MyController {
  @GetMapping("/my-endpoint")
  public ResponseEntity<?> myEndpoint() {
    // Get user information
    String userId = UserInformationJWT.getUserId();
    String email = UserInformationJWT.getEmail();
    String fullName = UserInformationJWT.getFullName();
    
    // Check roles
    if (UserInformationJWT.hasRole("lecturer")) {
      // Lecturer logic
    }
    
    return ResponseEntity.ok("Hello " + fullName);
  }
}

Available Methods

  • getUserId() - Get user ID (subject)
  • getUsername() - Get username (preferred_username)
  • getEmail() - Get email address
  • getFirstName() - Get first name
  • getLastName() - Get last name
  • getRoles() - Get all roles (from groups, realm_access, resource_access)
  • hasRole(String role) - Check if user has specific role (case-insensitive)
  • getClaim(String name) - Get custom JWT claim
  • isAuthenticated() - Check if user is authenticated

@Julius257 Julius257 self-assigned this Oct 16, 2025
Copilot AI review requested due to automatic review settings October 16, 2025 13:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds a backend helper class to extract user information from JWTs and a comprehensive test suite validating its behavior.

  • Introduces UserInformationJWT with static accessors for common claims, roles aggregation, and authentication checks
  • Adds unit tests that construct a representative Jwt and verify claim getters, role aggregation, case-insensitive role checks, and unauthenticated scenarios

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/main/java/com/ase/userservice/security/UserInformationJWT.java New helper class implementing claim getters, role aggregation, and auth checks
src/test/java/com/ase/userservice/security/UserInformationJWTTest.java New tests covering claim access, roles, case-insensitive checks, and unauthenticated cases

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@LucaSchmitz2003 LucaSchmitz2003 merged commit b5b6174 into main Oct 20, 2025
3 of 5 checks passed
@LucaSchmitz2003 LucaSchmitz2003 deleted the team-10-ASE767-userdata branch October 20, 2025 09:01
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.

3 participants