public void Setup()
        {
            this.appConfig = new AppConfig();
            this.authenticationProvider = new MockAuthenticationProvider();
            this.authenticationProvider.Setup(provider => provider.AuthenticateAsync()).Returns(Task.FromResult(new AccountSession()));
            this.authenticationProvider.Setup(provider => provider.AppendAuthHeaderAsync(It.IsAny <HttpRequestMessage>())).Returns(Task.FromResult(0));
            this.credentialCache     = new MockCredentialCache();
            this.serializer          = new MockSerializer();
            this.httpResponseMessage = new HttpResponseMessage();
            this.httpProvider        = new MockHttpProvider(this.httpResponseMessage, this.serializer.Object);
            this.serviceInfo         = new ServiceInfo
            {
                AuthenticationProvider = this.authenticationProvider.Object,
            };

            this.serviceInfoProvider = new MockServiceInfoProvider(this.serviceInfo);
            this.webUi          = new MockWebAuthenticationUi();
            this.oneDriveClient = new OneDriveClient(
                this.appConfig,
                this.credentialCache.Object,
                this.httpProvider.Object,
                this.serviceInfoProvider.Object)
            {
                BaseUrl     = string.Format(Constants.Authentication.OneDriveConsumerBaseUrlFormatString, "v1.0"),
                ServiceInfo = this.serviceInfo,
            };
        }
        public void Setup()
        {
            this.appConfig = new AppConfig();
            this.authenticationProvider = new MockAuthenticationProvider();
            this.authenticationProvider.Setup(provider => provider.AuthenticateAsync()).Returns(Task.FromResult(new AccountSession()));
            this.authenticationProvider.Setup(provider => provider.AppendAuthHeaderAsync(It.IsAny<HttpRequestMessage>())).Returns(Task.FromResult(0));
            this.credentialCache = new MockCredentialCache();
            this.serializer = new MockSerializer();
            this.httpResponseMessage = new HttpResponseMessage();
            this.httpProvider = new MockHttpProvider(this.httpResponseMessage, this.serializer.Object);
            this.serviceInfo = new ServiceInfo
            {
                AuthenticationProvider = this.authenticationProvider.Object,
            };

            this.serviceInfoProvider = new MockServiceInfoProvider(this.serviceInfo);
            this.webUi = new MockWebAuthenticationUi();
            this.oneDriveClient = new OneDriveClient(
                this.appConfig,
                this.credentialCache.Object,
                this.httpProvider.Object,
                this.serviceInfoProvider.Object)
            {
                BaseUrl = string.Format(Constants.Authentication.OneDriveConsumerBaseUrlFormatString, "v1.0"),
                ServiceInfo = this.serviceInfo,
            };
        }
Exemplo n.º 3
0
        public async Task GetAccountSessionAsync_SingleSignOn()
        {
            const string code  = "code";
            const string token = "token";

            var applicationCallbackUrl = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString();

            this.signOut = false;

            this.webAuthenticationUi.responseValues = new Dictionary <string, string> {
                { OAuthConstants.CodeKeyName, code }
            };
            this.webAuthenticationUi.OnAuthenticateAsync = (Uri requestUri, Uri callbackUri) =>
            {
                Assert.IsTrue(requestUri.ToString().Contains("response_type=code"), "Unexpected request Uri.");
                Assert.IsTrue(callbackUri.ToString().Equals(applicationCallbackUrl), "Unexpected callback Uri.");
            };

            using (var httpResponseMessage = new HttpResponseMessage())
                using (var responseStream = new MemoryStream())
                    using (var streamContent = new StreamContent(responseStream))
                    {
                        httpResponseMessage.Content = streamContent;

                        var mockSerializer = new MockSerializer();

                        mockSerializer.OnDeserializeObjectStream = (Stream stream) =>
                        {
                            mockSerializer.DeserializeObjectResponse = new Dictionary <string, string> {
                                { OAuthConstants.AccessTokenKeyName, token }
                            };
                        };

                        var httpProvider = new MockHttpProvider(httpResponseMessage, mockSerializer)
                        {
                            OnSendAsync = (HttpRequestMessage requestMessage) =>
                            {
                                Assert.IsTrue(
                                    requestMessage.RequestUri.ToString().Equals(OAuthConstants.MicrosoftAccountTokenServiceUrl),
                                    "Unexpected token request URL.");
                            }
                        };

                        this.authenticationProvider = new MsaAuthenticationProvider(
                            this.clientId,
                            /* returnUrl */ null,
                            this.scopes,
                            this.credentialCache);

                        this.authenticationProvider.webAuthenticationUi = this.webAuthenticationUi;

                        await this.authenticationProvider.AuthenticateUserAsync(httpProvider).ConfigureAwait(false);

                        Assert.IsNotNull(this.authenticationProvider.CurrentAccountSession, "No account session returned.");
                        Assert.AreEqual(token, this.authenticationProvider.CurrentAccountSession.AccessToken, "Unexpected token returned.");
                    }
        }
Exemplo n.º 4
0
        public virtual void Setup()
        {
            this.httpResponseMessage = new HttpResponseMessage();
            this.credentialCache     = new MockCredentialCache();
            this.serializer          = new MockSerializer();
            this.httpProvider        = new MockHttpProvider(this.httpResponseMessage, this.serializer.Object);
            this.webAuthenticationUi = new MockWebAuthenticationUi();

            this.oAuthHelper = new OAuthHelper();
        }
Exemplo n.º 5
0
        public void Setup()
        {
            this.credentialCache     = new MockCredentialCache();
            this.webAuthenticationUi = new MockWebAuthenticationUi();
            this.webAuthenticationUi.OnAuthenticateAsync = this.OnAuthenticateAsync;

            this.authenticationProvider = new MsaAuthenticationProvider(
                this.clientId,
                this.returnUrl,
                this.scopes,
                this.credentialCache);

            this.authenticationProvider.webAuthenticationUi = this.webAuthenticationUi;
        }
        public void Setup()
        {
            this.appConfig = new AppConfig
            {
                MicrosoftAccountAppId        = "12345",
                MicrosoftAccountClientSecret = "secret",
                MicrosoftAccountReturnUrl    = "https://localhost/return",
                MicrosoftAccountScopes       = new string[] { "scope" }
            };

            this.credentialCache     = new MockCredentialCache();
            this.httpResponseMessage = new HttpResponseMessage();
            this.httpProvider        = new MockHttpProvider(this.httpResponseMessage);
            this.webAuthenticationUi = new MockWebAuthenticationUi();
            this.serviceInfoProvider = new ServiceInfoProvider(this.webAuthenticationUi.Object);
        }
        public void Setup()
        {
            this.appConfig = new AppConfig
            {
                MicrosoftAccountAppId = "12345",
                MicrosoftAccountClientSecret = "secret",
                MicrosoftAccountReturnUrl = "https://localhost/return",
                MicrosoftAccountScopes = new string[] { "scope" }
            };

            this.credentialCache = new MockCredentialCache();
            this.httpResponseMessage = new HttpResponseMessage();
            this.httpProvider = new MockHttpProvider(this.httpResponseMessage);
            this.webAuthenticationUi = new MockWebAuthenticationUi();
            this.serviceInfoProvider = new ServiceInfoProvider(this.webAuthenticationUi.Object);
        }
        public virtual void Setup()
        {
            this.httpResponseMessage = new HttpResponseMessage();
            this.credentialCache     = new MockCredentialCache();
            this.serializer          = new MockSerializer();
            this.httpProvider        = new MockHttpProvider(this.httpResponseMessage, this.serializer.Object);
            this.webAuthenticationUi = new MockWebAuthenticationUi();

            this.authenticationProvider = new MsaAuthenticationProvider(
                MsaAuthenticationProviderTests.ClientId,
                MsaAuthenticationProviderTests.ClientSecret,
                MsaAuthenticationProviderTests.ReturnUrl,
                this.scopes,
                this.credentialCache.Object);

            this.authenticationProvider.webAuthenticationUi = this.webAuthenticationUi.Object;
        }
        public void Setup()
        {
            this.credentialCache = new MockCredentialCache();
            this.webAuthenticationUi = new MockWebAuthenticationUi();
            this.webAuthenticationUi.OnAuthenticateAsync = this.OnAuthenticateAsync;

            this.serviceInfo = new ServiceInfo
            {
                AppId = "12345",
                AuthenticationServiceUrl = "https://login.live.com/authenticate",
                CredentialCache = this.credentialCache,
                Scopes = new string[] { "scope1", "scope2" },
                SignOutUrl = "https://login.live.com/signout",
                TokenServiceUrl = "https://login.live.com/token",
                WebAuthenticationUi = this.webAuthenticationUi
            };

            this.authenticationProvider = new WebAuthenticationBrokerAuthenticationProvider(this.serviceInfo);
        }
Exemplo n.º 10
0
 public void Setup()
 {
     this.ClientId               = "123456";
     this.ClientSecret           = "QWERTY";
     this.ReturnUrl              = "http://www.returnurl.com";
     this.Scopes                 = new string[] { "Scope1", "Scope2" };
     this.CredentialCache        = new CredentialCache();
     this.Serializer             = new MockSerializer();
     this.ResponseMessage        = new HttpResponseMessage();
     this.HttpProvider           = new MockHttpProvider(this.ResponseMessage, this.Serializer);
     this.WebAuthenticationUi    = new MockWebAuthenticationUi();
     this.AuthenticationProvider = new MsaAuthenticationProvider(
         this.ClientId,
         this.ClientSecret,
         this.ReturnUrl,
         this.Scopes,
         this.CredentialCache,
         this.WebAuthenticationUi);
 }
Exemplo n.º 11
0
        public void Setup()
        {
            this.credentialCache     = new MockCredentialCache();
            this.webAuthenticationUi = new MockWebAuthenticationUi();
            this.webAuthenticationUi.OnAuthenticateAsync = this.OnAuthenticateAsync;

            this.serviceInfo = new ServiceInfo
            {
                AppId = "12345",
                AuthenticationServiceUrl = "https://login.live.com/authenticate",
                CredentialCache          = this.credentialCache,
                Scopes              = new string[] { "scope1", "scope2" },
                SignOutUrl          = "https://login.live.com/signout",
                TokenServiceUrl     = "https://login.live.com/token",
                WebAuthenticationUi = this.webAuthenticationUi
            };

            this.authenticationProvider = new WebAuthenticationBrokerAuthenticationProvider(this.serviceInfo);
        }
        public virtual void Setup()
        {
            this.httpResponseMessage = new HttpResponseMessage();
            this.credentialCache = new MockCredentialCache();
            this.serializer = new MockSerializer();
            this.httpProvider = new MockHttpProvider(this.httpResponseMessage, this.serializer.Object);
            this.webAuthenticationUi = new MockWebAuthenticationUi();

            this.serviceInfo = new ServiceInfo
            {
                AppId = "12345",
                AuthenticationServiceUrl = "https://login.live.com/authenticate",
                CredentialCache = this.credentialCache.Object,
                HttpProvider = this.httpProvider.Object,
                ReturnUrl = "https://login.live.com/return",
                Scopes = new string[] { "scope1", "scope2" },
                SignOutUrl = "https://login.live.com/signout",
                TokenServiceUrl = "https://login.live.com/token",
                WebAuthenticationUi = this.webAuthenticationUi.Object
            };
        }
Exemplo n.º 13
0
        public virtual void Setup()
        {
            this.httpResponseMessage = new HttpResponseMessage();
            this.credentialCache     = new MockCredentialCache();
            this.serializer          = new MockSerializer();
            this.httpProvider        = new MockHttpProvider(this.httpResponseMessage, this.serializer.Object);
            this.webAuthenticationUi = new MockWebAuthenticationUi();

            this.serviceInfo = new ServiceInfo
            {
                AppId = "12345",
                AuthenticationServiceUrl = "https://login.live.com/authenticate",
                CredentialCache          = this.credentialCache.Object,
                HttpProvider             = this.httpProvider.Object,
                ReturnUrl           = "https://login.live.com/return",
                Scopes              = new string[] { "scope1", "scope2" },
                SignOutUrl          = "https://login.live.com/signout",
                TokenServiceUrl     = "https://login.live.com/token",
                WebAuthenticationUi = this.webAuthenticationUi.Object
            };
        }
Exemplo n.º 14
0
        public async Task AuthenticateAsync_AuthenticateWithClientCertificate()
        {
            this.adalServiceInfo.ServiceResource = serviceResourceId;
            this.adalServiceInfo.BaseUrl         = "https://localhost";

            this.adalServiceInfo.ClientCertificate = new X509Certificate2(@"Certs\testwebapplication.pfx", "password");

            var mockAuthenticationResult = new MockAuthenticationResult();

            mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token");
            mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type");
            mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow);

            var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper();

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                                                       It.Is <string>(resource => resource.Equals(serviceResourceId)),
                                                       It.Is <string>(clientId => clientId.Equals(this.adalServiceInfo.AppId)))).Throws(new Exception());

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByAuthorizationCodeAsync(
                                                       It.Is <string>(code => code.Equals(Constants.Authentication.CodeKeyName)),
                                                       It.Is <Uri>(returnUri => returnUri.ToString().Equals(this.adalServiceInfo.ReturnUrl)),
                                                       It.Is <ClientAssertionCertificate>(certificate =>
                                                                                          certificate.Certificate == this.adalServiceInfo.ClientCertificate &&
                                                                                          certificate.ClientId == this.adalServiceInfo.AppId),
                                                       It.Is <string>(resource => resource.Equals(serviceResourceId))))
            .Returns(Task.FromResult(mockAuthenticationResult.Object));

            var webAuthenticationUi = new MockWebAuthenticationUi(
                new Dictionary <string, string>
            {
                { Constants.Authentication.CodeKeyName, Constants.Authentication.CodeKeyName }
            });

            this.adalServiceInfo.WebAuthenticationUi = webAuthenticationUi.Object;

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public void Setup()
        {
            this.credentialCache     = new MockAdalCredentialCache();
            this.httpResponseMessage = new HttpResponseMessage();
            this.serializer          = new Serializer();
            this.httpProvider        = new MockHttpProvider(this.httpResponseMessage, this.serializer);
            this.webAuthenticationUi = new MockWebAuthenticationUi();

            this.serviceInfo = new ActiveDirectoryServiceInfo
            {
                AppId = "12345",
                AuthenticationServiceUrl = "https://login.live.com/authenticate",
                CredentialCache          = this.credentialCache,
                HttpProvider             = this.httpProvider,
                ReturnUrl           = "https://login.live.com/return",
                SignOutUrl          = "https://login.live.com/signout",
                TokenServiceUrl     = "https://login.live.com/token",
                WebAuthenticationUi = this.webAuthenticationUi
            };

            this.authenticationProvider = new AdalAuthenticationProvider(this.serviceInfo);
        }
        public void Setup()
        {
            this.credentialCache = new MockAdalCredentialCache();
            this.httpResponseMessage = new HttpResponseMessage();
            this.serializer = new Serializer();
            this.httpProvider = new MockHttpProvider(this.httpResponseMessage, this.serializer);
            this.webAuthenticationUi = new MockWebAuthenticationUi();

            this.serviceInfo = new ActiveDirectoryServiceInfo
            {
                AppId = "12345",
                AuthenticationServiceUrl = "https://login.live.com/authenticate",
                CredentialCache = this.credentialCache,
                HttpProvider = this.httpProvider,
                ReturnUrl = "https://login.live.com/return",
                SignOutUrl = "https://login.live.com/signout",
                TokenServiceUrl = "https://login.live.com/token",
                WebAuthenticationUi = this.webAuthenticationUi
            };

            this.authenticationProvider = new AdalAuthenticationProvider(this.serviceInfo);
        }
Exemplo n.º 17
0
        public async Task SignOutAsync_SingleSignOn()
        {
            var applicationCallbackUrl = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString();

            this.signOut = true;
            var expectedSignOutUrl = string.Format(
                "{0}?client_id={1}&redirect_uri={2}",
                OAuthConstants.MicrosoftAccountSignOutUrl,
                this.clientId,
                applicationCallbackUrl);

            this.webAuthenticationUi.OnAuthenticateAsync = (Uri requestUri, Uri callbackUri) =>
            {
                Assert.AreEqual(expectedSignOutUrl, requestUri.ToString(), "Unexpected request Uri.");
                Assert.AreEqual(applicationCallbackUrl, callbackUri.ToString(), "Unexpected callback Uri.");
            };

            var accountSession = new AccountSession
            {
                AccessToken = "accessToken",
                ClientId    = "12345",
            };

            this.authenticationProvider = new MsaAuthenticationProvider(
                this.clientId,
                /* returnUrl */ null,
                this.scopes,
                this.credentialCache);

            this.authenticationProvider.webAuthenticationUi   = this.webAuthenticationUi;
            this.authenticationProvider.CurrentAccountSession = accountSession;

            await this.authenticationProvider.SignOutAsync().ConfigureAwait(false);

            Assert.IsNull(this.authenticationProvider.CurrentAccountSession, "Current account session not cleared.");
            Assert.IsTrue(this.credentialCache.DeleteFromCacheCalled, "DeleteFromCache not called.");
        }
        public async Task AuthenticateAsync_AuthenticateWithClientCredential()
        {
            this.adalServiceInfo.ServiceResource = serviceResourceId;
            this.adalServiceInfo.BaseUrl = "https://localhost";

            this.adalServiceInfo.ClientSecret = "clientSecret";

            var mockAuthenticationResult = new MockAuthenticationResult();
            mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token");
            mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type");
            mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow);

            var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper();

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                It.Is<string>(resource => resource.Equals(serviceResourceId)),
                It.Is<string>(clientId => clientId.Equals(this.adalServiceInfo.AppId)))).Throws(new Exception());

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByAuthorizationCodeAsync(
                It.Is<string>(code => code.Equals(Constants.Authentication.CodeKeyName)),
                It.Is<Uri>(returnUri => returnUri.ToString().Equals(this.adalServiceInfo.ReturnUrl)),
                It.Is<ClientCredential>(credential => credential.ClientId.Equals(this.adalServiceInfo.AppId)),
                It.Is<string>(resource => resource.Equals(serviceResourceId))))
                .Returns(Task.FromResult(mockAuthenticationResult.Object));

            var webAuthenticationUi = new MockWebAuthenticationUi(
                new Dictionary<string, string>
                {
                    { Constants.Authentication.CodeKeyName, Constants.Authentication.CodeKeyName }
                });

            this.adalServiceInfo.WebAuthenticationUi = webAuthenticationUi.Object;

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }