Search in sources :

Example 11 with Callback

use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.

the class RestAuthCallbackHandlerManagerTest method shouldHandleCallbacksInternally.

@Test
public void shouldHandleCallbacksInternally() throws RestAuthResponseException, RestAuthException {
    //Given
    HttpHeaders headers = mock(HttpHeaders.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    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);
    given(restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(Matchers.<Class<? extends Callback>>anyObject())).willReturn(restAuthCallbackHandler1).willReturn(restAuthCallbackHandler2);
    given(restAuthCallbackHandler1.updateCallbackFromRequest(request, response, callback1)).willReturn(true);
    given(restAuthCallbackHandler2.updateCallbackFromRequest(request, response, callback2)).willReturn(true);
    //When
    JsonValue jsonCallbacks = restAuthCallbackHandlerManager.handleCallbacks(request, response, callbacks);
    //Then
    verify(restAuthCallbackHandler1).updateCallbackFromRequest(request, response, callback1);
    verify(restAuthCallbackHandler2).updateCallbackFromRequest(request, response, callback2);
    verify(restAuthCallbackHandler1, never()).convertToJson(callback1, 1);
    verify(restAuthCallbackHandler2, never()).convertToJson(callback2, 2);
    assertEquals(jsonCallbacks.size(), 0);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(javax.ws.rs.core.HttpHeaders) Callback(javax.security.auth.callback.Callback) JsonValue(org.forgerock.json.JsonValue) HttpServletResponse(javax.servlet.http.HttpServletResponse) BeforeClass(org.testng.annotations.BeforeClass) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler) Test(org.testng.annotations.Test)

Example 12 with Callback

use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.

the class RestAuthCallbackHandlerManagerTest method shouldHandleJsonCallbacksMixedUp.

@Test
public void shouldHandleJsonCallbacksMixedUp() 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(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);
    given(jsonCallbacks.size()).willReturn(2);
    given(jsonCallbacks.get(0)).willReturn(jsonCallback1);
    given(jsonCallbacks.get(1)).willReturn(jsonCallback2);
    //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);
}
Also used : Callback(javax.security.auth.callback.Callback) JsonValue(org.forgerock.json.JsonValue) BeforeClass(org.testng.annotations.BeforeClass) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler) Test(org.testng.annotations.Test)

Example 13 with Callback

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);
}
Also used : Callback(javax.security.auth.callback.Callback) JsonValue(org.forgerock.json.JsonValue) BeforeClass(org.testng.annotations.BeforeClass) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler) Test(org.testng.annotations.Test)

Example 14 with Callback

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();
}
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 15 with Callback

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");
}
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)

Aggregations

Callback (javax.security.auth.callback.Callback)390 NameCallback (javax.security.auth.callback.NameCallback)250 PasswordCallback (javax.security.auth.callback.PasswordCallback)245 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)210 IOException (java.io.IOException)141 LoginException (javax.security.auth.login.LoginException)89 CallbackHandler (javax.security.auth.callback.CallbackHandler)76 Subject (javax.security.auth.Subject)52 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)52 Test (org.testng.annotations.Test)42 FailedLoginException (javax.security.auth.login.FailedLoginException)41 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)38 Principal (java.security.Principal)37 HashMap (java.util.HashMap)37 LoginContext (javax.security.auth.login.LoginContext)37 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)31 Test (org.junit.Test)31 RealmCallback (javax.security.sasl.RealmCallback)30 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)29 HttpServletRequest (javax.servlet.http.HttpServletRequest)28