Search in sources :

Example 41 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class MultipartRequestTest method shouldNotOverrideContentTypeHeader.

@Test
public void shouldNotOverrideContentTypeHeader() throws Exception {
    MultipartBody.Builder bodyBuilder = new MultipartBody.Builder("5c49fdf2");
    MultipartRequest<TokenHolder> request = new MultipartRequest<>(client, server.getBaseUrl(), "POST", new ObjectMapper(), tokenHolderType, bodyBuilder);
    request.addPart("non_empty", "body");
    request.addHeader("Content-Type", "plaintext");
    server.jsonResponse(AUTH_TOKENS, 200);
    request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getHeader("Content-Type"), is("multipart/form-data; boundary=5c49fdf2"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MultipartBody(okhttp3.MultipartBody) TokenHolder(com.auth0.json.auth.TokenHolder) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 42 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class MultipartRequestTest method shouldParseJSONErrorResponseWithDescription.

@Test
public void shouldParseJSONErrorResponseWithDescription() throws Exception {
    MultipartRequest<List> request = new MultipartRequest<>(client, server.getBaseUrl(), "POST", listType);
    request.addPart("non_empty", "body");
    server.jsonResponse(AUTH_ERROR_WITH_DESCRIPTION, 400);
    Exception exception = null;
    try {
        request.execute();
        server.takeRequest();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(notNullValue()));
    assertThat(exception, is(instanceOf(APIException.class)));
    assertThat(exception.getCause(), is(nullValue()));
    assertThat(exception.getMessage(), is("Request failed with status code 400: The user already exists."));
    APIException authException = (APIException) exception;
    assertThat(authException.getDescription(), is("The user already exists."));
    assertThat(authException.getError(), is("user_exists"));
    assertThat(authException.getStatusCode(), is(400));
}
Also used : APIException(com.auth0.exception.APIException) List(java.util.List) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ExpectedException(org.junit.rules.ExpectedException) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Example 43 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class TokenRequestTest method setUp.

@Before
public void setUp() throws Exception {
    client = new OkHttpClient();
    server = new MockServer();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) MockServer(com.auth0.client.MockServer) Before(org.junit.Before)

Example 44 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class TokenRequestTest method shouldCreateRequest.

@Test
public void shouldCreateRequest() throws Exception {
    TokenRequest request = new TokenRequest(client, server.getBaseUrl());
    request.addParameter("non_empty", "body");
    assertThat(request, is(notNullValue()));
    server.jsonResponse(AUTH_TOKENS, 200);
    TokenHolder response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod(), is("POST"));
    assertThat(response, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) TokenHolder(com.auth0.json.auth.TokenHolder) Test(org.junit.Test)

Example 45 with Client

use of com.auth0.json.mgmt.client.Client in project drug-formulary-ri by HL7-DaVinci.

the class AuthUtils method authCodeIsValid.

/**
 * Verify the authorization code provided in the POST request's claim to /token
 * path
 *
 * @param code        - the authorization code provided in the request
 * @param baseUrl     - this server base URL
 * @param redirectURI - the requestor/client redirect URI provided in the POST
 *                    request
 * @param clientId    - the client ID retrieved from the request's Authorization
 *                    Header
 * @return patientId if the authorization code is valid, otherwise null
 */
public static String authCodeIsValid(String code, String baseUrl, String redirectURI, String clientId) {
    String patientId = null;
    try {
        Algorithm algorithm = Algorithm.RSA256(OauthEndpointController.getPublicKey(), null);
        JWTVerifier verifier = JWT.require(algorithm).withIssuer(baseUrl).withAudience(baseUrl).withClaim(REDIRECT_URI_KEY, redirectURI).withClaim(CLIENT_ID_KEY, clientId).build();
        DecodedJWT jwt = verifier.verify(code);
        String username = jwt.getClaim("username").asString();
        User user = User.getUser(username);
        patientId = user != null ? user.getPatientId() : null;
    } catch (SignatureVerificationException | InvalidClaimException e) {
        logger.log(Level.SEVERE, "TokenEndpoint::Authorization code is invalid: Signature invalid or claim value invalid", e);
    } catch (AlgorithmMismatchException e) {
        logger.log(Level.SEVERE, "TokenEndpoint::Authorization code is invalid: Algorithm mismatch", e);
    } catch (TokenExpiredException e) {
        logger.log(Level.SEVERE, "TokenEndpoint::Authorization code is invalid: Token expired", e);
    } catch (JWTVerificationException e) {
        logger.log(Level.SEVERE, "TokenEndpoint::Authorization code is invalid: Please obtain a new code", e);
    }
    return patientId;
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) TokenExpiredException(com.auth0.jwt.exceptions.TokenExpiredException) SignatureVerificationException(com.auth0.jwt.exceptions.SignatureVerificationException) InvalidClaimException(com.auth0.jwt.exceptions.InvalidClaimException) Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) AlgorithmMismatchException(com.auth0.jwt.exceptions.AlgorithmMismatchException)

Aggregations

IOException (java.io.IOException)36 APIException (com.auth0.exception.APIException)27 Auth0Exception (com.auth0.exception.Auth0Exception)27 RateLimitException (com.auth0.exception.RateLimitException)27 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)26 List (java.util.List)25 Test (org.junit.Test)25 VoidRequest (com.auth0.net.VoidRequest)24 TokenHolder (com.auth0.json.auth.TokenHolder)22 JsonParseException (com.fasterxml.jackson.core.JsonParseException)19 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)19 ExpectedException (org.junit.rules.ExpectedException)19 RecordedMultipartRequest (com.auth0.net.multipart.RecordedMultipartRequest)16 Test (org.junit.jupiter.api.Test)14 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)14 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)9 AuthAPI (com.auth0.client.auth.AuthAPI)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 OkHttpClient (okhttp3.OkHttpClient)7