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; }); }
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; }); }
public void Setup() { _authenticator = RsdnClientHelpers.CreateAuthenticator( TestsBase.ServiceUri, TestsBase.TestClientID, TestsBase.TestClientSecret, "offline_access", false); }
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; })); }
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; }); }