use of com.auth0.jwt.JWTVerifier 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.JWTVerifier 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());
});
}
use of com.auth0.jwt.JWTVerifier in project notes by menhuan.
the class JwtUtil method verify.
/**
* 校验token是否正确
*
* @param token 密钥
* @param secret 用户的密码
* @return 是否正确
*/
public static boolean verify(String token, Map<String, Object> userToken, String secret) {
Algorithm algorithm = Algorithm.HMAC256(secret);
JWTVerifier verifier = JWT.require(algorithm).withClaim("userId", userToken.get("userId").toString()).withClaim("userName", userToken.get("userName").toString()).withClaim("timestamp", Long.parseLong(userToken.get("timestamp").toString())).build();
DecodedJWT jwt = verifier.verify(token);
return true;
}
use of com.auth0.jwt.JWTVerifier in project springboot by LiJinHongPassion.
the class JWTUtil method verify.
/**
* 校验token是否正确
* @param token token
* @param secret 用户的密码
* @return 是否正确
*/
public static boolean verify(String token, String username, String secret) {
try {
// 指定加密算法
Algorithm algorithm = Algorithm.HMAC256(secret);
JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build();
DecodedJWT jwt = verifier.verify(token);
return true;
} catch (Exception exception) {
return false;
}
}
use of com.auth0.jwt.JWTVerifier in project Automated-Parking-Lot by ParkingLotDevOps.
the class RoleToUserForm method refreshToken.
@GetMapping("/token/refresh")
public void refreshToken(HttpServletRequest request, HttpServletResponse response) throws IOException {
String authorizationHeader = request.getHeader("Authorization");
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
try {
String refresh_token = authorizationHeader.substring("Bearer ".length());
// TODO : de mutat in fisier de configurare
Algorithm algorithm = Algorithm.HMAC256("secret".getBytes());
JWTVerifier verifier = JWT.require(algorithm).build();
DecodedJWT decodedJWT = verifier.verify(refresh_token);
String username = decodedJWT.getSubject();
AppUser user = appUserService.getUser(username);
String access_token = JWT.create().withSubject(user.getEmail()).withExpiresAt(new Date(System.currentTimeMillis() + 30 * 60 * 1000)).withIssuer(request.getRequestURL().toString()).withClaim("roles", user.getRoles().stream().map(Role::getName).collect(Collectors.toList())).sign(algorithm);
Map<String, String> tokens = new HashMap<>();
tokens.put("access_token", access_token);
tokens.put("refresh_token", refresh_token);
response.setContentType("application/json");
new ObjectMapper().writeValue(response.getOutputStream(), tokens);
} catch (Exception exception) {
response.setHeader("error", exception.getMessage());
response.setStatus(403);
Map<String, String> error = new HashMap<>();
error.put("error", exception.getMessage());
response.setContentType("application/json");
new ObjectMapper().writeValue(response.getOutputStream(), error);
}
}
}
Aggregations