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");
}
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"));
}
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());
}
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);
}
}
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;
}
Aggregations