Esempio n. 1
0
 @Test
 public void api() {
   JWT jwt = WERVAL.application().plugin(JWT.class);
   String token = jwt.tokenForClaims(singletonMap(JWT.CLAIM_SUBJECT, "*****@*****.**"));
   Map<String, Object> parsed = jwt.claimsOfToken(token);
   assertThat(parsed.get(JWT.CLAIM_SUBJECT), equalTo("*****@*****.**"));
 }
Esempio n. 2
0
  @Test
  public void http() throws InterruptedException {
    String tokenHeaderName = WERVAL.application().config().string(JWT.HTTP_HEADER_CONFIG_KEY);
    JWT jwt = WERVAL.application().plugin(JWT.class);

    // Unauthorized access to authenticated resource
    when().get("/authenticated").then().statusCode(UNAUTHORIZED_CODE);

    // Login
    String token =
        given()
            .body("{\"email\":\"[email protected]\",\"password\":\"admin-password\"}")
            .contentType(APPLICATION_JSON)
            .when()
            .post("/login")
            .then()
            .statusCode(OK_CODE)
            .header(tokenHeaderName, notNullValue())
            .log()
            .all()
            .extract()
            .header(tokenHeaderName);

    // Authenticated access
    given().header(tokenHeaderName, token).when().get("/authenticated").then().statusCode(OK_CODE);

    // Authorized access
    given().header(tokenHeaderName, token).when().get("/authorized").then().statusCode(OK_CODE);

    // Gather time related claims from token
    ZoneId utc = ZoneId.of("UTC");
    Map<String, Object> claims = jwt.claimsOfToken(token);
    ZonedDateTime iat =
        ZonedDateTime.ofInstant(Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_ISSUED_AT)), utc);
    ZonedDateTime nbf =
        ZonedDateTime.ofInstant(
            Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_NOT_BEFORE)), utc);
    ZonedDateTime exp =
        ZonedDateTime.ofInstant(
            Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_EXPIRATION)), utc);

    // Wait at least one second before renewal so new dates will be different
    Thread.sleep(1200);

    // Renew token
    String renewed =
        given()
            .header(tokenHeaderName, token)
            .when()
            .post("/renew")
            .then()
            .statusCode(OK_CODE)
            .header(tokenHeaderName, notNullValue())
            .log()
            .all()
            .extract()
            .header(tokenHeaderName);

    // Gather time related claims from renewed token
    claims = jwt.claimsOfToken(renewed);
    ZonedDateTime renewedIat =
        ZonedDateTime.ofInstant(Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_ISSUED_AT)), utc);
    ZonedDateTime renewedNbf =
        ZonedDateTime.ofInstant(
            Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_NOT_BEFORE)), utc);
    ZonedDateTime renewedExp =
        ZonedDateTime.ofInstant(
            Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_EXPIRATION)), utc);

    // Assert renewed token time related claims are greater than the ones in the original token
    assertTrue(renewedIat.isAfter(iat));
    assertTrue(renewedNbf.isAfter(nbf));
    assertTrue(renewedExp.isAfter(exp));
  }