Example #1
0
        public static TokenFactory GetAccessTokenFactory(
            [NotNull] this RsdnApiAuthenticator authenticator,
            [NotNull] Func <AuthTokenResponse> getToken,
            [NotNull] Action <AuthTokenResponse> setToken)
        {
            Code.NotNull(authenticator, nameof(authenticator));

            var tokenLock = new AsyncLock();

            return(async ct =>
            {
                var token = getToken();
                if (token == null)
                {
                    return null;
                }
                if (token.ExpiresOn >= DateTimeOffset.UtcNow)
                {
                    return token.AccessToken;
                }
                using (await tokenLock.AcquireAsync(ct))
                    if (token.ExpiresOn < DateTimeOffset.UtcNow)
                    {
                        token = await authenticator.RefreshTokenAsync(token.RefreshToken, ct);

                        setToken(token);
                    }
                return token?.AccessToken;
            });
        }
Example #2
0
        public static TokenFactory GetAccessTokenFactory(
            [NotNull] this RsdnApiAuthenticator authenticator,
            [NotNull] string login,
            [NotNull] string password)
        {
            Code.NotNull(authenticator, nameof(authenticator));

            AuthTokenResponse token = null;
            var tokenLock           = new AsyncLock();

            return(async ct =>
            {
                if (token != null && token.ExpiresOn >= DateTimeOffset.UtcNow)
                {
                    return token.AccessToken;
                }
                using (await tokenLock.AcquireAsync(ct))
                    if (token == null || token.ExpiresOn < DateTimeOffset.UtcNow)
                    {
                        token = token == null
                                                        ? await authenticator.GetTokenByPasswordAsync(login, password, ct)
                                                        : await authenticator.RefreshTokenAsync(token.RefreshToken, ct)
                                ?? await authenticator.GetTokenByPasswordAsync(login, password, ct);
                    }
                return token.AccessToken;
            });
        }
Example #3
0
 public void Setup()
 {
     _authenticator =
         RsdnClientHelpers.CreateAuthenticator(
             TestsBase.ServiceUri,
             TestsBase.TestClientID,
             TestsBase.TestClientSecret,
             "offline_access",
             false);
 }
Example #4
0
        public ApiConnectionService(AccountsService accountsService)
        {
            _accountsService = accountsService;

            _authenticator = RsdnClientHelpers.CreateAuthenticator(
                //new Uri("https://localhost:44389"),
                _rsdnUri,
                "test_public_client",
                "",
                "offline_access");
            _token = _accountsService.GetCurrentToken();
            Client = RsdnClientHelpers.CreateClient(
                _rsdnUri,
                _authenticator.GetAccessTokenFactory(
                    () => _token,
                    token =>
            {
                accountsService.SetCurrentToken(token);
                _token = token;
            }));
        }
Example #5
0
        public static TokenFactory GetAccessTokenFactory(
            [NotNull] this RsdnApiAuthenticator authenticator,
            [NotNull] CodeFlowData flowData,
            [NotNull] IDictionary <string, string> redirectParams)
        {
            Code.NotNull(authenticator, nameof(authenticator));
            Code.NotNull(flowData, nameof(flowData));
            Code.NotNull(redirectParams, nameof(redirectParams));

            AuthTokenResponse token = null;
            var tokenLock           = new AsyncLock();

            return(async ct =>
            {
                if (token == null)
                {
                    using (await tokenLock.AcquireAsync(ct))
                        if (token == null)
                        {
                            token = await authenticator.GetTokenByCodeAsync(flowData, redirectParams, ct);

                            return token.AccessToken;
                        }
                }
                if (token.ExpiresOn >= DateTimeOffset.UtcNow)
                {
                    return token.AccessToken;
                }
                using (await tokenLock.AcquireAsync())
                    if (token.ExpiresOn < DateTimeOffset.UtcNow)
                    {
                        token = await authenticator.RefreshTokenAsync(token.RefreshToken, ct);
                    }
                return token?.AccessToken;
            });
        }