Skip to content

Commit 64d5212

Browse files
authored
Add main auth scheme interfaces (#3999)
* Add main auth scheme interfaces * Change Consumer interface and address comments * Move schemeId into Builder method instead of constructor
1 parent 0d7b693 commit 64d5212

File tree

5 files changed

+301
-0
lines changed

5 files changed

+301
-0
lines changed

core/http-auth-spi/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<artifactId>reactive-streams</artifactId>
5555
<version>${reactive-streams.version}</version>
5656
</dependency>
57+
<dependency>
58+
<groupId>software.amazon.awssdk</groupId>
59+
<artifactId>identity-spi</artifactId>
60+
<version>${awsjavasdk.version}</version>
61+
</dependency>
5762

5863
<dependency>
5964
<groupId>org.junit.jupiter</groupId>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.http.auth.spi;
17+
18+
import software.amazon.awssdk.annotations.SdkProtectedApi;
19+
import software.amazon.awssdk.http.auth.spi.internal.DefaultHttpAuthOption;
20+
import software.amazon.awssdk.identity.spi.IdentityProperty;
21+
import software.amazon.awssdk.utils.builder.SdkBuilder;
22+
23+
/**
24+
* An authentication scheme option, composed of the scheme ID and properties for use when resolving the identity and signing
25+
* the request.
26+
* <p>
27+
* This is used in the output from the auth scheme resolver. The resolver returns a list of these, in the order the auth scheme
28+
* resolver wishes to use them.
29+
*
30+
* @see HttpAuthScheme
31+
*/
32+
@SdkProtectedApi
33+
public interface HttpAuthOption {
34+
35+
/**
36+
* Get a new builder for creating a {@link HttpAuthOption}.
37+
*/
38+
static Builder builder() {
39+
return new DefaultHttpAuthOption.BuilderImpl();
40+
}
41+
42+
/**
43+
* Retrieve the scheme ID, a unique identifier for the authentication scheme (aws.auth#sigv4, smithy.api#httpBearerAuth).
44+
*/
45+
String schemeId();
46+
47+
/**
48+
* Retrieve the value of an {@link IdentityProperty}.
49+
*/
50+
<T> T identityProperty(IdentityProperty<T> property);
51+
52+
/**
53+
* Retrieve the value of an {@link SignerProperty}.
54+
*/
55+
<T> T signerProperty(SignerProperty<T> property);
56+
57+
/**
58+
* A method to operate on all {@link IdentityProperty} values of this HttpAuthOption.
59+
*/
60+
<T> void forEachIdentityProperty(IdentityPropertyConsumer consumer);
61+
62+
/**
63+
* A method to operate on all {@link SignerProperty} values of this HttpAuthOption.
64+
*/
65+
<T> void forEachSignerProperty(SignerPropertyConsumer consumer);
66+
67+
/**
68+
* Interface for operating on an {@link IdentityProperty} value.
69+
*/
70+
@FunctionalInterface
71+
interface IdentityPropertyConsumer {
72+
<T> void accept(IdentityProperty<T> propertyKey, T propertyValue);
73+
}
74+
75+
/**
76+
* Interface for operating on an {@link SignerProperty} value.
77+
*/
78+
@FunctionalInterface
79+
interface SignerPropertyConsumer {
80+
<T> void accept(SignerProperty<T> propertyKey, T propertyValue);
81+
}
82+
83+
interface Builder extends SdkBuilder<Builder, HttpAuthOption> {
84+
<T> Builder schemeId(String schemeId);
85+
86+
<T> Builder putIdentityProperty(IdentityProperty<T> key, T value);
87+
88+
<T> Builder putSignerProperty(SignerProperty<T> key, T value);
89+
}
90+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.http.auth.spi;
17+
18+
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
20+
import software.amazon.awssdk.identity.spi.Identity;
21+
import software.amazon.awssdk.identity.spi.IdentityProvider;
22+
import software.amazon.awssdk.identity.spi.TokenIdentity;
23+
24+
/**
25+
* An authentication scheme, composed of:
26+
* <ol>
27+
* <li>A scheme ID - A unique identifier for the authentication scheme.</li>
28+
* <li>An identity provider - An API that can be queried to acquire the customer's identity.</li>
29+
* <li>A signer - An API that can be used to sign HTTP requests.</li>
30+
* </ol>
31+
*
32+
* @see IdentityProvider
33+
* @see HttpSigner
34+
*
35+
* @param <T> The type of the {@link Identity} used by this authentication scheme.
36+
*/
37+
@SdkPublicApi
38+
public interface HttpAuthScheme<T extends Identity> {
39+
40+
/**
41+
* Retrieve the scheme ID, a unique identifier for the authentication scheme (aws.auth#sigv4, smithy.api#httpBearerAuth).
42+
*/
43+
String schemeId();
44+
45+
/**
46+
* Retrieve the identity provider associated with this authentication scheme. The identity generated by this provider is
47+
* guaranteed to be supported by the signer in this authentication scheme.
48+
* <p>
49+
* For example, if the scheme ID is aws.auth#sigv4, the provider returns an {@link AwsCredentialsIdentity}, if the scheme
50+
* ID is httpBearerAuth, the provider returns a {@link TokenIdentity}.
51+
* <p>
52+
* Note, the returned identity provider may differ from the type of identity provider retrieved from the provided identity
53+
* provider configuration.
54+
*/
55+
IdentityProvider<T> identityProvider(IdentityProviderConfiguration providers);
56+
57+
/**
58+
* Retrieve the signer associated with this authentication scheme. This signer is guaranteed to support the identity
59+
* generated by the identity provider in this authentication scheme.
60+
*/
61+
HttpSigner signer();
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.http.auth.spi;
17+
18+
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.identity.spi.Identity;
20+
import software.amazon.awssdk.identity.spi.IdentityProvider;
21+
22+
/**
23+
* The identity providers configured in the SDK.
24+
* <p>
25+
* Used by the {@link HttpAuthScheme} implementation to load any @{@link IdentityProvider}s it needs from the set that are
26+
* configured on the SDK.
27+
*/
28+
@SdkPublicApi
29+
@FunctionalInterface
30+
public interface IdentityProviderConfiguration {
31+
32+
/**
33+
* Retrieve an identity provider for the provided identity type.
34+
*/
35+
<T extends Identity> IdentityProvider<T> identityProvider(Class<T> identityType);
36+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.http.auth.spi.internal;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import software.amazon.awssdk.annotations.SdkInternalApi;
21+
import software.amazon.awssdk.http.auth.spi.HttpAuthOption;
22+
import software.amazon.awssdk.http.auth.spi.SignerProperty;
23+
import software.amazon.awssdk.identity.spi.IdentityProperty;
24+
import software.amazon.awssdk.utils.ToString;
25+
import software.amazon.awssdk.utils.Validate;
26+
27+
@SdkInternalApi
28+
public final class DefaultHttpAuthOption implements HttpAuthOption {
29+
30+
private final String schemeId;
31+
private final Map<IdentityProperty<?>, Object> identityProperties;
32+
private final Map<SignerProperty<?>, Object> signerProperties;
33+
34+
DefaultHttpAuthOption(BuilderImpl builder) {
35+
this.schemeId = Validate.paramNotBlank(builder.schemeId, "schemeId");
36+
this.identityProperties = new HashMap<>(builder.identityProperties);
37+
this.signerProperties = new HashMap<>(builder.signerProperties);
38+
}
39+
40+
@Override
41+
public String schemeId() {
42+
return schemeId;
43+
}
44+
45+
@Override
46+
public <T> T identityProperty(IdentityProperty<T> property) {
47+
return (T) identityProperties.get(property);
48+
}
49+
50+
@Override
51+
public <T> T signerProperty(SignerProperty<T> property) {
52+
return (T) signerProperties.get(property);
53+
}
54+
55+
@Override
56+
public <T> void forEachIdentityProperty(IdentityPropertyConsumer consumer) {
57+
for (IdentityProperty<?> p : identityProperties.keySet()) {
58+
IdentityProperty<T> property = (IdentityProperty<T>) p;
59+
consumer.accept(property, this.identityProperty(property));
60+
}
61+
}
62+
63+
@Override
64+
public <T> void forEachSignerProperty(SignerPropertyConsumer consumer) {
65+
for (SignerProperty<?> p : signerProperties.keySet()) {
66+
SignerProperty<T> property = (SignerProperty<T>) p;
67+
consumer.accept(property, this.signerProperty(property));
68+
}
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return ToString.builder("HttpAuthOption")
74+
.add("identityProperties", identityProperties)
75+
.add("signerProperties", signerProperties)
76+
.build();
77+
}
78+
79+
80+
public static final class BuilderImpl implements Builder {
81+
private String schemeId;
82+
private final Map<IdentityProperty<?>, Object> identityProperties = new HashMap<>();
83+
private final Map<SignerProperty<?>, Object> signerProperties = new HashMap<>();
84+
85+
@Override
86+
public <T> Builder schemeId(String schemeId) {
87+
this.schemeId = schemeId;
88+
return this;
89+
}
90+
91+
@Override
92+
public <T> Builder putIdentityProperty(IdentityProperty<T> key, T value) {
93+
this.identityProperties.put(key, value);
94+
return this;
95+
}
96+
97+
@Override
98+
public <T> Builder putSignerProperty(SignerProperty<T> key, T value) {
99+
this.signerProperties.put(key, value);
100+
return this;
101+
}
102+
103+
@Override
104+
public HttpAuthOption build() {
105+
return new DefaultHttpAuthOption(this);
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)