Skip to content

Commit

Permalink
feat: added getter for credentials object in HttpCredentialsAdapter (#…
Browse files Browse the repository at this point in the history
…658)

* feat: added getter for credentials object in HttpCredentialsAdapter

* add test for getCredentials

* fix imports and formatting
  • Loading branch information
shubha-rajan authored May 13, 2021
1 parent cc13ac6 commit 5a946ea
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public HttpCredentialsAdapter(Credentials credentials) {
this.credentials = credentials;
}

/** A getter for the credentials instance being used */
public Credentials getCredentials() {
return credentials;
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@

package com.google.auth.http;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory;
import com.google.auth.oauth2.MockTokenCheckingTransport;
import com.google.auth.oauth2.OAuth2Credentials;
Expand Down Expand Up @@ -149,4 +152,25 @@ public void initialize_noURI() throws IOException {
String authorizationHeader = requestHeaders.getAuthorization();
assertEquals(authorizationHeader, expectedAuthorization);
}

@Test
public void getCredentials() {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory tokenServerTransportFactory =
new MockTokenServerTransportFactory();
tokenServerTransportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
tokenServerTransportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken);

OAuth2Credentials credentials =
UserCredentials.newBuilder()
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setRefreshToken(REFRESH_TOKEN)
.setHttpTransportFactory(tokenServerTransportFactory)
.build();

HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(credentials);
Credentials returnedCredentials = adapter.getCredentials();
assertThat(returnedCredentials, instanceOf(Credentials.class));
}
}
6 changes: 6 additions & 0 deletions oauth2_http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

0 comments on commit 5a946ea

Please sign in to comment.