Search in sources :

Example 91 with Request

use of com.auth0.net.Request in project auth0-java by auth0.

the class UsersEntity method removePermissions.

/**
 * Remove permissions from a user.
 * A token with update:users is needed.
 * See https://auth0.com/docs/api/management/v2#!/Users/delete_permissions
 *
 * @param userId      the user id
 * @param permissions a list of permission objects to remove from the user
 * @return a Request to execute
 */
public Request<Void> removePermissions(String userId, List<Permission> permissions) {
    Asserts.assertNotNull(userId, "user id");
    Asserts.assertNotEmpty(permissions, "permissions");
    Map<String, List<Permission>> body = new HashMap<>();
    body.put("permissions", permissions);
    final String url = baseUrl.newBuilder().addPathSegments("api/v2/users").addPathSegments(userId).addPathSegments("permissions").build().toString();
    VoidRequest request = new VoidRequest(this.client, url, "DELETE");
    request.setBody(body);
    request.addHeader("Authorization", "Bearer " + apiToken);
    return request;
}
Also used : VoidRequest(com.auth0.net.VoidRequest) HashMap(java.util.HashMap) List(java.util.List)

Example 92 with Request

use of com.auth0.net.Request in project auth0-java by auth0.

the class UsersEntity method delete.

/**
 * Delete an existing User.
 * A token with scope delete:users is needed.
 * See https://auth0.com/docs/api/management/v2#!/Users/delete_users_by_id
 *
 * @param userId the user id
 * @return a Request to execute.
 */
public Request<Void> delete(String userId) {
    Asserts.assertNotNull(userId, "user id");
    String url = baseUrl.newBuilder().addPathSegments("api/v2/users").addPathSegment(userId).build().toString();
    VoidRequest request = new VoidRequest(client, url, "DELETE");
    request.addHeader("Authorization", "Bearer " + apiToken);
    return request;
}
Also used : VoidRequest(com.auth0.net.VoidRequest)

Example 93 with Request

use of com.auth0.net.Request in project auth0-java by auth0.

the class BaseRequest method executeAsync.

@Override
public CompletableFuture<T> executeAsync() {
    final CompletableFuture<T> future = new CompletableFuture<T>();
    okhttp3.Request request;
    try {
        request = createRequest();
    } catch (Auth0Exception e) {
        future.completeExceptionally(e);
        return future;
    }
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            future.completeExceptionally(new Auth0Exception("Failed to execute request", e));
        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            try {
                T parsedResponse = parseResponse(response);
                future.complete(parsedResponse);
            } catch (Auth0Exception e) {
                future.completeExceptionally(e);
            }
        }
    });
    return future;
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) CompletableFuture(java.util.concurrent.CompletableFuture) Callback(okhttp3.Callback) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException)

Example 94 with Request

use of com.auth0.net.Request in project auth0-java by auth0.

the class BaseRequestTest method alwaysCloseResponseOnRateLimitException.

@Test
public void alwaysCloseResponseOnRateLimitException() {
    Exception exception = null;
    try {
        new MockBaseRequest<String>(client) {

            @Override
            protected String parseResponse(Response response) throws Auth0Exception {
                throw new RateLimitException(-1, -1, -1);
            }
        }.execute();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(notNullValue()));
    assertThat(exception, is(instanceOf(RateLimitException.class)));
    assertThat(exception.getMessage(), is("Request failed with status code 429: Rate limit reached"));
    verify(response, times(1)).close();
}
Also used : RateLimitException(com.auth0.exception.RateLimitException) Auth0Exception(com.auth0.exception.Auth0Exception) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) Test(org.junit.Test)

Example 95 with Request

use of com.auth0.net.Request in project auth0-java by auth0.

the class BaseRequestTest method asyncCompletesWithExceptionWhenResponseParsingFails.

@Test
public void asyncCompletesWithExceptionWhenResponseParsingFails() throws Exception {
    doReturn(call).when(client).newCall(any());
    doAnswer(invocation -> {
        ((Callback) invocation.getArgument(0)).onResponse(call, response);
        return null;
    }).when(call).enqueue(any());
    CompletableFuture<?> request = new MockBaseRequest<String>(client) {

        @Override
        protected String parseResponse(Response response) throws Auth0Exception {
            throw new Auth0Exception("Response Parsing Error");
        }
    }.executeAsync();
    Exception exception = null;
    Object result = null;
    try {
        result = request.get();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(result, is(nullValue()));
    assertThat(exception, is(notNullValue()));
    assertThat(exception.getCause(), is(instanceOf(Auth0Exception.class)));
    assertThat(exception.getCause().getMessage(), is("Response Parsing Error"));
}
Also used : Auth0Exception(com.auth0.exception.Auth0Exception) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)193 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)185 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)77 IOException (java.io.IOException)76 List (java.util.List)63 Algorithm (com.auth0.jwt.algorithms.Algorithm)35 VoidRequest (com.auth0.net.VoidRequest)33 Auth0Exception (com.auth0.exception.Auth0Exception)30 APIException (com.auth0.exception.APIException)27 RateLimitException (com.auth0.exception.RateLimitException)25 HashMap (java.util.HashMap)24 PageFilter (com.auth0.client.mgmt.filter.PageFilter)23 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)23 ServletException (javax.servlet.ServletException)23 TokenHolder (com.auth0.json.auth.TokenHolder)22 JWTVerifier (com.auth0.jwt.JWTVerifier)22 ArrayList (java.util.ArrayList)22 Test (org.junit.jupiter.api.Test)22 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)20 Date (java.util.Date)20