Search in sources :

Example 1 with AuthContextLocalWrapper

use of org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper 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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthContextLocalWrapper(org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper) LoginProcess(org.forgerock.openam.core.rest.authn.core.LoginProcess) Test(org.testng.annotations.Test)

Example 2 with AuthContextLocalWrapper

use of org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper 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();
}
Also used : PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) HashMap(java.util.HashMap) JsonValue(org.forgerock.json.JsonValue) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthContextLocalWrapper(org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper) LoginProcess(org.forgerock.openam.core.rest.authn.core.LoginProcess) HttpServletRequest(javax.servlet.http.HttpServletRequest) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) Callback(javax.security.auth.callback.Callback) RestAuthResponseException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 3 with AuthContextLocalWrapper

use of org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper 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");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) Callback(javax.security.auth.callback.Callback) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) JsonValue(org.forgerock.json.JsonValue) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthContextLocalWrapper(org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper) LoginProcess(org.forgerock.openam.core.rest.authn.core.LoginProcess) Test(org.testng.annotations.Test)

Example 4 with AuthContextLocalWrapper

use of org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper in project OpenAM by OpenRock.

the class LoginAuthenticatorTest method shouldGetLoginProcessForInitialRequestWithAuthIndexTypeRoleWithSessionUpgradeButNotRequired.

@Test
public void shouldGetLoginProcessForInitialRequestWithAuthIndexTypeRoleWithSessionUpgradeButNotRequired() throws AuthException, AuthLoginException, SSOException, RestAuthException {
    //Given
    LoginConfiguration loginConfiguration = new LoginConfiguration();
    HttpServletRequest request = mock(HttpServletRequest.class);
    String sessionId = null;
    AuthIndexType authIndexType = AuthIndexType.ROLE;
    String authIndexValue = "INDEX_VALUE";
    String ssoTokenId = "SSO_TOKEN_ID";
    AuthContextLocalWrapper authContextLocalWrapper = mock(AuthContextLocalWrapper.class);
    SSOToken ssoToken = mock(SSOToken.class);
    loginConfiguration.httpRequest(request).sessionId(sessionId).indexType(authIndexType).indexValue(authIndexValue).sessionUpgrade(ssoTokenId);
    given(ssoToken.getProperty("Role")).willReturn("INDEX_VALUE");
    given(coreServicesWrapper.getDomainNameByRequest(request)).willReturn("ORG_DN");
    given(coreServicesWrapper.getAuthContext(eq(request), eq((HttpServletResponse) null), (SessionID) anyObject(), eq(false), eq(false))).willReturn(authContextLocalWrapper);
    given(coreServicesWrapper.getExistingValidSSOToken(eq(new SessionID("SSO_TOKEN_ID")))).willReturn(ssoToken);
    given(coreServicesWrapper.isNewRequest(authContextLocalWrapper)).willReturn(true);
    given(coreServicesWrapper.doesValueContainKey(anyString(), anyString())).willReturn(false);
    given(coreServicesWrapper.doesValueContainKey("INDEX_VALUE", "INDEX_VALUE")).willReturn(true);
    //When
    LoginProcess loginProcess = loginAuthenticator.getLoginProcess(loginConfiguration);
    //Then
    assertThat(loginProcess.isSuccessful()).isTrue();
    verify(authContextLocalWrapper, never()).login();
    assertNotNull(loginProcess);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SSOToken(com.iplanet.sso.SSOToken) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthContextLocalWrapper(org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper) SessionID(com.iplanet.dpro.session.SessionID) Test(org.testng.annotations.Test)

Example 5 with AuthContextLocalWrapper

use of org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper in project OpenAM by OpenRock.

the class LoginAuthenticatorTest method shouldGetLoginProcessForInitialRequestWithAuthIndexTypeServiceWithSessionUpgradeButNotRequired.

@Test
public void shouldGetLoginProcessForInitialRequestWithAuthIndexTypeServiceWithSessionUpgradeButNotRequired() throws AuthException, AuthLoginException, SSOException, RestAuthException {
    //Given
    LoginConfiguration loginConfiguration = new LoginConfiguration();
    HttpServletRequest request = mock(HttpServletRequest.class);
    String sessionId = null;
    AuthIndexType authIndexType = AuthIndexType.SERVICE;
    String authIndexValue = "INDEX_VALUE";
    String ssoTokenId = "SSO_TOKEN_ID";
    AuthContextLocalWrapper authContextLocalWrapper = mock(AuthContextLocalWrapper.class);
    SSOToken ssoToken = mock(SSOToken.class);
    loginConfiguration.httpRequest(request).sessionId(sessionId).indexType(authIndexType).indexValue(authIndexValue).sessionUpgrade(ssoTokenId);
    given(ssoToken.getProperty("Service")).willReturn("INDEX_VALUE");
    given(coreServicesWrapper.getDomainNameByRequest(request)).willReturn("ORG_DN");
    given(coreServicesWrapper.getAuthContext(eq(request), eq((HttpServletResponse) null), (SessionID) anyObject(), eq(false), eq(false))).willReturn(authContextLocalWrapper);
    given(coreServicesWrapper.getExistingValidSSOToken(eq(new SessionID("SSO_TOKEN_ID")))).willReturn(ssoToken);
    given(coreServicesWrapper.isNewRequest(authContextLocalWrapper)).willReturn(true);
    given(coreServicesWrapper.doesValueContainKey(anyString(), anyString())).willReturn(false);
    given(coreServicesWrapper.doesValueContainKey("INDEX_VALUE", "INDEX_VALUE")).willReturn(true);
    //When
    LoginProcess loginProcess = loginAuthenticator.getLoginProcess(loginConfiguration);
    //Then
    assertThat(loginProcess.isSuccessful()).isTrue();
    verify(authContextLocalWrapper, never()).login();
    assertNotNull(loginProcess);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SSOToken(com.iplanet.sso.SSOToken) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthContextLocalWrapper(org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper) SessionID(com.iplanet.dpro.session.SessionID) Test(org.testng.annotations.Test)

Aggregations

AuthContextLocalWrapper (org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper)30 Test (org.testng.annotations.Test)29 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)24 SessionID (com.iplanet.dpro.session.SessionID)17 SSOToken (com.iplanet.sso.SSOToken)17 LoginConfiguration (org.forgerock.openam.core.rest.authn.core.LoginConfiguration)8 LoginProcess (org.forgerock.openam.core.rest.authn.core.LoginProcess)7 JsonValue (org.forgerock.json.JsonValue)5 HashMap (java.util.HashMap)4 SSOTokenID (com.iplanet.sso.SSOTokenID)3 PagePropertiesCallback (com.sun.identity.authentication.spi.PagePropertiesCallback)3 Map (java.util.Map)3 Callback (javax.security.auth.callback.Callback)3 RestAuthException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthException)3 Set (java.util.Set)1 SignedJwt (org.forgerock.json.jose.jws.SignedJwt)1 JwtClaimsSet (org.forgerock.json.jose.jwt.JwtClaimsSet)1 RestAuthErrorCodeException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthErrorCodeException)1 RestAuthResponseException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException)1