Search in sources :

Example 21 with ActionResponse

use of org.forgerock.json.resource.ActionResponse in project OpenAM by OpenRock.

the class UmaPolicyResourceTest method shouldThrowNotSupportedExceptionForActionCollection.

@Test
@SuppressWarnings("unchecked")
public void shouldThrowNotSupportedExceptionForActionCollection() {
    //Given
    Context context = mock(Context.class);
    ActionRequest request = Requests.newActionRequest("/policies", "ACTION_ID");
    //When
    Promise<ActionResponse, ResourceException> result = policyResource.actionCollection(context, request);
    //Then
    assertThat(result).failedWithException().isInstanceOf(NotSupportedException.class);
}
Also used : Context(org.forgerock.services.context.Context) ActionRequest(org.forgerock.json.resource.ActionRequest) ResourceException(org.forgerock.json.resource.ResourceException) ActionResponse(org.forgerock.json.resource.ActionResponse) Test(org.testng.annotations.Test)

Example 22 with ActionResponse

use of org.forgerock.json.resource.ActionResponse in project OpenAM by OpenRock.

the class OathDevicesResourceTest method shouldFailOnUnknownAction.

@Test
public void shouldFailOnUnknownAction() throws ResourceException, SSOException {
    // given
    ActionRequest request = Requests.newActionRequest("instanceId", "fake");
    // when
    Promise<ActionResponse, ResourceException> promise = resource.actionCollection(ctx(), request);
    // then
    assertThat(promise).failedWithResourceException().withCode(ResourceException.NOT_SUPPORTED);
}
Also used : ActionRequest(org.forgerock.json.resource.ActionRequest) ResourceException(org.forgerock.json.resource.ResourceException) ActionResponse(org.forgerock.json.resource.ActionResponse) Test(org.testng.annotations.Test)

Example 23 with ActionResponse

use of org.forgerock.json.resource.ActionResponse in project OpenAM by OpenRock.

the class OathDevicesResourceTest method shouldExecuteTrueCheckAction.

@Test
public void shouldExecuteTrueCheckAction() throws ResourceException, SSOException, InterruptedException, ExecutionException {
    // give
    ActionRequest request = Requests.newActionRequest("instanceId", "check");
    // when
    Promise<ActionResponse, ResourceException> promise = resource.actionCollection(ctx(), request);
    // then
    assertThat(promise).succeeded().withContent().booleanAt("result").isTrue();
}
Also used : ActionRequest(org.forgerock.json.resource.ActionRequest) ResourceException(org.forgerock.json.resource.ResourceException) ActionResponse(org.forgerock.json.resource.ActionResponse) Test(org.testng.annotations.Test)

Example 24 with ActionResponse

use of org.forgerock.json.resource.ActionResponse in project OpenAM by OpenRock.

the class OathDevicesResourceTest method shouldExecuteSkipAction.

@Test
public void shouldExecuteSkipAction() throws ResourceException, SSOException, InterruptedException, ExecutionException {
    // given
    JsonValue contents = JsonValueBuilder.toJsonValue("{ \"value\" : true }");
    ActionRequest request = Requests.newActionRequest("instanceId", "skip");
    request.setContent(contents);
    // when
    Promise<ActionResponse, ResourceException> promise = resource.actionCollection(ctx(), request);
    // then
    assertThat(promise).succeeded().withContent().isObject().containsOnly();
}
Also used : ActionRequest(org.forgerock.json.resource.ActionRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) ActionResponse(org.forgerock.json.resource.ActionResponse) Test(org.testng.annotations.Test)

Example 25 with ActionResponse

use of org.forgerock.json.resource.ActionResponse in project OpenAM by OpenRock.

the class IdentityResourceV2 method anonymousUpdate.

/**
     * Perform an anonymous update of a user's password using the provided token.
     *
     * The token must match a token placed in the CTS in order for the request
     * to proceed.
     *
     * @param context Non null
     * @param request Non null
     * @param realm Non null
     */
private Promise<ActionResponse, ResourceException> anonymousUpdate(final Context context, final ActionRequest request, final String realm) {
    final String tokenID;
    String confirmationId;
    String username;
    String nwpassword;
    final JsonValue jVal = request.getContent();
    try {
        tokenID = jVal.get(TOKEN_ID).asString();
        jVal.remove(TOKEN_ID);
        confirmationId = jVal.get(CONFIRMATION_ID).asString();
        jVal.remove(CONFIRMATION_ID);
        username = jVal.get(USERNAME).asString();
        nwpassword = jVal.get("userpassword").asString();
        if (username == null || username.isEmpty()) {
            throw new BadRequestException("username not provided");
        }
        if (nwpassword == null || username.isEmpty()) {
            throw new BadRequestException("new password not provided");
        }
        validateToken(tokenID, realm, username, confirmationId);
        // update Identity
        SSOToken admin = RestUtils.getToken();
        // Update instance with new password value
        return updateInstance(admin, jVal, realm).thenAsync(new AsyncFunction<ActionResponse, ActionResponse, ResourceException>() {

            @Override
            public Promise<ActionResponse, ResourceException> apply(ActionResponse response) {
                // Only remove the token if the update was successful, errors will be set in the handler.
                try {
                    // Even though the generated token will eventually timeout, delete it after a successful read
                    // so that the reset password request cannot be made again using the same token.
                    CTSHolder.getCTS().deleteAsync(tokenID);
                } catch (DeleteFailedException e) {
                    // reading and deleting, the token has expired.
                    if (debug.messageEnabled()) {
                        debug.message("Deleting token " + tokenID + " after a successful " + "read failed due to " + e.getMessage(), e);
                    }
                } catch (CoreTokenException cte) {
                    // For any unexpected CTS error
                    debug.error("Error performing anonymousUpdate", cte);
                    return new InternalServerErrorException(cte.getMessage(), cte).asPromise();
                }
                return newResultPromise(response);
            }
        });
    } catch (BadRequestException bre) {
        // For any malformed request.
        debug.warning("Bad request received for anonymousUpdate ", bre);
        return bre.asPromise();
    } catch (ResourceException re) {
        debug.warning("Error performing anonymousUpdate", re);
        return re.asPromise();
    } catch (CoreTokenException cte) {
        // For any unexpected CTS error
        debug.error("Error performing anonymousUpdate", cte);
        return new InternalServerErrorException(cte).asPromise();
    }
}
Also used : Promises.newResultPromise(org.forgerock.util.promise.Promises.newResultPromise) Promise(org.forgerock.util.promise.Promise) SSOToken(com.iplanet.sso.SSOToken) DeleteFailedException(org.forgerock.openam.cts.exceptions.DeleteFailedException) JsonValue(org.forgerock.json.JsonValue) BadRequestException(org.forgerock.json.resource.BadRequestException) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ResourceException(org.forgerock.json.resource.ResourceException) ActionResponse(org.forgerock.json.resource.ActionResponse) Responses.newActionResponse(org.forgerock.json.resource.Responses.newActionResponse)

Aggregations

ActionResponse (org.forgerock.json.resource.ActionResponse)43 ResourceException (org.forgerock.json.resource.ResourceException)37 Test (org.testng.annotations.Test)34 ActionRequest (org.forgerock.json.resource.ActionRequest)29 Context (org.forgerock.services.context.Context)22 JsonValue (org.forgerock.json.JsonValue)20 RealmContext (org.forgerock.openam.rest.RealmContext)13 Promise (org.forgerock.util.promise.Promise)7 Promises.newResultPromise (org.forgerock.util.promise.Promises.newResultPromise)7 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)6 BadRequestException (org.forgerock.json.resource.BadRequestException)5 SSOToken (com.iplanet.sso.SSOToken)4 ServiceNotFoundException (com.sun.identity.sm.ServiceNotFoundException)4 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)4 Responses.newActionResponse (org.forgerock.json.resource.Responses.newActionResponse)4 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)4 DeleteFailedException (org.forgerock.openam.cts.exceptions.DeleteFailedException)4 FilterChain (org.forgerock.json.resource.FilterChain)3 NotFoundException (org.forgerock.json.resource.NotFoundException)3 Router (org.forgerock.json.resource.Router)3