|
23 | 23 | public enum AwsCredentialsUtils {
|
24 | 24 | ;
|
25 | 25 |
|
| 26 | + /** |
| 27 | + * Region supplier which matches any region. |
| 28 | + */ |
| 29 | + public static final Supplier<String> ANY_REGION = () -> "*"; |
| 30 | + |
26 | 31 | /**
|
27 | 32 | * @return an authorization predicate that ensures the authorization header matches the given access key, region and service name.
|
28 | 33 | * @see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html">AWS v4 Signatures</a>
|
29 |
| - * @param region the name of the AWS region used to sign the request, or {@code *} to skip validation of the region parameter |
| 34 | + * @param regionSupplier supplies the name of the AWS region used to sign the request, or {@code *} to skip validation of the region |
| 35 | + * parameter |
30 | 36 | */
|
31 |
| - public static BiPredicate<String, String> fixedAccessKey(String accessKey, String region, String serviceName) { |
32 |
| - return mutableAccessKey(() -> accessKey, region, serviceName); |
| 37 | + public static BiPredicate<String, String> fixedAccessKey(String accessKey, Supplier<String> regionSupplier, String serviceName) { |
| 38 | + return mutableAccessKey(() -> accessKey, regionSupplier, serviceName); |
33 | 39 | }
|
34 | 40 |
|
35 | 41 | /**
|
36 | 42 | * @return an authorization predicate that ensures the authorization header matches the access key supplied by the given supplier,
|
37 | 43 | * and also matches the given region and service name.
|
38 | 44 | * @see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html">AWS v4 Signatures</a>
|
39 |
| - * @param region the name of the AWS region used to sign the request, or {@code *} to skip validation of the region parameter |
| 45 | + * @param regionSupplier supplies the name of the AWS region used to sign the request, or {@code *} to skip validation of the region |
| 46 | + * parameter |
40 | 47 | */
|
41 |
| - public static BiPredicate<String, String> mutableAccessKey(Supplier<String> accessKeySupplier, String region, String serviceName) { |
| 48 | + public static BiPredicate<String, String> mutableAccessKey( |
| 49 | + Supplier<String> accessKeySupplier, |
| 50 | + Supplier<String> regionSupplier, |
| 51 | + String serviceName |
| 52 | + ) { |
42 | 53 | return (authorizationHeader, sessionTokenHeader) -> authorizationHeader != null
|
43 |
| - && isValidAwsV4SignedAuthorizationHeader(accessKeySupplier.get(), region, serviceName, authorizationHeader); |
| 54 | + && isValidAwsV4SignedAuthorizationHeader(accessKeySupplier.get(), regionSupplier.get(), serviceName, authorizationHeader); |
44 | 55 | }
|
45 | 56 |
|
46 | 57 | /**
|
@@ -72,16 +83,17 @@ public static boolean isValidAwsV4SignedAuthorizationHeader(
|
72 | 83 | /**
|
73 | 84 | * @return an authorization predicate that ensures the access key, session token, region and service name all match the given values.
|
74 | 85 | * @see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html">AWS v4 Signatures</a>
|
75 |
| - * @param region the name of the AWS region used to sign the request, or {@code *} to skip validation of the region parameter |
| 86 | + * @param regionSupplier supplies the name of the AWS region used to sign the request, or {@code *} to skip validation of the region |
| 87 | + * parameter |
76 | 88 | */
|
77 | 89 | public static BiPredicate<String, String> fixedAccessKeyAndToken(
|
78 | 90 | String accessKey,
|
79 | 91 | String sessionToken,
|
80 |
| - String region, |
| 92 | + Supplier<String> regionSupplier, |
81 | 93 | String serviceName
|
82 | 94 | ) {
|
83 | 95 | Objects.requireNonNull(sessionToken);
|
84 |
| - final var accessKeyPredicate = fixedAccessKey(accessKey, region, serviceName); |
| 96 | + final var accessKeyPredicate = fixedAccessKey(accessKey, regionSupplier, serviceName); |
85 | 97 | return (authorizationHeader, sessionTokenHeader) -> accessKeyPredicate.test(authorizationHeader, sessionTokenHeader)
|
86 | 98 | && sessionToken.equals(sessionTokenHeader);
|
87 | 99 | }
|
|
0 commit comments