Skip to content

Commit

Permalink
Making AwsSecurityCredentials public and change name to sessionToken
Browse files Browse the repository at this point in the history
  • Loading branch information
aeitzman committed Jan 22, 2024
1 parent 20c2f7d commit abd462b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
4 changes: 2 additions & 2 deletions oauth2_http/java/com/google/auth/oauth2/AwsRequestSigner.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ private Map<String, String> getCanonicalHeaders(String defaultDate) {
headers.put("x-amz-date", defaultDate);
}

if (awsSecurityCredentials.getToken() != null && !awsSecurityCredentials.getToken().isEmpty()) {
headers.put("x-amz-security-token", awsSecurityCredentials.getToken());
if (awsSecurityCredentials.getSessionToken() != null && !awsSecurityCredentials.getSessionToken().isEmpty()) {
headers.put("x-amz-security-token", awsSecurityCredentials.getSessionToken());
}

// Add all additional headers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,48 @@
* Defines AWS security credentials. These are either retrieved from the AWS security_credentials
* endpoint or AWS environment variables.
*/
class AwsSecurityCredentials {
public class AwsSecurityCredentials {

private final String accessKeyId;
private final String secretAccessKey;

@Nullable private final String token;
@Nullable private final String sessionToken;

AwsSecurityCredentials(String accessKeyId, String secretAccessKey, @Nullable String token) {
/**
* Constructor for AWSSecurityCredentials.
*
* @param accessKeyId the AWS access Key Id.
* @param secretAccessKey the AWS secret access key.
* @param sessionToken the AWS session token. Optional.
*/
public AwsSecurityCredentials(String accessKeyId, String secretAccessKey, @Nullable String sessionToken) {
this.accessKeyId = accessKeyId;
this.secretAccessKey = secretAccessKey;
this.token = token;
this.sessionToken = sessionToken;
}

String getAccessKeyId() {
/**
* Gets the AWS access key id.
* @return the AWS access key id.
*/
public String getAccessKeyId() {
return accessKeyId;
}

String getSecretAccessKey() {
/**
* Gets the AWS secret access key.
* @return the AWS secret access key.
*/
public String getSecretAccessKey() {
return secretAccessKey;
}

/**
* Gets the AWS session token.
* @return the AWS session token.
*/
@Nullable
String getToken() {
return token;
public String getSessionToken() {
return sessionToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ public void getAwsSecurityCredentials_fromEnvironmentVariablesNoToken() throws I

assertEquals("awsAccessKeyId", credentials.getAccessKeyId());
assertEquals("awsSecretAccessKey", credentials.getSecretAccessKey());
assertNull(credentials.getToken());
assertNull(credentials.getSessionToken());
}

@Test
Expand Down Expand Up @@ -756,7 +756,7 @@ public void getAwsSecurityCredentials_fromEnvironmentVariablesWithToken() throws

assertEquals("awsAccessKeyId", credentials.getAccessKeyId());
assertEquals("awsSecretAccessKey", credentials.getSecretAccessKey());
assertEquals("awsSessionToken", credentials.getToken());
assertEquals("awsSessionToken", credentials.getSessionToken());
}

@Test
Expand All @@ -778,7 +778,7 @@ public void getAwsSecurityCredentials_fromEnvironmentVariables_noMetadataServerC

assertEquals("awsAccessKeyId", credentials.getAccessKeyId());
assertEquals("awsSecretAccessKey", credentials.getSecretAccessKey());
assertEquals("awsSessionToken", credentials.getToken());
assertEquals("awsSessionToken", credentials.getSessionToken());
}

@Test
Expand All @@ -797,7 +797,7 @@ public void getAwsSecurityCredentials_fromMetadataServer() throws IOException {

assertEquals("accessKeyId", credentials.getAccessKeyId());
assertEquals("secretAccessKey", credentials.getSecretAccessKey());
assertEquals("token", credentials.getToken());
assertEquals("token", credentials.getSessionToken());

List<MockLowLevelHttpRequest> requests = transportFactory.transport.getRequests();
assertEquals(2, requests.size());
Expand Down

0 comments on commit abd462b

Please sign in to comment.