Пример #1
0
        public async Task AuthenticateAsync_AuthenticateWithRefreshToken_WithClientCredential()
        {
            string refreshToken = "refresh";

            this.authenticationProvider.CurrentAccountSession = new AccountSession {
                RefreshToken = refreshToken
            };

            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.AcquireTokenByRefreshTokenAsync(
                                                       It.Is <string>(token => token.Equals(refreshToken)),
                                                       It.Is <ClientCredential>(credential => credential.ClientId.Equals(this.adalServiceInfo.AppId)),
                                                       It.Is <string>(resource => resource.Equals(this.adalServiceInfo.ServiceResource)))).Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
Пример #2
0
        public async Task AuthenticateAsync_AuthenticateWithRefreshToken_WithClientCertificate()
        {
            string refreshToken = "refresh";

            this.authenticationProvider.CurrentAccountSession = new AccountSession {
                RefreshToken = refreshToken
            };

            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.AcquireTokenByRefreshTokenAsync(
                                                       It.Is <string>(token => token.Equals(refreshToken)),
                                                       It.Is <ClientAssertionCertificate>(certificate =>
                                                                                          certificate.ClientId.Equals(this.adalServiceInfo.AppId) &&
                                                                                          certificate.Certificate == this.adalServiceInfo.ClientCertificate),
                                                       It.Is <string>(resource => resource.Equals(serviceResourceId)))).Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public async Task AuthenticateAsync_AuthenticateSilentlyWithClientCredential()
        {
            this.serviceInfo.ServiceResource = serviceResourceId;
            this.serviceInfo.BaseUrl         = "https://localhost";

            this.serviceInfo.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.serviceInfo.AppId)))).Throws(new Exception());

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                                                       It.Is <string>(resource => resource.Equals(serviceResourceId)),
                                                       It.Is <ClientCredential>(credential => credential.ClientId.Equals(this.serviceInfo.AppId)),
                                                       UserIdentifier.AnyUser)).Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
Пример #4
0
        public async Task AuthenticateAsync_AuthenticateSilentlyWithClientCertificate()
        {
            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.AcquireTokenSilentAsync(
                                                       It.Is <string>(resource => resource.Equals(serviceResourceId)),
                                                       It.Is <ClientAssertionCertificate>(certificate =>
                                                                                          certificate.Certificate == this.adalServiceInfo.ClientCertificate &&
                                                                                          certificate.ClientId == this.adalServiceInfo.AppId),
                                                       UserIdentifier.AnyUser)).Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public async Task AuthenticateAsync_CachedCurrentAccountSessionExpiring()
        {
            var cachedAccountSession = new AccountSession
            {
                AccessToken  = "expiredToken",
                ExpiresOnUtc = DateTimeOffset.UtcNow,
            };

            this.authenticationProvider.CurrentAccountSession = cachedAccountSession;

            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.AddHours(1));

            var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper();

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                                                       It.Is <string>(resource => resource.Equals(serviceResourceId)),
                                                       It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId))))
            .Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public async Task AuthenticateAsync_AuthenticateWithoutDiscoveryService()
        {
            this.serviceInfo.ServiceResource = serviceResourceId;
            this.serviceInfo.BaseUrl         = "https://localhost";

            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.serviceInfo.AppId)))).Throws(new Exception());

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireToken(
                                                       It.Is <string>(resource => resource.Equals(serviceResourceId)),
                                                       It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId)),
                                                       It.Is <Uri>(returnUri => returnUri.ToString().Equals(this.serviceInfo.ReturnUrl)),
                                                       PromptBehavior.Always)).Returns(mockAuthenticationResult.Object);

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public async Task AuthenticateAsync_AuthenticateSilentlyWithDiscoveryService()
        {
            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.serviceInfo.AppId))))
            .Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithDiscoveryService(mockAuthenticationContextWrapper, mockAuthenticationResult.Object);
        }
Пример #8
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);
        }
Пример #9
0
        public async Task <AccountSession> AuthenticateWithDiscoveryService(
            MockAuthenticationContextWrapper mockAuthenticationContextWrapper,
            DiscoveryServiceResponse discoveryServiceResponse = null)
        {
            var mockAuthenticationResult = new MockAuthenticationResult();

            mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("discoveryResource");

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                                                       It.Is <string>(resource => resource.Equals(Constants.Authentication.ActiveDirectoryDiscoveryResource)),
                                                       It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId)))).Throws(new Exception());

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireToken(
                                                       It.Is <string>(resource => resource.Equals(Constants.Authentication.ActiveDirectoryDiscoveryResource)),
                                                       It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId)),
                                                       It.Is <Uri>(returnUri => returnUri.ToString().Equals(this.serviceInfo.ReturnUrl)),
                                                       PromptBehavior.Auto,
                                                       UserIdentifier.AnyUser)).Returns(mockAuthenticationResult.Object);

            if (discoveryServiceResponse == null)
            {
                discoveryServiceResponse = new DiscoveryServiceResponse
                {
                    Value = new List <DiscoveryService>
                    {
                        new DiscoveryService
                        {
                            Capability         = Constants.Authentication.MyFilesCapability,
                            ServiceApiVersion  = this.serviceInfo.OneDriveServiceEndpointVersion,
                            ServiceEndpointUri = serviceEndpointUri,
                            ServiceResourceId  = serviceResourceId,
                        }
                    }
                };
            }

            var requestBodyString = this.serializer.SerializeObject(discoveryServiceResponse);

            AccountSession accountSession;

            using (var stringContent = new StringContent(requestBodyString))
            {
                this.httpResponseMessage.Content = stringContent;
                this.authenticationProvider.authenticationContextWrapper = mockAuthenticationContextWrapper.Object;

                accountSession = await this.authenticationProvider.AuthenticateAsync();
            }

            return(accountSession);
        }
        public async Task AuthenticateAsync_AuthenticateSilentlyWithClientCredential()
        {
            this.serviceInfo.ServiceResource = serviceResourceId;
            this.serviceInfo.BaseUrl = "https://localhost";

            this.serviceInfo.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.serviceInfo.AppId)))).Throws(new Exception());

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                It.Is<string>(resource => resource.Equals(serviceResourceId)),
                It.Is<ClientCredential>(credential => credential.ClientId.Equals(this.serviceInfo.AppId)),
                UserIdentifier.AnyUser)).Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public async Task<AccountSession> AuthenticateWithDiscoveryService(
            MockAuthenticationContextWrapper mockAuthenticationContextWrapper,
            DiscoveryServiceResponse discoveryServiceResponse = null)
        {
            var mockAuthenticationResult = new MockAuthenticationResult();
            mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("discoveryResource");

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                It.Is<string>(resource => resource.Equals(Constants.Authentication.ActiveDirectoryDiscoveryResource)),
                It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId)))).Throws(new Exception());

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireToken(
                It.Is<string>(resource => resource.Equals(Constants.Authentication.ActiveDirectoryDiscoveryResource)),
                It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId)),
                It.Is<Uri>(returnUri => returnUri.ToString().Equals(this.serviceInfo.ReturnUrl)),
                PromptBehavior.Always)).Returns(mockAuthenticationResult.Object);

            if (discoveryServiceResponse == null)
            {
                discoveryServiceResponse = new DiscoveryServiceResponse
                {
                    Value = new List<DiscoveryService>
                    {
                        new DiscoveryService
                        {
                            Capability = Constants.Authentication.MyFilesCapability,
                            ServiceApiVersion = this.serviceInfo.OneDriveServiceEndpointVersion,
                            ServiceEndpointUri = serviceEndpointUri,
                            ServiceResourceId = serviceResourceId,
                        }
                    }
                };
            }

            var requestBodyString = this.serializer.SerializeObject(discoveryServiceResponse);

            AccountSession accountSession;

            using (var stringContent = new StringContent(requestBodyString))
            {
                this.httpResponseMessage.Content = stringContent;
                this.authenticationProvider.authenticationContextWrapper = mockAuthenticationContextWrapper.Object;

                accountSession = await this.authenticationProvider.AuthenticateAsync();
            }

            return accountSession;
        }
        public async Task AuthenticateAsync_CachedCurrentAccountSessionExpiring()
        {
            var cachedAccountSession = new AccountSession
            {
                AccessToken = "expiredToken",
                ExpiresOnUtc = DateTimeOffset.UtcNow,
            };

            this.authenticationProvider.CurrentAccountSession = cachedAccountSession;
            
            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.AddHours(1));

            var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper();
            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                It.Is<string>(resource => resource.Equals(serviceResourceId)),
                It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId))))
                .Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public async Task AuthenticateAsync_AuthenticateWithoutDiscoveryService()
        {
            this.serviceInfo.ServiceResource = serviceResourceId;
            this.serviceInfo.BaseUrl = "https://localhost";

            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.serviceInfo.AppId)))).Throws(new Exception());

            mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireToken(
                It.Is<string>(resource => resource.Equals(serviceResourceId)),
                It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId)),
                It.Is<Uri>(returnUri => returnUri.ToString().Equals(this.serviceInfo.ReturnUrl)),
                PromptBehavior.Always)).Returns(mockAuthenticationResult.Object);

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public async Task AuthenticateAsync_AuthenticateSilentlyWithoutDiscoveryService()
        {
            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.serviceInfo.AppId))))
                .Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public async Task AuthenticateAsync_AuthenticateSilentlyWithClientCertificate()
        {
            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.AcquireTokenSilentAsync(
                It.Is<string>(resource => resource.Equals(serviceResourceId)),
                It.Is<ClientAssertionCertificate>(certificate =>
                    certificate.Certificate == this.adalServiceInfo.ClientCertificate &&
                    certificate.ClientId == this.adalServiceInfo.AppId),
                UserIdentifier.AnyUser)).Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public async Task AuthenticateAsync_AuthenticateWithRefreshToken_WithClientCredential()
        {
            string refreshToken = "refresh";

            this.authenticationProvider.CurrentAccountSession = new AccountSession { RefreshToken = refreshToken };

            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.AcquireTokenByRefreshTokenAsync(
                It.Is<string>(token => token.Equals(refreshToken)),
                It.Is<ClientCredential>(credential => credential.ClientId.Equals(this.adalServiceInfo.AppId)),
                It.Is<string>(resource => resource.Equals(this.adalServiceInfo.ServiceResource)))).Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        public async Task AuthenticateAsync_AuthenticateWithRefreshToken_WithClientCertificate()
        {
            string refreshToken = "refresh";

            this.authenticationProvider.CurrentAccountSession = new AccountSession { RefreshToken = refreshToken };

            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.AcquireTokenByRefreshTokenAsync(
                It.Is<string>(token => token.Equals(refreshToken)),
                It.Is<ClientAssertionCertificate>(certificate =>
                    certificate.ClientId.Equals(this.adalServiceInfo.AppId) &&
                    certificate.Certificate == this.adalServiceInfo.ClientCertificate),
                It.Is<string>(resource => resource.Equals(serviceResourceId)))).Returns(Task.FromResult(mockAuthenticationResult.Object));

            await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService(
                mockAuthenticationContextWrapper.Object,
                mockAuthenticationResult.Object);
        }
        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);
        }
        public async Task <BusinessServiceInformation> AuthenticateWithDiscoveryServiceAsync(
            DiscoveryServiceResponse discoveryServiceResponse = null,
            string refreshToken = null)
        {
            bool refresh = refreshToken != null;

            var mockAuthenticationResult = new MockAuthenticationResult();

            mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token");
            mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns((string)null);
            mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow.AddHours(1));

            var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper();

            if (refresh)
            {
                mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByRefreshTokenAsync(
                                                           It.Is <string>(token => token.Equals(refreshToken)),
                                                           It.Is <string>(clientId => clientId.Equals(AuthenticationTestBase.ClientId)),
                                                           It.Is <string>(resource => resource.Equals(OAuthConstants.ActiveDirectoryDiscoveryResource))))
                .Returns(Task.FromResult(mockAuthenticationResult.Object));
            }
            else
            {
                mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                                                           It.Is <string>(resource => resource.Equals(OAuthConstants.ActiveDirectoryDiscoveryResource)),
                                                           It.Is <string>(clientId => clientId.Equals(AuthenticationTestBase.ClientId)),
                                                           UserIdentifier.AnyUser))
                .Returns(Task.FromResult(mockAuthenticationResult.Object));
            }

            var authenticationProvider = new AdalAuthenticationProvider(
                AuthenticationTestBase.ClientId,
                AuthenticationTestBase.ReturnUrl,
                mockAuthenticationContextWrapper.Object);

            var discoveryServiceHelper = new DiscoveryServiceHelper(authenticationProvider);

            if (discoveryServiceResponse == null)
            {
                discoveryServiceResponse = new DiscoveryServiceResponse
                {
                    Value = new List <DiscoveryService>
                    {
                        new DiscoveryService
                        {
                            Capability         = "MyFiles",
                            ServiceApiVersion  = "v2.0",
                            ServiceEndpointUri = AuthenticationTestBase.ServiceEndpointUrl,
                            ServiceResourceId  = AuthenticationTestBase.ServiceResourceId,
                        }
                    }
                };
            }

            var requestBodyString = this.serializer.SerializeObject(discoveryServiceResponse);

            BusinessServiceInformation businessServiceInformation = null;

            using (var stringContent = new StringContent(requestBodyString))
            {
                this.httpResponseMessage.Content = stringContent;

                if (refresh)
                {
                    businessServiceInformation = await discoveryServiceHelper.DiscoverFilesEndpointInformationForUserWithRefreshTokenAsync(
                        refreshToken,
                        httpProvider : this.httpProvider.Object);
                }
                else
                {
                    businessServiceInformation = await discoveryServiceHelper.DiscoverFilesEndpointInformationForUserAsync(httpProvider : this.httpProvider.Object);
                }
            }

            return(businessServiceInformation);
        }