/// <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,
                    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,
            };
            
            return Task.FromResult<ServiceInfo>(activeDirectoryServiceInfo);
        }
        public async override Task<ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType = ClientType.Business)
        {
            if (clientType == ClientType.Consumer)
            {
                throw new OneDriveException(
                    new Error
                    {
                        Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "AdalServiceInfoProvider only supports Active Directory authentication."
                    });
            }

            var serviceInfo = await base.GetServiceInfo(appConfig, credentialCache, httpProvider, clientType);

            serviceInfo.BaseUrl = appConfig.ActiveDirectoryServiceEndpointUrl;
            serviceInfo.ServiceResource = appConfig.ActiveDirectoryServiceResource;

            if (serviceInfo.AuthenticationProvider == null)
            {
                serviceInfo.AuthenticationProvider = new AdalAuthenticationProvider(serviceInfo);
            }

            return serviceInfo;
        }
        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 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 async override Task<ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType = ClientType.Business)
        {
            if (clientType == ClientType.Consumer)
            {
                throw new OneDriveException(
                    new Error
                    {
                        Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "AdalServiceInfoProvider only supports Active Directory authentication."
                    });
            }

            var serviceInfo = await base.GetServiceInfo(appConfig, null, httpProvider, clientType);

            serviceInfo.ServiceResource = appConfig.ActiveDirectoryServiceResource;

            if (string.IsNullOrEmpty(serviceInfo.BaseUrl) && !string.IsNullOrEmpty(serviceInfo.ServiceResource))
            {
                serviceInfo.BaseUrl = string.Format(
                    Constants.Authentication.OneDriveBusinessBaseUrlFormatString,
                    serviceInfo.ServiceResource.TrimEnd('/'),
                    "v2.0");
            }

            if (serviceInfo.AuthenticationProvider == null)
            {
                serviceInfo.AuthenticationProvider = new AdalAuthenticationProvider(serviceInfo);
            }

            return serviceInfo;
        }
예제 #6
0
        public override async Task<ServiceInfo> GetServiceInfo(AppConfig appConfig, CredentialCache credentialCache, IHttpProvider httpProvider)
        {
            ServiceInfo serviceInfo = await base.GetServiceInfo(appConfig, credentialCache, httpProvider);

            if (credentialCache.cacheDictionary.Count > 0)
            {
                var credentialPair = credentialCache.cacheDictionary.First();
                serviceInfo.UserId = credentialPair.Key.UserId;
            }

            return serviceInfo;
        }
        public override async Task<ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType) {
            var serviceInfo = await base.GetServiceInfo(appConfig, credentialCache, httpProvider, clientType);

            var authProvider = new IosAuthenticationProvider(serviceInfo);
            serviceInfo.AuthenticationProvider = authProvider;

            return serviceInfo;
        }
예제 #8
0
        /// <summary>
        /// Constructs a new <see cref="BaseClient"/>.
        /// </summary>
        public BaseClient(
            AppConfig appConfig,
            CredentialCache credentialCache = null,
            IHttpProvider httpProvider = null,
            IServiceInfoProvider serviceInfoProvider = null)
        {

            this.appConfig = appConfig;
            this.credentialCache = credentialCache ?? new CredentialCache();
            this.HttpProvider = httpProvider ?? new HttpProvider(new Serializer());
            this.serviceInfoProvider = serviceInfoProvider ?? new ServiceInfoProvider();
        }
예제 #9
0
 /// <summary>
 /// Constructs a new <see cref="BaseClient"/>.
 /// </summary>
 public BaseClient(
     AppConfig appConfig,
     CredentialCache credentialCache = null,
     IHttpProvider httpProvider = null,
     IServiceInfoProvider serviceInfoProvider = null,
     ClientType clientType = ClientType.Consumer)
 {
     this.appConfig = appConfig;
     this.ClientType = clientType;
     this.credentialCache = credentialCache;
     this.HttpProvider = httpProvider ?? new HttpProvider(new Serializer());
     this.serviceInfoProvider = serviceInfoProvider ?? new ServiceInfoProvider();
 }
 public void Setup()
 {
     this.appConfig = new AppConfig
     {
         ActiveDirectoryAppId = "12345",
         ActiveDirectoryReturnUrl = "https://localhost/return",
         ActiveDirectoryServiceResource = "https://resource/",
     };
     
     this.credentialCache = new MockAdalCredentialCache();
     this.httpProvider = new MockHttpProvider(null);
     this.serviceInfoProvider = new AdalAuthenticationByCodeServiceInfoProvider(authenticationCode) { UserSignInName = "12345" };
 }
        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 MockWebUi();
            this.serviceInfoProvider = new ServiceInfoProvider(this.webAuthenticationUi.Object);
        }
        /// <summary>
        /// Creates a OneDrive client for use against OneDrive consumer.
        /// </summary>
        /// <param name="appId">The application ID for Microsoft Account authentication.</param>
        /// <param name="returnUrl">The application return URL for Microsoft Account authentication.</param>
        /// <param name="scopes">The requested scopes for Microsoft Account authentication.</param>
        /// <param name="clientSecret">The client secret for Microsoft Account authentication.</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="serviceInfoProvider">The <see cref="IServiceInfoProvider"/> for initializing the <see cref="IServiceInfo"/> for the session.</param>
        /// <returns>The <see cref="IOneDriveClient"/> for the session.</returns>
        public static IOneDriveClient GetMicrosoftAccountClient(
            string appId,
            string returnUrl,
            string[] scopes,
            string clientSecret,
            CredentialCache credentialCache = null,
            IHttpProvider httpProvider = null,
            IServiceInfoProvider serviceInfoProvider = null)
        {
            var appConfig = new AppConfig
            {
                MicrosoftAccountAppId = appId,
                MicrosoftAccountReturnUrl = returnUrl,
                MicrosoftAccountScopes = scopes,
            };

            return new OneDriveClient(appConfig, credentialCache, httpProvider, serviceInfoProvider);
        }
        /// <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>
        /// Creates an authenticated client using the ADAL app-only authentication flow.
        /// </summary>
        /// <param name="appConfig">
        ///     The <see cref="BusinessAppConfig"/> for the application configuration.
        /// </param>
        /// <param name="serviceInfoProvider">The <see cref="IServiceInfoProvider"/> for initializing the <see cref="IServiceInfo"/> for the session.</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>
        internal static IOneDriveClient GetClientInternal(
            AppConfig appConfig,
            IServiceInfoProvider serviceInfoProvider,
            AdalCredentialCache credentialCache,
            IHttpProvider httpProvider)
        {
            if (string.IsNullOrEmpty(appConfig.ActiveDirectoryAppId))
            {
                throw new OneDriveException(
                    new Error
                    {
                        Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "ActiveDirectoryAppId is required for authentication."
                    });
            }

            return new OneDriveClient(
                appConfig,
                credentialCache ?? new AdalCredentialCache(),
                httpProvider ?? new HttpProvider(),
                serviceInfoProvider ?? new AdalServiceInfoProvider(),
                ClientType.Business);
        }
        /// <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 unauthenticated client using ADAL for authentication.
        /// </summary>
        /// <param name="appConfig">
        ///     The <see cref="AppConfig"/> for the application configuration.
        ///     Authentication requires the following to be initialized:
        ///         - ActiveDirectoryAppId
        ///         - ActiveDirectoryReturnUrl
        ///     To bypass using the Discovery Service for service endpoint lookup ActiveDirectoryServiceResource must also be set.
        /// </param>
        /// <param name="userId">The ID of the user to authenticate.</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> GetAuthenticatedClientAsync(
            AppConfig appConfig,
            string userId = null,
            AdalCredentialCache credentialCache = null,
            IHttpProvider httpProvider = null)
        {
            var client = BusinessClientExtensions.GetClient(
                appConfig,
                userId,
                credentialCache,
                httpProvider);

            await client.AuthenticateAsync();

            return client;
        }
        /// <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 async override Task<ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType = ClientType.Business)
        {
            if (clientType == ClientType.Consumer)
            {
                throw new OneDriveException(
                    new Error
                    {
                        Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "AdalAppOnlyServiceInfoProvider only supports Active Directory authentication."
                    });
            }

            var adalAppConfig = appConfig as BusinessAppConfig;

            if (adalAppConfig == null)
            {
                throw new OneDriveException(
                    new Error
                    {
                        Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "AdalAppOnlyServiceInfoProvider requires an AdalAppConfig."
                    });
            }

            if (string.IsNullOrEmpty(appConfig.ActiveDirectoryServiceResource))
            {
                throw new OneDriveException(
                    new Error
                    {
                        Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "Service resource ID is required for app-only authentication when service endpoint URL is not initialized.",
                    });
            }

            var serviceInfo = await base.GetServiceInfo(adalAppConfig, credentialCache, httpProvider, clientType);

            var adalServiceInfo = new AdalServiceInfo();
            adalServiceInfo.CopyFrom(serviceInfo);

            adalServiceInfo.ServiceResource = adalAppConfig.ActiveDirectoryServiceResource;

            if (string.IsNullOrEmpty(adalServiceInfo.BaseUrl))
            {
                adalServiceInfo.BaseUrl = string.Format(
                    Constants.Authentication.OneDriveBusinessBaseUrlFormatString,
                    adalAppConfig.ActiveDirectoryServiceResource.TrimEnd('/'),
                    serviceInfo.OneDriveServiceEndpointVersion);
            }

            adalServiceInfo.ClientCertificate = adalAppConfig.ActiveDirectoryClientCertificate;

            if (adalServiceInfo.AuthenticationProvider == null)
            {
                adalServiceInfo.AuthenticationProvider = new AdalAppOnlyAuthenticationProvider(adalServiceInfo);
            }

            return adalServiceInfo;
        }
        /// <summary>
        /// Creates an unauthenticated client using ADAL for authentication.
        /// </summary>
        /// <param name="appConfig">
        ///     The <see cref="AppConfig"/> for the application configuration.
        ///     Authentication requires the following to be initialized:
        ///         - ActiveDirectoryAppId
        ///         - ActiveDirectoryReturnUrl
        ///     To bypass using the Discovery Service for service endpoint lookup ActiveDirectoryServiceResource must also be set.
        /// </param>
        /// <param name="userId">The ID of the user to authenticate.</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 IOneDriveClient GetClient(
            AppConfig appConfig,
            string userId = null,
            AdalCredentialCache credentialCache = null,
            IHttpProvider httpProvider = null)
        {
            if (string.IsNullOrEmpty(appConfig.ActiveDirectoryReturnUrl))
            {
                throw new OneDriveException(
                    new Error
                    {
                        Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "ActiveDirectoryReturnUrl is required for authenticating a business client.",
                    });
            }

            appConfig.ActiveDirectoryAuthenticationServiceUrl = Constants.Authentication.ActiveDirectoryAuthenticationServiceUrl;

            return BusinessClientExtensions.GetClientInternal(
                appConfig,
                new AdalServiceInfoProvider() { UserSignInName = userId },
                credentialCache,
                httpProvider);
        }