Search in sources :

Example 6 with RestAuthCallbackHandler

use of org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler 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 7 with RestAuthCallbackHandler

use of org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler 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 8 with RestAuthCallbackHandler

use of org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler in project OpenAM by OpenRock.

the class RestAuthCallbackHandlerManagerTest method shouldHandleCallbacksIntoJson.

@Test
public void shouldHandleCallbacksIntoJson() throws RestAuthResponseException, RestAuthException {
    //Given
    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);
    JsonValue jsonCallback1 = new JsonValue(new LinkedHashMap<String, String>());
    jsonCallback1.put("KEY1", "VALUE1");
    JsonValue jsonCallback2 = new JsonValue(new LinkedHashMap<String, String>());
    jsonCallback2.put("KEY2", "VALUE2");
    given(restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(Matchers.<Class<? extends Callback>>anyObject())).willReturn(restAuthCallbackHandler1).willReturn(restAuthCallbackHandler1).willReturn(restAuthCallbackHandler2);
    given(restAuthCallbackHandler1.updateCallbackFromRequest(request, response, callback1)).willReturn(false);
    given(restAuthCallbackHandler2.updateCallbackFromRequest(request, response, callback2)).willReturn(false);
    given(restAuthCallbackHandler1.convertToJson(callback1, 1)).willReturn(jsonCallback1);
    given(restAuthCallbackHandler2.convertToJson(callback2, 2)).willReturn(jsonCallback2);
    //When
    JsonValue jsonCallbacks = restAuthCallbackHandlerManager.handleCallbacks(request, response, callbacks);
    //Then
    verify(restAuthCallbackHandler1).updateCallbackFromRequest(request, response, callback1);
    verify(restAuthCallbackHandler2, never()).updateCallbackFromRequest(request, response, callback2);
    verify(restAuthCallbackHandler1).convertToJson(callback1, 1);
    verify(restAuthCallbackHandler2).convertToJson(callback2, 2);
    assertEquals(jsonCallbacks.size(), 2);
    assertEquals(jsonCallbacks.get(0).get("KEY1").asString(), "VALUE1");
    assertEquals(jsonCallbacks.get(1).get("KEY2").asString(), "VALUE2");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Callback(javax.security.auth.callback.Callback) JsonValue(org.forgerock.json.JsonValue) HttpServletResponse(javax.servlet.http.HttpServletResponse) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler) Test(org.testng.annotations.Test)

Example 9 with RestAuthCallbackHandler

use of org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler in project OpenAM by OpenRock.

the class RestAuthCallbackHandlerManager method handleJsonCallbacks.

/**
     * Handles the JSON representations of Callbacks, converting them back to Callbacks by setting the values from
     * the JSONArray to the original Callbacks passed in.
     *
     * The method sets the appropriate values on the Callbacks parameter and returns the same Callbacks
     * parameter. This is required because of the way the AuthContext handles submitting requirements (Callbacks).
     *
     * The JSON callbacks array must be in the same order as it was sent it, so it matches the order of the Callback
     * object array.
     *
     * @param originalCallbacks The Callbacks to set values from the JSONArray onto.
     * @param jsonCallbacks The JSON representation of the Callbacks.
     * @return The same Callbacks as in the parameters with the required values set.
     */
public Callback[] handleJsonCallbacks(final Callback[] originalCallbacks, final JsonValue jsonCallbacks) throws RestAuthException {
    if (originalCallbacks.length != jsonCallbacks.size()) {
        logger.error("Incorrect number of callbacks found in JSON response");
        throw new RestAuthException(ResourceException.BAD_REQUEST, "Incorrect number of callbacks found in JSON response");
    }
    for (int i = 0; i < originalCallbacks.length; i++) {
        final Callback originalCallback = originalCallbacks[i];
        final RestAuthCallbackHandler restAuthCallbackHandler = restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(originalCallback.getClass());
        final JsonValue jsonCallback = jsonCallbacks.get(i);
        if (!restAuthCallbackHandler.getCallbackClassName().equals(jsonCallback.get("type").asString())) {
            logger.error("Required callback not found in JSON response");
            throw new RestAuthException(ResourceException.BAD_REQUEST, "Required callback not found in JSON response");
        }
        restAuthCallbackHandler.convertFromJson(originalCallback, jsonCallback);
    }
    return originalCallbacks;
}
Also used : RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) Callback(javax.security.auth.callback.Callback) JsonValue(org.forgerock.json.JsonValue) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler)

Example 10 with RestAuthCallbackHandler

use of org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler in project OpenAM by OpenRock.

the class RestAuthCallbackHandlerFactoryTest method shouldGetPasswordRestAuthCallbackHandler.

@Test
public void shouldGetPasswordRestAuthCallbackHandler() throws RestAuthException {
    //Given
    //When
    RestAuthCallbackHandler restAuthCallbackHandler = restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(PasswordCallback.class);
    //Then
    assertEquals(restAuthCallbackHandler.getClass(), RestAuthPasswordCallbackHandler.class);
}
Also used : RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler) Test(org.testng.annotations.Test)

Aggregations

RestAuthCallbackHandler (org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler)11 Callback (javax.security.auth.callback.Callback)9 JsonValue (org.forgerock.json.JsonValue)8 Test (org.testng.annotations.Test)8 BeforeClass (org.testng.annotations.BeforeClass)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 HttpHeaders (javax.ws.rs.core.HttpHeaders)2 ArrayList (java.util.ArrayList)1 RestAuthException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthException)1