Search in sources :

Example 6 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 7 with AccountException

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

the class HttpBasicServer 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();
        logger.debug("Login successful: {}", subject.toString());
        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()) {
                    logger.debug("Principal found in real: {}", p.getName());
                    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.getMessage());
        return null;
    } catch (LoginException e) {
        logger.debug("Login failed {}", e.getMessage());
        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 8 with AccountException

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

the class MavenSecureHttpContext 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();
        boolean found = false;
        for (String role : roles) {
            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);
                }
                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 " + roles);
        }
        return subject;
    } catch (AccountException e) {
        LOGGER.warn("Account failure", e);
        return null;
    } catch (LoginException e) {
        LOGGER.debug("Login failed", e);
        return null;
    } catch (GeneralSecurityException e) {
        LOGGER.error("General Security Exception", e);
        return null;
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) GeneralSecurityException(java.security.GeneralSecurityException) 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 9 with AccountException

use of javax.security.auth.login.AccountException in project kylo by Teradata.

the class LdapLoginModule method doLogin.

/* (non-Javadoc)
     * @see com.thinkbiganalytics.auth.jaas.AbstractLoginModule#doLogin()
     */
@Override
protected boolean doLogin() throws Exception {
    final NameCallback nameCallback = new NameCallback("Username: ");
    final PasswordCallback passwordCallback = new PasswordCallback("Password: ", false);
    handle(nameCallback, passwordCallback);
    if (nameCallback.getName() == null) {
        throw new AccountException("No username provided for authentication");
    }
    Principal userPrincipal = new UsernamePrincipal(nameCallback.getName());
    String password = new String(passwordCallback.getPassword());
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userPrincipal, password);
    try {
        log.debug("Authenticating: {}", userPrincipal);
        DirContextOperations dirContext = this.authenticator.authenticate(authentication);
        log.debug("Successfully Authenticated: {}", userPrincipal);
        setUserPrincipal(userPrincipal);
        for (GrantedAuthority grant : this.authoritiesPopulator.getGrantedAuthorities(dirContext, nameCallback.getName())) {
            String groupName = grant.getAuthority();
            log.debug("Found group for {}: {}", userPrincipal, groupName);
            if (groupName != null) {
                addNewGroupPrincipal(groupName);
            }
        }
        return true;
    } catch (BadCredentialsException e) {
        throw new CredentialException(e.getMessage());
    }
}
Also used : UsernamePrincipal(com.thinkbiganalytics.security.UsernamePrincipal) NameCallback(javax.security.auth.callback.NameCallback) AccountException(javax.security.auth.login.AccountException) DirContextOperations(org.springframework.ldap.core.DirContextOperations) CredentialException(javax.security.auth.login.CredentialException) GrantedAuthority(org.springframework.security.core.GrantedAuthority) PasswordCallback(javax.security.auth.callback.PasswordCallback) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UsernamePrincipal(com.thinkbiganalytics.security.UsernamePrincipal) Principal(java.security.Principal)

Example 10 with AccountException

use of javax.security.auth.login.AccountException in project cxf 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 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 == null ? null : 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) 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