use of com.auth0.jwt.JWTCreator.Builder in project actframework by actframework.
the class JWTTest method fromAuth0.
private String fromAuth0() throws Exception {
JWTCreator.Builder builder = com.auth0.jwt.JWT.create();
builder.withIssuer(ISSUER);
builder.withExpiresAt(new Date(EXPIRE_AT * 1000l));
builder.withJWTId(TOKEN_ID);
builder.withClaim(KEY_USERNAME, USERNAME);
Algorithm algorithm = Algorithm.HMAC256(SECRET);
return builder.sign(algorithm);
}
use of com.auth0.jwt.JWTCreator.Builder in project yyl_example by Relucent.
the class JwtDemo method main.
public static void main(String[] args) throws Exception {
long currentMillis = System.currentTimeMillis();
// JWT 生存时间(5秒)
long ttl = 5000;
// 生成JWT的时间
Date iat = new Date(currentMillis);
// 生成JWT失效时间
Date exp = new Date(currentMillis + ttl);
// 签名秘钥
String secret = "key";
// 签发人
String issuer = "root";
// 算法
Algorithm algorithm = Algorithm.HMAC256(secret);
// 本地的密码解码
JWTCreator.Builder builder = JWT.create();
// 签发时间
builder.withIssuedAt(iat);
// 签发人
builder.withIssuer(issuer);
// 过期时间
builder.withExpiresAt(exp);
// 主题
builder.withClaim("subject", "MySubject");
String token = builder.sign(algorithm);
System.out.println(token);
// 解密
JWTVerifier verifier = JWT.require(algorithm).withIssuer(issuer).build();
DecodedJWT jwt = verifier.verify(token);
Map<String, Claim> claims = jwt.getClaims();
NullClaim nullClaim = new NullClaim();
System.out.println(claims.getOrDefault("subject", nullClaim).asString());
// 等待5秒
System.out.println("Wait 5 seconds!");
Thread.sleep(5000);
try {
// 这时候Token已经超时了,会抛出异常
verifier.verify(token);
} catch (JWTVerificationException e) {
System.err.println(e);
}
}
use of com.auth0.jwt.JWTCreator.Builder in project ddf by codice.
the class TestOidc method createAccessToken.
/**
* Creates an access token
*
* @param valid - whether the access token should have a valid signature or not
* @return an access token to respond to DDF
*/
private String createAccessToken(boolean valid) {
long exp = Instant.now().plus(Duration.ofDays(3)).toEpochMilli();
String[] audience = { "master-realm", "account" };
String[] allowed = { SECURE_ROOT + HTTPS_PORT.getPort() };
String[] roles = { "create-realm", "offline_access", "admin", "uma_authorization" };
JSONObject realmAccess = new JSONObject();
realmAccess.put("roles", ImmutableList.of("create-realm", "offline_access", "admin", "uma_authorization"));
JSONObject resourceAccess = createMasterRealmJsonObject();
JWTCreator.Builder builder = JWT.create().withJWTId(UUID.randomUUID().toString()).withExpiresAt(new Date(exp)).withNotBefore(new Date(0)).withIssuedAt(new Date()).withIssuer(URL_START.toString() + "/auth/realms/master").withArrayClaim("aud", audience).withSubject(SUBJECT).withClaim("typ", "Bearer").withClaim(AZP, DDF_CLIENT_ID).withClaim("auth_time", new Date()).withArrayClaim("allowed-origins", allowed).withClaim("realm_access", realmAccess.toString()).withClaim("resource_access", resourceAccess.toString()).withClaim(SCOPE, "openid profile email").withArrayClaim(ROLES, roles).withClaim(EMAIL_VERIFIED, false).withClaim(PREFERRED_USERNAME, ADMIN);
return valid ? builder.sign(validAlgorithm) : builder.sign(invalidAlgorithm);
}
use of com.auth0.jwt.JWTCreator.Builder in project sonarqube by SonarSource.
the class GithubAppSecurityImpl method createAppToken.
@Override
public AppToken createAppToken(long appId, String privateKey) {
Algorithm algorithm = readApplicationPrivateKey(appId, privateKey);
LocalDateTime now = LocalDateTime.now(clock);
// Expiration period is configurable and could be greater if needed.
// See https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#authenticating-as-a-github-app
LocalDateTime expiresAt = now.plus(AppToken.EXPIRATION_PERIOD_IN_MINUTES, ChronoUnit.MINUTES);
ZoneOffset offset = clock.getZone().getRules().getOffset(now);
Date nowDate = Date.from(now.toInstant(offset));
Date expiresAtDate = Date.from(expiresAt.toInstant(offset));
JWTCreator.Builder builder = JWT.create().withIssuer(String.valueOf(appId)).withIssuedAt(nowDate).withExpiresAt(expiresAtDate);
return new AppToken(builder.sign(algorithm));
}
use of com.auth0.jwt.JWTCreator.Builder in project snow-owl by b2ihealthcare.
the class DefaultJWTGenerator method generate.
@Override
public String generate(String email, Map<String, Object> claims) {
Builder builder = JWT.create().withIssuer(issuer).withSubject(email).withIssuedAt(new Date()).withClaim(emailClaimProperty, email);
// add claims
claims.forEach((key, value) -> {
if (value instanceof String) {
builder.withClaim(key, (String) value);
} else if (value instanceof Iterable<?>) {
builder.withArrayClaim(key, Iterables.toArray((Iterable<String>) value, String.class));
} else if (value instanceof String[]) {
builder.withArrayClaim(key, (String[]) value);
}
});
try {
return builder.sign(algorithm);
} catch (Throwable e) {
e.printStackTrace();
throw e;
}
}
Aggregations