use of javax.security.auth.callback.NameCallback in project OpenAM by OpenRock.
the class RestAuthNameCallbackHandlerTest method shouldHandleCallback.
@Test
public void shouldHandleCallback() {
//Given
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
JsonValue jsonPostBody = mock(JsonValue.class);
NameCallback originalNameCallback = mock(NameCallback.class);
//When
NameCallback nameCallback = restAuthNameCallbackHandler.handle(request, response, jsonPostBody, originalNameCallback);
//Then
assertEquals(originalNameCallback, nameCallback);
}
use of javax.security.auth.callback.NameCallback in project OpenAM by OpenRock.
the class RestAuthNameCallbackHandlerTest method shouldUpdateCallbackFromRequest.
@Test
public void shouldUpdateCallbackFromRequest() throws RestAuthResponseException, RestAuthException {
//Given
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
NameCallback nameCallback = mock(NameCallback.class);
given(request.getHeader("X-OpenAM-Username")).willReturn("USERNAME");
//When
boolean updated = restAuthNameCallbackHandler.updateCallbackFromRequest(request, response, nameCallback);
//Then
verify(nameCallback).setName("USERNAME");
assertTrue(updated);
}
use of javax.security.auth.callback.NameCallback in project OpenAM by OpenRock.
the class RestAuthNameCallbackHandlerTest method shouldFailToConvertFromJsonWithInvalidType.
@Test(expectedExceptions = RestAuthException.class)
public void shouldFailToConvertFromJsonWithInvalidType() throws RestAuthException {
//Given
NameCallback nameCallback = new NameCallback("Enter username:");
JsonValue jsonNameCallback = JsonValueBuilder.jsonValue().array("input").addLast(JsonValueBuilder.jsonValue().put("value", "USERNAME").build()).array("output").addLast(JsonValueBuilder.jsonValue().put("value", "Enter username:").build()).put("type", "PasswordCallback").build();
//When
restAuthNameCallbackHandler.convertFromJson(nameCallback, jsonNameCallback);
//Then
fail();
}
use of javax.security.auth.callback.NameCallback in project OpenAM by OpenRock.
the class IdServicesImpl method authenticate.
/**
* Returns <code>true</code> if the data store has successfully
* authenticated the identity with the provided credentials. In case the
* data store requires additional credentials, the list would be returned
* via the <code>IdRepoException</code> exception.
*
* @param orgName
* realm name to which the identity would be authenticated
* @param credentials
* Array of callback objects containing information such as
* username and password.
*
* @return <code>true</code> if data store authenticates the identity;
* else <code>false</code>
*/
public boolean authenticate(String orgName, Callback[] credentials) throws IdRepoException, AuthLoginException {
if (DEBUG.messageEnabled()) {
DEBUG.message("IdServicesImpl.authenticate: called for org: " + orgName);
}
IdRepoException firstException = null;
AuthLoginException authException = null;
// Get the list of plugins and check if they support authN
Set cPlugins = null;
try {
cPlugins = idrepoCache.getIdRepoPlugins(orgName);
} catch (SSOException ex) {
// Debug the message and return false
if (DEBUG.messageEnabled()) {
DEBUG.message("IdServicesImpl.authenticate: " + "Error obtaining " + "IdRepo plugins for the org: " + orgName);
}
return (false);
} catch (IdRepoException ex) {
// Debug the message and return false
if (DEBUG.messageEnabled()) {
DEBUG.message("IdServicesImpl.authenticate: " + "Error obtaining " + "IdRepo plugins for the org: " + orgName);
}
return (false);
}
// Check for internal user. If internal user, use SpecialRepo only
String name = null;
for (int i = 0; i < credentials.length; i++) {
if (credentials[i] instanceof NameCallback) {
name = ((NameCallback) credentials[i]).getName();
if (LDAPUtils.isDN(name)) {
// Obtain the firsr RDN
name = LDAPUtils.rdnValueFromDn(name);
}
break;
}
}
SSOToken token = (SSOToken) AccessController.doPrivileged(AdminTokenAction.getInstance());
try {
if ((name != null) && isSpecialIdentity(token, name, IdType.USER, orgName)) {
for (Iterator tis = cPlugins.iterator(); tis.hasNext(); ) {
IdRepo idRepo = (IdRepo) tis.next();
if (idRepo.getClass().getName().equals(IdConstants.SPECIAL_PLUGIN)) {
if (idRepo.authenticate(credentials)) {
if (DEBUG.messageEnabled()) {
DEBUG.message("IdServicesImpl.authenticate: " + "AuthN success using special repo " + idRepo.getClass().getName() + " user: " + name);
}
return (true);
} else {
// Invalid password used for internal user
DEBUG.error("IdServicesImpl.authenticate: " + "AuthN failed using special repo " + idRepo.getClass().getName() + " user: " + name);
return (false);
}
}
}
}
} catch (SSOException ssoe) {
// Ignore the exception
DEBUG.error("IdServicesImpl.authenticate: AuthN failed " + "checking for special users", ssoe);
return (false);
}
for (Iterator items = cPlugins.iterator(); items.hasNext(); ) {
IdRepo idRepo = (IdRepo) items.next();
if (idRepo.supportsAuthentication()) {
if (DEBUG.messageEnabled()) {
DEBUG.message("IdServicesImpl.authenticate: " + "AuthN to " + idRepo.getClass().getName() + " in org: " + orgName);
}
try {
if (idRepo.authenticate(credentials)) {
// Successfully authenticated
if (DEBUG.messageEnabled()) {
DEBUG.message("IdServicesImpl.authenticate: " + "AuthN success for " + idRepo.getClass().getName());
}
return (true);
}
} catch (IdRepoException ide) {
// all authentication calls fail
if (firstException == null) {
firstException = ide;
}
} catch (AuthLoginException authex) {
if (authException == null) {
authException = authex;
}
}
} else if (DEBUG.messageEnabled()) {
DEBUG.message("IdServicesImpl.authenticate: AuthN " + "not supported by " + idRepo.getClass().getName());
}
}
if (authException != null) {
throw (authException);
}
if (firstException != null) {
throw (firstException);
}
return (false);
}
use of javax.security.auth.callback.NameCallback in project OpenAM by OpenRock.
the class AuthUtils method authenticate.
public static SSOToken authenticate(String realm, String userName, String password) throws Exception {
AuthContext lc = new AuthContext(realm);
lc.login();
while (lc.hasMoreRequirements()) {
Callback[] callbacks = lc.getRequirements();
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
NameCallback nc = (NameCallback) callbacks[i];
nc.setName(userName);
} else if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callbacks[i];
pc.setPassword(password.toCharArray());
} else {
throw new Exception("No callback");
}
}
lc.submitRequirements(callbacks);
}
return (lc.getStatus() != AuthContext.Status.SUCCESS) ? null : lc.getSSOToken();
}
Aggregations