Search in sources :

Example 1 with AccountException

use of javax.security.auth.login.AccountException in project cxf by apache.

the class WSDLGetAuthenticatorInterceptor method doAuthenticate.

public Subject doAuthenticate(final String username, final String password) {
    try {
        Subject subject = new Subject();
        LoginContext loginContext = new LoginContext(getContextName(), subject, new CallbackHandler() {

            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (int i = 0; i < callbacks.length; i++) {
                    if (callbacks[i] instanceof NameCallback) {
                        ((NameCallback) callbacks[i]).setName(username);
                    } else if (callbacks[i] instanceof PasswordCallback) {
                        ((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
                    } else {
                        throw new UnsupportedCallbackException(callbacks[i]);
                    }
                }
            }
        });
        loginContext.login();
        return subject;
    } catch (FailedLoginException e) {
        LOG.log(Level.FINE, "Login failed ", e);
        return null;
    } catch (AccountException e) {
        LOG.log(Level.WARNING, "Account failure ", e);
        return null;
    } catch (GeneralSecurityException e) {
        LOG.log(Level.SEVERE, "General Security Exception ", e);
        return null;
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) LoginContext(javax.security.auth.login.LoginContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) AccountException(javax.security.auth.login.AccountException) GeneralSecurityException(java.security.GeneralSecurityException) PasswordCallback(javax.security.auth.callback.PasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Subject(javax.security.auth.Subject)

Example 2 with AccountException

use of javax.security.auth.login.AccountException in project karaf by apache.

the class JaasSecurityProvider method doAuthenticate.

public Subject doAuthenticate(final String address, final String username, final String password) {
    try {
        Subject subject = new Subject();
        subject.getPrincipals().add(new ClientPrincipal("webconsole", address));
        LoginContext loginContext = new LoginContext(realm, subject, callbacks -> {
            for (Callback callback : callbacks) {
                if (callback instanceof NameCallback) {
                    ((NameCallback) callback).setName(username);
                } else if (callback instanceof PasswordCallback) {
                    ((PasswordCallback) callback).setPassword(password.toCharArray());
                } else {
                    throw new UnsupportedCallbackException(callback);
                }
            }
        });
        loginContext.login();
        if (role != null && role.length() > 0) {
            String clazz = "org.apache.karaf.jaas.boot.principal.RolePrincipal";
            String name = role;
            int idx = role.indexOf(':');
            if (idx > 0) {
                clazz = role.substring(0, idx);
                name = role.substring(idx + 1);
            }
            boolean found = false;
            for (Principal p : subject.getPrincipals()) {
                if (p.getClass().getName().equals(clazz) && p.getName().equals(name)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new FailedLoginException("User does not have the required role " + role);
            }
        }
        return subject;
    } catch (FailedLoginException e) {
        LOG.debug("Login failed", e);
        return null;
    } catch (AccountException e) {
        LOG.warn("Account failure", e);
        return null;
    } catch (GeneralSecurityException e) {
        LOG.error("General Security Exception", e);
        return null;
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) ClientPrincipal(org.apache.karaf.jaas.boot.principal.ClientPrincipal) Subject(javax.security.auth.Subject) LoginContext(javax.security.auth.login.LoginContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) AccountException(javax.security.auth.login.AccountException) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) ClientPrincipal(org.apache.karaf.jaas.boot.principal.ClientPrincipal) Principal(java.security.Principal)

Example 3 with AccountException

use of javax.security.auth.login.AccountException in project fabric8 by jboss-fuse.

the class JolokiaSecureHttpContext method doAuthenticate.

private Subject doAuthenticate(final String username, final String password) {
    try {
        Subject subject = new Subject();
        LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() {

            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (int i = 0; i < callbacks.length; i++) {
                    if (callbacks[i] instanceof NameCallback) {
                        ((NameCallback) callbacks[i]).setName(username);
                    } else if (callbacks[i] instanceof PasswordCallback) {
                        ((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
                    } else {
                        throw new UnsupportedCallbackException(callbacks[i]);
                    }
                }
            }
        });
        loginContext.login();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Login successful: {}", subject);
        }
        boolean found = false;
        for (String role : roles) {
            if (role != null && role.length() > 0 && !found) {
                String roleName = role.trim();
                int idx = roleName.indexOf(':');
                if (idx > 0) {
                    roleName = roleName.substring(idx + 1);
                }
                for (Principal p : subject.getPrincipals()) {
                    if (p.getName().equals(roleName)) {
                        found = true;
                        break;
                    }
                }
            }
        }
        if (!found) {
            throw new FailedLoginException("User does not have the required role " + Arrays.asList(roles));
        }
        return subject;
    } catch (AccountException e) {
        LOGGER.warn("Account failure", e);
        return null;
    } catch (LoginException e) {
        LOGGER.debug("Login failed", e);
        return null;
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) IOException(java.io.IOException) Subject(javax.security.auth.Subject) LoginContext(javax.security.auth.login.LoginContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) AccountException(javax.security.auth.login.AccountException) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Principal(java.security.Principal)

Example 4 with AccountException

use of javax.security.auth.login.AccountException in project fabric8 by jboss-fuse.

the class GitSecureHttpContext method doAuthenticate.

private Subject doAuthenticate(final String username, final String password) {
    try {
        Subject subject = new Subject();
        LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() {

            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (int i = 0; i < callbacks.length; i++) {
                    if (callbacks[i] instanceof NameCallback) {
                        ((NameCallback) callbacks[i]).setName(username);
                    } else if (callbacks[i] instanceof PasswordCallback) {
                        ((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
                    } else {
                        throw new UnsupportedCallbackException(callbacks[i]);
                    }
                }
            }
        });
        loginContext.login();
        boolean found = false;
        main: for (String role : roles) {
            if (role != null && role.length() > 0) {
                for (Principal p : subject.getPrincipals()) {
                    if (role.equals(p.getName()) || p instanceof Group && isGroupMember((Group) p, role)) {
                        found = true;
                        break main;
                    }
                }
            }
        }
        if (!found) {
            throw new FailedLoginException("User does not have any of the required roles: " + Arrays.asList(roles));
        }
        return subject;
    } catch (AccountException e) {
        LOGGER.debug("Account failure", e);
        return null;
    } catch (LoginException e) {
        LOGGER.debug("Login failed", e);
        return null;
    }
}
Also used : Group(java.security.acl.Group) CallbackHandler(javax.security.auth.callback.CallbackHandler) IOException(java.io.IOException) Subject(javax.security.auth.Subject) LoginContext(javax.security.auth.login.LoginContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) AccountException(javax.security.auth.login.AccountException) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Principal(java.security.Principal)

Example 5 with AccountException

use of javax.security.auth.login.AccountException in project tomee by apache.

the class ServiceListJAASAuthenticator method doAuthenticate.

public Subject doAuthenticate(final String username, final String password) {
    try {
        Subject subject = new Subject();
        LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() {

            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (int i = 0; i < callbacks.length; i++) {
                    if (callbacks[i] instanceof NameCallback) {
                        ((NameCallback) callbacks[i]).setName(username);
                    } else if (callbacks[i] instanceof PasswordCallback) {
                        ((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
                    } else {
                        throw new UnsupportedCallbackException(callbacks[i]);
                    }
                }
            }
        });
        loginContext.login();
        return subject;
    } catch (FailedLoginException e) {
        LOG.log(Level.FINE, "Login failed ", e);
        return null;
    } catch (AccountException e) {
        LOG.log(Level.WARNING, "Account failure ", e);
        return null;
    } catch (GeneralSecurityException e) {
        LOG.log(Level.SEVERE, "General Security Exception ", e);
        return null;
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) LoginContext(javax.security.auth.login.LoginContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) AccountException(javax.security.auth.login.AccountException) GeneralSecurityException(java.security.GeneralSecurityException) PasswordCallback(javax.security.auth.callback.PasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Subject(javax.security.auth.Subject)

Aggregations

NameCallback (javax.security.auth.callback.NameCallback)10 PasswordCallback (javax.security.auth.callback.PasswordCallback)10 AccountException (javax.security.auth.login.AccountException)10 Subject (javax.security.auth.Subject)8 Callback (javax.security.auth.callback.Callback)8 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)8 FailedLoginException (javax.security.auth.login.FailedLoginException)8 LoginContext (javax.security.auth.login.LoginContext)8 Principal (java.security.Principal)7 CallbackHandler (javax.security.auth.callback.CallbackHandler)7 IOException (java.io.IOException)6 GeneralSecurityException (java.security.GeneralSecurityException)5 LoginException (javax.security.auth.login.LoginException)4 UsernamePrincipal (com.thinkbiganalytics.security.UsernamePrincipal)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 Group (java.security.acl.Group)1 CredentialException (javax.security.auth.login.CredentialException)1 ClientPrincipal (org.apache.karaf.jaas.boot.principal.ClientPrincipal)1 DirContextOperations (org.springframework.ldap.core.DirContextOperations)1