Search in sources :

Example 26 with OAuthJSONProvider

use of org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider in project cxf by apache.

the class OAuthClientUtils method getAccessToken.

/**
 * Obtains the access token from OAuth AccessToken Service
 * @param accessTokenServiceUri the AccessToken endpoint address
 * @param consumer {@link Consumer} representing the registered client
 * @param grant {@link AccessTokenGrant} grant
 * @param setAuthorizationHeader if set to true then HTTP Basic scheme
 *           will be used to pass client id and secret, otherwise they will
 *           be passed in the form payload
 * @return {@link ClientAccessToken} access token
 * @throws OAuthServiceException
 */
public static ClientAccessToken getAccessToken(String accessTokenServiceUri, Consumer consumer, AccessTokenGrant grant, boolean setAuthorizationHeader) throws OAuthServiceException {
    OAuthJSONProvider provider = new OAuthJSONProvider();
    WebClient accessTokenService = WebClient.create(accessTokenServiceUri, Collections.singletonList(provider));
    accessTokenService.accept("application/json");
    return getAccessToken(accessTokenService, consumer, grant, setAuthorizationHeader);
}
Also used : OAuthJSONProvider(org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 27 with OAuthJSONProvider

use of org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider in project meecrowave by apache.

the class OAuth2Test method getRefreshTokenNoClient.

@Test
public void getRefreshTokenNoClient() {
    final Client client = ClientBuilder.newClient().register(new OAuthJSONProvider());
    try {
        // password
        final ClientAccessToken primary = client.target("http://localhost:" + MEECROWAVE.getConfiguration().getHttpPort()).path("oauth2/token").request(APPLICATION_JSON_TYPE).post(entity(new Form().param("grant_type", "password").param("username", "test").param("password", "pwd"), APPLICATION_FORM_URLENCODED_TYPE), ClientAccessToken.class);
        // refresh
        final ClientAccessToken token = client.target("http://localhost:" + MEECROWAVE.getConfiguration().getHttpPort()).path("oauth2/token").request(APPLICATION_JSON_TYPE).post(entity(new Form().param("grant_type", "refresh_token").param("refresh_token", primary.getRefreshToken()), APPLICATION_FORM_URLENCODED_TYPE), ClientAccessToken.class);
        assertNotNull(token);
        assertEquals("Bearer", token.getTokenType());
        assertIsJwt(token.getTokenKey(), "__default");
        assertEquals(3600, token.getExpiresIn());
        assertNotEquals(0, token.getIssuedAt());
        assertNotNull(token.getRefreshToken());
    } finally {
        client.close();
    }
}
Also used : Form(javax.ws.rs.core.Form) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) OAuthJSONProvider(org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Aggregations

OAuthJSONProvider (org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider)21 OAuthAuthorizationData (org.apache.cxf.rs.security.oauth2.common.OAuthAuthorizationData)16 JSONProvider (org.apache.cxf.jaxrs.provider.json.JSONProvider)15 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)8 JsonMapObjectProvider (org.apache.cxf.jaxrs.provider.json.JsonMapObjectProvider)7 JsonWebKeysProvider (org.apache.cxf.rs.security.jose.jaxrs.JsonWebKeysProvider)7 ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)7 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Annotation (java.lang.annotation.Annotation)4 Form (javax.ws.rs.core.Form)4 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)4 Client (javax.ws.rs.client.Client)3 TokenIntrospection (org.apache.cxf.rs.security.oauth2.common.TokenIntrospection)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Response (javax.ws.rs.core.Response)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ProcessingException (javax.ws.rs.ProcessingException)1