All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vertx.effect.httpclient.oauth.AuthorizationCodeModule Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
package vertx.effect.httpclient.oauth;

import io.vavr.Tuple2;
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpClientOptions;
import jsonvalues.JsObj;
import vertx.effect.core.OauthModule;
import vertx.effect.Val;
import vertx.effect.λ;
import vertx.effect.httpclient.HttpClientModule;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

public class AuthorizationCodeModule extends OauthModule {

    protected String refreshToken;


    protected BiFunction> authenticateReq;
    protected λ> readTokensAfterAuthentication;

    AuthorizationCodeModule(final HttpClientOptions options,
                            final String address,
                            final Function>> accessTokenReq,
                            final String authorizationHeaderName,
                            final Function authorizationHeaderValue,
                            final λ readNewAccessTokenAfterRefresh,
                            final Predicate refreshTokenPredicate,
                            final Predicate retryAccessTokenReqPredicate,
                            final Predicate retryReqPredicate,
                            final int accessTokenReqAttempts,
                            final int reqAttempts,
                            final String refreshToken
                           ) {

        super(options,
              address,
              authorizationHeaderName,
              authorizationHeaderValue,
              readNewAccessTokenAfterRefresh,
              refreshTokenPredicate,
              retryAccessTokenReqPredicate,
              retryReqPredicate,
              accessTokenReqAttempts,
              reqAttempts
             );
        this.accessTokenReq = accessTokenReq.apply(refreshToken);
        this.refreshToken = refreshToken;
    }


    AuthorizationCodeModule(final HttpClientOptions options,
                            final String address,
                            final BiFunction> authenticateReq,
                            final Function>> accessTokenReq,
                            final λ> readTokensAfterAuthentication,
                            final String authorizationHeaderName,
                            final Function authorizationHeaderValue,
                            final λ readNewAccessTokenAfterRefresh,
                            final Predicate refreshTokenPredicate,
                            final Predicate retryAccessTokenReqPredicate,
                            final Predicate retryReqPredicate,
                            final int accessTokenReqAttempts,
                            final int reqAttempts
                           ) {

        super(options,
              address,
              authorizationHeaderName,
              authorizationHeaderValue,
              readNewAccessTokenAfterRefresh,
              refreshTokenPredicate,
              retryAccessTokenReqPredicate,
              retryReqPredicate,
              accessTokenReqAttempts,
              reqAttempts
             );
        this.accessTokenReq = accessTokenReq.apply(refreshToken);
        this.authenticateReq = authenticateReq;
        this.readTokensAfterAuthentication = readTokensAfterAuthentication;

    }


    public Val> authenticate(JsObj input) {
        return authenticateReq.apply(this,
                                     input
                                    )
                              .flatMap(readTokensAfterAuthentication)
                              .onSuccess(tokens -> {
                                  this.accessToken = tokens._1;
                                  this.refreshToken = tokens._2;
                              });
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy