Ejemplo n.º 1
0
        public async Task HandlerNotifiedOfExceptionIn2FAChallengeResponse()
        {
            var client            = Substitute.For <IGitHubClient>();
            var twoFaException    = new TwoFactorChallengeFailedException();
            var forbiddenResponse = Substitute.For <IResponse>();

            forbiddenResponse.StatusCode.Returns(HttpStatusCode.Forbidden);
            var loginAttemptsException = new LoginAttemptsExceededException(forbiddenResponse);

            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>())
            .Returns <ApplicationAuthorization>(_ => { throw twoFaException; });
            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>(), "111111")
            .Returns <ApplicationAuthorization>(_ => { throw loginAttemptsException; });

            var loginCache = Substitute.For <ILoginCache>();
            var tfa        = Substitute.For <ITwoFactorChallengeHandler>();

            tfa.HandleTwoFactorException(twoFaException).Returns(
                new TwoFactorChallengeResult("111111"),
                new TwoFactorChallengeResult("123456"));

            var target = new LoginManager(loginCache, tfa, "id", "secret");

            Assert.ThrowsAsync <LoginAttemptsExceededException>(async() => await target.Login(host, client, "foo", "bar"));

            await client.Authorization.Received(1).GetOrCreateApplicationAuthentication(
                "id",
                "secret",
                Arg.Any <NewAuthorization>(),
                "111111");

            tfa.Received(1).ChallengeFailed(loginAttemptsException);
        }
        public async Task TwoFactorExceptionIsPassedToHandler()
        {
            var client    = CreateClient();
            var exception = new TwoFactorChallengeFailedException();

            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>())
            .Returns <ApplicationAuthorization>(_ => { throw exception; });
            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>(), "123456")
            .Returns(new ApplicationAuthorization("123abc"));

            var keychain      = Substitute.For <IKeychain>();
            var tfa           = new Lazy <ITwoFactorChallengeHandler>(() => Substitute.For <ITwoFactorChallengeHandler>());
            var oauthListener = Substitute.For <IOAuthCallbackListener>();

            tfa.Value.HandleTwoFactorException(exception).Returns(new TwoFactorChallengeResult("123456"));

            var target = new LoginManager(keychain, tfa, oauthListener, "id", "secret", scopes);
            await target.Login(host, client, "foo", "bar");

            await client.Authorization.Received().GetOrCreateApplicationAuthentication(
                "id",
                "secret",
                Arg.Any <NewAuthorization>(),
                "123456");
        }
Ejemplo n.º 3
0
        public async Task Failed2FACodeResultsInRetry()
        {
            var client    = Substitute.For <IGitHubClient>();
            var exception = new TwoFactorChallengeFailedException();

            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>())
            .Returns <ApplicationAuthorization>(_ => { throw exception; });
            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>(), "111111")
            .Returns <ApplicationAuthorization>(_ => { throw exception; });
            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>(), "123456")
            .Returns(new ApplicationAuthorization("123abc"));

            var keychain = Substitute.For <IKeychain>();
            var tfa      = new Lazy <ITwoFactorChallengeHandler>(() => Substitute.For <ITwoFactorChallengeHandler>());

            tfa.Value.HandleTwoFactorException(exception).Returns(
                new TwoFactorChallengeResult("111111"),
                new TwoFactorChallengeResult("123456"));

            var target = new LoginManager(keychain, tfa, "id", "secret");
            await target.Login(host, client, "foo", "bar");

            await client.Authorization.Received(1).GetOrCreateApplicationAuthentication(
                "id",
                "secret",
                Arg.Any <NewAuthorization>(),
                "111111");

            await client.Authorization.Received(1).GetOrCreateApplicationAuthentication(
                "id",
                "secret",
                Arg.Any <NewAuthorization>(),
                "123456");
        }
        public async Task HandlerNotifiedOfExceptionIn2FAChallengeResponse()
        {
            var client            = CreateClient();
            var twoFaException    = new TwoFactorChallengeFailedException();
            var forbiddenResponse = Substitute.For <IResponse>();

            forbiddenResponse.StatusCode.Returns(HttpStatusCode.Forbidden);
            var loginAttemptsException = new LoginAttemptsExceededException(forbiddenResponse);

            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>())
            .Returns <ApplicationAuthorization>(_ => { throw twoFaException; });
            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>(), "111111")
            .Returns <ApplicationAuthorization>(_ => { throw loginAttemptsException; });

            var keychain      = Substitute.For <IKeychain>();
            var tfa           = new Lazy <ITwoFactorChallengeHandler>(() => Substitute.For <ITwoFactorChallengeHandler>());
            var oauthListener = Substitute.For <IOAuthCallbackListener>();

            tfa.Value.HandleTwoFactorException(twoFaException).Returns(
                new TwoFactorChallengeResult("111111"),
                new TwoFactorChallengeResult("123456"));

            var target = new LoginManager(keychain, tfa, oauthListener, "id", "secret", scopes);

            Assert.ThrowsAsync <LoginAttemptsExceededException>(async() => await target.Login(host, client, "foo", "bar"));

            await client.Authorization.Received(1).GetOrCreateApplicationAuthentication(
                "id",
                "secret",
                Arg.Any <NewAuthorization>(),
                "111111");

            await tfa.Value.Received(1).ChallengeFailed(loginAttemptsException);
        }
            public void InvalidAuthenticationCodeIsSetWhenRetryFailed()
            {
                var target    = CreateTarget();
                var exception = new TwoFactorChallengeFailedException();

                target.Show(new TwoFactorRequiredUserError(exception));

                Assert.True(target.InvalidAuthenticationCode);
            }
            public void ClearsIsBusy()
            {
                var target    = CreateTarget();
                var exception = new TwoFactorChallengeFailedException();

                target.OkCommand.ExecuteAsync();
                target.Show(new TwoFactorRequiredUserError(exception));

                Assert.False(target.IsBusy);
            }
            public async Task OkCommandCompletesAndReturnsNullWithNoAuthorizationCodeAsync()
            {
                var target    = CreateTarget();
                var exception = new TwoFactorChallengeFailedException();
                var userError = new TwoFactorRequiredUserError(exception);
                var task      = target.Show(userError).ToTask();

                target.OkCommand.Execute(null);
                var result = await task;

                Assert.That(result, Is.Null);
            }
            public async Task CancelCommandCompletesAndReturnsNullAsync()
            {
                var target    = CreateTarget();
                var exception = new TwoFactorChallengeFailedException();
                var userError = new TwoFactorRequiredUserError(exception, TwoFactorType.AuthenticatorApp);
                var task      = target.Show(userError).ToTask();

                target.AuthenticationCode = "123456";
                target.Cancel();
                var result = await task;

                Assert.False(target.IsBusy);
                Assert.That(result, Is.Null);
            }
            public async Task ResendCodeCommandCompletesAndReturnsRequestResendCodeAsync()
            {
                var target    = CreateTarget();
                var exception = new TwoFactorChallengeFailedException();
                var userError = new TwoFactorRequiredUserError(exception);
                var task      = target.Show(userError).ToTask();

                target.AuthenticationCode = "123456";
                target.ResendCodeCommand.Execute(null);
                var result = await task;

                Assert.False(target.IsBusy);
                Assert.That(TwoFactorChallengeResult.RequestResendCode, Is.EqualTo(result));
            }
Ejemplo n.º 10
0
            public async Task OkCommandCompletesAndReturnsAuthorizationCode()
            {
                var target    = CreateTarget();
                var exception = new TwoFactorChallengeFailedException();
                var userError = new TwoFactorRequiredUserError(exception);
                var task      = target.Show(userError).ToTask();

                target.AuthenticationCode = "123456";
                target.OkCommand.Execute(null);

                var result = await task;

                Assert.That("123456", Is.EqualTo(result.AuthenticationCode));
            }
            public async Task ShowErrorMessageIsClearedWhenAuthenticationCodeSentAsync()
            {
                var target    = CreateTarget();
                var exception = new TwoFactorChallengeFailedException();
                var userError = new TwoFactorRequiredUserError(exception);
                var task      = target.Show(userError).ToTask();

                Assert.True(target.ShowErrorMessage);
                target.ResendCodeCommand.Execute(null);

                var result = await task;

                Assert.False(target.ShowErrorMessage);
            }
            public async Task OkCommandCompletesAndReturnsNullWithNoAuthorizationCode()
            {
                var target    = CreateTarget();
                var exception = new TwoFactorChallengeFailedException();
                var userError = new TwoFactorRequiredUserError(exception);
                var task      = target.Show(userError).ToTask();

                target.OkCommand.Execute(null);
                var result = await task;

                // This isn't correct but it doesn't matter as the dialog will be closed.
                Assert.True(target.IsBusy);

                Assert.Null(result);
            }
Ejemplo n.º 13
0
        public async Task ErasesLoginWhenNonOctokitExceptionThrownIn2FA()
        {
            var client    = Substitute.For <IGitHubClient>();
            var user      = new User();
            var exception = new TwoFactorChallengeFailedException();

            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>())
            .Returns <ApplicationAuthorization>(_ => { throw exception; });
            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>(), "123456")
            .Returns <ApplicationAuthorization>(_ => { throw new InvalidOperationException(); });
            client.User.Current().Returns(user);

            var loginCache = Substitute.For <ILoginCache>();
            var tfa        = Substitute.For <ITwoFactorChallengeHandler>();

            tfa.HandleTwoFactorException(exception).Returns(new TwoFactorChallengeResult("123456"));

            var target = new LoginManager(loginCache, tfa, "id", "secret");
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await target.Login(host, client, "foo", "bar"));

            await loginCache.Received().EraseLogin(host);
        }
Ejemplo n.º 14
0
            public void ThrowsTwoFactorChallengeFailedExceptionWhenProvidedCodeIsIncorrect()
            {
                var challengeResults = new Queue <TwoFactorChallengeResult>(new[]
                {
                    TwoFactorChallengeResult.RequestResendCode,
                    new TwoFactorChallengeResult("wrong-code")
                });
                var twoFactorFailedException = new TwoFactorChallengeFailedException();
                var data   = new NewAuthorization();
                var client = Substitute.For <IObservableAuthorizationsClient>();

                client.GetOrCreateApplicationAuthentication("clientId", "secret", Arg.Any <NewAuthorization>())
                .Returns(Observable.Throw <ApplicationAuthorization>(new TwoFactorRequiredException()));
                client.GetOrCreateApplicationAuthentication("clientId",
                                                            "secret",
                                                            Arg.Any <NewAuthorization>(),
                                                            "wrong-code")
                .Returns(Observable.Throw <ApplicationAuthorization>(twoFactorFailedException));
                var observer = Substitute.For <System.IObserver <ApplicationAuthorization> >();

                client.GetOrCreateApplicationAuthentication(
                    "clientId",
                    "secret",
                    data,
                    _ => Observable.Return(challengeResults.Dequeue()))
                .Subscribe(observer);

                observer.Received().OnError(twoFactorFailedException);
                client.Received().GetOrCreateApplicationAuthentication("clientId",
                                                                       "secret",
                                                                       Arg.Any <NewAuthorization>());
                client.Received().GetOrCreateApplicationAuthentication("clientId",
                                                                       "secret",
                                                                       Arg.Any <NewAuthorization>(),
                                                                       "wrong-code");
            }
Ejemplo n.º 15
0
        public async Task TwoFactorExceptionIsPassedToHandler()
        {
            var client    = Substitute.For <IGitHubClient>();
            var exception = new TwoFactorChallengeFailedException();

            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>())
            .Returns <ApplicationAuthorization>(_ => { throw exception; });
            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>(), "123456")
            .Returns(new ApplicationAuthorization("123abc"));

            var loginCache = Substitute.For <ILoginCache>();
            var tfa        = Substitute.For <ITwoFactorChallengeHandler>();

            tfa.HandleTwoFactorException(exception).Returns(new TwoFactorChallengeResult("123456"));

            var target = new LoginManager(loginCache, tfa, "id", "secret");
            await target.Login(host, client, "foo", "bar");

            await client.Authorization.Received().GetOrCreateApplicationAuthentication(
                "id",
                "secret",
                Arg.Any <NewAuthorization>(),
                "123456");
        }
Ejemplo n.º 16
0
        public async Task RequestResendCodeResultsInRetryingLogin()
        {
            var client    = Substitute.For <IGitHubClient>();
            var exception = new TwoFactorChallengeFailedException();
            var user      = new User();

            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>())
            .Returns <ApplicationAuthorization>(_ => { throw exception; });
            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>(), "123456")
            .Returns(new ApplicationAuthorization("456def"));
            client.User.Current().Returns(user);

            var loginCache = Substitute.For <ILoginCache>();
            var tfa        = Substitute.For <ITwoFactorChallengeHandler>();

            tfa.HandleTwoFactorException(exception).Returns(
                TwoFactorChallengeResult.RequestResendCode,
                new TwoFactorChallengeResult("123456"));

            var target = new LoginManager(loginCache, tfa, "id", "secret");
            await target.Login(host, client, "foo", "bar");

            await client.Authorization.Received(2).GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>());
        }
Ejemplo n.º 17
0
        public async Task ErasesLoginWhenNonOctokitExceptionThrownIn2FA()
        {
            var client    = CreateClient();
            var user      = new User();
            var exception = new TwoFactorChallengeFailedException();

            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>())
            .Returns <ApplicationAuthorization>(_ => { throw exception; });
            client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any <NewAuthorization>(), "123456")
            .Returns <ApplicationAuthorization>(_ => { throw new InvalidOperationException(); });
            client.User.Current().Returns(user);

            var keychain      = Substitute.For <IKeychain>();
            var tfa           = new Lazy <ITwoFactorChallengeHandler>(() => Substitute.For <ITwoFactorChallengeHandler>());
            var oauthListener = Substitute.For <IOAuthCallbackListener>();

            tfa.Value.HandleTwoFactorException(exception).Returns(new TwoFactorChallengeResult("123456"));

            var target = new LoginManager(keychain, tfa, oauthListener, "id", "secret", scopes);

            Assert.ThrowsAsync <InvalidOperationException>(async() => await target.Login(host, client, "foo", "bar"));

            await keychain.Received().Delete(host);
        }
Ejemplo n.º 18
0
            public void SetsDefaultMessage()
            {
                var exception = new TwoFactorChallengeFailedException();

                Assert.Equal("The two-factor authentication code supplied is not correct", exception.Message);
            }
            public void ThrowsTwoFactorChallengeFailedExceptionWhenProvidedCodeIsIncorrect()
            {
                var challengeResults = new Queue<TwoFactorChallengeResult>(new[]
                {
                    TwoFactorChallengeResult.RequestResendCode,
                    new TwoFactorChallengeResult("wrong-code"), 
                });
                var twoFactorFailedException = new TwoFactorChallengeFailedException();
                var data = new NewAuthorization();
                var client = Substitute.For<IObservableAuthorizationsClient>();
                client.GetOrCreateApplicationAuthentication("clientId", "secret", Arg.Any<NewAuthorization>())
                    .Returns(Observable.Throw<Authorization>(new TwoFactorRequiredException()));
                client.GetOrCreateApplicationAuthentication("clientId",
                    "secret",
                    Arg.Any<NewAuthorization>(),
                    "wrong-code")
                    .Returns(Observable.Throw<Authorization>(twoFactorFailedException));
                var observer = Substitute.For<System.IObserver<Authorization>>();

                client.GetOrCreateApplicationAuthentication(
                        "clientId",
                        "secret",
                        data,
                        _ => Observable.Return(challengeResults.Dequeue()))
                        .Subscribe(observer);

                observer.Received().OnError(twoFactorFailedException);
                client.Received().GetOrCreateApplicationAuthentication("clientId",
                    "secret",
                    Arg.Any<NewAuthorization>());
                client.Received().GetOrCreateApplicationAuthentication("clientId",
                    "secret",
                    Arg.Any<NewAuthorization>(),
                    "wrong-code");
            }