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