Search in sources :

Example 36 with Builder

use of javax.ws.rs.client.Invocation.Builder in project oxAuth by GluuFederation.

the class TTokenRequest method requestToken.

private void requestToken(final String tokenPath, final String umaClientId, final String umaClientSecret, final String umaRedirectUri) throws Exception {
    if (token == null || StringUtils.isBlank(token.getAuthorizationCode())) {
        throw new IllegalArgumentException("Authorization code is not initialized.");
    }
    TokenRequest tokenRequest = new TokenRequest(GrantType.AUTHORIZATION_CODE);
    tokenRequest.setCode(token.getAuthorizationCode());
    tokenRequest.setRedirectUri(umaRedirectUri);
    tokenRequest.setAuthUsername(umaClientId);
    tokenRequest.setAuthPassword(umaClientSecret);
    tokenRequest.setAuthenticationMethod(AuthenticationMethod.CLIENT_SECRET_BASIC);
    tokenRequest.setScope(token.getScope());
    Builder request = ResteasyClientBuilder.newClient().target(baseUri.toString() + tokenPath).request();
    request.header("Authorization", "Basic " + tokenRequest.getEncodedCredentials());
    Response response = request.post(Entity.form(new MultivaluedHashMap<String, String>(tokenRequest.getParameters())));
    String entity = response.readEntity(String.class);
    BaseTest.showResponse("TTokenClient.requestToken() :", response, entity);
    assertEquals(response.getStatus(), 200, "Unexpected response code.");
    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"), "Unexpected result: id_token
        // not found");
        String accessToken = jsonObj.getString("access_token");
        String refreshToken = jsonObj.getString("refresh_token");
        // String idToken = jsonObj.getString("id_token");
        token.setAccessToken(accessToken);
        token.setRefreshToken(refreshToken);
    // m_token.setIdToken(idToken);
    } 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) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException)

Example 37 with Builder

use of javax.ws.rs.client.Invocation.Builder in project oxAuth by GluuFederation.

the class TTokenRequest method requestAuthorizationCode.

private void requestAuthorizationCode(final String authorizePath, final String userId, final String userSecret, final String umaClientId, final String umaRedirectUri, final String p_scopeType) throws Exception {
    List<ResponseType> responseTypes = new ArrayList<ResponseType>();
    responseTypes.add(ResponseType.CODE);
    responseTypes.add(ResponseType.ID_TOKEN);
    List<String> scopes = new ArrayList<String>();
    scopes.add(p_scopeType);
    String state = UUID.randomUUID().toString();
    String nonce = UUID.randomUUID().toString();
    AuthorizationRequest authorizationRequest = new AuthorizationRequest(responseTypes, umaClientId, scopes, umaRedirectUri, nonce);
    authorizationRequest.setState(state);
    authorizationRequest.setAuthUsername(userId);
    authorizationRequest.setAuthPassword(userSecret);
    authorizationRequest.getPrompts().add(Prompt.NONE);
    Builder request = ResteasyClientBuilder.newClient().target(baseUri.toString() + authorizePath + "?" + authorizationRequest.getQueryString()).request();
    request.header("Authorization", "Basic " + authorizationRequest.getEncodedCredentials());
    request.header("Accept", MediaType.TEXT_PLAIN);
    Response response = request.get();
    String entity = response.readEntity(String.class);
    BaseTest.showResponse("TTokenClient.requestAuthorizationCode() : ", response, entity);
    assertEquals(response.getStatus(), 302, "Unexpected response code.");
    assertNotNull(response.getLocation(), "Unexpected result: " + response.getLocation());
    if (response.getLocation() != null) {
        try {
            final String location = response.getLocation().toString();
            final int fragmentIndex = location.indexOf("#");
            Map<String, String> params = new HashMap<String, String>();
            if (fragmentIndex != -1) {
                String fragment = location.substring(fragmentIndex + 1);
                params = QueryStringDecoder.decode(fragment);
            } else {
                int queryStringIndex = location.indexOf("?");
                if (queryStringIndex != -1) {
                    String queryString = location.substring(queryStringIndex + 1);
                    params = QueryStringDecoder.decode(queryString);
                }
            }
            assertNotNull(params.get("code"), "The code is null");
            assertNotNull(params.get("scope"), "The scope is null");
            assertNotNull(params.get("state"), "The state is null");
            token.setAuthorizationCode(params.get("code"));
            token.setScope(params.get("scope"));
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) AuthorizationRequest(org.xdi.oxauth.client.AuthorizationRequest) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) ResponseType(org.xdi.oxauth.model.common.ResponseType)

Example 38 with Builder

use of javax.ws.rs.client.Invocation.Builder in project oxAuth by GluuFederation.

the class AuthorizeRestWebServiceEmbeddedTest method requestAuthorizationTokenFail1.

@Parameters({ "authorizePath", "userId", "userSecret", "redirectUri" })
@Test
public void requestAuthorizationTokenFail1(final String authorizePath, final String userId, final String userSecret, final String redirectUri) throws Exception {
    final String state = UUID.randomUUID().toString();
    // Testing with missing parameters
    List<ResponseType> responseTypes = Arrays.asList(ResponseType.TOKEN, ResponseType.ID_TOKEN);
    List<String> scopes = Arrays.asList("openid", "profile", "address", "email");
    String nonce = UUID.randomUUID().toString();
    AuthorizationRequest authorizationRequest = new AuthorizationRequest(responseTypes, null, scopes, redirectUri, nonce);
    authorizationRequest.setState(state);
    authorizationRequest.setAuthUsername(userId);
    authorizationRequest.setAuthPassword(userSecret);
    Builder request = ResteasyClientBuilder.newClient().target(url.toString() + authorizePath + "?" + authorizationRequest.getQueryString()).request();
    request.header("Authorization", "Basic " + authorizationRequest.getEncodedCredentials());
    request.header("Accept", MediaType.TEXT_PLAIN);
    Response response = request.get();
    String entity = response.readEntity(String.class);
    showResponse("requestAuthorizationTokenFail1", response, entity);
    assertEquals(response.getStatus(), 400, "Unexpected response code.");
    assertNotNull(entity, "Unexpected result: " + entity);
    try {
        JSONObject jsonObj = new JSONObject(entity);
        assertTrue(jsonObj.has("error"), "The error type is null");
        assertEquals(jsonObj.getString("error"), "invalid_request");
        assertTrue(jsonObj.has("error_description"), "The error description is null");
        assertEquals(jsonObj.get(AuthorizeResponseParam.STATE), state);
    } catch (JSONException e) {
        e.printStackTrace();
        fail(e.getMessage() + "\nResponse was: " + entity);
    }
}
Also used : RegisterResponse(org.xdi.oxauth.client.RegisterResponse) Response(javax.ws.rs.core.Response) AuthorizationRequest(org.xdi.oxauth.client.AuthorizationRequest) JSONObject(org.codehaus.jettison.json.JSONObject) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) JSONException(org.codehaus.jettison.json.JSONException) ResponseType(org.xdi.oxauth.model.common.ResponseType) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test) BaseTest(org.xdi.oxauth.BaseTest)

Example 39 with Builder

use of javax.ws.rs.client.Invocation.Builder in project oxAuth by GluuFederation.

the class AuthorizeRestWebServiceEmbeddedTest method requestAuthorizationPromptNoneLoginConsentFail.

@Parameters({ "authorizePath", "userId", "userSecret", "redirectUri" })
@Test(dependsOnMethods = "dynamicClientRegistration")
public void requestAuthorizationPromptNoneLoginConsentFail(final String authorizePath, final String userId, final String userSecret, final String redirectUri) throws Exception {
    final String state = UUID.randomUUID().toString();
    List<ResponseType> responseTypes = Arrays.asList(ResponseType.CODE);
    List<String> scopes = Arrays.asList("openid", "profile", "address", "email");
    AuthorizationRequest authorizationRequest = new AuthorizationRequest(responseTypes, clientId1, scopes, redirectUri, null);
    authorizationRequest.setState(state);
    authorizationRequest.getPrompts().add(Prompt.NONE);
    authorizationRequest.getPrompts().add(Prompt.LOGIN);
    authorizationRequest.getPrompts().add(Prompt.CONSENT);
    authorizationRequest.setAuthUsername(userId);
    authorizationRequest.setAuthPassword(userSecret);
    Builder request = ResteasyClientBuilder.newClient().target(url.toString() + authorizePath + "?" + authorizationRequest.getQueryString()).request();
    request.header("Authorization", "Basic " + authorizationRequest.getEncodedCredentials());
    Response response = request.get();
    String entity = response.readEntity(String.class);
    showResponse("requestAuthorizationPromptNoneLoginConsentFail", response, entity);
    assertEquals(response.getStatus(), 302, "Unexpected response code.");
    assertNotNull(response.getLocation(), "Unexpected result: " + response.getLocation());
    if (response.getLocation() != null) {
        try {
            URI uri = new URI(response.getLocation().toString());
            assertNotNull(uri.getQuery(), "Query is null");
            Map<String, String> params = QueryStringDecoder.decode(uri.getQuery());
            assertNotNull(params.get("error"), "The error value is null");
            assertNotNull(params.get("error_description"), "The errorDescription value is null");
            assertNotNull(params.get(AuthorizeResponseParam.STATE), "The state is null");
            assertEquals(params.get(AuthorizeResponseParam.STATE), state);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            fail("Response URI is not well formed");
        }
    }
}
Also used : RegisterResponse(org.xdi.oxauth.client.RegisterResponse) Response(javax.ws.rs.core.Response) AuthorizationRequest(org.xdi.oxauth.client.AuthorizationRequest) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) URISyntaxException(java.net.URISyntaxException) REGISTRATION_CLIENT_URI(org.xdi.oxauth.model.register.RegisterResponseParam.REGISTRATION_CLIENT_URI) URI(java.net.URI) ResponseType(org.xdi.oxauth.model.common.ResponseType) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test) BaseTest(org.xdi.oxauth.BaseTest)

Example 40 with Builder

use of javax.ws.rs.client.Invocation.Builder in project oxAuth by GluuFederation.

the class AuthorizeRestWebServiceEmbeddedTest method requestAuthorizationCodeFail3.

@Parameters({ "authorizePath", "userId", "userSecret", "redirectUri" })
@Test
public void requestAuthorizationCodeFail3(final String authorizePath, final String userId, final String userSecret, final String redirectUri) throws Exception {
    final String state = UUID.randomUUID().toString();
    String clientId = "@!1111!0008!INVALID_VALUE";
    List<ResponseType> responseTypes = Arrays.asList(ResponseType.CODE);
    List<String> scopes = Arrays.asList("openid", "profile", "address", "email");
    AuthorizationRequest authorizationRequest = new AuthorizationRequest(responseTypes, clientId, scopes, redirectUri, null);
    authorizationRequest.setState(state);
    authorizationRequest.getPrompts().add(Prompt.NONE);
    authorizationRequest.setAuthUsername(userId);
    authorizationRequest.setAuthPassword(userSecret);
    Builder request = ResteasyClientBuilder.newClient().target(url.toString() + authorizePath + "?" + authorizationRequest.getQueryString()).request();
    request.header("Authorization", "Basic " + authorizationRequest.getEncodedCredentials());
    request.header("Accept", MediaType.TEXT_PLAIN);
    Response response = request.get();
    String entity = response.readEntity(String.class);
    showResponse("requestAuthorizationCodeFail3", 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");
        assertEquals(jsonObj.getString("error"), "unauthorized_client");
        assertTrue(jsonObj.has("error_description"), "The error description is null");
        assertEquals(jsonObj.get(AuthorizeResponseParam.STATE), state);
    } catch (JSONException e) {
        e.printStackTrace();
        fail(e.getMessage() + "\nResponse was: " + entity);
    }
}
Also used : RegisterResponse(org.xdi.oxauth.client.RegisterResponse) Response(javax.ws.rs.core.Response) AuthorizationRequest(org.xdi.oxauth.client.AuthorizationRequest) JSONObject(org.codehaus.jettison.json.JSONObject) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) JSONException(org.codehaus.jettison.json.JSONException) ResponseType(org.xdi.oxauth.model.common.ResponseType) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test) BaseTest(org.xdi.oxauth.BaseTest)

Aggregations

Builder (javax.ws.rs.client.Invocation.Builder)319 Response (javax.ws.rs.core.Response)316 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)287 Test (org.testng.annotations.Test)273 BaseTest (org.xdi.oxauth.BaseTest)273 Parameters (org.testng.annotations.Parameters)270 JSONException (org.codehaus.jettison.json.JSONException)248 JSONObject (org.codehaus.jettison.json.JSONObject)173 URISyntaxException (java.net.URISyntaxException)121 ResponseType (org.xdi.oxauth.model.common.ResponseType)120 RegisterResponse (org.xdi.oxauth.client.RegisterResponse)92 AuthorizationRequest (org.xdi.oxauth.client.AuthorizationRequest)88 URI (java.net.URI)86 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)79 RegisterRequest (org.xdi.oxauth.client.RegisterRequest)78 REGISTRATION_CLIENT_URI (org.xdi.oxauth.model.register.RegisterResponseParam.REGISTRATION_CLIENT_URI)73 OxAuthCryptoProvider (org.xdi.oxauth.model.crypto.OxAuthCryptoProvider)60 JwtAuthorizationRequest (org.xdi.oxauth.client.model.authorize.JwtAuthorizationRequest)44 TokenRequest (org.xdi.oxauth.client.TokenRequest)39 Claim (org.xdi.oxauth.client.model.authorize.Claim)39