Search in sources :

Example 41 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project oxAuth by GluuFederation.

the class ResponseTypesRestrictionEmbeddedTest method responseTypesCodeIdTokenStep3b.

@Parameters({ "tokenPath", "redirectUri" })
@Test(dependsOnMethods = { "responseTypesCodeIdTokenStep3a" })
public void responseTypesCodeIdTokenStep3b(final String tokenPath, final String redirectUri) throws Exception {
    Builder request = ResteasyClientBuilder.newClient().target(url.toString() + tokenPath).request();
    TokenRequest tokenRequest = new TokenRequest(GrantType.AUTHORIZATION_CODE);
    tokenRequest.setCode(authorizationCode2);
    tokenRequest.setRedirectUri(redirectUri);
    tokenRequest.setAuthUsername(clientId2);
    tokenRequest.setAuthPassword(clientSecret2);
    request.header("Authorization", "Basic " + tokenRequest.getEncodedCredentials());
    request.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    Response response = request.post(Entity.form(new MultivaluedHashMap<String, String>(tokenRequest.getParameters())));
    String entity = response.readEntity(String.class);
    showResponse("responseTypesCodeIdTokenStep3b", response, entity);
    assertEquals(response.getStatus(), 200, "Unexpected response code.");
    assertTrue(response.getHeaderString("Cache-Control") != null && response.getHeaderString("Cache-Control").equals("no-store"), "Unexpected result: " + response.getHeaderString("Cache-Control"));
    assertTrue(response.getHeaderString("Pragma") != null && response.getHeaderString("Pragma").equals("no-cache"), "Unexpected result: " + response.getHeaderString("Pragma"));
    assertNotNull(entity, "Unexpected result: " + entity);
    try {
        JSONObject jsonObj = new JSONObject(entity);
        assertTrue(jsonObj.has("access_token"), "Unexpected result: access_token not found");
        assertTrue(jsonObj.has("token_type"), "Unexpected result: token_type not found");
        assertTrue(jsonObj.has("refresh_token"), "Unexpected result: refresh_token not found");
        assertTrue(jsonObj.has("id_token"));
    } catch (JSONException e) {
        e.printStackTrace();
        fail(e.getMessage() + "\nResponse was: " + entity);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JSONObject(org.codehaus.jettison.json.JSONObject) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) TokenRequest(org.xdi.oxauth.client.TokenRequest) JSONException(org.codehaus.jettison.json.JSONException) URISyntaxException(java.net.URISyntaxException) JSONException(org.codehaus.jettison.json.JSONException) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test) BaseTest(org.xdi.oxauth.BaseTest)

Example 42 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project oxAuth by GluuFederation.

the class TokenEndpointAuthMethodRestrictionEmbeddedTest method tokenEndpointAuthMethodClientSecretBasicFail1.

/**
	 * Fail 1: Call to Token Endpoint with Auth Method
	 * <code>client_secret_post</code> should fail.
	 */
@Parameters({ "tokenPath", "userId", "userSecret" })
@Test(dependsOnMethods = "tokenEndpointAuthMethodClientSecretBasicStep2")
public void tokenEndpointAuthMethodClientSecretBasicFail1(final String tokenPath, final String userId, final String userSecret) throws Exception {
    Builder request = ResteasyClientBuilder.newClient().target(url.toString() + tokenPath).request();
    TokenRequest tokenRequest = new TokenRequest(GrantType.RESOURCE_OWNER_PASSWORD_CREDENTIALS);
    tokenRequest.setAuthenticationMethod(AuthenticationMethod.CLIENT_SECRET_POST);
    tokenRequest.setUsername(userId);
    tokenRequest.setPassword(userSecret);
    tokenRequest.setScope("email read_stream manage_pages");
    tokenRequest.setAuthUsername(clientId2);
    tokenRequest.setAuthPassword(clientSecret2);
    request.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    Response response = request.post(Entity.form(new MultivaluedHashMap<String, String>(tokenRequest.getParameters())));
    String entity = response.readEntity(String.class);
    showResponse("tokenEndpointAuthMethodClientSecretBasicFail1", response, entity);
    assertEquals(response.getStatus(), 401, "Unexpected response code.");
    assertNotNull(entity, "Unexpected result: " + entity);
    try {
        JSONObject jsonObj = new JSONObject(entity);
        assertTrue(jsonObj.has("error"), "The error type is null");
        assertTrue(jsonObj.has("error_description"), "The error description is null");
    } catch (JSONException e) {
        e.printStackTrace();
        fail(e.getMessage() + "\nResponse was: " + entity);
    }
}
Also used : Response(javax.ws.rs.core.Response) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JSONObject(org.codehaus.jettison.json.JSONObject) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) TokenRequest(org.xdi.oxauth.client.TokenRequest) JSONException(org.codehaus.jettison.json.JSONException) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test) BaseTest(org.xdi.oxauth.BaseTest)

Example 43 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project oxAuth by GluuFederation.

the class TokenEndpointAuthMethodRestrictionEmbeddedTest method tokenEndpointAuthMethodClientSecretPostFail1.

/**
	 * Fail 1: Call to Token Endpoint with Auth Method
	 * <code>client_secret_basic</code> should fail.
	 */
@Parameters({ "tokenPath", "userId", "userSecret" })
@Test(dependsOnMethods = "tokenEndpointAuthMethodClientSecretPostStep2")
public void tokenEndpointAuthMethodClientSecretPostFail1(final String tokenPath, final String userId, final String userSecret) throws Exception {
    Builder request = ResteasyClientBuilder.newClient().target(url.toString() + tokenPath).request();
    TokenRequest tokenRequest = new TokenRequest(GrantType.RESOURCE_OWNER_PASSWORD_CREDENTIALS);
    tokenRequest.setAuthenticationMethod(AuthenticationMethod.CLIENT_SECRET_BASIC);
    tokenRequest.setUsername(userId);
    tokenRequest.setPassword(userSecret);
    tokenRequest.setScope("email read_stream manage_pages");
    tokenRequest.setAuthUsername(clientId3);
    tokenRequest.setAuthPassword(clientSecret3);
    request.header("Authorization", "Basic " + tokenRequest.getEncodedCredentials());
    request.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    Response response = request.post(Entity.form(new MultivaluedHashMap<String, String>(tokenRequest.getParameters())));
    String entity = response.readEntity(String.class);
    showResponse("tokenEndpointAuthMethodClientSecretPostFail1", response, entity);
    assertEquals(response.getStatus(), 401, "Unexpected response code.");
    assertNotNull(entity, "Unexpected result: " + entity);
    try {
        JSONObject jsonObj = new JSONObject(entity);
        assertTrue(jsonObj.has("error"), "The error type is null");
        assertTrue(jsonObj.has("error_description"), "The error description is null");
    } catch (JSONException e) {
        e.printStackTrace();
        fail(e.getMessage() + "\nResponse was: " + entity);
    }
}
Also used : Response(javax.ws.rs.core.Response) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JSONObject(org.codehaus.jettison.json.JSONObject) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) TokenRequest(org.xdi.oxauth.client.TokenRequest) JSONException(org.codehaus.jettison.json.JSONException) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test) BaseTest(org.xdi.oxauth.BaseTest)

Example 44 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project camel by apache.

the class BonitaAuthFilter method filter.

@Override
public void filter(ClientRequestContext requestContext) throws IOException {
    if (requestContext.getCookies().get("JSESSIONID") == null) {
        String username = bonitaApiConfig.getUsername();
        String password = bonitaApiConfig.getPassword();
        String bonitaApiToken = null;
        if (ObjectHelper.isEmpty(username)) {
            throw new IllegalArgumentException("Username provided is null or empty.");
        }
        if (ObjectHelper.isEmpty(password)) {
            throw new IllegalArgumentException("Password provided is null or empty.");
        }
        ClientBuilder clientBuilder = ClientBuilder.newBuilder();
        Client client = clientBuilder.build();
        WebTarget webTarget = client.target(bonitaApiConfig.getBaseBonitaURI()).path("loginservice");
        MultivaluedMap<String, String> form = new MultivaluedHashMap<String, String>();
        form.add("username", username);
        form.add("password", password);
        form.add("redirect", "false");
        Response response = webTarget.request().accept(MediaType.APPLICATION_FORM_URLENCODED).post(Entity.form(form));
        Map<String, NewCookie> cr = response.getCookies();
        ArrayList<Object> cookies = new ArrayList<>();
        for (NewCookie cookie : cr.values()) {
            if ("X-Bonita-API-Token".equals(cookie.getName())) {
                bonitaApiToken = cookie.getValue();
                requestContext.getHeaders().add("X-Bonita-API-Token", bonitaApiToken);
            }
            cookies.add(cookie.toCookie());
        }
        requestContext.getHeaders().put("Cookie", cookies);
    }
}
Also used : ArrayList(java.util.ArrayList) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) ClientBuilder(javax.ws.rs.client.ClientBuilder) NewCookie(javax.ws.rs.core.NewCookie)

Example 45 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project camel by apache.

the class BonitaAuthFilterConnectionTest method setup.

@Before
public void setup() {
    Mockito.when(requestContext.getCookies()).thenReturn(new HashMap<String, Cookie>());
    Mockito.when(requestContext.getHeaders()).thenReturn(new MultivaluedHashMap());
}
Also used : Cookie(javax.ws.rs.core.Cookie) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Before(org.junit.Before)

Aggregations

MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)301 Response (javax.ws.rs.core.Response)191 Builder (javax.ws.rs.client.Invocation.Builder)151 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)150 Test (org.testng.annotations.Test)146 Parameters (org.testng.annotations.Parameters)144 Test (org.junit.Test)95 JSONException (org.codehaus.jettison.json.JSONException)77 JSONObject (org.codehaus.jettison.json.JSONObject)73 JSONException (org.json.JSONException)73 BaseTest (org.xdi.oxauth.BaseTest)73 BaseTest (org.gluu.oxauth.BaseTest)71 JSONObject (org.json.JSONObject)69 URISyntaxException (java.net.URISyntaxException)58 TokenRequest (org.gluu.oxauth.client.TokenRequest)40 TokenRequest (org.xdi.oxauth.client.TokenRequest)39 URI (java.net.URI)34 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Matchers.containsString (org.hamcrest.Matchers.containsString)26 HashMap (java.util.HashMap)24