use of com.auth0.json.mgmt.Token in project goobi-workflow by intranda.
the class JwtHelper method createToken.
public static String createToken(Map<String, String> map, Date expiryDate) throws ConfigurationException {
String secret = ConfigurationHelper.getInstance().getJwtSecret();
if (secret == null) {
throw new ConfigurationException("Could not get JWT secret from configuration. Please configure the key 'jwtSecret' in the file goobi_config.properties");
}
if (map == null || map.isEmpty()) {
throw new ConfigurationException("Could not generate token from an empty map.");
}
Algorithm algorithm = createSigningAlgorithm(secret);
Builder tokenBuilder = JWT.create().withIssuer("Goobi");
for (String key : map.keySet()) {
tokenBuilder = tokenBuilder.withClaim(key, map.get(key));
}
return tokenBuilder.withExpiresAt(expiryDate).sign(algorithm);
}
use of com.auth0.json.mgmt.Token in project goobi-workflow by intranda.
the class JwtHelper method createSigningAlgorithm.
/**
* creates a rotated token. Rotation is done by appending a timestamp
*
* @param secret
* @return
*/
private static Algorithm createSigningAlgorithm(String secret) {
long currentTime = System.currentTimeMillis();
long rotationTime = (currentTime / rotationDuration) * rotationDuration;
Algorithm algorithm = Algorithm.HMAC256(secret + rotationTime);
return algorithm;
}
use of com.auth0.json.mgmt.Token in project goobi-workflow by intranda.
the class AuthorizationFilter method checkJwt.
/**
* Verifies the JSON web token and checks if the "api_path" and "api_methods" claims match the actual request
*
* @param jwt
* @param path the endpoint path the request tries to use
* @param method the HTTP method used in the request
* @return true, if the JWT authorizes the usage of the API path and method. Else: false
*/
public static boolean checkJwt(String jwt, String path, String method) {
if (StringUtils.isBlank(jwt)) {
return false;
}
try {
DecodedJWT decodedJWT = JwtHelper.verifyTokenAndReturnClaims(jwt);
Claim pathClaim = decodedJWT.getClaim("api_path");
if (pathClaim == null || pathClaim.isNull()) {
return false;
}
if (!Pattern.matches(pathClaim.asString(), path)) {
return false;
}
Claim methodsClaim = decodedJWT.getClaim("api_methods");
if (methodsClaim == null) {
return false;
}
boolean methodMatch = Arrays.stream(methodsClaim.asArray(String.class)).anyMatch(claimMethod -> method.equalsIgnoreCase(claimMethod));
if (!methodMatch) {
return false;
}
return true;
} catch (javax.naming.ConfigurationException | JWTVerificationException e) {
log.error(e);
return false;
}
}
use of com.auth0.json.mgmt.Token in project spring-learning by moon-zhou.
the class JWTUtils method getToken.
/**
* 生产token
*/
public static String getToken(Map<String, String> map) {
JWTCreator.Builder builder = JWT.create();
// payload
map.forEach((k, v) -> {
builder.withClaim(k, v);
});
Calendar instance = Calendar.getInstance();
// 默认7天过期
instance.add(Calendar.DATE, 7);
// 指定令牌的过期时间
builder.withExpiresAt(instance.getTime());
// 签名
String token = builder.sign(Algorithm.HMAC256(SECRET));
return token;
}
use of com.auth0.json.mgmt.Token in project spring-learning by moon-zhou.
the class JWTTest method testJWTVerify.
/**
* 验证JWT生成的token
*
* 为了方便测试,定义了类变量,整体用例可直接执行
*/
@Test
public void testJWTVerify() {
final JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(SIGN)).build();
final DecodedJWT decodedJWT = jwtVerifier.verify(token);
int decodeUserId = decodedJWT.getClaim(USER_ID).asInt();
String decodeUserName = decodedJWT.getClaim(USER_NAME).asString();
System.out.println("用户Id:" + decodeUserId);
System.out.println("用户名:" + decodeUserName);
System.out.println("过期时间:" + decodedJWT.getExpiresAt());
Assertions.assertEquals(userId, decodeUserId);
Assertions.assertEquals(userName, decodeUserName);
}
Aggregations