Search in sources :

Example 1 with Cookie

use of org.pac4j.core.context.Cookie in project ratpack by ratpack.

the class RatpackWebContext method getRequestCookies.

@Override
public Collection<Cookie> getRequestCookies() {
    final List<Cookie> newCookies = new ArrayList<>();
    final Set<io.netty.handler.codec.http.cookie.Cookie> cookies = request.getCookies();
    for (final io.netty.handler.codec.http.cookie.Cookie cookie : cookies) {
        final Cookie newCookie = new Cookie(cookie.name(), cookie.value());
        newCookie.setDomain(cookie.domain());
        newCookie.setPath(cookie.path());
        newCookie.setMaxAge((int) cookie.maxAge());
        newCookie.setSecure(cookie.isSecure());
        newCookie.setHttpOnly(cookie.isHttpOnly());
        newCookies.add(newCookie);
    }
    return newCookies;
}
Also used : Cookie(org.pac4j.core.context.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) ratpack.http(ratpack.http)

Example 2 with Cookie

use of org.pac4j.core.context.Cookie in project knox by apache.

the class KnoxSessionStore method set.

public void set(WebContext context, String key, Object value) {
    logger.debug("Save in session: {} = {}", key, value);
    final Cookie cookie = new Cookie(PAC4J_SESSION_PREFIX + key, compressEncryptBase64(value));
    try {
        String domain = Urls.getDomainName(context.getFullRequestURL(), this.domainSuffix);
        if (domain == null) {
            domain = context.getServerName();
        }
        cookie.setDomain(domain);
    } catch (final Exception e) {
        throw new TechnicalException(e);
    }
    cookie.setHttpOnly(true);
    cookie.setSecure(ContextHelper.isHttpsOrSecure(context));
    context.addResponseCookie(cookie);
}
Also used : Cookie(org.pac4j.core.context.Cookie) TechnicalException(org.pac4j.core.exception.TechnicalException) IOException(java.io.IOException) TechnicalException(org.pac4j.core.exception.TechnicalException)

Example 3 with Cookie

use of org.pac4j.core.context.Cookie in project knox by apache.

the class KnoxSessionStore method get.

public Object get(WebContext context, String key) {
    final Cookie cookie = ContextHelper.getCookie(context, PAC4J_SESSION_PREFIX + key);
    Object value = null;
    if (cookie != null) {
        value = uncompressDecryptBase64(cookie.getValue());
    }
    logger.debug("Get from session: {} = {}", key, value);
    return value;
}
Also used : Cookie(org.pac4j.core.context.Cookie)

Example 4 with Cookie

use of org.pac4j.core.context.Cookie in project pac4j by pac4j.

the class CookieClientTests method testAuthentication.

@Test
public void testAuthentication() {
    final CookieClient client = new CookieClient(USERNAME, new SimpleTestTokenAuthenticator());
    final MockWebContext context = MockWebContext.create();
    final Cookie c = new Cookie(USERNAME, Base64.getEncoder().encodeToString(getClass().getName().getBytes(StandardCharsets.UTF_8)));
    context.getRequestCookies().add(c);
    final TokenCredentials credentials = client.getCredentials(context);
    final CommonProfile profile = client.getUserProfile(credentials, context);
    assertEquals(c.getValue(), profile.getId());
}
Also used : Cookie(org.pac4j.core.context.Cookie) MockWebContext(org.pac4j.core.context.MockWebContext) CommonProfile(org.pac4j.core.profile.CommonProfile) SimpleTestTokenAuthenticator(org.pac4j.http.credentials.authenticator.test.SimpleTestTokenAuthenticator) TokenCredentials(org.pac4j.core.credentials.TokenCredentials) Test(org.junit.Test)

Example 5 with Cookie

use of org.pac4j.core.context.Cookie in project pac4j by pac4j.

the class CsrfTokenGeneratorAuthorizer method isAuthorized.

@Override
public boolean isAuthorized(final WebContext context, final List<CommonProfile> profiles) {
    CommonHelper.assertNotNull("csrfTokenGenerator", csrfTokenGenerator);
    final String token = csrfTokenGenerator.get(context);
    context.setRequestAttribute(Pac4jConstants.CSRF_TOKEN, token);
    final Cookie cookie = new Cookie(Pac4jConstants.CSRF_TOKEN, token);
    if (domain != null) {
        cookie.setDomain(domain);
    } else {
        cookie.setDomain(context.getServerName());
    }
    if (path != null) {
        cookie.setPath(path);
    }
    if (httpOnly != null) {
        cookie.setHttpOnly(httpOnly.booleanValue());
    }
    if (secure != null) {
        cookie.setSecure(secure.booleanValue());
    }
    context.addResponseCookie(cookie);
    return true;
}
Also used : Cookie(org.pac4j.core.context.Cookie)

Aggregations

Cookie (org.pac4j.core.context.Cookie)5 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)1 IOException (java.io.IOException)1 Test (org.junit.Test)1 MockWebContext (org.pac4j.core.context.MockWebContext)1 TokenCredentials (org.pac4j.core.credentials.TokenCredentials)1 TechnicalException (org.pac4j.core.exception.TechnicalException)1 CommonProfile (org.pac4j.core.profile.CommonProfile)1 SimpleTestTokenAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestTokenAuthenticator)1 ratpack.http (ratpack.http)1