Search in sources :

Example 1 with JsonWebTokenAuthentication

use of com.vsct.vboard.config.cognito.JsonWebTokenAuthentication in project vboard by voyages-sncf-technologies.

the class AuthenticationController method getUserEmailFromAuth.

private static String getUserEmailFromAuth(Authentication auth) {
    if (auth instanceof JsonWebTokenAuthentication) {
        return ((JsonWebTokenAuthentication) auth).getEmail();
    }
    final KeycloakPrincipal userDetails = (KeycloakPrincipal) auth.getPrincipal();
    final IDToken idToken = userDetails.getKeycloakSecurityContext().getToken();
    return idToken.getEmail();
}
Also used : JsonWebTokenAuthentication(com.vsct.vboard.config.cognito.JsonWebTokenAuthentication) IDToken(org.keycloak.representations.IDToken) KeycloakPrincipal(org.keycloak.KeycloakPrincipal)

Example 2 with JsonWebTokenAuthentication

use of com.vsct.vboard.config.cognito.JsonWebTokenAuthentication in project vboard by voyages-sncf-technologies.

the class AuthenticationController method createUserFromAuth.

@NotNull
@SuppressFBWarnings("CLI_CONSTANT_LIST_INDEX")
private static User createUserFromAuth(Authentication auth) {
    if (auth instanceof JsonWebTokenAuthentication) {
        JsonWebTokenAuthentication jwtAuth = ((JsonWebTokenAuthentication) auth);
        String username = jwtAuth.getName();
        String[] parts = StringUtils.split(username, "\\");
        if (parts != null) {
            username = parts[1];
        }
        parts = StringUtils.split(username, "_");
        if (parts == null) {
            throw new IllegalArgumentException("The username in the JWT token provided does not contain a '_'");
        }
        String firstName = StringUtils.capitalize(parts[0]);
        String lastName = StringUtils.capitalize(parts[1]);
        LOGGER.info("createUserFromAuth/JWT: email={} firstName={} lastName={}", jwtAuth.getEmail(), firstName, lastName);
        return new User(jwtAuth.getEmail(), firstName, lastName);
    }
    final KeycloakPrincipal userDetails = (KeycloakPrincipal) auth.getPrincipal();
    final IDToken idToken = userDetails.getKeycloakSecurityContext().getToken();
    LOGGER.info("createUserFromAuth/Keycloak: email={} firstName={} lastName={}", idToken.getEmail(), idToken.getGivenName(), idToken.getFamilyName());
    return new User(idToken.getEmail(), idToken.getGivenName(), idToken.getFamilyName());
}
Also used : User(com.vsct.vboard.models.User) JsonWebTokenAuthentication(com.vsct.vboard.config.cognito.JsonWebTokenAuthentication) IDToken(org.keycloak.representations.IDToken) KeycloakPrincipal(org.keycloak.KeycloakPrincipal) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) NotNull(javax.validation.constraints.NotNull)

Aggregations

JsonWebTokenAuthentication (com.vsct.vboard.config.cognito.JsonWebTokenAuthentication)2 KeycloakPrincipal (org.keycloak.KeycloakPrincipal)2 IDToken (org.keycloak.representations.IDToken)2 User (com.vsct.vboard.models.User)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 NotNull (javax.validation.constraints.NotNull)1