use of javax.security.auth.callback.UnsupportedCallbackException in project jackrabbit by apache.
the class CallbackHandlerImpl method handle.
/**
* @param callbacks
* @throws IOException
* @throws UnsupportedCallbackException
* @see CallbackHandler#handle(Callback[])
*/
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof CredentialsCallback) {
((CredentialsCallback) callback).setCredentials(credentials);
} else if (callback instanceof RepositoryCallback) {
/*
if callback handler has been created with null session or
null principalProviderRegistry this handler cannot properly
deal with RepositoryCallback
*/
if (session == null || principalProviderRegistry == null) {
throw new UnsupportedCallbackException(callback);
}
RepositoryCallback rcb = (RepositoryCallback) callback;
rcb.setSession(session);
rcb.setPrincipalProviderRegistry(principalProviderRegistry);
rcb.setAdminId(adminId);
rcb.setAnonymousId(anonymousId);
} else if (credentials != null && credentials instanceof SimpleCredentials) {
SimpleCredentials simpleCreds = (SimpleCredentials) credentials;
if (callback instanceof NameCallback) {
String userId = simpleCreds.getUserID();
((NameCallback) callback).setName(userId);
} else if (callback instanceof PasswordCallback) {
char[] pw = simpleCreds.getPassword();
((PasswordCallback) callback).setPassword(pw);
} else if (callback instanceof ImpersonationCallback) {
Object impersAttr = simpleCreds.getAttribute(SecurityConstants.IMPERSONATOR_ATTRIBUTE);
((ImpersonationCallback) callback).setImpersonator(impersAttr);
} else {
throw new UnsupportedCallbackException(callback);
}
} else {
throw new UnsupportedCallbackException(callback);
}
}
}
use of javax.security.auth.callback.UnsupportedCallbackException in project zm-mailbox by Zimbra.
the class OAuth2SaslClient method evaluateChallenge.
public byte[] evaluateChallenge(byte[] challenge) throws SaslException {
if (isComplete) {
// Empty final response from server, just ignore it.
return new byte[] {};
}
NameCallback nameCallback = new NameCallback("Enter name");
Callback[] callbacks = new Callback[] { nameCallback };
try {
callbackHandler.handle(callbacks);
} catch (UnsupportedCallbackException e) {
throw new SaslException("Unsupported callback: " + e);
} catch (IOException e) {
throw new SaslException("Failed to execute callback: " + e);
}
String username = nameCallback.getName();
byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", username, oauthToken).getBytes();
isComplete = true;
return response;
}
use of javax.security.auth.callback.UnsupportedCallbackException 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;
}
use of javax.security.auth.callback.UnsupportedCallbackException in project mongo-java-driver by mongodb.
the class PlainAuthenticator method createSaslClient.
@Override
protected SaslClient createSaslClient(final ServerAddress serverAddress) {
final MongoCredential credential = getMongoCredential();
isTrue("mechanism is PLAIN", credential.getAuthenticationMechanism() == PLAIN);
try {
return Sasl.createSaslClient(new String[] { PLAIN.getMechanismName() }, credential.getUserName(), DEFAULT_PROTOCOL, serverAddress.getHost(), null, new CallbackHandler() {
@Override
public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (final Callback callback : callbacks) {
if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword(credential.getPassword());
} else if (callback instanceof NameCallback) {
((NameCallback) callback).setName(credential.getUserName());
}
}
}
});
} catch (SaslException e) {
throw new MongoSecurityException(credential, "Exception initializing SASL client", e);
}
}
use of javax.security.auth.callback.UnsupportedCallbackException in project j2objc by google.
the class myCallback method testUnsupportedCallbackException04.
/**
* javax.security.auth.callback.UnsupportedCallbackExceptionTest#UnsupportedCallbackException(Callback callback, String msg)
* Assertion: constructs with null callback parameter and not null message.
*/
public void testUnsupportedCallbackException04() {
UnsupportedCallbackException ucE;
for (int i = 0; i < msgs.length; i++) {
ucE = new UnsupportedCallbackException(null, msgs[i]);
assertEquals("getMessage() must return: ".concat(msgs[i]), ucE.getMessage(), msgs[i]);
assertNull("getCallback() must return null.", ucE.getCallback());
}
}
Aggregations