Search in sources :

Example 41 with AccessToken

use of com.google.auth.oauth2.AccessToken in project google-auth-library-java by google.

the class AppEngineCredentials method refreshAccessToken.

/**
 * Refresh the access token by getting it from the App Identity service
 */
@Override
public AccessToken refreshAccessToken() throws IOException {
    if (createScopedRequired()) {
        throw new IOException("AppEngineCredentials requires createScoped call before use.");
    }
    GetAccessTokenResult accessTokenResponse = appIdentityService.getAccessToken(scopes);
    String accessToken = accessTokenResponse.getAccessToken();
    Date expirationTime = accessTokenResponse.getExpirationTime();
    return new AccessToken(accessToken, expirationTime);
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) IOException(java.io.IOException) GetAccessTokenResult(com.google.appengine.api.appidentity.AppIdentityService.GetAccessTokenResult) Date(java.util.Date)

Example 42 with AccessToken

use of com.google.auth.oauth2.AccessToken in project api-framework by vinscom.

the class LoadUserFromAccessTokenRouteBuillder method handle.

public void handle(RoutingContext pRoutingContext) {
    if (pRoutingContext.user() == null) {
        String access_token = pRoutingContext.request().getHeader(HttpHeaders.AUTHORIZATION);
        if (!Strings.isNullOrEmpty(access_token)) {
            OAuth2AuthProviderImpl provider = (OAuth2AuthProviderImpl) getOAuth2Auth().getDelegate();
            JsonObject accessToken = new JsonObject().put("access_token", access_token.split(" ")[1]);
            try {
                OAuth2TokenImpl token = new OAuth2TokenImpl(provider, accessToken);
                pRoutingContext.setUser(new AccessToken(token));
            } catch (RuntimeException e) {
                getLog().error(e);
                pRoutingContext.fail(401);
                return;
            }
        }
    }
    pRoutingContext.next();
}
Also used : AccessToken(io.vertx.reactivex.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) OAuth2AuthProviderImpl(io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)

Example 43 with AccessToken

use of com.google.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class OAuth2IntrospectTest method introspectAccessTokenKeyCloakWay.

@Test
public void introspectAccessTokenKeyCloakWay() {
    config = oauthIntrospect;
    fixture = fixtureKeycloak;
    oauth2.introspectToken(token, res -> {
        if (res.failed()) {
            fail(res.cause().getMessage());
        } else {
            AccessToken token = res.result();
            assertNotNull(token);
            JsonObject principal = token.principal();
            assertTrue(principal.getBoolean("active"));
            testComplete();
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 44 with AccessToken

use of com.google.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class OAuth2IntrospectTest method introspectAccessTokenGoogleWay.

@Test
public void introspectAccessTokenGoogleWay() {
    config = oauthIntrospect;
    fixture = fixtureGoogle;
    oauth2.introspectToken(token, res -> {
        if (res.failed()) {
            fail(res.cause().getMessage());
        } else {
            AccessToken token = res.result();
            assertNotNull(token);
            // make a copy because later we need to original data
            JsonObject principal = token.principal().copy();
            // clean time specific value
            principal.remove("expires_at");
            principal.remove("access_token");
            assertEquals(fixtureGoogle.getMap(), principal.getMap());
            token.isAuthorized("profile", res0 -> {
                if (res0.failed()) {
                    fail(res0.cause().getMessage());
                } else {
                    if (res0.result()) {
                        // Issue #142
                        // the test is a replay of the same test so all checks have
                        // been done above.
                        // the replay shows that the api can be used from the user object
                        // directly too
                        token.introspect(v -> {
                            if (v.failed()) {
                                fail(v.cause());
                            } else {
                                testComplete();
                            }
                        });
                    } else {
                        fail("Should be allowed");
                    }
                }
            });
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 45 with AccessToken

use of com.google.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class OAuth2UserInfoTest method getUserInfoWithParams.

@Test
public void getUserInfoWithParams() {
    final AccessToken accessToken = new OAuth2TokenImpl((OAuth2AuthProviderImpl) oauth2, new JsonObject("{\"access_token\":\"eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJhdXRob3JpemF0aW9uIjp7InBlcm1pc3Npb25zIjpbeyJyZXNvdXJjZV9zZXRfaWQiOiJkMmZlOTg0My02NDYyLTRiZmMtYmFiYS1iNTc4N2JiNmUwZTciLCJyZXNvdXJjZV9zZXRfbmFtZSI6IkhlbGxvIFdvcmxkIFJlc291cmNlIn1dfSwianRpIjoiZDYxMDlhMDktNzhmZC00OTk4LWJmODktOTU3MzBkZmQwODkyLTE0NjQ5MDY2Nzk0MDUiLCJleHAiOjk5OTk5OTk5OTksIm5iZiI6MCwiaWF0IjoxNDY0OTA2NjcxLCJzdWIiOiJmMTg4OGY0ZC01MTcyLTQzNTktYmUwYy1hZjMzODUwNWQ4NmMiLCJ0eXAiOiJrY19ldHQiLCJhenAiOiJoZWxsby13b3JsZC1hdXRoei1zZXJ2aWNlIn0\",\"active\":true,\"scope\":\"scopeA scopeB\",\"client_id\":\"client-id\",\"username\":\"username\",\"token_type\":\"bearer\",\"expires_at\":99999999999000}"));
    accessToken.userInfo(userInfo -> {
        if (userInfo.failed()) {
            fail(userInfo.cause().getMessage());
        } else {
            assertEquals(fixture, userInfo.result());
            testComplete();
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)40 AccessToken (com.google.auth.oauth2.AccessToken)17 URI (java.net.URI)14 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)13 MockTokenServerTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory)12 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)8 JsonObject (io.vertx.core.json.JsonObject)8 Date (java.util.Date)8 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)7 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)6 MockHttpTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory)6 IOException (java.io.IOException)5 Client (javax.ws.rs.client.Client)5 AccessToken (org.glassfish.jersey.client.oauth1.AccessToken)5 ConsumerCredentials (org.glassfish.jersey.client.oauth1.ConsumerCredentials)5 Metadata (io.grpc.Metadata)4 Feature (javax.ws.rs.core.Feature)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 HttpRequest (com.google.api.client.http.HttpRequest)3 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)3