Search in sources :

Example 71 with Algorithm

use of com.auth0.jwt.Algorithm in project spring-cloud-alibaba by longlylong.

the class TokenUtil method makeToken.

private static String makeToken(Map<String, Object> map) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        String payloadJson = mapper.writeValueAsString(map);
        JWTCreator.Builder builder = JWT.create().withClaim("payload", payloadJson);
        builder.withExpiresAt(new Date(new Date().getTime() + TokenConstant.Token_Expires));
        return builder.sign(algorithm);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return "";
}
Also used : JWTCreator(com.auth0.jwt.JWTCreator) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Date(java.util.Date)

Example 72 with Algorithm

use of com.auth0.jwt.Algorithm in project alibaba-rsocket-broker by alibaba.

the class AuthenticationServiceJwtImpl method generateCredentials.

public String generateCredentials(String id, String[] organizations, String[] serviceAccounts, String[] roles, String[] authorities, String sub, String[] audience) throws Exception {
    Algorithm algorithmRSA256Private = Algorithm.RSA256(null, readPrivateKey());
    Arrays.sort(audience);
    Arrays.sort(organizations);
    JWTCreator.Builder builder = JWT.create().withIssuer(iss).withSubject(sub).withAudience(audience).withIssuedAt(new Date()).withClaim("id", id).withArrayClaim("sas", serviceAccounts).withArrayClaim("orgs", organizations);
    if (roles != null && roles.length > 0) {
        Arrays.sort(roles);
        builder = builder.withArrayClaim("roles", roles);
    }
    if (authorities != null && authorities.length > 0) {
        builder = builder.withArrayClaim("authorities", authorities);
    }
    return builder.sign(algorithmRSA256Private);
}
Also used : JWTCreator(com.auth0.jwt.JWTCreator) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date)

Example 73 with Algorithm

use of com.auth0.jwt.Algorithm in project toy by gmoon92.

the class JwtUtil method generate.

public String generate(User user) {
    try {
        ZonedDateTime today = ZonedDateTime.now();
        String token = JWT.create().withIssuer(apiVersion).withClaim("username", user.getUsername()).withClaim("role", user.getRole().name()).withIssuedAt(Date.from(today.toInstant())).withExpiresAt(Date.from(today.plusDays(DAY_OF_EXPIRATION).toInstant())).sign(algorithm);
        return String.format("%s %s", AuthenticationSchema.BEARER.getName(), token);
    } catch (JWTCreationException e) {
        throw new JWTCreationException("Invalid Signing configuration or Couldn't convert Claims.", e);
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) JWTCreationException(com.auth0.jwt.exceptions.JWTCreationException)

Example 74 with Algorithm

use of com.auth0.jwt.Algorithm in project jpsonic by tesshucom.

the class JWTSecurityService method verify.

public static DecodedJWT verify(String jwtKey, String token) {
    Algorithm algorithm = JWTSecurityService.getAlgorithm(jwtKey);
    JWTVerifier verifier = JWT.require(algorithm).build();
    if (token.split("\\.").length == WITH_FILE_EXTENSION) {
        return verifier.verify(FilenameUtils.removeExtension(token));
    }
    return verifier.verify(token);
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier)

Example 75 with Algorithm

use of com.auth0.jwt.Algorithm in project jpsonic by tesshucom.

the class JWTSecurityServiceTest method testAddJWTToken.

// false positive
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
@Test
void testAddJWTToken() {
    // Originally Parameterized was used. If possible, it is better to rewrite to the new
    // spring-method.
    Arrays.asList(new Object[][] { { "http://localhost:8080/jpsonic/stream?id=4", "/jpsonic/stream?id=4" }, { "/jpsonic/stream?id=4", "/jpsonic/stream?id=4" } }).forEach(o -> {
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(o[0].toString());
        String actualUri = jwtSecurityService.addJWTToken(builder).build().toUriString();
        String jwtToken = UriComponentsBuilder.fromUriString(actualUri).build().getQueryParams().getFirst(JWTSecurityService.JWT_PARAM_NAME);
        Algorithm algorithm = JWTSecurityService.getAlgorithm(settingsService.getJWTKey());
        JWTVerifier verifier = JWT.require(algorithm).build();
        DecodedJWT verify = verifier.verify(jwtToken);
        Claim claim = verify.getClaim(JWTSecurityService.CLAIM_PATH);
        assertEquals(o[1], claim.asString());
    });
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Claim(com.auth0.jwt.interfaces.Claim) Test(org.junit.jupiter.api.Test)

Aggregations

Algorithm (com.auth0.jwt.algorithms.Algorithm)206 Test (org.junit.Test)160 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)90 JWTVerifier (com.auth0.jwt.JWTVerifier)79 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)79 ECDSAAlgorithmTest (com.auth0.jwt.algorithms.ECDSAAlgorithmTest)61 Date (java.util.Date)57 ECDSAKeyProvider (com.auth0.jwt.interfaces.ECDSAKeyProvider)51 RSAPublicKey (java.security.interfaces.RSAPublicKey)36 ECPublicKey (java.security.interfaces.ECPublicKey)34 RSAKeyProvider (com.auth0.jwt.interfaces.RSAKeyProvider)31 IOException (java.io.IOException)30 JWTCreator (com.auth0.jwt.JWTCreator)28 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)25 ECPrivateKey (java.security.interfaces.ECPrivateKey)23 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)21 HashMap (java.util.HashMap)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 JsonObject (com.google.gson.JsonObject)15