Search in sources :

Example 1 with Holder

use of org.xdi.oxauth.model.common.Holder in project oxAuth by GluuFederation.

the class TRegisterPermission method registerPermission.

public PermissionTicket registerPermission(final Token p_pat, final String p_umaAmHost, String p_umaHost, final UmaPermission p_request, String path) {
    final Holder<PermissionTicket> ticketH = new Holder<PermissionTicket>();
    Builder request = ResteasyClientBuilder.newClient().target(baseUri.toString() + path).request();
    request.header("Accept", UmaConstants.JSON_MEDIA_TYPE);
    request.header("Authorization", "Bearer " + p_pat.getAccessToken());
    request.header("Host", p_umaAmHost);
    String json = null;
    try {
        json = ServerUtil.createJsonMapper().writeValueAsString(p_request);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    Response response = request.post(Entity.json(json));
    String entity = response.readEntity(String.class);
    BaseTest.showResponse("UMA : TRegisterPermission.registerPermission() : ", response, entity);
    assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(), "Unexpected response code.");
    try {
        final PermissionTicket t = ServerUtil.createJsonMapper().readValue(entity, PermissionTicket.class);
        UmaTestUtil.assert_(t);
        ticketH.setT(t);
    } catch (IOException e) {
        e.printStackTrace();
        fail();
    }
    return ticketH.getT();
}
Also used : Response(javax.ws.rs.core.Response) Holder(org.xdi.oxauth.model.common.Holder) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with Holder

use of org.xdi.oxauth.model.common.Holder in project oxAuth by GluuFederation.

the class TAuthorization method requestAuthorization.

public RptAuthorizationResponse requestAuthorization(String p_umaPermissionAuthorizationPath, final String p_umaAmHost, final Token p_aat, final RptAuthorizationRequest p_request) {
    final Holder<RptAuthorizationResponse> h = new Holder<RptAuthorizationResponse>();
    try {
        Builder request = ResteasyClientBuilder.newClient().target(baseUri.toString() + p_umaPermissionAuthorizationPath).request();
        request.header("Accept", UmaConstants.JSON_MEDIA_TYPE);
        request.header("Authorization", "Bearer " + p_aat.getAccessToken());
        request.header("Host", p_umaAmHost);
        final String json = ServerUtil.createJsonMapper().writeValueAsString(p_request);
        Response response = request.post(Entity.json(json));
        String entity = response.readEntity(String.class);
        BaseTest.showResponse("UMA : TAuthorization.requestAuthorization() : ", response, entity);
        assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "Unexpected response code.");
        try {
            RptAuthorizationResponse result = ServerUtil.createJsonMapper().readValue(entity, RptAuthorizationResponse.class);
            // UmaTestUtil.assert_(result);
            h.setT(result);
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    return h.getT();
}
Also used : Response(javax.ws.rs.core.Response) Holder(org.xdi.oxauth.model.common.Holder) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) IOException(java.io.IOException) IOException(java.io.IOException)

Example 3 with Holder

use of org.xdi.oxauth.model.common.Holder in project oxAuth by GluuFederation.

the class TTokenRequest method newTokenByRefreshToken.

public Token newTokenByRefreshToken(final String tokenPath, final Token p_oldToken, final String umaClientId, final String umaClientSecret) {
    if (p_oldToken == null || StringUtils.isBlank(p_oldToken.getRefreshToken()) || StringUtils.isBlank(tokenPath)) {
        throw new IllegalArgumentException("Refresh token or tokenPath is empty.");
    }
    final Holder<Token> t = new Holder<Token>();
    try {
        TokenRequest tokenRequest = new TokenRequest(GrantType.REFRESH_TOKEN);
        tokenRequest.setAuthUsername(umaClientId);
        tokenRequest.setAuthPassword(umaClientSecret);
        tokenRequest.setRefreshToken(p_oldToken.getRefreshToken());
        tokenRequest.setScope(p_oldToken.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");
            final Token newToken = new Token();
            newToken.setAccessToken(accessToken);
            newToken.setRefreshToken(refreshToken);
            t.setT(newToken);
        } catch (JSONException e) {
            e.printStackTrace();
            fail(e.getMessage() + "\nResponse was: " + entity);
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    return t.getT();
}
Also used : Response(javax.ws.rs.core.Response) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JSONObject(org.codehaus.jettison.json.JSONObject) Holder(org.xdi.oxauth.model.common.Holder) 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) Token(org.xdi.oxauth.model.uma.wrapper.Token) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException)

Example 4 with Holder

use of org.xdi.oxauth.model.common.Holder in project oxAuth by GluuFederation.

the class TTokenRequest method requestRpt.

public RPTResponse requestRpt(final Token p_aat, final String p_rptPath, final String p_umaAmHost) {
    final Holder<RPTResponse> h = new Holder<RPTResponse>();
    try {
        Builder request = ResteasyClientBuilder.newClient().target(baseUri.toString() + p_rptPath).request();
        request.header("Accept", UmaConstants.JSON_MEDIA_TYPE);
        request.header("Authorization", "Bearer " + p_aat.getAccessToken());
        request.header("Host", p_umaAmHost);
        Response response = request.post(Entity.form(new Form()));
        String entity = response.readEntity(String.class);
        BaseTest.showResponse("UMA : TTokenRequest.requestRpt() : ", response, entity);
        assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(), "Unexpected response code.");
        try {
            String tokenResponse = entity;
            final JSONObject jsonObj = new JSONObject(tokenResponse);
            if (jsonObj.has("requesterPermissionTokenResponse")) {
                tokenResponse = jsonObj.get("requesterPermissionTokenResponse").toString();
            }
            System.out.println("Token response = " + tokenResponse);
            RPTResponse result = ServerUtil.createJsonMapper().readValue(tokenResponse, RPTResponse.class);
            UmaTestUtil.assert_(result);
            h.setT(result);
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        } catch (JSONException e) {
            e.printStackTrace();
            fail();
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    return h.getT();
}
Also used : Response(javax.ws.rs.core.Response) JSONObject(org.codehaus.jettison.json.JSONObject) Form(javax.ws.rs.core.Form) Holder(org.xdi.oxauth.model.common.Holder) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) JSONException(org.codehaus.jettison.json.JSONException) IOException(java.io.IOException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException)

Example 5 with Holder

use of org.xdi.oxauth.model.common.Holder in project oxAuth by GluuFederation.

the class TTokenRequest method requestRptStatus.

public RptIntrospectionResponse requestRptStatus(String p_umaRptStatusPath, final String p_umaAmHost, final Token p_aat, final String rpt) {
    final Holder<RptIntrospectionResponse> h = new Holder<RptIntrospectionResponse>();
    try {
        Builder request = ResteasyClientBuilder.newClient().target(baseUri.toString() + p_umaRptStatusPath).request();
        request.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
        request.header("Authorization", "Bearer " + p_aat.getAccessToken());
        Response response = request.post(Entity.form(new Form("token", rpt)));
        String entity = response.readEntity(String.class);
        // request.addHeader("Host", p_umaAmHost);
        // try {
        // final String json =
        // ServerUtil.createJsonMapper().writeValueAsString(rpt);
        // request.setContent(Util.getBytes(json));
        // request.setContentType(UmaConstants.JSON_MEDIA_TYPE);
        // } catch (IOException e) {
        // e.printStackTrace();
        // fail();
        // }
        BaseTest.showResponse("UMA : TTokenRequest.requestRptStatus() : ", response, entity);
        assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "Unexpected response code.");
        try {
            final RptIntrospectionResponse result = ServerUtil.createJsonMapper().readValue(entity, RptIntrospectionResponse.class);
            Assert.assertNotNull(result);
            h.setT(result);
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    return h.getT();
}
Also used : Response(javax.ws.rs.core.Response) Form(javax.ws.rs.core.Form) Holder(org.xdi.oxauth.model.common.Holder) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) IOException(java.io.IOException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException)

Aggregations

IOException (java.io.IOException)5 Builder (javax.ws.rs.client.Invocation.Builder)5 Response (javax.ws.rs.core.Response)5 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)5 Holder (org.xdi.oxauth.model.common.Holder)5 JSONException (org.codehaus.jettison.json.JSONException)3 Form (javax.ws.rs.core.Form)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 TokenRequest (org.xdi.oxauth.client.TokenRequest)1 Token (org.xdi.oxauth.model.uma.wrapper.Token)1