Search in sources :

Example 6 with Session

use of io.undertow.server.session.Session in project undertow by undertow-io.

the class Sessions method getSession.

private static Session getSession(final HttpServerExchange exchange, boolean create) {
    SessionManager sessionManager = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
    SessionConfig sessionConfig = exchange.getAttachment(SessionConfig.ATTACHMENT_KEY);
    if (sessionManager == null) {
        throw UndertowMessages.MESSAGES.sessionManagerNotFound();
    }
    Session session = sessionManager.getSession(exchange, sessionConfig);
    if (session == null && create) {
        session = sessionManager.createSession(exchange, sessionConfig);
    }
    return session;
}
Also used : SessionManager(io.undertow.server.session.SessionManager) SessionConfig(io.undertow.server.session.SessionConfig) Session(io.undertow.server.session.Session)

Example 7 with Session

use of io.undertow.server.session.Session in project undertow by undertow-io.

the class FormAuthenticationMechanism method storeInitialLocation.

protected void storeInitialLocation(final HttpServerExchange exchange) {
    Session session = Sessions.getOrCreateSession(exchange);
    session.setAttribute(LOCATION_ATTRIBUTE, RedirectBuilder.redirect(exchange, exchange.getRelativePath()));
}
Also used : Session(io.undertow.server.session.Session)

Example 8 with Session

use of io.undertow.server.session.Session in project undertow by undertow-io.

the class SingleSignOnAuthenticationMechanism method authenticate.

@Override
public AuthenticationMechanismOutcome authenticate(HttpServerExchange exchange, SecurityContext securityContext) {
    Cookie cookie = null;
    for (Cookie c : exchange.requestCookies()) {
        if (cookieName.equals(c.getName())) {
            cookie = c;
        }
    }
    if (cookie != null) {
        final String ssoId = cookie.getValue();
        log.tracef("Found SSO cookie %s", ssoId);
        try (SingleSignOn sso = this.singleSignOnManager.findSingleSignOn(ssoId)) {
            if (sso != null) {
                if (log.isTraceEnabled()) {
                    log.tracef("SSO session with ID: %s found.", ssoId);
                }
                Account verified = getIdentityManager(securityContext).verify(sso.getAccount());
                if (verified == null) {
                    if (log.isTraceEnabled()) {
                        log.tracef("Account not found. Returning 'not attempted' here.");
                    }
                    // we return not attempted here to allow other mechanisms to proceed as normal
                    return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
                }
                final Session session = getSession(exchange);
                registerSessionIfRequired(sso, session);
                securityContext.authenticationComplete(verified, sso.getMechanismName(), false);
                securityContext.registerNotificationReceiver(new NotificationReceiver() {

                    @Override
                    public void handleNotification(SecurityNotification notification) {
                        if (notification.getEventType() == SecurityNotification.EventType.LOGGED_OUT) {
                            singleSignOnManager.removeSingleSignOn(sso);
                        }
                    }
                });
                log.tracef("Authenticated account %s using SSO", verified.getPrincipal().getName());
                return AuthenticationMechanismOutcome.AUTHENTICATED;
            }
        }
        clearSsoCookie(exchange);
    }
    exchange.addResponseWrapper(responseListener);
    return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
}
Also used : Cookie(io.undertow.server.handlers.Cookie) Account(io.undertow.security.idm.Account) NotificationReceiver(io.undertow.security.api.NotificationReceiver) Session(io.undertow.server.session.Session) SecurityNotification(io.undertow.security.api.SecurityNotification)

Example 9 with Session

use of io.undertow.server.session.Session in project undertow by undertow-io.

the class SavedRequest method trySaveRequest.

public static void trySaveRequest(final HttpServerExchange exchange, final byte[] buffer, int length) {
    int maxSize = exchange.getConnection().getUndertowOptions().get(UndertowOptions.MAX_BUFFERED_REQUEST_SIZE, UndertowOptions.DEFAULT_MAX_BUFFERED_REQUEST_SIZE);
    if (maxSize > 0) {
        if (length > maxSize) {
            UndertowLogger.REQUEST_LOGGER.debugf("Request to %s was to large to save", exchange.getRequestURI());
            // failed to save the request, we just return
            return;
        }
        // TODO: we should really be used pooled buffers
        // TODO: we should probably limit the number of saved requests at any given time
        HeaderMap headers = new HeaderMap();
        for (HeaderValues entry : exchange.getRequestHeaders()) {
            if (entry.getHeaderName().equals(Headers.CONTENT_LENGTH) || entry.getHeaderName().equals(Headers.TRANSFER_ENCODING) || entry.getHeaderName().equals(Headers.CONNECTION)) {
                continue;
            }
            headers.putAll(entry.getHeaderName(), entry);
        }
        SavedRequest request = new SavedRequest(buffer, length, exchange.getRequestMethod(), exchange.getRelativePath(), exchange.getRequestHeaders());
        final ServletRequestContext sc = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
        HttpSessionImpl session = sc.getCurrentServletContext().getSession(exchange, true);
        Session underlyingSession;
        if (System.getSecurityManager() == null) {
            underlyingSession = session.getSession();
        } else {
            underlyingSession = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(session));
        }
        underlyingSession.setAttribute(SESSION_KEY, request);
    }
}
Also used : HeaderMap(io.undertow.util.HeaderMap) HttpSessionImpl(io.undertow.servlet.spec.HttpSessionImpl) HeaderValues(io.undertow.util.HeaderValues) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpSession(javax.servlet.http.HttpSession) Session(io.undertow.server.session.Session)

Example 10 with Session

use of io.undertow.server.session.Session in project undertow by undertow-io.

the class SavedRequest method tryRestoreRequest.

public static void tryRestoreRequest(final HttpServerExchange exchange, HttpSession session) {
    if (session instanceof HttpSessionImpl) {
        Session underlyingSession;
        if (System.getSecurityManager() == null) {
            underlyingSession = ((HttpSessionImpl) session).getSession();
        } else {
            underlyingSession = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(session));
        }
        SavedRequest request = (SavedRequest) underlyingSession.getAttribute(SESSION_KEY);
        if (request != null) {
            if (request.requestPath.equals(exchange.getRelativePath()) && exchange.isRequestComplete()) {
                UndertowLogger.REQUEST_LOGGER.debugf("restoring request body for request to %s", request.requestPath);
                exchange.setRequestMethod(request.method);
                Connectors.ungetRequestBytes(exchange, new ImmediatePooledByteBuffer(ByteBuffer.wrap(request.data, 0, request.dataLength)));
                underlyingSession.removeAttribute(SESSION_KEY);
                // clear the existing header map of everything except the connection header
                // TODO: are there other headers we should preserve?
                Iterator<HeaderValues> headerIterator = exchange.getRequestHeaders().iterator();
                while (headerIterator.hasNext()) {
                    HeaderValues header = headerIterator.next();
                    if (!header.getHeaderName().equals(Headers.CONNECTION)) {
                        headerIterator.remove();
                    }
                }
                for (Map.Entry<HttpString, List<String>> header : request.headerMap.entrySet()) {
                    exchange.getRequestHeaders().putAll(header.getKey(), header.getValue());
                }
            }
        }
    }
}
Also used : HttpSessionImpl(io.undertow.servlet.spec.HttpSessionImpl) HeaderValues(io.undertow.util.HeaderValues) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) HeaderMap(io.undertow.util.HeaderMap) Map(java.util.Map) HttpSession(javax.servlet.http.HttpSession) Session(io.undertow.server.session.Session) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) HttpString(io.undertow.util.HttpString)

Aggregations

Session (io.undertow.server.session.Session)33 SessionManager (io.undertow.server.session.SessionManager)19 Test (org.junit.Test)10 HttpServerExchange (io.undertow.server.HttpServerExchange)9 AuthenticatedSession (io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession)7 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)7 HttpSessionImpl (io.undertow.servlet.spec.HttpSessionImpl)7 HttpString (io.undertow.util.HttpString)7 HttpHandler (io.undertow.server.HttpHandler)6 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)6 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)6 IOException (java.io.IOException)6 BatchContext (org.wildfly.clustering.ee.BatchContext)6 SessionConfig (io.undertow.server.session.SessionConfig)5 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)4 TestHttpClient (io.undertow.testutils.TestHttpClient)4 Map (java.util.Map)4 Header (org.apache.http.Header)4 HttpResponse (org.apache.http.HttpResponse)4 HeaderMap (io.undertow.util.HeaderMap)3