1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 package software.amazon.awssdk.auth.credentials;
17
18 import static software.amazon.awssdk.utils.StringUtils.trimToNull;
19
20 import java.util.Objects;
21 import software.amazon.awssdk.annotations.Immutable;
22 import software.amazon.awssdk.annotations.SdkInternalApi;
23 import software.amazon.awssdk.annotations.SdkPublicApi;
24 import software.amazon.awssdk.utils.ToString;
25 import software.amazon.awssdk.utils.Validate;
26
27 /**
28 * Provides access to the AWS credentials used for accessing AWS services: AWS access key ID and secret access key. These
29 * credentials are used to securely sign requests to AWS services.
30 *
31 * <p>For more details on AWS access keys, see:
32 * <a href="http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys">
33 * http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys</a></p>
34 *
35 * @see AwsCredentialsProvider
36 */
37 @Immutable
38 @SdkPublicApi
39 public final class AwsBasicCredentials implements AwsCredentials {
40 /**
41 * A set of AWS credentials without an access key or secret access key, indicating that anonymous access should be used.
42 *
43 * This should be accessed via {@link AnonymousCredentialsProvider#resolveCredentials()}.
44 */
45 @SdkInternalApi
46 static final AwsBasicCredentials ANONYMOUS_CREDENTIALS = new AwsBasicCredentials(null, null, false);
47
48 private final String accessKeyId;
49 private final String secretAccessKey;
50
51 /**
52 * Constructs a new credentials object, with the specified AWS access key, AWS secret key and AWS session token.
53 *
54 * @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
55 * @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
56 */
57 protected AwsBasicCredentials(String accessKeyId, String secretAccessKey) {
58 this(accessKeyId, secretAccessKey, true);
59 }
60
61 private AwsBasicCredentials(String accessKeyId, String secretAccessKey, boolean validateCredentials) {
62 this.accessKeyId = trimToNull(accessKeyId);
63 this.secretAccessKey = trimToNull(secretAccessKey);
64
65 if (validateCredentials) {
66 Validate.notNull(this.accessKeyId, "Access key ID cannot be blank.");
67 Validate.notNull(this.secretAccessKey, "Secret access key cannot be blank.");
68 }
69 }
70
71 /**
72 * Constructs a new credentials object, with the specified AWS access key, AWS secret key and AWS session token.
73 *
74 * @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
75 * @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
76 * */
77 public static AwsBasicCredentials create(String accessKeyId, String secretAccessKey) {
78 return new AwsBasicCredentials(accessKeyId, secretAccessKey);
79 }
80
81 /**
82 * Retrieve the AWS access key, used to identify the user interacting with AWS.
83 */
84 public String accessKeyId() {
85 return accessKeyId;
86 }
87
88 /**
89 * Retrieve the AWS secret access key, used to authenticate the user interacting with AWS.
90 */
91 public String secretAccessKey() {
92 return secretAccessKey;
93 }
94
95 @Override
96 public String toString() {
97 return ToString.builder("AwsCredentials")
98 .add("accessKeyId", accessKeyId)
99 .build();
100 }
101
102 @Override
103 public boolean equals(Object o) {
104 if (this == o) {
105 return true;
106 }
107 if (o == null || getClass() != o.getClass()) {
108 return false;
109 }
110 AwsBasicCredentials that = (AwsBasicCredentials) o;
111 return Objects.equals(accessKeyId, that.accessKeyId) &&
112 Objects.equals(secretAccessKey, that.secretAccessKey);
113 }
114
115 @Override
116 public int hashCode() {
117 int hashCode = 1;
118 hashCode = 31 * hashCode + Objects.hashCode(accessKeyId());
119 hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey());
120 return hashCode;
121 }
122 }
123