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;
}
Aggregations