Search in sources :

Example 41 with Role

use of com.auth0.json.mgmt.Role in project auth0-java by auth0.

the class RolesEntityTest method shouldThrowOnUpdateRoleWithNullId.

@Test
public void shouldThrowOnUpdateRoleWithNullId() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("'role id' cannot be null!");
    api.roles().update(null, new Role());
}
Also used : Role(com.auth0.json.mgmt.Role) Test(org.junit.Test)

Example 42 with Role

use of com.auth0.json.mgmt.Role in project auth0-java by auth0.

the class RolesEntityTest method shouldUpdateRole.

@Test
public void shouldUpdateRole() throws Exception {
    Role updatedRole = new Role();
    updatedRole.setName("roleId");
    Request<Role> request = api.roles().update("1", updatedRole);
    assertThat(request, is(notNullValue()));
    server.jsonResponse(MGMT_ROLE, 200);
    Role response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest, hasMethodAndPath("PATCH", "/api/v2/roles/1"));
    assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
    assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
    Map<String, Object> body = bodyFromRequest(recordedRequest);
    assertThat(body.size(), is(1));
    assertThat(body, hasEntry("name", "roleId"));
    assertThat(response, is(notNullValue()));
}
Also used : Role(com.auth0.json.mgmt.Role) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 43 with Role

use of com.auth0.json.mgmt.Role in project tutorials by csh0034.

the class JwtUtils method decode.

public TokenUser decode(String token) {
    jwtVerifier.verify(token);
    DecodedJWT jwt = JWT.decode(token);
    String id = jwt.getSubject();
    String role = jwt.getClaim(ROLE_CLAIM_KEY).asString();
    return new TokenUser(id, role);
}
Also used : DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 44 with Role

use of com.auth0.json.mgmt.Role in project toy by gmoon92.

the class JwtUtils method decode.

public User decode(String tokenOfIncludeSchema) {
    String token = obtainTokenWithoutSchema(tokenOfIncludeSchema);
    verify(token);
    DecodedJWT jwt = JWT.decode(token);
    String username = jwt.getClaim("username").asString();
    Role role = Role.valueOf(jwt.getClaim("role").asString());
    return User.create(username, "", role);
}
Also used : Role(com.gmoon.resourceserver.user.Role) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 45 with Role

use of com.auth0.json.mgmt.Role in project toy by gmoon92.

the class JwtUtils method generate.

public String generate(User user) {
    try {
        ZonedDateTime today = ZonedDateTime.now();
        String token = JWT.create().withIssuer(apiVersion).withClaim("username", user.getUsername()).withClaim("role", user.getRole().name()).withIssuedAt(Date.from(today.toInstant())).withExpiresAt(Date.from(today.plusDays(DAY_OF_EXPIRATION).toInstant())).sign(algorithm);
        return String.format("%s %s", AuthenticationSchema.BEARER.getName(), token);
    } catch (JWTCreationException e) {
        throw new JWTCreationException("Invalid Signing configuration or Couldn't convert Claims.", e);
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) JWTCreationException(com.auth0.jwt.exceptions.JWTCreationException)

Aggregations

Algorithm (com.auth0.jwt.algorithms.Algorithm)20 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)17 IOException (java.io.IOException)17 java.util (java.util)14 JWT (com.auth0.jwt.JWT)13 Maps (io.gravitee.common.util.Maps)12 DEFAULT_JWT_ISSUER (io.gravitee.rest.api.service.common.JWTHelper.DefaultValues.DEFAULT_JWT_ISSUER)12 Duration (java.time.Duration)12 Instant (java.time.Instant)12 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)10 JWTHelper (io.gravitee.rest.api.service.common.JWTHelper)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)10 Authentication (org.springframework.security.core.Authentication)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 HashMap (java.util.HashMap)9 Collectors (java.util.stream.Collectors)9 Cookie (javax.servlet.http.Cookie)9 SecurityContextHolder (org.springframework.security.core.context.SecurityContextHolder)9 UserDetails (io.gravitee.rest.api.idp.api.authentication.UserDetails)8 CookieGenerator (io.gravitee.rest.api.security.cookies.CookieGenerator)8