Search in sources :

Example 1 with RestAuthCallbackHandler

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

the class RestAuthCallbackHandlerManager method handleResponseCallbacks.

/**
     * Handles the processing of the JSON given in the request and updates the Callback objects from it.
     *
     * This is for special circumstances where the JSON from the request does not contain a "callback" attribute,
     * where the <code>handleJsonCallbacks()</code> method should be used.
     *
     * @param request The HttpServletRequest from the request.
     * @param response The HttpServletResponse from the request.
     * @param originalCallbacks The Callbacks to set values from the JSONArray onto.
     * @param jsonRequestObject The JSON object that was sent in the POST of the request.
     * @return The updated originalCallbacks.
     */
public Callback[] handleResponseCallbacks(HttpServletRequest request, HttpServletResponse response, Callback[] originalCallbacks, JsonValue jsonRequestObject) throws RestAuthException {
    for (Callback originalCallback : originalCallbacks) {
        RestAuthCallbackHandler restAuthCallbackHandler = restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(originalCallback.getClass());
        restAuthCallbackHandler.handle(request, response, jsonRequestObject, originalCallback);
    }
    return originalCallbacks;
}
Also used : Callback(javax.security.auth.callback.Callback) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler)

Example 2 with RestAuthCallbackHandler

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

the class RestAuthCallbackHandlerManager method handleCallbacks.

/**
     * Handles Callbacks by either updating them with their required values from the headers and request or
     * converting them to JSON representations to be sent back to the client.
     *
     * @param request The HttpServletRequest from the request.
     * @param response The HttpServletResponse from the request.
     * @param callbacks The Callbacks to handle.
     * @return A JSONArray of Callbacks or empty if the Callbacks have been updated from the headers and request.
     */
public JsonValue handleCallbacks(HttpServletRequest request, HttpServletResponse response, Callback[] callbacks) throws RestAuthException {
    List<Object> jsonCallbacks = new ArrayList<>();
    int callbackIndex = 0;
    // check if can be completed by headers and/or request
    // if so then attempt it and response true if successful
    boolean handledInternally = handleCallbacksInternally(request, response, callbacks);
    // else or on false convert callback into json
    if (!handledInternally) {
        logger.message("Cannot handle callbacks internally. Converting to JSON instead.");
        for (Callback callback : callbacks) {
            callbackIndex++;
            RestAuthCallbackHandler restAuthCallbackHandler = restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(callback.getClass());
            JsonValue jsonCallback = restAuthCallbackHandler.convertToJson(callback, callbackIndex);
            jsonCallbacks.add(jsonCallback.getObject());
        }
    }
    return new JsonValue(jsonCallbacks);
}
Also used : Callback(javax.security.auth.callback.Callback) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler)

Example 3 with RestAuthCallbackHandler

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

the class RestAuthCallbackHandlerManagerTest method shouldFailToHandleJsonCallbacksWithMissingJSONCallback.

@Test(expectedExceptions = RestAuthException.class)
public void shouldFailToHandleJsonCallbacksWithMissingJSONCallback() throws RestAuthException {
    //Given
    Callback callback1 = mock(Callback.class);
    Callback callback2 = mock(Callback.class);
    Callback callback3 = mock(Callback.class);
    Callback[] callbacks = new Callback[] { callback1, callback2, callback3 };
    RestAuthCallbackHandler restAuthCallbackHandler1 = mock(RestAuthCallbackHandler.class);
    RestAuthCallbackHandler restAuthCallbackHandler2 = mock(RestAuthCallbackHandler.class);
    RestAuthCallbackHandler restAuthCallbackHandler3 = 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).willReturn(restAuthCallbackHandler3);
    given(restAuthCallbackHandler1.getCallbackClassName()).willReturn("CALLBACK1");
    given(restAuthCallbackHandler2.getCallbackClassName()).willReturn("CALLBACK2");
    given(restAuthCallbackHandler2.getCallbackClassName()).willReturn("CALLBACK3");
    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
    restAuthCallbackHandlerManager.handleJsonCallbacks(callbacks, jsonCallbacks);
    //Then
    fail();
}
Also used : Callback(javax.security.auth.callback.Callback) JsonValue(org.forgerock.json.JsonValue) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler) Test(org.testng.annotations.Test)

Example 4 with RestAuthCallbackHandler

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

the class RestAuthCallbackHandlerFactoryTest method shouldGetNameRestAuthCallbackHandler.

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

Example 5 with RestAuthCallbackHandler

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

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