use of org.forgerock.openam.core.rest.authn.core.LoginProcess in project OpenAM by OpenRock.
the class RestAuthenticationHandler method authenticate.
/**
* Handles either the creation or retrieval of the Login Process, dependent on if the request is a new
* authentication request or a continuation of one.
*
* @param request The HttpServletRequest.
* @param response The HttpServletResponse.
* @param postBody The post body of the request.
* @param authIndexType The authentication index type.
* @param indexValue The authentication index value.
* @param sessionUpgradeSSOTokenId The SSO Token Id of the user's current session, null if not performing a session
* upgrade.
* @return The Response of the authentication request.
*/
private JsonValue authenticate(HttpServletRequest request, HttpServletResponse response, JsonValue postBody, String authIndexType, String indexValue, String sessionUpgradeSSOTokenId) throws RestAuthException {
LoginProcess loginProcess = null;
try {
AuthIndexType indexType = getAuthIndexType(authIndexType);
String authId = null;
String sessionId = null;
if (postBody != null) {
authId = getAuthId(postBody);
if (authId != null) {
SignedJwt jwt = authIdHelper.reconstructAuthId(authId);
sessionId = getSessionId(jwt);
indexType = getAuthIndexType(jwt);
indexValue = getAuthIndexValue(jwt);
String realmDN = getRealmDomainName(jwt);
AuditRequestContext.putProperty(SESSION_ID, sessionId);
authIdHelper.verifyAuthId(realmDN, authId);
}
}
LoginConfiguration loginConfiguration = new LoginConfiguration().httpRequest(request).httpResponse(response).indexType(indexType).indexValue(indexValue).sessionId(sessionId).forceAuth(request.getParameter(AuthUtils.FORCE_AUTH)).sessionUpgrade(sessionUpgradeSSOTokenId);
loginProcess = loginAuthenticator.getLoginProcess(loginConfiguration);
return processAuthentication(request, response, postBody, authId, loginProcess, loginConfiguration);
} catch (RestAuthException e) {
if (loginProcess != null) {
String failureUrl = urlValidator.getRedirectUrl(loginProcess.getAuthContext().getOrgDN(), loginProcess.getFailureURL(), null);
e.setFailureUrl(failureUrl);
}
throw e;
} catch (L10NMessageImpl e) {
throw new RestAuthException(amAuthErrorCodeResponseStatusMapping.getAuthLoginExceptionResponseStatus(e.getErrorCode()), e);
} catch (JsonException e) {
throw new RestAuthException(ResourceException.INTERNAL_ERROR, e);
} catch (SignatureException e) {
throw new RestAuthException(ResourceException.INTERNAL_ERROR, e);
} catch (AuthLoginException e) {
throw new RestAuthException(amAuthErrorCodeResponseStatusMapping.getAuthLoginExceptionResponseStatus(e.getErrorCode()), e);
} catch (JwsSigningException jse) {
DEBUG.error("JwsSigningException", jse);
throw new RestAuthException(ResourceException.INTERNAL_ERROR, "JwsSigningException, " + jse.getMessage());
}
}
use of org.forgerock.openam.core.rest.authn.core.LoginProcess in project OpenAM by OpenRock.
the class RestAuthenticationHandlerTest method shouldCleanupAfterAuthenticationComplete.
@Test
public void shouldCleanupAfterAuthenticationComplete() throws Exception {
// Given
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
String module = "LDAP";
String existingSesssionId = "session1";
AuthContextLocalWrapper authContextLocalWrapper = mock(AuthContextLocalWrapper.class);
LoginProcess loginProcess = mock(LoginProcess.class);
given(loginProcess.getLoginStage()).willReturn(LoginStage.COMPLETE);
given(loginProcess.isSuccessful()).willReturn(true);
given(loginProcess.getAuthContext()).willReturn(authContextLocalWrapper);
given(loginAuthenticator.getLoginProcess(Matchers.<LoginConfiguration>anyObject())).willReturn(loginProcess);
// When
restAuthenticationHandler.initiateAuthentication(request, response, "module", module, existingSesssionId);
// Then
verify(loginProcess).cleanup();
}
use of org.forgerock.openam.core.rest.authn.core.LoginProcess 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 org.forgerock.openam.core.rest.authn.core.LoginProcess 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 org.forgerock.openam.core.rest.authn.core.LoginProcess in project OpenAM by OpenRock.
the class RestAuthenticationHandlerTest method shouldInitiateAuthenticationViaGET1.
@Test
public void shouldInitiateAuthenticationViaGET1() throws AuthLoginException, L10NMessageImpl, JSONException, IOException, RestAuthException, RestAuthResponseException {
//Given
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse httpResponse = mock(HttpServletResponse.class);
String authIndexType = AuthIndexType.MODULE.toString();
String indexValue = "INDEX_VALUE";
String sessionUpgradeSSOTokenId = null;
AuthContextLocalWrapper authContextLocalWrapper = mock(AuthContextLocalWrapper.class);
given(authContextLocalWrapper.getErrorCode()).willReturn("ERROR_CODE");
given(authContextLocalWrapper.getErrorMessage()).willReturn("ERROR_MESSAGE");
LoginProcess loginProcess = mock(LoginProcess.class);
given(loginProcess.getLoginStage()).willReturn(LoginStage.COMPLETE);
given(loginProcess.isSuccessful()).willReturn(false);
given(loginProcess.getAuthContext()).willReturn(authContextLocalWrapper);
given(loginAuthenticator.getLoginProcess(Matchers.<LoginConfiguration>anyObject())).willReturn(loginProcess);
//When
try {
restAuthenticationHandler.initiateAuthentication(request, httpResponse, authIndexType, indexValue, sessionUpgradeSSOTokenId);
} catch (RestAuthErrorCodeException e) {
assertEquals(e.getStatusCode(), 401);
ArgumentCaptor<LoginConfiguration> argumentCaptor = ArgumentCaptor.forClass(LoginConfiguration.class);
verify(loginAuthenticator).getLoginProcess(argumentCaptor.capture());
LoginConfiguration loginConfiguration = argumentCaptor.getValue();
assertEquals(loginConfiguration.getHttpRequest(), request);
assertEquals(loginConfiguration.getIndexType(), AuthIndexType.MODULE);
assertEquals(loginConfiguration.getIndexValue(), "INDEX_VALUE");
assertEquals(loginConfiguration.getSessionId(), "");
assertEquals(loginConfiguration.getSSOTokenId(), "");
return;
}
//Then
fail();
}
Aggregations