Search in sources :

Example 66 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.

the class OAuthJSONProviderTest method doReadClientAccessToken.

@SuppressWarnings({ "unchecked", "rawtypes" })
public ClientAccessToken doReadClientAccessToken(String response, String expectedTokenType, Map<String, String> expectedParams) throws Exception {
    OAuthJSONProvider provider = new OAuthJSONProvider();
    ClientAccessToken token = (ClientAccessToken) provider.readFrom((Class) ClientAccessToken.class, ClientAccessToken.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(response.getBytes()));
    assertEquals("1234", token.getTokenKey());
    assertTrue(expectedTokenType.equalsIgnoreCase(token.getTokenType()));
    assertEquals("5678", token.getRefreshToken());
    assertEquals(12345, token.getExpiresIn());
    assertEquals("read", token.getApprovedScope());
    Map<String, String> extraParams = token.getParameters();
    if (expectedParams != null) {
        assertEquals(expectedParams, extraParams);
    }
    assertEquals("http://abc", extraParams.get("my_parameter"));
    return token;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) Annotation(java.lang.annotation.Annotation)

Example 67 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.

the class OAuthJSONProviderTest method testReadHawkClientAccessToken.

@Test
public void testReadHawkClientAccessToken() throws Exception {
    String response = "{" + "\"access_token\":\"1234\"," + "\"token_type\":\"hawk\"," + "\"refresh_token\":\"5678\"," + "\"expires_in\":12345," + "\"scope\":\"read\"," + "\"secret\":\"adijq39jdlaska9asud\"," + "\"algorithm\":\"hmac-sha-256\"," + "\"my_parameter\":\"http://abc\"" + "}";
    ClientAccessToken macToken = doReadClientAccessToken(response, "hawk", null);
    assertEquals("adijq39jdlaska9asud", macToken.getParameters().get(OAuthConstants.HAWK_TOKEN_KEY));
    assertEquals("hmac-sha-256", macToken.getParameters().get(OAuthConstants.HAWK_TOKEN_ALGORITHM));
}
Also used : ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) Test(org.junit.Test)

Example 68 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.

the class OAuthJSONProviderTest method testWriteHawkClientAccessToken.

@Test
public void testWriteHawkClientAccessToken() throws Exception {
    ClientAccessToken token = new ClientAccessToken("hawk", "1234");
    token.setExpiresIn(12345);
    token.setRefreshToken("5678");
    token.setApprovedScope("read");
    Map<String, String> params = new LinkedHashMap<String, String>();
    params.put(OAuthConstants.HAWK_TOKEN_KEY, "test_mac_secret");
    params.put(OAuthConstants.HAWK_TOKEN_ALGORITHM, OAuthConstants.HMAC_ALGO_SHA_1);
    params.put("my_parameter", "http://abc");
    token.setParameters(params);
    OAuthJSONProvider provider = new OAuthJSONProvider();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(token, ClientAccessToken.class, ClientAccessToken.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), bos);
    doReadClientAccessToken(bos.toString(), OAuthConstants.HAWK_TOKEN_TYPE, params);
}
Also used : ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 69 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.

the class AbstractImplicitGrantService method getClientAccessToken.

protected ClientAccessToken getClientAccessToken(OAuthRedirectionState state, Client client, List<String> requestedScope, List<String> approvedScope, UserSubject userSubject, ServerAccessToken preAuthorizedToken) {
    ServerAccessToken token = null;
    if (preAuthorizedToken == null) {
        AccessTokenRegistration reg = createTokenRegistration(state, client, requestedScope, approvedScope, userSubject);
        token = getDataProvider().createAccessToken(reg);
    } else {
        token = preAuthorizedToken;
        if (state.getNonce() != null) {
            JAXRSUtils.getCurrentMessage().getExchange().put(OAuthConstants.NONCE, state.getNonce());
        }
    }
    ClientAccessToken clientToken = OAuthUtils.toClientAccessToken(token, isWriteOptionalParameters());
    processClientAccessToken(clientToken, token);
    return clientToken;
}
Also used : ServerAccessToken(org.apache.cxf.rs.security.oauth2.common.ServerAccessToken) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) AccessTokenRegistration(org.apache.cxf.rs.security.oauth2.common.AccessTokenRegistration)

Example 70 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.

the class AbstractImplicitGrantService method prepareFormResponse.

protected AbstractFormImplicitResponse prepareFormResponse(OAuthRedirectionState state, Client client, List<String> requestedScope, List<String> approvedScope, UserSubject userSubject, ServerAccessToken preAuthorizedToken) {
    ClientAccessToken clientToken = getClientAccessToken(state, client, requestedScope, approvedScope, userSubject, preAuthorizedToken);
    FormTokenResponse bean = new FormTokenResponse();
    bean.setResponseType(OAuthConstants.TOKEN_RESPONSE_TYPE);
    bean.setRedirectUri(state.getRedirectUri());
    bean.setState(state.getState());
    bean.setAccessToken(clientToken.getTokenKey());
    bean.setAccessTokenType(clientToken.getTokenType());
    bean.setAccessTokenExpiresIn(clientToken.getExpiresIn());
    bean.getParameters().putAll(clientToken.getParameters());
    return bean;
}
Also used : FormTokenResponse(org.apache.cxf.rs.security.oauth2.common.FormTokenResponse) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)

Aggregations

ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)80 WebClient (org.apache.cxf.jaxrs.client.WebClient)62 URL (java.net.URL)44 Response (javax.ws.rs.core.Response)30 Form (javax.ws.rs.core.Form)20 Test (org.junit.Test)18 Book (org.apache.cxf.systest.jaxrs.security.Book)10 JwsJwtCompactConsumer (org.apache.cxf.rs.security.jose.jws.JwsJwtCompactConsumer)7 Consumer (org.apache.cxf.rs.security.oauth2.client.Consumer)7 OAuthJSONProvider (org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider)6 OAuthServiceException (org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException)6 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)5 JsonMapObjectProvider (org.apache.cxf.jaxrs.provider.json.JsonMapObjectProvider)5 TokenIntrospection (org.apache.cxf.rs.security.oauth2.common.TokenIntrospection)5 ClientRegistration (org.apache.cxf.rs.security.oauth2.services.ClientRegistration)5 ClientRegistrationResponse (org.apache.cxf.rs.security.oauth2.services.ClientRegistrationResponse)5 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)3 Produces (javax.ws.rs.Produces)3 Client (javax.ws.rs.client.Client)3