コード例 #1
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);
        }
コード例 #2
0
        public async Task <AccountSession> AuthenticateWithDiscoveryService(
            MockAuthenticationContextWrapper.AuthenticationResultCallback authenticationResultCallback,
            MockAuthenticationContextWrapper.AuthenticationResultSilentCallback authenticationResultSilentCallback,
            DiscoveryServiceResponse discoveryServiceResponse = null)
        {
            const string serviceEndpointUri = "https://localhost";
            const string serviceResourceId  = "https://localhost/resource/";

            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 = new MockAuthenticationContextWrapper
                {
                    AcquireTokenAsyncCallback       = authenticationResultCallback,
                    AcquireTokenSilentAsyncCallback = authenticationResultSilentCallback,
                };

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

            return(accountSession);
        }
コード例 #3
0
        public async Task AuthenticateAsync_AuthenticateWithDiscoveryService(
            IAuthenticationResult authenticationResult,
            MockAuthenticationContextWrapper.AuthenticationResultCallback authenticationResultCallback,
            MockAuthenticationContextWrapper.AuthenticationResultSilentCallback authenticationResultSilentCallback)
        {
            const string serviceEndpointUri = "https://localhost";
            const string serviceResourceId  = "https://localhost/resource/";

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

            var accountSession = await this.AuthenticateWithDiscoveryService(
                authenticationResultCallback,
                authenticationResultSilentCallback);

            Assert.AreEqual(serviceEndpointUri, this.serviceInfo.BaseUrl, "Base URL not set.");
            Assert.AreEqual(serviceResourceId, this.serviceInfo.ServiceResource, "Service resource not set.");
            Assert.AreEqual(authenticationResult.AccessToken, accountSession.AccessToken, "Unexpected access token set.");
            Assert.AreEqual(authenticationResult.AccessTokenType, accountSession.AccessTokenType, "Unexpected access token type set.");
            Assert.AreEqual(AccountType.ActiveDirectory, accountSession.AccountType, "Unexpected account type set.");
            Assert.IsTrue(accountSession.CanSignOut, "CanSignOut set to false.");
            Assert.AreEqual(this.serviceInfo.AppId, accountSession.ClientId, "Unexpected client ID set.");
            Assert.AreEqual(authenticationResult.ExpiresOn, accountSession.ExpiresOnUtc, "Unexpected expiration set.");
            Assert.IsNull(accountSession.UserId, "Unexpected user ID set.");
        }
        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);
        }