Search in sources :

Example 6 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class RestAuthTextInputCallbackHandler method convertFromJson.

/**
     * {@inheritDoc}
     */
public TextInputCallback convertFromJson(TextInputCallback callback, JsonValue jsonCallback) throws RestAuthException {
    validateCallbackType(CALLBACK_NAME, jsonCallback);
    JsonValue input = jsonCallback.get("input");
    if (input.size() != 1) {
        throw new JsonException("JSON Callback does not include a input field");
    }
    JsonValue inputField = input.get(0);
    String value = inputField.get("value").asString();
    callback.setText(value);
    return callback;
}
Also used : JsonException(org.forgerock.json.JsonException) JsonValue(org.forgerock.json.JsonValue)

Example 7 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class AuthenticationServiceV1 method authenticate.

/**
     * Handles both initial and subsequent RESTful calls from clients submitting Callbacks for the authentication
     * process to continue. This is determined by checking if the POST body is empty or not. If it is empty then this
     * is initiating the authentication process otherwise it is a subsequent call submitting Callbacks.
     *
     * Initiating authentication request using the query parameters from the URL starts the login process and either
     * returns an SSOToken on successful authentication or a number of Callbacks needing to be completed before
     * authentication can proceed or an exception if any problems occurred whilst trying to authenticate.
     *
     * Using the body of the POST request the method continues the login process, submitting the given Callbacks and
     * then either returns an SSOToken on successful authentication or a number of additional Callbacks needing to be
     * completed before authentication can proceed or an exception if any problems occurred whilst trying to
     * authenticate.
     *
     * @param context The request context.
     * @param httpRequest The HTTP request.
     * @return A Json Representation of the response body. The response will contain either a JSON object containing the
     * SSOToken id from a successful authentication, a JSON object containing a number of Callbacks for the client to
     * complete and return or a JSON object containing an exception message.
     * @throws ResourceException If there is an error processing the authentication request.
     */
@Post
public Response authenticate(@Contextual Context context, @Contextual Request httpRequest) {
    if (!isSupportedMediaType(httpRequest)) {
        if (DEBUG.errorEnabled()) {
            DEBUG.error("AuthenticationService :: Unable to handle media type request : " + ContentTypeHeader.valueOf(httpRequest).getType());
        }
        return handleErrorResponse(httpRequest, Status.UNSUPPORTED_MEDIA_TYPE, null);
    }
    final HttpServletResponse response = getHttpServletResponse(context);
    Form urlQueryString = getUrlQueryString(httpRequest);
    final String sessionUpgradeSSOTokenId = urlQueryString.getFirst("sessionUpgradeSSOTokenId");
    try {
        JsonValue jsonContent;
        try {
            jsonContent = getJsonContent(httpRequest);
        } catch (IOException e) {
            DEBUG.message("AuthenticationService.authenticate() :: JSON parsing error", e);
            return handleErrorResponse(httpRequest, Status.BAD_REQUEST, e);
        }
        final HttpServletRequest request = getHttpServletRequest(context, jsonContent);
        JsonValue jsonResponse;
        if (jsonContent != null && jsonContent.size() > 0) {
            // submit requirements
            jsonResponse = restAuthenticationHandler.continueAuthentication(request, response, jsonContent, sessionUpgradeSSOTokenId);
        } else {
            // initiate
            final String authIndexType = urlQueryString.getFirst("authIndexType");
            final String authIndexValue = urlQueryString.getFirst("authIndexValue");
            jsonResponse = restAuthenticationHandler.initiateAuthentication(request, response, authIndexType, authIndexValue, sessionUpgradeSSOTokenId);
        }
        return createResponse(jsonResponse);
    } catch (RestAuthResponseException e) {
        DEBUG.message("AuthenticationService.authenticate() :: Exception from CallbackHandler", e);
        return handleErrorResponse(httpRequest, Status.valueOf(e.getStatusCode()), e);
    } catch (RestAuthException e) {
        DEBUG.message("AuthenticationService.authenticate() :: Rest Authentication Exception", e);
        return handleErrorResponse(httpRequest, Status.valueOf(e.getStatusCode()), e);
    } catch (IOException e) {
        DEBUG.error("AuthenticationService.authenticate() :: Internal Error", e);
        return handleErrorResponse(httpRequest, Status.INTERNAL_SERVER_ERROR, e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) Form(org.forgerock.http.protocol.Form) RestAuthResponseException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException) JsonValue(org.forgerock.json.JsonValue) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Post(org.forgerock.openam.http.annotations.Post)

Example 8 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class RestAuthHiddenValueCallbackHandler method convertToJson.

/**
     * {@inheritDoc}
     */
public JsonValue convertToJson(HiddenValueCallback callback, int index) {
    String id = callback.getId();
    String value = callback.getValue();
    JsonValue jsonValue = JsonValueBuilder.jsonValue().put("type", CALLBACK_NAME).array("output").addLast(createOutputField("value", value)).array("input").addLast(createInputField(index, id)).build();
    return jsonValue;
}
Also used : JsonValue(org.forgerock.json.JsonValue)

Example 9 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class RestAuthLanguageCallbackHandler method convertFromJson.

/**
     * {@inheritDoc}
     */
public LanguageCallback convertFromJson(LanguageCallback callback, JsonValue jsonCallback) throws RestAuthException {
    validateCallbackType(CALLBACK_NAME, jsonCallback);
    JsonValue input = jsonCallback.get("input");
    if (input.size() != 2) {
        throw new JsonException("JSON Callback does not include the required input fields");
    }
    String language = null;
    String country = null;
    for (int i = 0; i < input.size(); i++) {
        JsonValue inputField = input.get(i);
        String value = inputField.get("value").asString();
        if (i == 0) {
            language = value;
        } else {
            country = value;
        }
    }
    callback.setLocale(createLocale(language, country));
    return callback;
}
Also used : JsonException(org.forgerock.json.JsonException) JsonValue(org.forgerock.json.JsonValue)

Example 10 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class RestAuthNameCallbackHandler method convertFromJson.

/**
     * {@inheritDoc}
     */
public NameCallback convertFromJson(NameCallback callback, JsonValue jsonCallback) throws RestAuthException {
    validateCallbackType(CALLBACK_NAME, jsonCallback);
    JsonValue input = jsonCallback.get("input");
    if (input.size() != 1) {
        throw new JsonException("JSON Callback does not include a input field");
    }
    JsonValue inputField = input.get(0);
    String value = inputField.get("value").asString();
    callback.setName(value);
    return callback;
}
Also used : JsonException(org.forgerock.json.JsonException) JsonValue(org.forgerock.json.JsonValue)

Aggregations

JsonValue (org.forgerock.json.JsonValue)575 Test (org.testng.annotations.Test)333 ResourceException (org.forgerock.json.resource.ResourceException)144 ResourceResponse (org.forgerock.json.resource.ResourceResponse)123 RealmContext (org.forgerock.openam.rest.RealmContext)70 Context (org.forgerock.services.context.Context)63 HashSet (java.util.HashSet)56 SSOException (com.iplanet.sso.SSOException)54 ArrayList (java.util.ArrayList)51 BadRequestException (org.forgerock.json.resource.BadRequestException)47 Privilege (com.sun.identity.entitlement.Privilege)46 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)46 SSOToken (com.iplanet.sso.SSOToken)43 SMSException (com.sun.identity.sm.SMSException)42 HashMap (java.util.HashMap)42 NotFoundException (org.forgerock.json.resource.NotFoundException)41 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)41 CreateRequest (org.forgerock.json.resource.CreateRequest)40 OpenSSOPrivilege (com.sun.identity.entitlement.opensso.OpenSSOPrivilege)39 Subject (javax.security.auth.Subject)32