Skip to content

Add main auth scheme interfaces #3999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/http-auth-spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<artifactId>reactive-streams</artifactId>
<version>${reactive-streams.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>identity-spi</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.http.auth.spi;

import software.amazon.awssdk.annotations.SdkProtectedApi;
import software.amazon.awssdk.http.auth.spi.internal.DefaultHttpAuthOption;
import software.amazon.awssdk.identity.spi.IdentityProperty;
import software.amazon.awssdk.utils.builder.SdkBuilder;

/**
* An authentication scheme option, composed of the scheme ID and properties for use when resolving the identity and signing
* the request.
* <p>
* This is used in the output from the auth scheme resolver. The resolver returns a list of these, in the order the auth scheme
* resolver wishes to use them.
*
* @see HttpAuthScheme
*/
@SdkProtectedApi
public interface HttpAuthOption {

/**
* Get a new builder for creating a {@link HttpAuthOption}.
*/
static Builder builder() {
return new DefaultHttpAuthOption.BuilderImpl();
}

/**
* Retrieve the scheme ID, a unique identifier for the authentication scheme (aws.auth#sigv4, smithy.api#httpBearerAuth).
*/
String schemeId();

/**
* Retrieve the value of an {@link IdentityProperty}.
*/
<T> T identityProperty(IdentityProperty<T> property);

/**
* Retrieve the value of an {@link SignerProperty}.
*/
<T> T signerProperty(SignerProperty<T> property);

/**
* A method to operate on all {@link IdentityProperty} values of this HttpAuthOption.
*/
<T> void forEachIdentityProperty(IdentityPropertyConsumer consumer);

/**
* A method to operate on all {@link SignerProperty} values of this HttpAuthOption.
*/
<T> void forEachSignerProperty(SignerPropertyConsumer consumer);

/**
* Interface for operating on an {@link IdentityProperty} value.
*/
@FunctionalInterface
interface IdentityPropertyConsumer {
<T> void accept(IdentityProperty<T> propertyKey, T propertyValue);
}

/**
* Interface for operating on an {@link SignerProperty} value.
*/
@FunctionalInterface
interface SignerPropertyConsumer {
<T> void accept(SignerProperty<T> propertyKey, T propertyValue);
}

interface Builder extends SdkBuilder<Builder, HttpAuthOption> {
<T> Builder schemeId(String schemeId);

<T> Builder putIdentityProperty(IdentityProperty<T> key, T value);

<T> Builder putSignerProperty(SignerProperty<T> key, T value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.http.auth.spi;

import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
import software.amazon.awssdk.identity.spi.Identity;
import software.amazon.awssdk.identity.spi.IdentityProvider;
import software.amazon.awssdk.identity.spi.TokenIdentity;

/**
* An authentication scheme, composed of:
* <ol>
* <li>A scheme ID - A unique identifier for the authentication scheme.</li>
* <li>An identity provider - An API that can be queried to acquire the customer's identity.</li>
* <li>A signer - An API that can be used to sign HTTP requests.</li>
* </ol>
*
* @see IdentityProvider
* @see HttpSigner
*
* @param <T> The type of the {@link Identity} used by this authentication scheme.
*/
@SdkPublicApi
public interface HttpAuthScheme<T extends Identity> {

/**
* Retrieve the scheme ID, a unique identifier for the authentication scheme (aws.auth#sigv4, smithy.api#httpBearerAuth).
*/
String schemeId();

/**
* Retrieve the identity provider associated with this authentication scheme. The identity generated by this provider is
* guaranteed to be supported by the signer in this authentication scheme.
* <p>
* For example, if the scheme ID is aws.auth#sigv4, the provider returns an {@link AwsCredentialsIdentity}, if the scheme
* ID is httpBearerAuth, the provider returns a {@link TokenIdentity}.
* <p>
* Note, the returned identity provider may differ from the type of identity provider retrieved from the provided identity
* provider configuration.
*/
IdentityProvider<T> identityProvider(IdentityProviderConfiguration providers);

/**
* Retrieve the signer associated with this authentication scheme. This signer is guaranteed to support the identity
* generated by the identity provider in this authentication scheme.
*/
HttpSigner signer();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.http.auth.spi;

import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.identity.spi.Identity;
import software.amazon.awssdk.identity.spi.IdentityProvider;

/**
* The identity providers configured in the SDK.
* <p>
* Used by the {@link HttpAuthScheme} implementation to load any @{@link IdentityProvider}s it needs from the set that are
* configured on the SDK.
*/
@SdkPublicApi
@FunctionalInterface
public interface IdentityProviderConfiguration {

/**
* Retrieve an identity provider for the provided identity type.
*/
<T extends Identity> IdentityProvider<T> identityProvider(Class<T> identityType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.http.auth.spi.internal;

import java.util.HashMap;
import java.util.Map;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.http.auth.spi.HttpAuthOption;
import software.amazon.awssdk.http.auth.spi.SignerProperty;
import software.amazon.awssdk.identity.spi.IdentityProperty;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;

@SdkInternalApi
public final class DefaultHttpAuthOption implements HttpAuthOption {

private final String schemeId;
private final Map<IdentityProperty<?>, Object> identityProperties;
private final Map<SignerProperty<?>, Object> signerProperties;

DefaultHttpAuthOption(BuilderImpl builder) {
this.schemeId = Validate.paramNotBlank(builder.schemeId, "schemeId");
this.identityProperties = new HashMap<>(builder.identityProperties);
this.signerProperties = new HashMap<>(builder.signerProperties);
}

@Override
public String schemeId() {
return schemeId;
}

@Override
public <T> T identityProperty(IdentityProperty<T> property) {
return (T) identityProperties.get(property);
}

@Override
public <T> T signerProperty(SignerProperty<T> property) {
return (T) signerProperties.get(property);
}

@Override
public <T> void forEachIdentityProperty(IdentityPropertyConsumer consumer) {
for (IdentityProperty<?> p : identityProperties.keySet()) {
IdentityProperty<T> property = (IdentityProperty<T>) p;
consumer.accept(property, this.identityProperty(property));
}
}

@Override
public <T> void forEachSignerProperty(SignerPropertyConsumer consumer) {
for (SignerProperty<?> p : signerProperties.keySet()) {
SignerProperty<T> property = (SignerProperty<T>) p;
consumer.accept(property, this.signerProperty(property));
}
Comment on lines +65 to +68
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is a better way to do this.

}

@Override
public String toString() {
return ToString.builder("HttpAuthOption")
.add("identityProperties", identityProperties)
.add("signerProperties", signerProperties)
.build();
}


public static final class BuilderImpl implements Builder {
private String schemeId;
private final Map<IdentityProperty<?>, Object> identityProperties = new HashMap<>();
private final Map<SignerProperty<?>, Object> signerProperties = new HashMap<>();

@Override
public <T> Builder schemeId(String schemeId) {
this.schemeId = schemeId;
return this;
}

@Override
public <T> Builder putIdentityProperty(IdentityProperty<T> key, T value) {
this.identityProperties.put(key, value);
return this;
}

@Override
public <T> Builder putSignerProperty(SignerProperty<T> key, T value) {
this.signerProperties.put(key, value);
return this;
}

@Override
public HttpAuthOption build() {
return new DefaultHttpAuthOption(this);
}
}
}