public void OnRedirect_AuthFlow_RetrievesAccessToken()
        {
            OpenIDConnectService oidc         = new OpenIDConnectService();
            IOidcProvider        oidcProvider = A.Fake <IOidcProvider>();

            A.CallTo(() => oidcProvider.GetAuthorizationCode(A <Dictionary <string, string> > .Ignored)).Returns("myCode");
            A.CallTo(() => oidcProvider.GetAccessTokenFromCodeAsync(A <string> .Ignored, A <string> .Ignored))
            .Returns(Task.FromResult("myAccessToken"));
            A.CallTo(() => oidcProvider.AuthorizationFlow).Returns(AuthorizationFlow.AUTHORIZATION_CODE);
            oidc.OidcProvider = oidcProvider;
            IRedirectServerListener serverListener = A.Fake <IRedirectServerListener>();

            oidc.ServerListener = serverListener;

            oidc.OpenLoginPage();

            RedirectReceivedEventArgs redirectReceivedEventArgs = A.Fake <RedirectReceivedEventArgs>();

            serverListener.RedirectReceived += Raise.With(redirectReceivedEventArgs);

            oidc.Update();

            A.CallTo(() => oidcProvider.GetAccessTokenFromCodeAsync("myCode", A <string> .Ignored)).MustHaveHappenedOnceExactly();
            Assert.AreEqual("myAccessToken", oidc.AccessToken);
        }
        public void OnRedirect_RedirectContainsError_ErrorLogged()
        {
            OpenIDConnectService oidc         = new OpenIDConnectService();
            IOidcProvider        oidcProvider = A.Fake <IOidcProvider>();
            string errorMsg;

            A.CallTo(() => oidcProvider.ParametersContainError(A <Dictionary <string, string> > .Ignored, out errorMsg))
            .Returns(true)
            .AssignsOutAndRefParameters("This is a simulated fail");
            oidc.OidcProvider = oidcProvider;
            IRedirectServerListener   serverListener            = A.Fake <IRedirectServerListener>();
            RedirectReceivedEventArgs redirectReceivedEventArgs = A.Fake <RedirectReceivedEventArgs>();

            oidc.ServerListener = serverListener;

            LogAssert.Expect(LogType.Error, new Regex(@"\w*This is a simulated fail\w*"));

            oidc.OpenLoginPage();

            serverListener.RedirectReceived += Raise.With(redirectReceivedEventArgs);

            oidc.Update();

            A.CallTo(() => oidcProvider.ParametersContainError(A <Dictionary <string, string> > .Ignored, out errorMsg))
            .MustHaveHappenedOnceExactly();
            Assert.IsTrue(string.IsNullOrEmpty(oidc.AccessToken));
        }
        public void OpenLoginPage_OidcProviderNull_LogsError()
        {
            OpenIDConnectService oidc = new OpenIDConnectService();

            oidc.OidcProvider = null;

            LogAssert.Expect(LogType.Error, new Regex(@"\w*OIDC provider is not set\w*"));

            oidc.OpenLoginPage();
        }
        public void OpenLoginPage_ServerListenerNull_LogsError()
        {
            OpenIDConnectService oidc = new OpenIDConnectService();

            oidc.OidcProvider   = A.Fake <IOidcProvider>();
            oidc.ServerListener = null;

            LogAssert.Expect(LogType.Error, new Regex(@"\w*Redirect server listener is not set\w*"));

            oidc.OpenLoginPage();
        }
        public void OpenLoginPage_Called_ServerStarted()
        {
            OpenIDConnectService oidc     = new OpenIDConnectService();
            IOidcProvider        provider = A.Fake <IOidcProvider>();

            oidc.OidcProvider = provider;
            IRedirectServerListener server = A.Fake <IRedirectServerListener>();

            oidc.ServerListener = server;

            oidc.OpenLoginPage();
            A.CallTo(() => server.StartServer()).MustHaveHappened();
        }
        public void OpenLoginPage_NoRedirectUriGiven_UsesDefaultPage()
        {
            OpenIDConnectService oidc     = new OpenIDConnectService();
            IOidcProvider        provider = A.Fake <IOidcProvider>();

            oidc.OidcProvider = provider;
            IRedirectServerListener server = A.Fake <IRedirectServerListener>();

            oidc.ServerListener = server;

            oidc.OpenLoginPage();

            Assert.IsFalse(server.ResponseString.Contains("<meta http-equiv=\"Refresh\""));
        }
        public void OpenLoginpage_RedirectUriGiven_RedirectUriInResponseString()
        {
            OpenIDConnectService oidc     = new OpenIDConnectService();
            IOidcProvider        provider = A.Fake <IOidcProvider>();

            oidc.OidcProvider = provider;
            oidc.RedirectURI  = "http://test.com";
            IRedirectServerListener server = A.Fake <IRedirectServerListener>();

            oidc.ServerListener = server;

            oidc.OpenLoginPage();

            Debug.Log("Resp: " + server.ResponseString);
            Assert.IsTrue(server.ResponseString.Contains("<meta http-equiv=\"Refresh\" content=\"0; url = http://test.com\" />"));
        }
        public void OpenLoginPage_OpensLoginPageWithHttpRedirect()
        {
            OpenIDConnectService oidc     = new OpenIDConnectService();
            IOidcProvider        provider = A.Fake <IOidcProvider>();

            oidc.OidcProvider = provider;
            IRedirectServerListener server = A.Fake <IRedirectServerListener>();

            A.CallTo(() => server.GenerateListeningUri(A <string> .Ignored))
            .ReturnsLazily((string schema) => schema + "://127.0.0.1:1234");
            server.ListeningUri = "http://127.0.0.1:1234";
            oidc.ServerListener = server;

            oidc.OpenLoginPage();
            A.CallTo(() => provider.OpenLoginPage(A <string[]> .Ignored, A <string> .That.IsEqualTo("http://127.0.0.1:1234")))
            .MustHaveHappened();
        }
        public void OnRedirect_AuthFlow_ExtractsCode()
        {
            OpenIDConnectService oidc         = new OpenIDConnectService();
            IOidcProvider        oidcProvider = A.Fake <IOidcProvider>();

            A.CallTo(() => oidcProvider.AuthorizationFlow).Returns(AuthorizationFlow.AUTHORIZATION_CODE);
            oidc.OidcProvider = oidcProvider;
            IRedirectServerListener serverListener = A.Fake <IRedirectServerListener>();

            oidc.ServerListener = serverListener;

            oidc.OpenLoginPage();

            RedirectReceivedEventArgs redirectReceivedEventArgs = A.Fake <RedirectReceivedEventArgs>();

            serverListener.RedirectReceived += Raise.With(redirectReceivedEventArgs);

            oidc.Update();

            A.CallTo(() => oidcProvider.GetAuthorizationCode(A <Dictionary <string, string> > .Ignored)).MustHaveHappenedOnceExactly();
        }
        public void IsLoggedIn_SuccessfulLogout_ReturnsFalse()
        {
            OpenIDConnectService oidc         = new OpenIDConnectService();
            IOidcProvider        oidcProvider = A.Fake <IOidcProvider>();

            A.CallTo(() => oidcProvider.GetAccessToken(A <Dictionary <string, string> > .Ignored)).Returns("myAccessToken");
            A.CallTo(() => oidcProvider.AuthorizationFlow).Returns(AuthorizationFlow.IMPLICIT);
            oidc.OidcProvider = oidcProvider;
            IRedirectServerListener serverListener = A.Fake <IRedirectServerListener>();

            oidc.ServerListener = serverListener;

            oidc.OpenLoginPage();

            RedirectReceivedEventArgs redirectReceivedEventArgs = A.Fake <RedirectReceivedEventArgs>();

            serverListener.RedirectReceived += Raise.With(redirectReceivedEventArgs);

            oidc.Logout();

            Assert.IsFalse(oidc.IsLoggedIn);
        }
        public void OnRedirect_ImplicitFlow_AccessTokenRetrieved()
        {
            OpenIDConnectService oidc         = new OpenIDConnectService();
            IOidcProvider        oidcProvider = A.Fake <IOidcProvider>();

            A.CallTo(() => oidcProvider.GetAccessToken(A <Dictionary <string, string> > .Ignored)).Returns("myAccessToken");
            A.CallTo(() => oidcProvider.AuthorizationFlow).Returns(AuthorizationFlow.IMPLICIT);
            oidc.OidcProvider = oidcProvider;
            IRedirectServerListener serverListener = A.Fake <IRedirectServerListener>();

            oidc.ServerListener = serverListener;

            oidc.OpenLoginPage();

            RedirectReceivedEventArgs redirectReceivedEventArgs = A.Fake <RedirectReceivedEventArgs>();

            serverListener.RedirectReceived += Raise.With(redirectReceivedEventArgs);

            oidc.Update();

            Assert.AreEqual("myAccessToken", oidc.AccessToken);
        }
        public void OnRedirect_Success_EventRaised()
        {
            OpenIDConnectService oidc         = new OpenIDConnectService();
            IOidcProvider        oidcProvider = A.Fake <IOidcProvider>();

            oidc.OidcProvider = oidcProvider;
            IRedirectServerListener serverListener = A.Fake <IRedirectServerListener>();

            oidc.ServerListener = serverListener;
            RedirectReceivedEventArgs redirectReceivedEventArgs = A.Fake <RedirectReceivedEventArgs>();

            int eventCalls = 0;

            oidc.LoginCompleted += delegate
            {
                eventCalls++;
            };
            oidc.OpenLoginPage();

            serverListener.RedirectReceived += Raise.With(redirectReceivedEventArgs);
            oidc.Update();

            Assert.AreEqual(1, eventCalls);
        }