Search in sources :

Example 1 with Builder

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);
}
Also used : JWTCreator(com.auth0.jwt.JWTCreator) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date)

Example 2 with Builder

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);
    }
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) JWTCreator(com.auth0.jwt.JWTCreator) NullClaim(com.auth0.jwt.impl.NullClaim) Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Date(java.util.Date) NullClaim(com.auth0.jwt.impl.NullClaim) Claim(com.auth0.jwt.interfaces.Claim)

Example 3 with Builder

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);
}
Also used : JWTCreator(com.auth0.jwt.JWTCreator) JSONObject(org.json.simple.JSONObject) Date(java.util.Date)

Example 4 with Builder

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));
}
Also used : LocalDateTime(java.time.LocalDateTime) JWTCreator(com.auth0.jwt.JWTCreator) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date) ZoneOffset(java.time.ZoneOffset)

Example 5 with Builder

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;
    }
}
Also used : Builder(com.auth0.jwt.JWTCreator.Builder) Date(java.util.Date)

Aggregations

Date (java.util.Date)5 JWTCreator (com.auth0.jwt.JWTCreator)4 Algorithm (com.auth0.jwt.algorithms.Algorithm)3 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)3 Claim (com.auth0.jwt.interfaces.Claim)3 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)3 Builder (com.auth0.jwt.JWTCreator.Builder)2 ByteBuffer (java.nio.ByteBuffer)2 LLDPTLV (net.floodlightcontroller.packet.LLDPTLV)2 JWTVerifier (com.auth0.jwt.JWTVerifier)1 NullClaim (com.auth0.jwt.impl.NullClaim)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InetSocketAddress (java.net.InetSocketAddress)1 LocalDateTime (java.time.LocalDateTime)1 ZoneOffset (java.time.ZoneOffset)1 FloodlightModuleException (net.floodlightcontroller.core.module.FloodlightModuleException)1 Ethernet (net.floodlightcontroller.packet.Ethernet)1 IPv4 (net.floodlightcontroller.packet.IPv4)1 UDP (net.floodlightcontroller.packet.UDP)1