use of com.auth0.jwt.Claim in project openware by open-inc.
the class UserService method jwtToUser.
public User jwtToUser(String token) {
if (jwtVerifier == null)
return null;
try {
DecodedJWT userJWT = jwtVerifier.verify(token);
Claim userid = userJWT.getClaim("uid");
if (!userid.isNull())
return getUserByUID(userid.asString());
Claim username = userJWT.getClaim("username");
if (!username.isNull())
return getUserByUsername(username.asString());
Claim usermail = userJWT.getClaim("usermail");
if (!usermail.isNull())
return getActiveUsers().stream().filter(new Predicate<User>() {
@Override
public boolean test(User t) {
return t.getEmail().toLowerCase().equals(usermail.asString().toLowerCase());
}
}).findFirst().get();
return null;
} catch (JWTVerificationException e) {
return null;
}
}
use of com.auth0.jwt.Claim in project foundation-java by soffalabs.
the class DefaultJwtProcessor method decode.
@Override
public Optional<Authentication> decode(String token, ClaimsExtractor claimsExtractor) {
try {
Algorithm algorithm = Algorithm.HMAC256(config.getSecret());
JWTVerifier verifier = JWT.require(algorithm).withIssuer(config.getIssuer()).build();
DecodedJWT jwt = verifier.verify(token);
Map<String, Claim> baseClaims = jwt.getClaims();
Map<String, Object> claims = new HashMap<>();
for (Map.Entry<String, Claim> entry : baseClaims.entrySet()) {
claims.put(entry.getKey(), entry.getValue().asString());
}
return Optional.of(claimsExtractor.extractInfo(new Jwt(token, jwt.getSubject(), claims)));
} catch (Exception e) {
LOG.error(e);
return Optional.empty();
}
}
use of com.auth0.jwt.Claim in project libresonic by Libresonic.
the class JWTAuthenticationProvider method authenticate.
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
JWTAuthenticationToken authentication = (JWTAuthenticationToken) auth;
if (authentication.getCredentials() == null || !(authentication.getCredentials() instanceof String)) {
logger.error("Credentials not present");
return null;
}
String rawToken = (String) auth.getCredentials();
DecodedJWT token = JWTSecurityService.verify(jwtKey, rawToken);
Claim path = token.getClaim(JWTSecurityService.CLAIM_PATH);
authentication.setAuthenticated(true);
// TODO:AD This is super unfortunate, but not sure there is a better way when using JSP
if (StringUtils.contains(authentication.getRequestedPath(), "/WEB-INF/jsp/")) {
logger.warn("BYPASSING AUTH FOR WEB-INF page");
} else if (!roughlyEqual(path.asString(), authentication.getRequestedPath())) {
throw new InsufficientAuthenticationException("Credentials not valid for path " + authentication.getRequestedPath() + ". They are valid for " + path.asString());
}
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("IS_AUTHENTICATED_FULLY"));
authorities.add(new SimpleGrantedAuthority("ROLE_TEMP"));
return new JWTAuthenticationToken(authorities, rawToken, authentication.getRequestedPath());
}
use of com.auth0.jwt.Claim in project nexus-public by sonatype.
the class JwtHelperTest method assertJwt.
private void assertJwt(final String jwt) {
DecodedJWT decode = decodeJwt(jwt);
Claim user = decode.getClaim(USER);
Claim userId = decode.getClaim(USER_SESSION_ID);
Claim issuer = decode.getClaim("iss");
Claim realm = decode.getClaim(REALM);
assertEquals("admin", user.asString());
assertNotNull(userId.asString());
assertEquals(ISSUER, issuer.asString());
assertEquals("NexusAuthorizingRealm", realm.asString());
}
use of com.auth0.jwt.Claim in project nexus-public by sonatype.
the class JwtHelperTest method testVerifyAndRefresh_success.
@Test
public void testVerifyAndRefresh_success() throws Exception {
String jwt = makeValidJwt();
DecodedJWT decodedJWT = decodeJwt(jwt);
Cookie refreshed = underTest.verifyAndRefreshJwtCookie(jwt);
assertCookie(refreshed);
DecodedJWT refreshedJwt = decodeJwt(refreshed.getValue());
Claim userSessionId = decodedJWT.getClaim(USER_SESSION_ID);
assertEquals(userSessionId.asString(), refreshedJwt.getClaim(USER_SESSION_ID).asString());
assertJwt(refreshed.getValue());
}
Aggregations