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 "";
}
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);
}
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);
}
}
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);
}
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());
});
}
Aggregations