• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.internal.scheme;
17 
18 import java.util.concurrent.CompletableFuture;
19 import software.amazon.awssdk.annotations.SdkInternalApi;
20 import software.amazon.awssdk.http.auth.scheme.NoAuthAuthScheme;
21 import software.amazon.awssdk.http.auth.spi.signer.AsyncSignRequest;
22 import software.amazon.awssdk.http.auth.spi.signer.AsyncSignedRequest;
23 import software.amazon.awssdk.http.auth.spi.signer.HttpSigner;
24 import software.amazon.awssdk.http.auth.spi.signer.SignRequest;
25 import software.amazon.awssdk.http.auth.spi.signer.SignedRequest;
26 import software.amazon.awssdk.identity.spi.IdentityProvider;
27 import software.amazon.awssdk.identity.spi.IdentityProviders;
28 import software.amazon.awssdk.identity.spi.ResolveIdentityRequest;
29 
30 /**
31  * A default implementation of {@link NoAuthAuthScheme}. This implementation always:
32  *
33  * <ul>
34  *     <li>Returns an {@link IdentityProvider} that always returns the same static instance that implements the
35  *     {@link AnonymousIdentity} interface</li>
36  *     <li>Returns an {@link HttpSigner} that returns the same request given in the signing request.</li>
37  * </ul>
38  */
39 @SdkInternalApi
40 public final class DefaultNoAuthAuthScheme implements NoAuthAuthScheme {
41     private static final DefaultNoAuthAuthScheme DEFAULT = new DefaultNoAuthAuthScheme();
42     private static final IdentityProvider<AnonymousIdentity> DEFAULT_IDENTITY_PROVIDER = noAuthIdentityProvider();
43     private static final HttpSigner<AnonymousIdentity> DEFAULT_SIGNER = noAuthSigner();
44     private static final AnonymousIdentity ANONYMOUS_IDENTITY = anonymousIdentity();
45 
46     /**
47      * Returns an instance of the {@link NoAuthAuthScheme}.
48      */
create()49     public static NoAuthAuthScheme create() {
50         return DEFAULT;
51     }
52 
53     @Override
schemeId()54     public String schemeId() {
55         return SCHEME_ID;
56     }
57 
58     @Override
identityProvider(IdentityProviders providers)59     public IdentityProvider<AnonymousIdentity> identityProvider(IdentityProviders providers) {
60         return DEFAULT_IDENTITY_PROVIDER;
61     }
62 
63     @Override
signer()64     public HttpSigner<AnonymousIdentity> signer() {
65         return DEFAULT_SIGNER;
66     }
67 
noAuthIdentityProvider()68     private static IdentityProvider<AnonymousIdentity> noAuthIdentityProvider() {
69         return new IdentityProvider<AnonymousIdentity>() {
70 
71             @Override
72             public Class identityType() {
73                 return AnonymousIdentity.class;
74             }
75 
76             @Override
77             public CompletableFuture<AnonymousIdentity> resolveIdentity(ResolveIdentityRequest request) {
78                 return CompletableFuture.completedFuture(ANONYMOUS_IDENTITY);
79             }
80         };
81     }
82 
83     private static HttpSigner<AnonymousIdentity> noAuthSigner() {
84         return new HttpSigner<AnonymousIdentity>() {
85             @Override
86             public SignedRequest sign(SignRequest<? extends AnonymousIdentity> request) {
87                 return SignedRequest.builder()
88                                     .request(request.request())
89                                     .payload(request.payload().orElse(null))
90                                     .build();
91             }
92 
93             @Override
94             public CompletableFuture<AsyncSignedRequest> signAsync(AsyncSignRequest<?
95                 extends AnonymousIdentity> request) {
96                 return CompletableFuture.completedFuture(
97                     AsyncSignedRequest.builder()
98                                       .request(request.request())
99                                       .payload(request.payload().orElse(null))
100                                       .build()
101                 );
102             }
103         };
104     }
105 
106     private static AnonymousIdentity anonymousIdentity() {
107         return new AnonymousIdentity() {
108         };
109     }
110 }
111