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 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 async Task RefreshToken() { var token = await _authenticator.GetTokenByPasswordAsync( TestsBase.TestUserLogin, TestsBase.TestUserPassword); var newToken = await _authenticator.RefreshTokenAsync(token.RefreshToken); Assert.IsNotEmpty(newToken.AccessToken); Assert.NotZero(newToken.ExpiresIn); Assert.AreNotEqual(token.AccessToken, newToken.AccessToken); }
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; }); }