public void Setup()
 {
     this.appConfig = new BusinessAppConfig
     {
         ActiveDirectoryAppId = "12345",
         ActiveDirectoryReturnUrl = "https://localhost/return",
         ActiveDirectoryServiceResource = "https://resource/",
     };
     
     this.credentialCache = new MockAdalCredentialCache();
     this.httpProvider = new MockHttpProvider(null);
     this.serviceInfoProvider = new AdalServiceInfoProvider { UserSignInName = "12345" };
 }
Example #2
0
        /// <summary>
        /// Creates an authenticated client from a refresh token using ADAL for authentication.
        /// </summary>
        /// <param name="appConfig">
        ///     The <see cref="AppConfig"/> for the application configuration.
        ///     Authentication requires the following to be initialized:
        ///         - ActiveDirectoryAppId
        ///         - ActiveDirectoryServiceResource
        /// </param>
        /// <param name="refreshToken">The refresh token to redeem for an access token.</param>
        /// <param name="credentialCache">The cache instance for storing user credentials.</param>
        /// <param name="httpProvider">The <see cref="IHttpProvider"/> for sending HTTP requests.</param>
        /// <returns>The <see cref="IOneDriveClient"/> for the session.</returns>
        public static async Task <IOneDriveClient> GetSilentlyAuthenticatedClientAsync(
            AppConfig appConfig,
            string refreshToken,
            AdalCredentialCache credentialCache = null,
            IHttpProvider httpProvider          = null)
        {
            if (string.IsNullOrEmpty(refreshToken))
            {
                throw new OneDriveException(
                          new Error
                {
                    Code    = OneDriveErrorCode.AuthenticationFailure.ToString(),
                    Message = "Refresh token is required for silently authenticating a business client.",
                });
            }

            if (string.IsNullOrEmpty(appConfig.ActiveDirectoryServiceResource))
            {
                throw new OneDriveException(
                          new Error
                {
                    Code    = OneDriveErrorCode.AuthenticationFailure.ToString(),
                    Message = "ActiveDirectoryServiceResource is required for silently authenticating a business client.",
                });
            }

            var serviceInfoProvider = new AdalServiceInfoProvider();

            var client = BusinessClientExtensions.GetClientInternal(
                appConfig,
                serviceInfoProvider,
                credentialCache,
                httpProvider) as OneDriveClient;

            if (client.ServiceInfo == null)
            {
                client.ServiceInfo = await serviceInfoProvider.GetServiceInfo(
                    client.appConfig,
                    client.credentialCache,
                    client.HttpProvider,
                    client.ClientType);
            }

            client.AuthenticationProvider.CurrentAccountSession = new AccountSession {
                RefreshToken = refreshToken
            };

            await client.AuthenticateAsync();

            return(client);
        }
        /// <summary>
        /// Creates an authenticated client from a refresh token using ADAL for authentication.
        /// </summary>
        /// <param name="appConfig">
        ///     The <see cref="AppConfig"/> for the application configuration.
        ///     Authentication requires the following to be initialized:
        ///         - ActiveDirectoryAppId
        ///         - ActiveDirectoryServiceResource
        /// </param>
        /// <param name="refreshToken">The refresh token to redeem for an access token.</param>
        /// <param name="credentialCache">The cache instance for storing user credentials.</param>
        /// <param name="httpProvider">The <see cref="IHttpProvider"/> for sending HTTP requests.</param>
        /// <returns>The <see cref="IOneDriveClient"/> for the session.</returns>
        public static async Task<IOneDriveClient> GetSilentlyAuthenticatedClientAsync(
            AppConfig appConfig,
            string refreshToken,
            AdalCredentialCache credentialCache = null,
            IHttpProvider httpProvider = null)
        {
            if (string.IsNullOrEmpty(refreshToken))
            {
                throw new OneDriveException(
                    new Error
                    {
                        Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "Refresh token is required for silently authenticating a business client.",
                    });
            }

            if (string.IsNullOrEmpty(appConfig.ActiveDirectoryServiceResource))
            {
                throw new OneDriveException(
                    new Error
                    {
                        Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "ActiveDirectoryServiceResource is required for silently authenticating a business client.",
                    });  
            }

            var serviceInfoProvider = new AdalServiceInfoProvider();

            var client = BusinessClientExtensions.GetClientInternal(
                appConfig,
                serviceInfoProvider,
                credentialCache,
                httpProvider) as OneDriveClient;

            if (client.ServiceInfo == null)
            {
                client.ServiceInfo = await serviceInfoProvider.GetServiceInfo(
                    client.appConfig,
                    client.credentialCache,
                    client.HttpProvider,
                    client.ClientType);
            }

            client.AuthenticationProvider.CurrentAccountSession = new AccountSession { RefreshToken = refreshToken };

            await client.AuthenticateAsync();

            return client;
        }
        public async Task GetServiceInfo_AuthenticationProviderAlreadySet()
        {
            var authenticationProvider = new MockAuthenticationProvider();
            this.serviceInfoProvider = new AdalServiceInfoProvider(authenticationProvider.Object);
            var serviceInfo = await this.serviceInfoProvider.GetServiceInfo(
                this.appConfig,
                this.credentialCache.Object,
                this.httpProvider.Object,
                ClientType.Business);

            Assert.IsNotInstanceOfType(serviceInfo.AuthenticationProvider, typeof(AdalAuthenticationProvider), "Unexpected authentication provider type.");
            Assert.AreEqual(authenticationProvider.Object, serviceInfo.AuthenticationProvider, "Unexpected authentication provider set.");
        }