All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fivefaces.structureclient.config.security.User Maven / Gradle / Ivy

There is a newer version: 1.0.62
Show newest version
package com.fivefaces.structureclient.config.security;

import com.nimbusds.jose.shaded.json.JSONArray;
import com.nimbusds.jwt.JWT;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.security.authentication.BadCredentialsException;

import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@AllArgsConstructor
public class User {

    private final String oid;
    private final String username;
    private final String firstName;
    private final String lastName;
    private final String email;
    private final List roles;
    private final UserType userType;

    public static User fromJwt(JWT parsedJwt, UserType denomination) {
        try {
            List roles = ((JSONArray) parsedJwt.getJWTClaimsSet().getClaim("roles")).stream()
                    .map(o -> (String) o).collect(Collectors.toList());
            return new User(
                    (String) parsedJwt.getJWTClaimsSet().getClaim("oid"),
                    (String) parsedJwt.getJWTClaimsSet().getClaim("preferred_username"),
                    (String) parsedJwt.getJWTClaimsSet().getClaim("given_name"),
                    (String) parsedJwt.getJWTClaimsSet().getClaim("family_name"),
                    (String) parsedJwt.getJWTClaimsSet().getClaim("email"),
                    roles,
                    denomination
            );
        } catch (ParseException e) {
            throw new BadCredentialsException("Could not parse JWT token", e);
        }
    }

    public static User unmanned(String token) {
        String oid = "UNMANNED-" + token;
        return new User(
                oid, oid, oid, oid, oid,
                List.of("UNMANNED"),
                UserType.UNMANNED
        );
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy