99import java .nio .ByteBuffer ;
1010import java .nio .charset .Charset ;
1111import java .util .ArrayList ;
12+ import java .util .List ;
13+ import java .util .concurrent .CompletableFuture ;
1214
1315import software .amazon .awssdk .crt .http .HttpHeader ;
1416import software .amazon .awssdk .crt .http .HttpProxyOptions ;
@@ -47,6 +49,7 @@ static public class CognitoCredentialsProviderBuilder {
4749 private String identity ;
4850 private String customRoleArn ;
4951 private ArrayList <CognitoLoginTokenPair > logins = new ArrayList <CognitoLoginTokenPair >();
52+ private CognitoLoginTokenSource loginTokenSource ;
5053
5154 private TlsContext tlsContext ;
5255 private ClientBootstrap clientBootstrap ;
@@ -148,6 +151,23 @@ public CognitoCredentialsProviderBuilder withHttpProxyOptions(HttpProxyOptions h
148151
149152 HttpProxyOptions getHttpProxyOptions () { return httpProxyOptions ; }
150153
154+ /**
155+ * Sets a login token source for the credentials provider. The login token source will be used to
156+ * gather additional login tokens to submit as part of the HTTP request sent to Cognito. A login token source
157+ * allows you to dynamically add login tokens on a per-request basis. Using a login token source requires
158+ * you to follow certain requirements in order to avoid undesirable behavior. See the documentation for
159+ * `CognitoLoginTokenSource` for further details.
160+ *
161+ * @param loginTokenSource object to source login tokens from before every HTTP request to Cognito
162+ * @return The current builder
163+ */
164+ public CognitoCredentialsProviderBuilder withLoginTokenSource (CognitoLoginTokenSource loginTokenSource ) {
165+ this .loginTokenSource = loginTokenSource ;
166+
167+ return this ;
168+ }
169+
170+ CognitoLoginTokenSource getLoginTokenSource () { return loginTokenSource ; }
151171
152172 /**
153173 * Creates a new Cognito credentials provider, based on this builder's configuration
@@ -213,14 +233,15 @@ private CognitoCredentialsProvider(CognitoCredentialsProviderBuilder builder) {
213233 proxyTlsContextHandle ,
214234 proxyAuthorizationType ,
215235 proxyAuthorizationUsername != null ? proxyAuthorizationUsername .getBytes (UTF8 ) : null ,
216- proxyAuthorizationPassword != null ? proxyAuthorizationPassword .getBytes (UTF8 ) : null );
236+ proxyAuthorizationPassword != null ? proxyAuthorizationPassword .getBytes (UTF8 ) : null ,
237+ builder .loginTokenSource );
217238
218239 acquireNativeHandle (nativeHandle );
219240 addReferenceTo (clientBootstrap );
220241 addReferenceTo (tlsContext );
221242 }
222243
223- private void writeLengthPrefixedBytesSafe (ByteBuffer buffer , byte [] bytes ) {
244+ private static void writeLengthPrefixedBytesSafe (ByteBuffer buffer , byte [] bytes ) {
224245 if (bytes != null ) {
225246 buffer .putInt (bytes .length );
226247 buffer .put (bytes );
@@ -229,7 +250,7 @@ private void writeLengthPrefixedBytesSafe(ByteBuffer buffer, byte[] bytes) {
229250 }
230251 }
231252
232- private byte [] marshalLoginsForJni (ArrayList <CognitoLoginTokenPair > logins ) {
253+ private static byte [] marshalLoginsForJni (List <CognitoLoginTokenPair > logins ) {
233254 int size = 0 ;
234255
235256 for (CognitoLoginTokenPair login : logins ) {
@@ -256,6 +277,16 @@ private byte[] marshalLoginsForJni(ArrayList<CognitoLoginTokenPair> logins) {
256277 return buffer .array ();
257278 }
258279
280+ private static CompletableFuture <List <CognitoLoginTokenPair >> createChainedFuture (long invocationHandle , CompletableFuture <List <CognitoLoginTokenPair >> baseFuture ) {
281+ return baseFuture .whenComplete ((token_pairs , ex ) -> {
282+ if (ex == null ) {
283+ completeLoginTokenFetch (invocationHandle , marshalLoginsForJni (token_pairs ), null );
284+ } else {
285+ completeLoginTokenFetch (invocationHandle , null , ex );
286+ }
287+ });
288+ }
289+
259290 /*******************************************************************************
260291 * Native methods
261292 ******************************************************************************/
@@ -273,5 +304,8 @@ private static native long cognitoCredentialsProviderNew(CognitoCredentialsProvi
273304 long proxyTlsContext ,
274305 int proxyAuthorizationType ,
275306 byte [] proxyAuthorizationUsername ,
276- byte [] proxyAuthorizationPassword );
307+ byte [] proxyAuthorizationPassword ,
308+ CognitoLoginTokenSource loginTokenSource );
309+
310+ private static native void completeLoginTokenFetch (long invocationHandle , byte [] marshalledLogins , Throwable ex );
277311}
0 commit comments