public Task<ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType = ClientType.Consumer)
        {
            if (clientType == ClientType.Business)
            {
                throw new OneDriveException(
                    new Error
                    {
                        Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "OnlineIdServiceProvider only supports Microsoft Account authentication."
                    });
            }

            var microsoftAccountServiceInfo = new MicrosoftAccountServiceInfo
            {
                AppId = appConfig.MicrosoftAccountAppId,
                ClientSecret = appConfig.MicrosoftAccountClientSecret,
                CredentialCache = credentialCache,
                HttpProvider = httpProvider,
                Scopes = appConfig.MicrosoftAccountScopes,
            };

            microsoftAccountServiceInfo.AuthenticationProvider = this.AuthenticationProvider ?? new OnlineIdAuthenticationProvider(microsoftAccountServiceInfo);
            return Task.FromResult<ServiceInfo>(microsoftAccountServiceInfo);
        }
        public Task <ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType = ClientType.Consumer)
        {
            if (clientType == ClientType.Business)
            {
                throw new OneDriveException(
                          new Error
                {
                    Code    = OneDriveErrorCode.AuthenticationFailure.ToString(),
                    Message = "OnlineIdServiceProvider only supports Microsoft account authentication."
                });
            }

            var microsoftAccountServiceInfo = new MicrosoftAccountServiceInfo
            {
                AppId           = appConfig.MicrosoftAccountAppId,
                ClientSecret    = appConfig.MicrosoftAccountClientSecret,
                CredentialCache = credentialCache,
                HttpProvider    = httpProvider,
                Scopes          = appConfig.MicrosoftAccountScopes,
            };

            microsoftAccountServiceInfo.AuthenticationProvider = this.AuthenticationProvider ?? new OnlineIdAuthenticationProvider(microsoftAccountServiceInfo);
            return(Task.FromResult <ServiceInfo>(microsoftAccountServiceInfo));
        }
        public void VerifyClassInitialization()
        {
            var serviceInfo = new MicrosoftAccountServiceInfo();

            Assert.AreEqual(AccountType.MicrosoftAccount, serviceInfo.AccountType, "Unexpected account type.");
            Assert.AreEqual(Constants.Authentication.MicrosoftAccountAuthenticationServiceUrl, serviceInfo.AuthenticationServiceUrl, "Unexpected authentication service URL.");
            Assert.AreEqual(string.Format(Constants.Authentication.OneDriveConsumerBaseUrlFormatString, "v1.0"), serviceInfo.BaseUrl, "Unexpected base URL.");
            Assert.AreEqual(Constants.Authentication.MicrosoftAccountSignOutUrl, serviceInfo.SignOutUrl, "Unexpected sign out URL.");
            Assert.AreEqual(Constants.Authentication.MicrosoftAccountTokenServiceUrl, serviceInfo.TokenServiceUrl, "Unexpected token service URL.");
        }
        /// <summary>
        /// Generates the <see cref="ServiceInfo"/> for the current application configuration.
        /// </summary>
        /// <param name="appConfig">The <see cref="AppConfig"/> for the current application.</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="ServiceInfo"/> for the current session.</returns>
        public virtual Task <ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider)
        {
            var microsoftAccountServiceInfo = new MicrosoftAccountServiceInfo
            {
                AppId               = appConfig.MicrosoftAccountAppId,
                ClientSecret        = appConfig.MicrosoftAccountClientSecret,
                CredentialCache     = credentialCache,
                HttpProvider        = httpProvider,
                ReturnUrl           = appConfig.MicrosoftAccountReturnUrl,
                Scopes              = appConfig.MicrosoftAccountScopes,
                WebAuthenticationUi = this.webAuthenticationUi,
            };

            microsoftAccountServiceInfo.AuthenticationProvider = this.AuthenticationProvider ?? new MicrosoftAccountAuthenticationProvider(microsoftAccountServiceInfo);
            return(Task.FromResult <ServiceInfo>(microsoftAccountServiceInfo));
        }
        /// <summary>
        /// Generates the <see cref="ServiceInfo"/> for the current application configuration.
        /// </summary>
        /// <param name="appConfig">The <see cref="AppConfig"/> for the current application.</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="ServiceInfo"/> for the current session.</returns>
        public Task<ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider)
        {
            var microsoftAccountServiceInfo = new MicrosoftAccountServiceInfo
            {
                AppId = appConfig.MicrosoftAccountAppId,
                ClientSecret = appConfig.MicrosoftAccountClientSecret,
                CredentialCache = credentialCache,
                HttpProvider = httpProvider,
                ReturnUrl = appConfig.MicrosoftAccountReturnUrl,
                Scopes = appConfig.MicrosoftAccountScopes,
                WebAuthenticationUi = this.webAuthenticationUi,
            };

            microsoftAccountServiceInfo.AuthenticationProvider = this.AuthenticationProvider ?? new MicrosoftAccountAuthenticationProvider(microsoftAccountServiceInfo);
            return Task.FromResult<ServiceInfo>(microsoftAccountServiceInfo);
        }
        /// <summary>
        /// Generates the <see cref="ServiceInfo"/> for the current application configuration.
        /// </summary>
        /// <param name="appConfig">The <see cref="AppConfig"/> for the current application.</param>
        /// <param name="credentialCache">The cache instance for storing user credentials.</param>
        /// <param name="httpProvider">The <see cref="IHttpProvider"/> for sending HTTP requests.</param>
        /// <param name="clientType">The <see cref="ClientType"/> to specify the business or consumer service.</param>
        /// <returns>The <see cref="ServiceInfo"/> for the current session.</returns>
        public virtual Task <ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType)
        {
            if (clientType == ClientType.Consumer)
            {
                var microsoftAccountServiceInfo = new MicrosoftAccountServiceInfo
                {
                    AppId               = appConfig.MicrosoftAccountAppId,
                    ClientSecret        = appConfig.MicrosoftAccountClientSecret,
                    CredentialCache     = credentialCache,
                    HttpProvider        = httpProvider,
                    ReturnUrl           = appConfig.MicrosoftAccountReturnUrl,
                    Scopes              = appConfig.MicrosoftAccountScopes,
                    UserId              = this.UserSignInName,
                    WebAuthenticationUi = this.webAuthenticationUi,
                };

                microsoftAccountServiceInfo.AuthenticationProvider = this.AuthenticationProvider ?? new MicrosoftAccountAuthenticationProvider(microsoftAccountServiceInfo);
                return(Task.FromResult <ServiceInfo>(microsoftAccountServiceInfo));
            }

            var activeDirectoryServiceInfo = new ActiveDirectoryServiceInfo
            {
                AppId = appConfig.ActiveDirectoryAppId,
                AuthenticationProvider   = this.AuthenticationProvider,
                AuthenticationServiceUrl = string.IsNullOrEmpty(appConfig.ActiveDirectoryAuthenticationServiceUrl)
                    ? Constants.Authentication.ActiveDirectoryAuthenticationServiceUrl
                    : appConfig.ActiveDirectoryAuthenticationServiceUrl,
                BaseUrl         = appConfig.ActiveDirectoryServiceEndpointUrl,
                ClientSecret    = appConfig.ActiveDirectoryClientSecret,
                CredentialCache = credentialCache,
                HttpProvider    = httpProvider,
                ReturnUrl       = appConfig.ActiveDirectoryReturnUrl,
                ServiceResource = appConfig.ActiveDirectoryServiceResource,
                UserId          = this.UserSignInName,
            };

            return(Task.FromResult <ServiceInfo>(activeDirectoryServiceInfo));
        }
        /// <summary>
        /// Generates the <see cref="ServiceInfo"/> for the current application configuration.
        /// </summary>
        /// <param name="appConfig">The <see cref="AppConfig"/> for the current application.</param>
        /// <param name="credentialCache">The cache instance for storing user credentials.</param>
        /// <param name="httpProvider">The <see cref="IHttpProvider"/> for sending HTTP requests.</param>
        /// <param name="clientType">The <see cref="ClientType"/> to specify the business or consumer service.</param>
        /// <returns>The <see cref="ServiceInfo"/> for the current session.</returns>
        public virtual Task<ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType)
        {
            if (clientType == ClientType.Consumer)
            {
                var microsoftAccountServiceInfo = new MicrosoftAccountServiceInfo
                {
                    AppId = appConfig.MicrosoftAccountAppId,
                    ClientSecret = appConfig.MicrosoftAccountClientSecret,
                    CredentialCache = credentialCache,
                    HttpProvider = httpProvider,
                    ReturnUrl = appConfig.MicrosoftAccountReturnUrl,
                    Scopes = appConfig.MicrosoftAccountScopes,
                    UserId = this.UserSignInName,
                    WebAuthenticationUi = this.webAuthenticationUi,
                };

                microsoftAccountServiceInfo.AuthenticationProvider = this.AuthenticationProvider?? new MicrosoftAccountAuthenticationProvider(microsoftAccountServiceInfo);
                return Task.FromResult<ServiceInfo>(microsoftAccountServiceInfo);
            }

            var activeDirectoryServiceInfo = new ActiveDirectoryServiceInfo
            {
                AppId = appConfig.ActiveDirectoryAppId,
                AuthenticationProvider = this.AuthenticationProvider,
                ClientSecret = appConfig.ActiveDirectoryClientSecret,
                CredentialCache = credentialCache,
                HttpProvider = httpProvider,
                ReturnUrl = appConfig.ActiveDirectoryReturnUrl,
                UserId = this.UserSignInName,
            };
            
            return Task.FromResult<ServiceInfo>(activeDirectoryServiceInfo);
        }