Search in sources :

Example 1 with JWT

use of io.gravitee.am.common.jwt.JWT in project gravitee-access-management by gravitee-io.

the class ErrorEndpoint method renderErrorPage.

private void renderErrorPage(RoutingContext routingContext, Client client) {
    final HttpServerRequest request = routingContext.request();
    final String error = request.getParam(ERROR_PARAM);
    String errorDescription = request.getParam(ERROR_DESCRIPTION_PARAM);
    if (errorDescription != null) {
        try {
            errorDescription = java.net.URLDecoder.decode(request.getParam(ERROR_DESCRIPTION_PARAM), StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
        // unable to decode UTF-8 encoded query parameter
        }
    }
    final Map<String, String> errorParams = new HashMap<>();
    errorParams.put(ERROR_PARAM, error);
    errorParams.put(ERROR_DESCRIPTION_PARAM, errorDescription);
    Single<Map<String, String>> singlePageRendering = Single.just(errorParams);
    final String jarm = request.getParam(io.gravitee.am.common.oidc.Parameters.RESPONSE);
    if (error == null && jarm != null) {
        // extract error details from the JWT provided as response parameter
        singlePageRendering = this.jwtService.decode(jarm).map(jwt -> {
            Map<String, String> result = new HashMap<>();
            result.put(ERROR_PARAM, (String) jwt.get(ERROR_PARAM));
            result.put(ERROR_DESCRIPTION_PARAM, (String) jwt.get(ERROR_DESCRIPTION_PARAM));
            return result;
        });
    }
    singlePageRendering.subscribe(params -> render(routingContext, client, params), // single contains an error due to JWT decoding, return the default error page without error details
    (exception) -> render(routingContext, client, errorParams));
}
Also used : HttpHeaders(io.gravitee.common.http.HttpHeaders) Client(io.gravitee.am.model.oidc.Client) ThymeleafTemplateEngine(io.vertx.reactivex.ext.web.templ.thymeleaf.ThymeleafTemplateEngine) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Single(io.reactivex.Single) JWTService(io.gravitee.am.gateway.handler.common.jwt.JWTService) Map(java.util.Map) ClientSyncService(io.gravitee.am.gateway.handler.common.client.ClientSyncService) AsyncResult(io.vertx.core.AsyncResult) Logger(org.slf4j.Logger) HttpServerRequest(io.vertx.reactivex.core.http.HttpServerRequest) JWT(io.gravitee.am.common.jwt.JWT) ClientNotFoundException(io.gravitee.am.service.exception.ClientNotFoundException) Domain(io.gravitee.am.model.Domain) Consumer(io.reactivex.functions.Consumer) Future(io.vertx.core.Future) RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) StandardCharsets(java.nio.charset.StandardCharsets) ThymeleafDataHelper.generateData(io.gravitee.am.gateway.handler.common.utils.ThymeleafDataHelper.generateData) MediaType(io.gravitee.common.http.MediaType) ThymeleafDataHelper(io.gravitee.am.gateway.handler.common.utils.ThymeleafDataHelper) Template(io.gravitee.am.model.Template) Handler(io.vertx.core.Handler) Parameters(io.gravitee.am.common.oauth2.Parameters) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HashMap(java.util.HashMap) HttpServerRequest(io.vertx.reactivex.core.http.HttpServerRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with JWT

use of io.gravitee.am.common.jwt.JWT in project gravitee-access-management by gravitee-io.

the class CookieSession method value.

@Override
public String value() {
    JWT jwt = new JWT(this.data());
    jwt.setExp((System.currentTimeMillis() + this.timeout()) / 1000);
    return this.jwtService.encode(jwt, certificateProvider).blockingGet();
}
Also used : JWT(io.gravitee.am.common.jwt.JWT)

Example 3 with JWT

use of io.gravitee.am.common.jwt.JWT in project gravitee-access-management by gravitee-io.

the class IntrospectionTokenServiceTest method shouldIntrospect_validToken_offline_verification.

@Test
public void shouldIntrospect_validToken_offline_verification() {
    final String token = "token";
    final JWT jwt = new JWT();
    jwt.setJti("jti");
    jwt.setDomain("domain");
    jwt.setAud("client");
    final Client client = new Client();
    client.setClientId("client-id");
    when(jwtService.decode(token)).thenReturn(Single.just(jwt));
    when(clientService.findByDomainAndClientId(jwt.getDomain(), jwt.getAud())).thenReturn(Maybe.just(client));
    when(jwtService.decodeAndVerify(token, client)).thenReturn(Single.just(jwt));
    TestObserver testObserver = introspectionTokenService.introspect(token, true).test();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(accessTokenRepository, never()).findByToken(jwt.getJti());
}
Also used : JWT(io.gravitee.am.common.jwt.JWT) Client(io.gravitee.am.model.oidc.Client) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 4 with JWT

use of io.gravitee.am.common.jwt.JWT in project gravitee-access-management by gravitee-io.

the class IntrospectionTokenServiceTest method shouldIntrospect_invalidValidToken_token_expired.

@Test
public void shouldIntrospect_invalidValidToken_token_expired() {
    final String token = "token";
    final JWT jwt = new JWT();
    jwt.setJti("jti");
    jwt.setDomain("domain");
    jwt.setAud("client");
    jwt.setIat(Instant.now().minus(1, ChronoUnit.DAYS).getEpochSecond());
    final Client client = new Client();
    client.setClientId("client-id");
    final AccessToken accessToken = new AccessToken();
    accessToken.setExpireAt(new Date(Instant.now().minus(1, ChronoUnit.DAYS).toEpochMilli()));
    when(jwtService.decode(token)).thenReturn(Single.just(jwt));
    when(clientService.findByDomainAndClientId(jwt.getDomain(), jwt.getAud())).thenReturn(Maybe.just(client));
    when(jwtService.decodeAndVerify(token, client)).thenReturn(Single.just(jwt));
    when(accessTokenRepository.findByToken(jwt.getJti())).thenReturn(Maybe.just(accessToken));
    TestObserver testObserver = introspectionTokenService.introspect(token, false).test();
    testObserver.assertError(InvalidTokenException.class);
    verify(accessTokenRepository, times(1)).findByToken(jwt.getJti());
}
Also used : JWT(io.gravitee.am.common.jwt.JWT) AccessToken(io.gravitee.am.repository.oauth2.model.AccessToken) Client(io.gravitee.am.model.oidc.Client) Date(java.util.Date) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 5 with JWT

use of io.gravitee.am.common.jwt.JWT in project gravitee-access-management by gravitee-io.

the class IntrospectionTokenServiceTest method shouldIntrospect_invalidValidToken_jwt_exception.

@Test
public void shouldIntrospect_invalidValidToken_jwt_exception() {
    final String token = "token";
    final JWT jwt = new JWT();
    jwt.setJti("jti");
    jwt.setDomain("domain");
    jwt.setAud("client");
    jwt.setIat(Instant.now().getEpochSecond());
    final Client client = new Client();
    client.setClientId("client-id");
    when(jwtService.decode(token)).thenReturn(Single.just(jwt));
    when(clientService.findByDomainAndClientId(jwt.getDomain(), jwt.getAud())).thenReturn(Maybe.just(client));
    when(jwtService.decodeAndVerify(token, client)).thenReturn(Single.error(new JWTException("invalid token")));
    TestObserver testObserver = introspectionTokenService.introspect(token, false).test();
    testObserver.assertError(InvalidTokenException.class);
    verify(accessTokenRepository, never()).findByToken(jwt.getJti());
}
Also used : JWTException(io.gravitee.am.common.exception.jwt.JWTException) JWT(io.gravitee.am.common.jwt.JWT) Client(io.gravitee.am.model.oidc.Client) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Aggregations

JWT (io.gravitee.am.common.jwt.JWT)130 Test (org.junit.Test)76 Client (io.gravitee.am.model.oidc.Client)72 User (io.gravitee.am.model.User)35 Maybe (io.reactivex.Maybe)27 Json (io.vertx.core.json.Json)26 HttpHeaders (io.gravitee.common.http.HttpHeaders)23 MediaType (io.gravitee.common.http.MediaType)23 Single (io.reactivex.Single)22 ConstantKeys (io.gravitee.am.common.utils.ConstantKeys)19 InvalidTokenException (io.gravitee.am.common.exception.oauth2.InvalidTokenException)17 JWTService (io.gravitee.am.gateway.handler.common.jwt.JWTService)17 Handler (io.vertx.core.Handler)16 RxWebTestBase (io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase)14 HttpMethod (io.vertx.core.http.HttpMethod)14 BodyHandler (io.vertx.reactivex.ext.web.handler.BodyHandler)14 RunWith (org.junit.runner.RunWith)14 InjectMocks (org.mockito.InjectMocks)14 Mock (org.mockito.Mock)14 OAuth2Request (io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request)11