Search in sources :

Example 1 with SessionEvent

use of org.gluu.oxauth.service.external.session.SessionEvent in project oxAuth by GluuFederation.

the class SessionIdService method generateAuthenticatedSessionId.

public SessionId generateAuthenticatedSessionId(HttpServletRequest httpRequest, String userDn, Map<String, String> sessionIdAttributes) throws InvalidSessionStateException {
    SessionId sessionId = generateSessionId(userDn, new Date(), SessionIdState.AUTHENTICATED, sessionIdAttributes, true);
    reportActiveUser(sessionId);
    if (externalApplicationSessionService.isEnabled()) {
        String userName = sessionId.getSessionAttributes().get(Constants.AUTHENTICATED_USER);
        boolean externalResult = externalApplicationSessionService.executeExternalStartSessionMethods(httpRequest, sessionId);
        log.info("Start session result for '{}': '{}'", userName, "start", externalResult);
        if (!externalResult) {
            reinitLogin(sessionId, true);
            throw new InvalidSessionStateException("Session creation is prohibited by external session script!");
        }
        externalEvent(new SessionEvent(SessionEventType.AUTHENTICATED, sessionId).setHttpRequest(httpRequest));
    }
    return sessionId;
}
Also used : SessionEvent(org.gluu.oxauth.service.external.session.SessionEvent) SessionId(org.gluu.oxauth.model.common.SessionId) InvalidSessionStateException(org.gluu.oxauth.model.exception.InvalidSessionStateException)

Example 2 with SessionEvent

use of org.gluu.oxauth.service.external.session.SessionEvent in project oxAuth by GluuFederation.

the class SessionIdService method reinitLogin.

/**
 * @param session
 * @param force
 * @return returns whether session was updated
 */
public boolean reinitLogin(SessionId session, boolean force) {
    final Map<String, String> sessionAttributes = session.getSessionAttributes();
    final Map<String, String> currentSessionAttributes = getCurrentSessionAttributes(sessionAttributes);
    if (force || shouldReinitSession(sessionAttributes, currentSessionAttributes)) {
        sessionAttributes.putAll(currentSessionAttributes);
        // Reinit login
        sessionAttributes.put("c", "1");
        for (Iterator<Entry<String, String>> it = currentSessionAttributes.entrySet().iterator(); it.hasNext(); ) {
            Entry<String, String> currentSessionAttributesEntry = it.next();
            String name = currentSessionAttributesEntry.getKey();
            if (name.startsWith("auth_step_passed_")) {
                it.remove();
            }
        }
        session.setSessionAttributes(currentSessionAttributes);
        if (force) {
            // Reset state to unauthenticated
            session.setState(SessionIdState.UNAUTHENTICATED);
            externalEvent(new SessionEvent(SessionEventType.UNAUTHENTICATED, session));
        }
        boolean updateResult = updateSessionId(session, true, true, true);
        if (!updateResult) {
            log.debug("Failed to update session entry: '{}'", session.getId());
        }
        return updateResult;
    }
    return false;
}
Also used : Entry(java.util.Map.Entry) SessionEvent(org.gluu.oxauth.service.external.session.SessionEvent)

Example 3 with SessionEvent

use of org.gluu.oxauth.service.external.session.SessionEvent in project oxAuth by GluuFederation.

the class SessionIdService method mergeWithRetry.

private void mergeWithRetry(final SessionId sessionId) {
    final Pair<Date, Integer> expiration = expirationDate(sessionId.getCreationDate(), sessionId.getState());
    sessionId.setExpirationDate(expiration.getFirst());
    sessionId.setTtl(expiration.getSecond());
    EntryPersistenceException lastException = null;
    for (int i = 1; i <= MAX_MERGE_ATTEMPTS; i++) {
        try {
            if (appConfiguration.getSessionIdPersistInCache()) {
                cacheService.put(expiration.getSecond(), sessionId.getDn(), sessionId);
            } else {
                persistenceEntryManager.merge(sessionId);
            }
            localCacheService.put(DEFAULT_LOCAL_CACHE_EXPIRATION, sessionId.getDn(), sessionId);
            externalEvent(new SessionEvent(SessionEventType.UPDATED, sessionId));
            return;
        } catch (EntryPersistenceException ex) {
            lastException = ex;
            if (ex.getCause() instanceof LDAPException) {
                LDAPException parentEx = ((LDAPException) ex.getCause());
                log.debug("LDAP exception resultCode: '{}'", parentEx.getResultCode().intValue());
                if ((parentEx.getResultCode().intValue() == ResultCode.NO_SUCH_ATTRIBUTE_INT_VALUE) || (parentEx.getResultCode().intValue() == ResultCode.ATTRIBUTE_OR_VALUE_EXISTS_INT_VALUE)) {
                    log.warn("Session entry update attempt '{}' was unsuccessfull", i);
                    continue;
                }
            }
            throw ex;
        }
    }
    log.error("Session entry update attempt was unsuccessfull after '{}' attempts", MAX_MERGE_ATTEMPTS);
    throw lastException;
}
Also used : SessionEvent(org.gluu.oxauth.service.external.session.SessionEvent) LDAPException(com.unboundid.ldap.sdk.LDAPException) EntryPersistenceException(org.gluu.persist.exception.EntryPersistenceException)

Example 4 with SessionEvent

use of org.gluu.oxauth.service.external.session.SessionEvent in project oxAuth by GluuFederation.

the class SessionIdService method remove.

public boolean remove(SessionId sessionId) {
    try {
        if (appConfiguration.getSessionIdPersistInCache()) {
            cacheService.remove(sessionId.getDn());
        } else {
            persistenceEntryManager.remove(sessionId.getDn(), SessionId.class);
        }
        localCacheService.remove(sessionId.getDn());
        externalEvent(new SessionEvent(SessionEventType.GONE, sessionId));
        return true;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return false;
    }
}
Also used : SessionEvent(org.gluu.oxauth.service.external.session.SessionEvent) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) InvalidSessionStateException(org.gluu.oxauth.model.exception.InvalidSessionStateException) EntryPersistenceException(org.gluu.persist.exception.EntryPersistenceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AcrChangedException(org.gluu.oxauth.model.exception.AcrChangedException) LDAPException(com.unboundid.ldap.sdk.LDAPException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 5 with SessionEvent

use of org.gluu.oxauth.service.external.session.SessionEvent in project oxAuth by GluuFederation.

the class SessionIdService method setSessionIdStateAuthenticated.

public SessionId setSessionIdStateAuthenticated(HttpServletRequest httpRequest, HttpServletResponse httpResponse, SessionId sessionId, String p_userDn) {
    sessionId.setUserDn(p_userDn);
    sessionId.setAuthenticationTime(new Date());
    sessionId.setState(SessionIdState.AUTHENTICATED);
    final User user = getUser(sessionId);
    if (user != null) {
        statService.reportActiveUser(user.getUserId());
    }
    final boolean persisted;
    if (appConfiguration.getChangeSessionIdOnAuthentication() && httpResponse != null) {
        final String oldSessionId = sessionId.getId();
        final String newSessionId = UUID.randomUUID().toString();
        log.debug("Changing session id from {} to {} ...", oldSessionId, newSessionId);
        remove(sessionId);
        sessionId.setId(newSessionId);
        sessionId.setDn(buildDn(newSessionId));
        sessionId.getSessionAttributes().put(SessionId.OLD_SESSION_ID_ATTR_KEY, oldSessionId);
        if (sessionId.getIsJwt()) {
            sessionId.setJwt(generateJwt(sessionId, sessionId.getUserDn()).asString());
        }
        persisted = persistSessionId(sessionId, true);
        cookieService.createSessionIdCookie(sessionId, httpRequest, httpResponse, false);
        log.debug("Session identifier changed from {} to {} .", oldSessionId, newSessionId);
    } else {
        persisted = updateSessionId(sessionId, true, true, true);
    }
    auditLogging(sessionId);
    log.trace("Authenticated session, id = '{}', state = '{}', persisted = '{}'", sessionId.getId(), sessionId.getState(), persisted);
    if (externalApplicationSessionService.isEnabled()) {
        String userName = sessionId.getSessionAttributes().get(Constants.AUTHENTICATED_USER);
        boolean externalResult = externalApplicationSessionService.executeExternalStartSessionMethods(httpRequest, sessionId);
        log.info("Start session result for '{}': '{}'", userName, "start", externalResult);
        if (!externalResult) {
            reinitLogin(sessionId, true);
            throw new InvalidSessionStateException("Session creation is prohibited by external session script!");
        }
        externalEvent(new SessionEvent(SessionEventType.AUTHENTICATED, sessionId).setHttpRequest(httpRequest).setHttpResponse(httpResponse));
    }
    return sessionId;
}
Also used : SessionEvent(org.gluu.oxauth.service.external.session.SessionEvent) User(org.gluu.oxauth.model.common.User) InvalidSessionStateException(org.gluu.oxauth.model.exception.InvalidSessionStateException)

Aggregations

SessionEvent (org.gluu.oxauth.service.external.session.SessionEvent)6 InvalidSessionStateException (org.gluu.oxauth.model.exception.InvalidSessionStateException)3 LDAPException (com.unboundid.ldap.sdk.LDAPException)2 EntryPersistenceException (org.gluu.persist.exception.EntryPersistenceException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 Entry (java.util.Map.Entry)1 SessionId (org.gluu.oxauth.model.common.SessionId)1 User (org.gluu.oxauth.model.common.User)1 AcrChangedException (org.gluu.oxauth.model.exception.AcrChangedException)1 JSONException (org.json.JSONException)1