Search in sources :

Example 11 with Account

use of io.undertow.security.idm.Account 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 12 with Account

use of io.undertow.security.idm.Account in project undertow by undertow-io.

the class SecurityContextImpl method logout.

@Override
public void logout() {
    Account authenticatedAccount = getAuthenticatedAccount();
    if (authenticatedAccount != null) {
        UndertowLogger.SECURITY_LOGGER.debugf("Logging out user %s for %s", authenticatedAccount.getPrincipal().getName(), exchange);
    } else {
        UndertowLogger.SECURITY_LOGGER.debugf("Logout called with no authenticated user in exchange %s", exchange);
    }
    super.logout();
    this.authenticationState = AuthenticationState.NOT_ATTEMPTED;
}
Also used : Account(io.undertow.security.idm.Account)

Example 13 with Account

use of io.undertow.security.idm.Account in project undertow by undertow-io.

the class SecurityContextImpl method login.

@Override
public boolean login(final String username, final String password) {
    UndertowLogger.SECURITY_LOGGER.debugf("Attempting programatic login for user %s for request %s", username, exchange);
    final Account account;
    if (System.getSecurityManager() == null) {
        account = identityManager.verify(username, new PasswordCredential(password.toCharArray()));
    } else {
        account = AccessController.doPrivileged(new PrivilegedAction<Account>() {

            @Override
            public Account run() {
                return identityManager.verify(username, new PasswordCredential(password.toCharArray()));
            }
        });
    }
    if (account == null) {
        return false;
    }
    authenticationComplete(account, programaticMechName, true);
    this.authenticationState = AuthenticationState.AUTHENTICATED;
    return true;
}
Also used : Account(io.undertow.security.idm.Account) PrivilegedAction(java.security.PrivilegedAction) PasswordCredential(io.undertow.security.idm.PasswordCredential)

Example 14 with Account

use of io.undertow.security.idm.Account in project undertow by undertow-io.

the class HttpServletRequestImpl method isUserInRole.

@Override
public boolean isUserInRole(final String role) {
    if (role == null) {
        return false;
    }
    // according to the servlet spec this aways returns false
    if (role.equals("*")) {
        return false;
    }
    SecurityContext sc = exchange.getSecurityContext();
    Account account = sc != null ? sc.getAuthenticatedAccount() : null;
    if (account == null) {
        return false;
    }
    ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (role.equals("**")) {
        Set<String> roles = servletRequestContext.getDeployment().getDeploymentInfo().getSecurityRoles();
        if (!roles.contains("**")) {
            return true;
        }
    }
    final ServletChain servlet = servletRequestContext.getCurrentServlet();
    final Deployment deployment = servletContext.getDeployment();
    final AuthorizationManager authorizationManager = deployment.getDeploymentInfo().getAuthorizationManager();
    return authorizationManager.isUserInRole(role, account, servlet.getManagedServlet().getServletInfo(), this, deployment);
}
Also used : Account(io.undertow.security.idm.Account) ServletChain(io.undertow.servlet.handlers.ServletChain) SecurityContext(io.undertow.security.api.SecurityContext) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) Deployment(io.undertow.servlet.api.Deployment) HttpString(io.undertow.util.HttpString) AuthorizationManager(io.undertow.servlet.api.AuthorizationManager)

Example 15 with Account

use of io.undertow.security.idm.Account in project undertow by undertow-io.

the class HttpServletRequestImpl method getUserPrincipal.

@Override
public Principal getUserPrincipal() {
    SecurityContext securityContext = exchange.getSecurityContext();
    Principal result = null;
    Account account = null;
    if (securityContext != null && (account = securityContext.getAuthenticatedAccount()) != null) {
        result = account.getPrincipal();
    }
    return result;
}
Also used : Account(io.undertow.security.idm.Account) SecurityContext(io.undertow.security.api.SecurityContext) Principal(java.security.Principal)

Aggregations

Account (io.undertow.security.idm.Account)28 IdentityManager (io.undertow.security.idm.IdentityManager)8 AuthenticatedSession (io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession)6 Test (org.junit.Test)6 PasswordCredential (io.undertow.security.idm.PasswordCredential)5 HashMap (java.util.HashMap)5 BatchContext (org.wildfly.clustering.ee.BatchContext)5 SecurityContext (io.undertow.security.api.SecurityContext)4 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)4 CachedAuthenticatedSessionHandler (io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler)4 Map (java.util.Map)4 Batch (org.wildfly.clustering.ee.Batch)4 Credential (io.undertow.security.idm.Credential)3 IOException (java.io.IOException)3 AuthenticatedSessionManager (io.undertow.security.api.AuthenticatedSessionManager)2 DigestAlgorithm (io.undertow.security.idm.DigestAlgorithm)2 Session (io.undertow.server.session.Session)2 HttpString (io.undertow.util.HttpString)2 ByteBuffer (java.nio.ByteBuffer)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2