Skip to content

Commit 747c4f6

Browse files
committed
Add initial default implementation of BearerHttpSigner
1 parent 8b86bd4 commit 747c4f6

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

core/http-auth/src/main/java/software/amazon/awssdk/http/auth/BearerHttpSigner.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@
1616
package software.amazon.awssdk.http.auth;
1717

1818
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.http.auth.internal.DefaultBearerHttpSigner;
1920
import software.amazon.awssdk.http.auth.spi.HttpSigner;
21+
import software.amazon.awssdk.identity.spi.TokenIdentity;
2022

23+
/**
24+
* An {@link HttpSigner} that will sign a request with Bearer token
25+
* authorization.
26+
*/
2127
@SdkPublicApi
2228
public interface BearerHttpSigner extends HttpSigner {
2329

30+
static BearerHttpSigner create(TokenIdentity tokenIdentity) {
31+
return new DefaultBearerHttpSigner(tokenIdentity);
32+
}
2433
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package software.amazon.awssdk.http.auth.internal;
2+
3+
import org.reactivestreams.Publisher;
4+
import software.amazon.awssdk.annotations.SdkInternalApi;
5+
import software.amazon.awssdk.http.ContentStreamProvider;
6+
import software.amazon.awssdk.http.SdkHttpRequest;
7+
import software.amazon.awssdk.http.auth.BearerHttpSigner;
8+
import software.amazon.awssdk.http.auth.spi.HttpSignRequest;
9+
import software.amazon.awssdk.http.auth.spi.SignedHttpRequest;
10+
import software.amazon.awssdk.identity.spi.TokenIdentity;
11+
12+
13+
import java.nio.ByteBuffer;
14+
15+
/**
16+
* A default implementation of {@link BearerHttpSigner}
17+
*/
18+
@SdkInternalApi
19+
public class DefaultBearerHttpSigner implements BearerHttpSigner {
20+
21+
private static final String AUTHZ_HEADER = "Authorization";
22+
private static final String BEARER_LABEL = "Bearer";
23+
24+
private final TokenIdentity tokenIdentity;
25+
26+
public DefaultBearerHttpSigner(TokenIdentity tokenIdentity) {
27+
this.tokenIdentity = tokenIdentity;
28+
}
29+
30+
@Override
31+
public SignedHttpRequest<? extends ContentStreamProvider> sign(HttpSignRequest<? extends ContentStreamProvider> request) {
32+
return doSign(request);
33+
}
34+
35+
@Override
36+
public SignedHttpRequest<? extends Publisher<? extends ByteBuffer>>
37+
signAsync(HttpSignRequest<? extends Publisher<? extends ByteBuffer>> request) {
38+
return doSign(request);
39+
}
40+
41+
private <T> SignedHttpRequest<T> doSign(HttpSignRequest<T> request) {
42+
SdkHttpRequest signedRequest = request.request().toBuilder()
43+
.putHeader(
44+
AUTHZ_HEADER,
45+
buildAuthorizationHeader(tokenIdentity))
46+
.build();
47+
48+
return SignedHttpRequest.builder(request.payloadType())
49+
.request(signedRequest)
50+
.payload(request.payload().orElse(null))
51+
.build();
52+
}
53+
54+
private String buildAuthorizationHeader(TokenIdentity tokenIdentity) {
55+
return String.format("%s %s", BEARER_LABEL, tokenIdentity.token());
56+
}
57+
58+
}

0 commit comments

Comments
 (0)