public async Task WhenExistingAuthLacksScopes_TryLoadExistingAuthorizationAsyncReturnsNullAndExistingAuthzIsDeleted() { var tokenResponse = new TokenResponse() { RefreshToken = "rt", Scope = "one two" }; var adapter = new Mock <IAuthAdapter>(); adapter.Setup(a => a.GetStoredRefreshTokenAsync(It.IsAny <CancellationToken>())) .Returns(Task.FromResult(tokenResponse)); adapter.Setup(a => a.IsRefreshTokenValid(tokenResponse)) .Returns(true); adapter.SetupGet(a => a.Scopes) .Returns(new[] { "one", "two", "email" }); var authz = await OAuthAuthorization.TryLoadExistingAuthorizationAsync( adapter.Object, CancellationToken.None); Assert.IsNull(authz); adapter.Verify(a => a.DeleteStoredRefreshToken(), Times.Once); }
public async Task WhenExistingAuthIsOk_TryLoadExistingAuthorizationAsyncReturnsAuthorization() { var tokenResponse = new TokenResponse() { RefreshToken = "rt", Scope = "email one two" }; var adapter = new Mock <IAuthAdapter>(); adapter.Setup(a => a.GetStoredRefreshTokenAsync(It.IsAny <CancellationToken>())) .Returns(Task.FromResult(tokenResponse)); adapter.Setup(a => a.IsRefreshTokenValid(tokenResponse)) .Returns(true); adapter.SetupGet(a => a.Scopes) .Returns(new[] { "one", "two", "email" }); var authz = await OAuthAuthorization.TryLoadExistingAuthorizationAsync( adapter.Object, CancellationToken.None); Assert.IsNotNull(authz); adapter.Verify(a => a.AuthorizeUsingRefreshToken(tokenResponse), Times.Once); adapter.Verify(a => a.QueryUserInfoAsync( It.IsAny <ICredential>(), It.IsAny <CancellationToken>()), Times.Once); }
public async Task WhenNoExistingAuthPresent_TryLoadExistingAuthorizationAsyncReturnsNull() { var adapter = new Mock <IAuthAdapter>(); adapter.Setup(a => a.GetStoredRefreshTokenAsync(It.IsAny <CancellationToken>())) .Returns(Task.FromResult <TokenResponse>(null)); var authz = await OAuthAuthorization.TryLoadExistingAuthorizationAsync( adapter.Object, CancellationToken.None); Assert.IsNull(authz); }
public static IAuthorization Authorize( Control parent, ClientSecrets clientSecrets, string[] scopes, IDataStore dataStore) { // N.B. Do not dispose the adapter (and embedded GoogleAuthorizationCodeFlow) // as it might be needed for token refreshes later. var oauthAdapter = new GoogleAuthAdapter( new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = clientSecrets, Scopes = scopes, DataStore = dataStore }, Resources.AuthorizationSuccessful); Exception caughtException = null; using (var dialog = new AuthorizeDialog()) { Task.Run(async() => { try { // Try to authorize using OAuth. dialog.authorization = await OAuthAuthorization.TryLoadExistingAuthorizationAsync( oauthAdapter, CancellationToken.None) .ConfigureAwait(true); if (dialog.authorization != null) { // We have existing credentials, there is no need to even // show the "Sign In" button. parent.BeginInvoke((Action)(() => dialog.Close())); } else { // No valid credentials present, request user to authroize // by showing the "Sign In" button. parent.BeginInvoke((Action)(() => dialog.ToggleSignInButton())); } } catch (Exception) { // Something went wrong trying to load existing credentials. parent.BeginInvoke((Action)(() => dialog.ToggleSignInButton())); } }); dialog.signInButton.Click += async(sender, args) => { // Switch to showing spinner so that a user cannot click twice. dialog.ToggleSignInButton(); try { dialog.authorization = await OAuthAuthorization.CreateAuthorizationAsync( oauthAdapter, CancellationToken.None) .ConfigureAwait(true); } catch (Exception e) { caughtException = e; } dialog.Close(); }; dialog.ShowDialog(parent); #pragma warning disable CA1508 // Avoid dead conditional code if (caughtException != null) { throw caughtException; } else { return(dialog.authorization); } #pragma warning restore CA1508 // Avoid dead conditional code } }