Skip to content

Instantly share code, notes, and snippets.

@vdelacou
Last active June 8, 2018 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdelacou/a3a8c8286e9bdf553ad0cb886aafe389 to your computer and use it in GitHub Desktop.
Save vdelacou/a3a8c8286e9bdf553ad0cb886aafe389 to your computer and use it in GitHub Desktop.
Change TokenProvider in JHipster app to add Auth0
package com.seelix.api.security.jwt;
import java.security.interfaces.RSAPublicKey;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import com.auth0.jwk.InvalidPublicKeyException;
import com.auth0.jwk.Jwk;
import com.auth0.jwk.JwkException;
import com.auth0.jwk.JwkProvider;
import com.auth0.jwk.JwkProviderBuilder;
import com.auth0.jwk.SigningKeyNotFoundException;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.seelix.api.config.ApplicationProperties;
@Component
public class TokenProvider {
private final Logger log = LoggerFactory.getLogger(TokenProvider.class);
private static final String AUTHORITIES_KEY = "scope";
private String issuer;
private String audience;
private final ApplicationProperties applicationProperties;
private JwkProvider provider;
public TokenProvider(ApplicationProperties applicationProperties) {
this.applicationProperties = applicationProperties;
}
@PostConstruct
public void init() {
this.issuer = applicationProperties.getSecurity().getAuthentication().getAuth0().getIssuer();
this.audience = applicationProperties.getSecurity().getAuthentication().getAuth0().getAudience();
this.provider = new JwkProviderBuilder(issuer).build();
}
public Optional<Authentication> getAuthentication(String token) {
try {
DecodedJWT jwt = JWT.decode(token);
if (jwt.getKeyId() == null) {
return Optional.empty();
}
final Jwk jwk = provider.get(jwt.getKeyId());
@SuppressWarnings("deprecation")
JWTVerifier verifier = JWT.require(Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey())).withIssuer(issuer)
.withAudience(audience).build();
DecodedJWT claims = verifier.verify(token);
Collection<? extends GrantedAuthority> authorities = Arrays
.stream(claims.getClaim(AUTHORITIES_KEY).asString().split(" ")).map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
User principal = new User(claims.getSubject(), "", authorities);
return Optional.of(new UsernamePasswordAuthenticationToken(principal, token, authorities));
} catch (InvalidPublicKeyException e) {
log.info("Invalid Public key.");
log.trace("Invalid Public key trace: {}", e);
} catch (SigningKeyNotFoundException e) {
log.info("Cannot get signing Key.");
log.trace("Cannot get signing Key trace: {}", e);
} catch (JwkException e) {
log.info("Error during remote certificate.");
log.trace("Error during remote certificate: {}", e);
} catch (JWTDecodeException e) {
log.info("Invalid JWT token.");
log.trace("Invalid JWT token trace: {}", e);
} catch (TokenExpiredException e) {
log.info("Expired JWT token.");
log.trace("Expired JWT token trace: {}", e);
} catch (JWTVerificationException e) {
log.info("Not Verify JWT token.");
log.trace("Not Verify JWT token trace: {}", e);
} catch (IllegalArgumentException e) {
log.info("JWT token compact of handler are invalid.");
log.trace("JWT token compact of handler are invalid trace: {}", e);
}
return Optional.empty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment