Search in sources :

Example 96 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project artifact-registry-maven-tools by GoogleCloudPlatform.

the class ArtifactRegistryRequestInitializerTest method testInitialize.

@Test
public void testInitialize() throws Exception {
    Credentials creds = GoogleCredentials.create(new AccessToken("test-access-token", Date.from(Instant.now().plusSeconds(1000))));
    ArtifactRegistryRequestInitializer initializer = new ArtifactRegistryRequestInitializer(creds, 100);
    MockHttpTransport transport = new MockHttpTransport.Builder().setLowLevelHttpResponse(new MockLowLevelHttpResponse().setContent("test content")).build();
    GenericUrl url = new GenericUrl("https://www.example.com");
    HttpRequestFactory requestFactory = transport.createRequestFactory(initializer);
    HttpRequest request = requestFactory.buildHeadRequest(url);
    Assert.assertEquals(request.getReadTimeout(), 100);
    Assert.assertEquals(request.getHeaders().getFirstHeaderStringValue("Authorization"), "Bearer test-access-token");
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) AccessToken(com.google.auth.oauth2.AccessToken) GenericUrl(com.google.api.client.http.GenericUrl) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Credentials(com.google.auth.Credentials) Test(org.junit.Test)

Example 97 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project artifact-registry-maven-tools by GoogleCloudPlatform.

the class ArtifactRegistryWagonTest method testHeadExists.

@Test
public void testHeadExists() throws Exception {
    MockHttpTransport transport = new MockHttpTransport.Builder().setLowLevelHttpResponse(new MockLowLevelHttpResponse().setStatusCode(HttpStatusCodes.STATUS_CODE_OK)).build();
    ArtifactRegistryWagon wagon = new ArtifactRegistryWagon();
    wagon.setCredentialProvider(() -> GoogleCredentials.create(new AccessToken("test-access-token", Date.from(Instant.now().plusSeconds(1000)))));
    wagon.setHttpTransportFactory(() -> transport);
    wagon.connect(new Repository("my-repo", REPO_URL));
    Assert.assertTrue(wagon.resourceExists("my/resource"));
}
Also used : Repository(org.apache.maven.wagon.repository.Repository) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) AccessToken(com.google.auth.oauth2.AccessToken) Test(org.junit.Test)

Example 98 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project artifact-registry-maven-tools by GoogleCloudPlatform.

the class ArtifactRegistryWagonTest method testAuthenticatedPut.

@Test
public void testAuthenticatedPut() throws Exception {
    MockHttpTransport transport = new MockHttpTransport.Builder().setLowLevelHttpResponse(new MockLowLevelHttpResponse()).build();
    ArtifactRegistryWagon wagon = new ArtifactRegistryWagon();
    wagon.setCredentialProvider(() -> GoogleCredentials.create(new AccessToken("test-access-token", Date.from(Instant.now().plusSeconds(1000)))));
    wagon.setHttpTransportFactory(() -> transport);
    wagon.connect(new Repository("my-repo", REPO_URL));
    File f = FileTestUtils.createUniqueFile("my/artifact/dir", "test");
    Files.asCharSink(f, Charset.defaultCharset()).write("test content");
    wagon.put(f, "my/resource");
    String authHeader = transport.getLowLevelHttpRequest().getFirstHeaderValue("Authorization");
    Assert.assertEquals("Bearer test-access-token", authHeader);
    Assert.assertEquals("test content", transport.getLowLevelHttpRequest().getContentAsString());
    Assert.assertEquals("https://maven.pkg.dev/my-project/my-repo/my/resource", transport.getLowLevelHttpRequest().getUrl());
}
Also used : Repository(org.apache.maven.wagon.repository.Repository) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) AccessToken(com.google.auth.oauth2.AccessToken) File(java.io.File) Test(org.junit.Test)

Example 99 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project artifact-registry-maven-tools by GoogleCloudPlatform.

the class ArtifactRegistryGradlePlugin method apply.

@Override
public void apply(Object o) {
    ArtifactRegistryPasswordCredentials crd = null;
    try {
        GoogleCredentials credentials = (GoogleCredentials) credentialProvider.getCredential();
        credentials.refreshIfExpired();
        AccessToken accessToken = credentials.getAccessToken();
        String token = accessToken.getTokenValue();
        crd = new ArtifactRegistryPasswordCredentials("oauth2accesstoken", token);
    } catch (IOException e) {
        logger.info("Failed to get access token from gcloud or Application Default Credentials", e);
    }
    if (o instanceof Project) {
        applyProject((Project) o, crd);
    } else if (o instanceof Gradle) {
        applyGradle((Gradle) o, crd);
    } else if (o instanceof Settings) {
        applySettings((Settings) o, crd);
    }
}
Also used : Project(org.gradle.api.Project) AccessToken(com.google.auth.oauth2.AccessToken) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) Gradle(org.gradle.api.invocation.Gradle) Settings(org.gradle.api.initialization.Settings)

Example 100 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project microsoft-authentication-library-common-for-android by AzureAD.

the class ConfidentialClientHelper method requestAccessTokenForAutomation.

/**
 * Yep.  Hardcoding this method to retrieve access token for MSIDLABS
 */
private String requestAccessTokenForAutomation() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, IOException {
    String accessToken = null;
    final TokenRequest tokenRequest = this.createTokenRequest();
    tokenRequest.setGrantType(CLIENT_CREDENTIALS);
    final AccountsInOneOrganization aadAudience = new AccountsInOneOrganization(TENANT_ID);
    final AzureActiveDirectoryAuthority authority = new AzureActiveDirectoryAuthority(aadAudience);
    try {
        final OAuth2StrategyParameters strategyParameters = new OAuth2StrategyParameters();
        OAuth2Strategy<AccessToken, BaseAccount, AuthorizationRequest, AuthorizationRequest.Builder, AuthorizationStrategy, OAuth2Configuration, OAuth2StrategyParameters, AuthorizationResponse, RefreshToken, TokenRequest, TokenResponse, TokenResult, AuthorizationResult> strategy = authority.createOAuth2Strategy(strategyParameters);
        TokenResult tokenResult = strategy.requestToken(tokenRequest);
        if (tokenResult.getSuccess()) {
            accessToken = tokenResult.getTokenResponse().getAccessToken();
        } else {
            throw new RuntimeException(tokenResult.getErrorResponse().getErrorDescription());
        }
    } catch (final ClientException e) {
        e.printStackTrace();
    }
    return accessToken;
}
Also used : AuthorizationStrategy(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationStrategy) AuthorizationRequest(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationRequest) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) OAuth2StrategyParameters(com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters) AuthorizationResult(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult) AuthorizationResponse(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResponse) RefreshToken(com.microsoft.identity.common.internal.providers.oauth2.RefreshToken) TokenResponse(com.microsoft.identity.common.internal.providers.oauth2.TokenResponse) AccessToken(com.microsoft.identity.common.internal.providers.oauth2.AccessToken) BaseAccount(com.microsoft.identity.common.BaseAccount) AccountsInOneOrganization(com.microsoft.identity.common.internal.authorities.AccountsInOneOrganization) TokenRequest(com.microsoft.identity.common.internal.providers.oauth2.TokenRequest) AzureActiveDirectoryAuthority(com.microsoft.identity.common.internal.authorities.AzureActiveDirectoryAuthority) OAuth2Configuration(com.microsoft.identity.common.internal.providers.oauth2.OAuth2Configuration) ClientException(com.microsoft.identity.common.exception.ClientException)

Aggregations

AccessToken (com.google.auth.oauth2.AccessToken)72 Test (org.junit.Test)41 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)30 Date (java.util.Date)22 IOException (java.io.IOException)19 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)16 Client (javax.ws.rs.client.Client)10 AccessToken (org.glassfish.jersey.client.oauth1.AccessToken)10 ConsumerCredentials (org.glassfish.jersey.client.oauth1.ConsumerCredentials)10 JsonObject (io.vertx.core.json.JsonObject)9 URI (java.net.URI)9 Feature (javax.ws.rs.core.Feature)8 JerseyTest (org.glassfish.jersey.test.JerseyTest)8 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)6 InputStreamReader (java.io.InputStreamReader)6 Instant (java.time.Instant)6 WebTarget (javax.ws.rs.client.WebTarget)6 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)6 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)5 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)5