Search in sources :

Example 1 with JWTService

use of io.gravitee.am.gateway.handler.common.jwt.JWTService in project gravitee-access-management by gravitee-io.

the class AuthenticationFlowHandlerTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    List<AuthenticationFlowStep> steps = new LinkedList<>();
    steps.add(new MFAEnrollStep(RedirectHandler.create("/mfa/enroll"), ruleEngine, factorManager));
    steps.add(new MFAChallengeStep(RedirectHandler.create("/mfa/challenge"), ruleEngine, factorManager));
    AuthenticationFlowChainHandler authenticationFlowChainHandler = new AuthenticationFlowChainHandler(steps);
    when(jwtService.encode(any(JWT.class), (CertificateProvider) eq(null))).thenReturn(Single.just("token"));
    Factor factor = new Factor();
    factor.setFactorType(FactorType.SMS);
    when(factorManager.getFactor(anyString())).thenReturn(factor);
    router.route("/login").order(Integer.MIN_VALUE).handler(new CookieSessionHandler(jwtService, certificateManager, userService, "am-cookie", 30 * 60 * 60));
    router.route("/login").handler(authenticationFlowChainHandler).handler(rc -> rc.response().setStatusCode(200).end()).failureHandler(new ErrorHandler());
}
Also used : FactorType(io.gravitee.am.common.factor.FactorType) Factor(io.gravitee.am.model.Factor) MFASettings(io.gravitee.am.model.MFASettings) ArgumentMatchers(org.mockito.ArgumentMatchers) MFAChallengeStep(io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.mfa.MFAChallengeStep) Client(io.gravitee.am.model.oidc.Client) AuthenticationFlowChainHandler(io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.AuthenticationFlowChainHandler) Mock(org.mockito.Mock) UserService(io.gravitee.am.service.UserService) RunWith(org.junit.runner.RunWith) ConstantKeys(io.gravitee.am.common.utils.ConstantKeys) Single(io.reactivex.Single) RememberDeviceSettings(io.gravitee.am.model.RememberDeviceSettings) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) EnrolledFactor(io.gravitee.am.model.factor.EnrolledFactor) JWTService(io.gravitee.am.gateway.handler.common.jwt.JWTService) User(io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User) RxWebTestBase(io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase) JsonObject(io.vertx.core.json.JsonObject) LinkedList(java.util.LinkedList) SpELRuleEngine(io.gravitee.am.gateway.handler.common.ruleengine.SpELRuleEngine) JWT(io.gravitee.am.common.jwt.JWT) CertificateManager(io.gravitee.am.gateway.handler.common.certificate.CertificateManager) AuthenticationFlowStep(io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.AuthenticationFlowStep) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DEVICE_ALREADY_EXISTS_KEY(io.gravitee.am.common.utils.ConstantKeys.DEVICE_ALREADY_EXISTS_KEY) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) CertificateProvider(io.gravitee.am.gateway.certificate.CertificateProvider) MFAEnrollStep(io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.mfa.MFAEnrollStep) CookieSessionHandler(io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.CookieSessionHandler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) FactorManager(io.gravitee.am.gateway.handler.common.factor.FactorManager) MFAChallengeStep(io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.mfa.MFAChallengeStep) MFAEnrollStep(io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.mfa.MFAEnrollStep) AuthenticationFlowStep(io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.AuthenticationFlowStep) JWT(io.gravitee.am.common.jwt.JWT) Factor(io.gravitee.am.model.Factor) EnrolledFactor(io.gravitee.am.model.factor.EnrolledFactor) AuthenticationFlowChainHandler(io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.AuthenticationFlowChainHandler) CookieSessionHandler(io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.CookieSessionHandler) LinkedList(java.util.LinkedList)

Example 2 with JWTService

use of io.gravitee.am.gateway.handler.common.jwt.JWTService in project gravitee-access-management by gravitee-io.

the class CookieSessionHandler method handle.

@Override
public void handle(RoutingContext context) {
    if (logger.isDebugEnabled()) {
        String uri = context.request().absoluteURI();
        if (!uri.startsWith("https:")) {
            logger.debug("Using session cookies without https could make you susceptible to session hijacking: " + uri);
        }
    }
    Cookie sessionCookie = context.getCookie(cookieName);
    CookieSession session = new CookieSession(jwtService, certificateManager.defaultCertificateProvider(), timeout);
    registerSession(context, session);
    Single<CookieSession> sessionObs = Single.just(session);
    if (sessionCookie != null) {
        sessionObs = session.setValue(sessionCookie.getValue()).flatMap(currentSession -> {
            String userId = currentSession.get(USER_ID_KEY);
            if (!StringUtils.isEmpty(userId)) {
                // Load the user and put it back in the context.
                return userService.findById(userId).doOnSuccess(user -> context.getDelegate().setUser(new User(user))).flatMap(user -> userService.enhance(user).toMaybe()).map(user -> currentSession).switchIfEmpty(cleanupSession(currentSession)).onErrorResumeNext(cleanupSession(currentSession));
            } else {
                return Single.just(currentSession);
            }
        });
    }
    // Need to wait the session to be ready before invoking next.
    sessionObs.doOnError(t -> logger.warn("Unable to restore the session", t)).doFinally(context::next).subscribe();
}
Also used : Cookie(io.vertx.reactivex.core.http.Cookie) Cookie(io.vertx.reactivex.core.http.Cookie) CertificateManager(io.gravitee.am.gateway.handler.common.certificate.CertificateManager) UserService(io.gravitee.am.service.UserService) DEFAULT_SESSION_TIMEOUT(io.vertx.ext.web.handler.SessionHandler.DEFAULT_SESSION_TIMEOUT) Single(io.reactivex.Single) RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) LoggerFactory(io.vertx.core.logging.LoggerFactory) Value(org.springframework.beans.factory.annotation.Value) TimeUnit(java.util.concurrent.TimeUnit) JWTService(io.gravitee.am.gateway.handler.common.jwt.JWTService) User(io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) StringUtils(org.springframework.util.StringUtils) User(io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User)

Aggregations

CertificateManager (io.gravitee.am.gateway.handler.common.certificate.CertificateManager)2 JWTService (io.gravitee.am.gateway.handler.common.jwt.JWTService)2 User (io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User)2 UserService (io.gravitee.am.service.UserService)2 Single (io.reactivex.Single)2 FactorType (io.gravitee.am.common.factor.FactorType)1 JWT (io.gravitee.am.common.jwt.JWT)1 ConstantKeys (io.gravitee.am.common.utils.ConstantKeys)1 DEVICE_ALREADY_EXISTS_KEY (io.gravitee.am.common.utils.ConstantKeys.DEVICE_ALREADY_EXISTS_KEY)1 CertificateProvider (io.gravitee.am.gateway.certificate.CertificateProvider)1 FactorManager (io.gravitee.am.gateway.handler.common.factor.FactorManager)1 SpELRuleEngine (io.gravitee.am.gateway.handler.common.ruleengine.SpELRuleEngine)1 RxWebTestBase (io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase)1 CookieSessionHandler (io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.CookieSessionHandler)1 AuthenticationFlowChainHandler (io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.AuthenticationFlowChainHandler)1 AuthenticationFlowStep (io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.AuthenticationFlowStep)1 MFAChallengeStep (io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.mfa.MFAChallengeStep)1 MFAEnrollStep (io.gravitee.am.gateway.handler.common.vertx.web.handler.impl.internal.mfa.MFAEnrollStep)1 Factor (io.gravitee.am.model.Factor)1 MFASettings (io.gravitee.am.model.MFASettings)1