use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.
the class RestAuthCallbackHandlerManagerTest method shouldHandleJsonCallbacks.
@Test
public void shouldHandleJsonCallbacks() throws RestAuthException {
//Given
Callback callback1 = mock(Callback.class);
Callback callback2 = mock(Callback.class);
Callback[] callbacks = new Callback[] { callback1, callback2 };
RestAuthCallbackHandler restAuthCallbackHandler1 = mock(RestAuthCallbackHandler.class);
RestAuthCallbackHandler restAuthCallbackHandler2 = mock(RestAuthCallbackHandler.class);
JsonValue jsonCallback1 = mock(JsonValue.class);
JsonValue jsonCallback2 = mock(JsonValue.class);
JsonValue jsonCallbacks = mock(JsonValue.class);
JsonValue jsonCallback1Type = mock(JsonValue.class);
JsonValue jsonCallback2Type = mock(JsonValue.class);
given(jsonCallbacks.size()).willReturn(2);
given(jsonCallbacks.get(0)).willReturn(jsonCallback1);
given(jsonCallbacks.get(1)).willReturn(jsonCallback2);
given(restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(Matchers.<Class<? extends Callback>>anyObject())).willReturn(restAuthCallbackHandler1).willReturn(restAuthCallbackHandler2);
given(restAuthCallbackHandler1.getCallbackClassName()).willReturn("CALLBACK1");
given(restAuthCallbackHandler2.getCallbackClassName()).willReturn("CALLBACK2");
given(jsonCallback1.get("type")).willReturn(jsonCallback1Type);
given(jsonCallback2.get("type")).willReturn(jsonCallback2Type);
given(jsonCallback1Type.asString()).willReturn("CALLBACK1");
given(jsonCallback2Type.asString()).willReturn("CALLBACK2");
given(restAuthCallbackHandler1.convertFromJson(callback1, jsonCallback1)).willReturn(callback1);
given(restAuthCallbackHandler2.convertFromJson(callback2, jsonCallback2)).willReturn(callback2);
//When
Callback[] originalCallbacks = restAuthCallbackHandlerManager.handleJsonCallbacks(callbacks, jsonCallbacks);
//Then
verify(restAuthCallbackHandler1).convertFromJson(callback1, jsonCallback1);
verify(restAuthCallbackHandler2).convertFromJson(callback2, jsonCallback2);
assertEquals(originalCallbacks.length, 2);
assertEquals(originalCallbacks[0], callback1);
assertEquals(originalCallbacks[1], callback2);
}
use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.
the class RestAuthenticationHandlerTest method shouldInitiateAuthenticationViaGET4.
@Test
public void shouldInitiateAuthenticationViaGET4() throws AuthLoginException, L10NMessageImpl, JSONException, IOException, RestAuthResponseException, SignatureException, RestAuthException {
//Given
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse httpResponse = mock(HttpServletResponse.class);
String authIndexType = null;
String indexValue = null;
String sessionUpgradeSSOTokenId = null;
Callback[] callbacks = new Callback[0];
AuthContextLocalWrapper authContextLocalWrapper = mock(AuthContextLocalWrapper.class);
LoginProcess loginProcess = mock(LoginProcess.class);
given(loginProcess.getLoginStage()).willReturn(LoginStage.REQUIREMENTS_WAITING);
given(loginProcess.getCallbacks()).willReturn(callbacks);
given(loginProcess.getAuthContext()).willReturn(authContextLocalWrapper);
PagePropertiesCallback pagePropertiesCallback = mock(PagePropertiesCallback.class);
given(pagePropertiesCallback.getTemplateName()).willReturn("TEMPLATE_NAME");
given(pagePropertiesCallback.getModuleName()).willReturn("MODULE_NAME");
given(pagePropertiesCallback.getPageState()).willReturn("PAGE_STATE");
JsonValue jsonCallbacks = new JsonValue(new HashMap<String, Object>());
jsonCallbacks.add("KEY", "VALUE");
Map<String, String> responseHeaders = new HashMap<String, String>();
responseHeaders.put("HEADER_KEY", "HEADER_VALUE");
JsonValue jsonResponse = new JsonValue(new HashMap<String, Object>());
jsonResponse.add("KEY", "VALUE");
RestAuthResponseException restAuthResponseException = new RestAuthResponseException(999, responseHeaders, jsonResponse);
given(loginAuthenticator.getLoginProcess(Matchers.<LoginConfiguration>anyObject())).willReturn(loginProcess);
given(restAuthCallbackHandlerManager.handleCallbacks(request, httpResponse, callbacks)).willThrow(restAuthResponseException);
given(authIdHelper.createAuthId(Matchers.<LoginConfiguration>anyObject(), eq(authContextLocalWrapper))).willReturn("AUTH_ID");
//When
try {
restAuthenticationHandler.initiateAuthentication(request, httpResponse, authIndexType, indexValue, sessionUpgradeSSOTokenId);
} catch (RestAuthResponseException e) {
JsonValue response = e.getJsonResponse();
assertEquals(response.size(), 2);
assertEquals(response.get("authId").asString(), "AUTH_ID");
assertEquals(response.get("KEY").asString(), "VALUE");
Map<String, String> headers = e.getResponseHeaders();
assertEquals(headers.get("HEADER_KEY"), "HEADER_VALUE");
assertEquals(e.getStatusCode(), 999);
return;
}
//Then
fail();
}
use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.
the class RestAuthenticationHandlerTest method shouldInitiateAuthenticationViaGET2.
@Test
public void shouldInitiateAuthenticationViaGET2() throws AuthLoginException, L10NMessageImpl, JSONException, IOException, RestAuthResponseException, SignatureException, RestAuthException {
//Given
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse httpResponse = mock(HttpServletResponse.class);
String authIndexType = null;
String indexValue = null;
String sessionUpgradeSSOTokenId = null;
Callback callbackOne = mock(Callback.class);
Callback callbackTwo = mock(Callback.class);
Callback[] callbacks = new Callback[] { callbackOne, callbackTwo };
PagePropertiesCallback pagePropertiesCallback = mock(PagePropertiesCallback.class);
given(pagePropertiesCallback.getTemplateName()).willReturn("TEMPLATE_NAME");
given(pagePropertiesCallback.getModuleName()).willReturn("MODULE_NAME");
given(pagePropertiesCallback.getPageState()).willReturn("PAGE_STATE");
given(pagePropertiesCallback.getHeader()).willReturn("HEADER");
AuthContextLocalWrapper authContextLocalWrapper = mock(AuthContextLocalWrapper.class);
LoginProcess loginProcess = mock(LoginProcess.class);
given(loginProcess.getLoginStage()).willReturn(LoginStage.REQUIREMENTS_WAITING);
given(loginProcess.getCallbacks()).willReturn(callbacks);
given(loginProcess.getPagePropertiesCallback()).willReturn(pagePropertiesCallback);
given(loginProcess.getAuthContext()).willReturn(authContextLocalWrapper);
JsonValue jsonCallbacks = new JsonValue(new HashMap<String, Object>());
jsonCallbacks.add("KEY", "VALUE");
given(loginAuthenticator.getLoginProcess(Matchers.<LoginConfiguration>anyObject())).willReturn(loginProcess);
given(restAuthCallbackHandlerManager.handleCallbacks(request, httpResponse, callbacks)).willReturn(jsonCallbacks);
given(authIdHelper.createAuthId(Matchers.<LoginConfiguration>anyObject(), eq(authContextLocalWrapper))).willReturn("AUTH_ID");
//When
JsonValue response = restAuthenticationHandler.initiateAuthentication(request, httpResponse, authIndexType, indexValue, sessionUpgradeSSOTokenId);
//Then
assertEquals(response.size(), 5);
assertEquals(response.get("authId").asString(), "AUTH_ID");
assertEquals(response.get("template").asString(), "TEMPLATE_NAME");
assertEquals(response.get("stage").asString(), "MODULE_NAMEPAGE_STATE");
assertEquals(response.get("header").asString(), "HEADER");
assertEquals(response.get("callbacks").get("KEY").asString(), "VALUE");
}
use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.
the class SAML2 method initiateSAMLLoginAtIDP.
/**
* Performs similar to SPSSOFederate.initiateAuthnRequest by returning to the next auth stage
* with a redirect (either GET or POST depending on the config) which triggers remote IdP authentication.
*/
private int initiateSAMLLoginAtIDP(final HttpServletResponse response, final HttpServletRequest request) throws SAML2Exception, AuthLoginException {
if (reqBinding == null) {
reqBinding = SAML2Constants.HTTP_REDIRECT;
}
final String spEntityID = SPSSOFederate.getSPEntityId(metaAlias);
final IDPSSODescriptorElement idpsso = SPSSOFederate.getIDPSSOForAuthnReq(realm, entityName);
final SPSSODescriptorElement spsso = SPSSOFederate.getSPSSOForAuthnReq(realm, spEntityID);
if (idpsso == null || spsso == null) {
return processError(bundle.getString("samlLocalConfigFailed"), "SAML2 :: initiateSAMLLoginAtIDP() : {}", bundle.getString("samlLocalConfigFailed"));
}
final String ssoURL = SPSSOFederate.getSSOURL(idpsso.getSingleSignOnService(), reqBinding);
final List extensionsList = SPSSOFederate.getExtensionsList(spEntityID, realm);
final Map<String, Collection<String>> spConfigAttrsMap = SPSSOFederate.getAttrsMapForAuthnReq(realm, spEntityID);
authnRequest = SPSSOFederate.createAuthnRequest(realm, spEntityID, params, spConfigAttrsMap, extensionsList, spsso, idpsso, ssoURL, false);
final AuthnRequestInfo reqInfo = new AuthnRequestInfo(request, response, realm, spEntityID, null, authnRequest, null, params);
synchronized (SPCache.requestHash) {
SPCache.requestHash.put(authnRequest.getID(), reqInfo);
}
saveAuthnRequest(authnRequest, reqInfo);
final Callback[] nextCallbacks = getCallback(REDIRECT);
final RedirectCallback redirectCallback = (RedirectCallback) nextCallbacks[0];
setCookiesForRedirects(request, response);
//we only handle Redirect and POST
if (SAML2Constants.HTTP_POST.equals(reqBinding)) {
final String postMsg = SPSSOFederate.getPostBindingMsg(idpsso, spsso, spConfigAttrsMap, authnRequest);
configurePostRedirectCallback(postMsg, ssoURL, redirectCallback);
} else {
final String authReqXMLString = authnRequest.toXMLString(true, true);
final String redirectUrl = SPSSOFederate.getRedirect(authReqXMLString, null, ssoURL, idpsso, spsso, spConfigAttrsMap);
configureGetRedirectCallback(redirectUrl, redirectCallback);
}
return REDIRECT;
}
use of javax.security.auth.callback.Callback 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