Search in sources :

Example 6 with OAuthJSONProvider

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

the class BigQueryServer method getAccessToken.

private static ClientAccessToken getAccessToken(PrivateKey privateKey, String issuer) {
    JwsHeaders headers = new JwsHeaders(JoseType.JWT, SignatureAlgorithm.RS256);
    JwtClaims claims = new JwtClaims();
    claims.setIssuer(issuer);
    claims.setAudience("https://www.googleapis.com/oauth2/v3/token");
    long issuedAt = OAuthUtils.getIssuedAt();
    claims.setIssuedAt(issuedAt);
    claims.setExpiryTime(issuedAt + 60 * 60);
    claims.setProperty("scope", "https://www.googleapis.com/auth/bigquery.readonly");
    JwtToken token = new JwtToken(headers, claims);
    JwsJwtCompactProducer p = new JwsJwtCompactProducer(token);
    String base64UrlAssertion = p.signWith(privateKey);
    JwtBearerGrant grant = new JwtBearerGrant(base64UrlAssertion);
    WebClient accessTokenService = WebClient.create("https://www.googleapis.com/oauth2/v3/token", Arrays.asList(new OAuthJSONProvider(), new AccessTokenGrantWriter()));
    WebClient.getConfig(accessTokenService).getInInterceptors().add(new LoggingInInterceptor());
    accessTokenService.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON);
    return accessTokenService.post(grant, ClientAccessToken.class);
}
Also used : JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) JwsJwtCompactProducer(org.apache.cxf.rs.security.jose.jws.JwsJwtCompactProducer) JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) JwtBearerGrant(org.apache.cxf.rs.security.oauth2.grants.jwt.JwtBearerGrant) AccessTokenGrantWriter(org.apache.cxf.rs.security.oauth2.client.AccessTokenGrantWriter) OAuthJSONProvider(org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider) LoggingInInterceptor(org.apache.cxf.interceptor.LoggingInInterceptor) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 7 with OAuthJSONProvider

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

the class OAuth2TestUtils method setupProviders.

public static List<Object> setupProviders() {
    List<Object> providers = new ArrayList<>();
    JSONProvider<OAuthAuthorizationData> jsonP = new JSONProvider<OAuthAuthorizationData>();
    jsonP.setNamespaceMap(Collections.singletonMap("http://org.apache.cxf.rs.security.oauth", "ns2"));
    providers.add(jsonP);
    providers.add(new OAuthJSONProvider());
    providers.add(new JsonWebKeysProvider());
    providers.add(new JsonMapObjectProvider());
    return providers;
}
Also used : JsonWebKeysProvider(org.apache.cxf.rs.security.jose.jaxrs.JsonWebKeysProvider) ArrayList(java.util.ArrayList) JSONProvider(org.apache.cxf.jaxrs.provider.json.JSONProvider) OAuthJSONProvider(org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider) OAuthJSONProvider(org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider) JsonMapObjectProvider(org.apache.cxf.jaxrs.provider.json.JsonMapObjectProvider) OAuthAuthorizationData(org.apache.cxf.rs.security.oauth2.common.OAuthAuthorizationData)

Example 8 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());
        assertNotNull(token.getTokenKey());
        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)

Example 9 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 10 with OAuthJSONProvider

use of org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider 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)

Aggregations

Test (org.junit.Test)8 ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)7 OAuthJSONProvider (org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider)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 OAuthAuthorizationData (org.apache.cxf.rs.security.oauth2.common.OAuthAuthorizationData)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ProcessingException (javax.ws.rs.ProcessingException)1 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)1