net.leanix.dropkit.oauth.token.OAuth2TokenParser Maven / Gradle / Ivy
package net.leanix.dropkit.oauth.token;
import net.leanix.dropkit.oauth.models.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.Key;
import java.util.Date;
import java.util.UUID;
public class OAuth2TokenParser {
private static final Logger log = LoggerFactory.getLogger(OAuth2TokenParser.class);
OAuth2TokenConfig config;
Key key;
public OAuth2TokenParser(OAuth2TokenConfig config) throws ConfigException {
this.config = config;
this.key = config.getSigningKey();
}
/**
* Parse a Jwt and return OAuth2Token
*
* @param accessToken
* @return
*/
public OAuth2Token parse(String accessToken, Class userClass) {
Claims claims = Jwts.parser().setSigningKey(this.key).parseClaimsJws(accessToken).getBody();
ObjectMapper mapper = new ObjectMapper();
U user = mapper.convertValue(claims.get("principal"), userClass);
OAuth2Token token = new OAuth2Token<>();
if (claims.getId() != null) {
token.setId(UUID.fromString(claims.getId()));
}
else {
log.error("no jti claim in JWT! Why? This is safe, and should disappear once all old JWT have expired");
}
token.setPrincipal(user);
user.setAccessToken(accessToken);
token.setExpiration(claims.getExpiration());
if (claims.get("refresh_token") != null) {
token.setRefreshToken(claims.get("refresh_token", String.class));
}
if (claims.get("refresh_expiration") != null) {
token.setRefreshExpiration(claims.get("refresh_exp", Date.class));
}
return token;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy