Search in sources :

Example 1 with UserInfo

use of org.eclipse.jetty.jaas.spi.UserInfo in project KeyBox by skavanagh.

the class ExternalAuthUtil method login.

/**
 * external auth login method
 *
 * @return auth token if success
 * @auth authentication credentials
 */
public static String login(final Auth auth) {
    Connection con = null;
    String authToken = null;
    if (externalAuthEnabled && auth != null && StringUtils.isNotEmpty(auth.getUsername()) && StringUtils.isNotEmpty(auth.getPassword())) {
        try {
            // create login context
            LoginContext loginContext = new LoginContext(JAAS_MODULE, new CallbackHandler() {

                @Override
                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    for (Callback callback : callbacks) {
                        if (callback instanceof NameCallback) {
                            ((NameCallback) callback).setName(auth.getUsername());
                        } else if (callback instanceof ObjectCallback) {
                            ((ObjectCallback) callback).setObject(auth.getPassword().toCharArray());
                        } else if (callback instanceof PasswordCallback) {
                            ((PasswordCallback) callback).setPassword(auth.getPassword().toCharArray());
                        }
                    }
                }
            });
            // will throw exception if login fail
            loginContext.login();
            con = DBUtils.getConn();
            User user = AuthDB.getUserByUID(con, auth.getUsername());
            Field field = LoginContext.class.getDeclaredField("moduleStack");
            field.setAccessible(true);
            Object[] modules = (Object[]) field.get(loginContext);
            for (Object entry : modules) {
                field = entry.getClass().getDeclaredField("module");
                field.setAccessible(true);
                Object module = field.get(entry);
                field = entry.getClass().getDeclaredField("entry");
                field.setAccessible(true);
                AppConfigurationEntry appEntry = (AppConfigurationEntry) field.get(entry);
                if (module instanceof LdapLoginModule) {
                    // get callback handler
                    field = LoginContext.class.getDeclaredField("callbackHandler");
                    field.setAccessible(true);
                    CallbackHandler callbackHandler = (CallbackHandler) field.get(loginContext);
                    // get state
                    field = LoginContext.class.getDeclaredField("state");
                    field.setAccessible(true);
                    Map state = (Map) field.get(loginContext);
                    LdapLoginModule loginModule = (LdapLoginModule) module;
                    loginModule.initialize(loginContext.getSubject(), callbackHandler, state, appEntry.getOptions());
                    UserInfo userInfo = loginModule.getUserInfo(auth.getUsername());
                    // fetch assigned roles
                    userInfo.fetchRoles();
                    // dir context context
                    field = loginModule.getClass().getDeclaredField("_rootContext");
                    field.setAccessible(true);
                    DirContext dirContext = (DirContext) field.get(loginModule);
                    // role name attribute
                    field = loginModule.getClass().getDeclaredField("_roleNameAttribute");
                    field.setAccessible(true);
                    String roleNameAttribute = (String) field.get(loginModule);
                    // base dn for role
                    field = loginModule.getClass().getDeclaredField("_roleBaseDn");
                    field.setAccessible(true);
                    String roleBaseDn = (String) field.get(loginModule);
                    // role object class
                    field = loginModule.getClass().getDeclaredField("_roleObjectClass");
                    field.setAccessible(true);
                    String roleObjectClass = (String) field.get(loginModule);
                    // all attributes for user
                    field = LdapLoginModule.LDAPUserInfo.class.getDeclaredField("attributes");
                    field.setAccessible(true);
                    Attributes userAttributes = (Attributes) field.get(userInfo);
                    List<String> allRoles = getAllRoles(dirContext, roleBaseDn, roleNameAttribute, roleObjectClass);
                    if (user == null) {
                        user = new User();
                        user.setUserType(User.ADMINISTRATOR);
                        user.setUsername(auth.getUsername());
                        // set attributes from ldap
                        String givenName = userAttributes.get("givenName") != null ? (String) userAttributes.get("givenName").get() : null;
                        String sn = userAttributes.get("sn") != null ? (String) userAttributes.get("sn").get() : null;
                        String displayName = userAttributes.get("displayName") != null ? (String) userAttributes.get("displayName").get() : null;
                        String cn = userAttributes.get("cn") != null ? (String) userAttributes.get("cn").get() : null;
                        String email = userAttributes.get("mail") != null ? (String) userAttributes.get("mail").get() : null;
                        if (StringUtils.isNotEmpty(givenName) && StringUtils.isNotEmpty(sn)) {
                            user.setFirstNm(givenName);
                            user.setLastNm(sn);
                        } else if (StringUtils.isNotEmpty(displayName) && displayName.contains(" ")) {
                            String[] name = displayName.split(" ");
                            if (name.length > 1) {
                                user.setFirstNm(name[0]);
                                user.setLastNm(name[name.length - 1]);
                            }
                        } else if (StringUtils.isNotEmpty(cn) && cn.contains(" ")) {
                            String[] name = cn.split(" ");
                            if (name.length > 1) {
                                user.setFirstNm(name[0]);
                                user.setLastNm(name[name.length - 1]);
                            }
                        }
                        // set email
                        if (StringUtils.isNotEmpty(email)) {
                            user.setEmail(email);
                        } else if (auth.getUsername().contains("@")) {
                            user.setEmail(auth.getUsername());
                        }
                        user.setId(UserDB.insertUser(con, user));
                    }
                    // assign profiles for user
                    UserProfileDB.assignProfilesToUser(con, user.getId(), allRoles, userInfo.getRoleNames());
                    dirContext.close();
                    loginModule.commit();
                } else {
                    Subject subject = loginContext.getSubject();
                    if (user == null) {
                        user = new User();
                        user.setUserType(User.ADMINISTRATOR);
                        user.setUsername(auth.getUsername());
                        // if it looks like name is returned default it
                        for (Principal p : subject.getPrincipals()) {
                            if (p.getName().contains(" ")) {
                                String[] name = p.getName().split(" ");
                                if (name.length > 1) {
                                    user.setFirstNm(name[0]);
                                    user.setLastNm(name[name.length - 1]);
                                }
                            }
                        }
                        // set email
                        if (auth.getUsername().contains("@")) {
                            user.setEmail(auth.getUsername());
                        }
                        user.setId(UserDB.insertUser(con, user));
                    }
                }
                if (StringUtils.isNotEmpty(DEFAULT_LDAP_PROFILE)) {
                    UserProfileDB.assignProfileToUser(con, user.getId(), DEFAULT_LDAP_PROFILE);
                }
                authToken = UUID.randomUUID().toString();
                user.setAuthToken(authToken);
                user.setAuthType(Auth.AUTH_EXTERNAL);
                // set auth token
                AuthDB.updateLogin(con, user);
            }
            DBUtils.closeConn(con);
        } catch (LoginException le) {
            authToken = null;
            log.debug(le.toString(), le);
        } catch (Exception ex) {
            authToken = null;
            log.error(ex.toString(), ex);
        }
    }
    return authToken;
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) User(io.bastillion.manage.model.User) Attributes(javax.naming.directory.Attributes) UserInfo(org.eclipse.jetty.jaas.spi.UserInfo) DirContext(javax.naming.directory.DirContext) LdapLoginModule(org.eclipse.jetty.jaas.spi.LdapLoginModule) Field(java.lang.reflect.Field) LoginContext(javax.security.auth.login.LoginContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) ObjectCallback(org.eclipse.jetty.jaas.callback.ObjectCallback) Connection(java.sql.Connection) IOException(java.io.IOException) Subject(javax.security.auth.Subject) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) NamingException(javax.naming.NamingException) IOException(java.io.IOException) AppConfigurationEntry(javax.security.auth.login.AppConfigurationEntry) ObjectCallback(org.eclipse.jetty.jaas.callback.ObjectCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) LoginException(javax.security.auth.login.LoginException) Map(java.util.Map) Principal(java.security.Principal)

Aggregations

User (io.bastillion.manage.model.User)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Principal (java.security.Principal)1 Connection (java.sql.Connection)1 Map (java.util.Map)1 NamingException (javax.naming.NamingException)1 Attributes (javax.naming.directory.Attributes)1 DirContext (javax.naming.directory.DirContext)1 Subject (javax.security.auth.Subject)1 Callback (javax.security.auth.callback.Callback)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 NameCallback (javax.security.auth.callback.NameCallback)1 PasswordCallback (javax.security.auth.callback.PasswordCallback)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 AppConfigurationEntry (javax.security.auth.login.AppConfigurationEntry)1 LoginContext (javax.security.auth.login.LoginContext)1 LoginException (javax.security.auth.login.LoginException)1 ObjectCallback (org.eclipse.jetty.jaas.callback.ObjectCallback)1 LdapLoginModule (org.eclipse.jetty.jaas.spi.LdapLoginModule)1