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.signer;
17 
18 import java.time.Clock;
19 import java.util.concurrent.CompletableFuture;
20 import java.util.function.Consumer;
21 import software.amazon.awssdk.annotations.SdkPublicApi;
22 import software.amazon.awssdk.http.auth.spi.internal.signer.DefaultAsyncSignRequest;
23 import software.amazon.awssdk.http.auth.spi.internal.signer.DefaultSignRequest;
24 import software.amazon.awssdk.identity.spi.Identity;
25 
26 /**
27  * Interface for the process of modifying a request destined for a service so that the service can authenticate the SDK
28  * customer’s identity.
29  *
30  * @param <IdentityT> The type of the identity.
31  */
32 @SdkPublicApi
33 public interface HttpSigner<IdentityT extends Identity> {
34 
35     /**
36      * A {@link Clock} to be used to derive the signing time. This property defaults to the system clock.
37      *
38      * <p>Note, signing time may not be relevant to some signers.
39      */
40     SignerProperty<Clock> SIGNING_CLOCK = SignerProperty.create(HttpSigner.class, "SigningClock");
41 
42     /**
43      * Method that takes in inputs to sign a request with sync payload and returns a signed version of the request.
44      *
45      * @param request The inputs to sign a request.
46      * @return A signed version of the request.
47      */
sign(SignRequest<? extends IdentityT> request)48     SignedRequest sign(SignRequest<? extends IdentityT> request);
49 
50     /**
51      * Method that takes in inputs to sign a request with sync payload and returns a signed version of the request.
52      * <p>
53      * Similar to {@link #sign(SignRequest)}, but takes a lambda to configure a new {@link SignRequest.Builder}.
54      * This removes the need to call {@link SignRequest#builder(IdentityT)}} and
55      * {@link SignRequest.Builder#build()}.
56      *
57      * @param consumer A {@link Consumer} to which an empty {@link SignRequest.Builder} will be given.
58      * @return A signed version of the request.
59      */
sign(Consumer<SignRequest.Builder<IdentityT>> consumer)60     default SignedRequest sign(Consumer<SignRequest.Builder<IdentityT>> consumer) {
61         return sign(DefaultSignRequest.<IdentityT>builder().applyMutation(consumer).build());
62     }
63 
64     /**
65      * Method that takes in inputs to sign a request with async payload and returns a future containing the signed version of the
66      * request.
67      *
68      * @param request The inputs to sign a request.
69      * @return A future containing the signed version of the request.
70      */
signAsync(AsyncSignRequest<? extends IdentityT> request)71     CompletableFuture<AsyncSignedRequest> signAsync(AsyncSignRequest<? extends IdentityT> request);
72 
73     /**
74      * Method that takes in inputs to sign a request with async payload and returns a future containing the signed version of the
75      * request.
76      * <p>
77      * Similar to {@link #signAsync(AsyncSignRequest)}, but takes a lambda to configure a new
78      * {@link AsyncSignRequest.Builder}. This removes the need to call {@link AsyncSignRequest#builder(IdentityT)}} and
79      * {@link AsyncSignRequest.Builder#build()}.
80      *
81      * @param consumer A {@link Consumer} to which an empty {@link BaseSignRequest.Builder} will be given.
82      * @return A future containing the signed version of the request.
83      */
signAsync(Consumer<AsyncSignRequest.Builder<IdentityT>> consumer)84     default CompletableFuture<AsyncSignedRequest> signAsync(Consumer<AsyncSignRequest.Builder<IdentityT>> consumer) {
85         return signAsync(DefaultAsyncSignRequest.<IdentityT>builder().applyMutation(consumer).build());
86     }
87 }
88