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; }
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); }
/// <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 void Setup() { this.credentialCache = new CredentialCache(); this.afterAccessCalled = false; this.beforeAccessCalled = false; this.beforeWriteCalled = false; }
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; }
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; }
/// <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(); }
/// <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(); }
/// <summary> /// Creates an authenticated 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="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 Task<IOneDriveClient> GetAuthenticatedMicrosoftAccountClient( string appId, string returnUrl, string[] scopes, CredentialCache credentialCache = null, IHttpProvider httpProvider = null) { return OneDriveClient.GetAuthenticatedMicrosoftAccountClient( appId, returnUrl, scopes, /* clientSecret */ null, new ServiceInfoProvider(), credentialCache, httpProvider); }
/// <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="credentialCache">The cache instance for storing user credentials.</param> /// <param name="httpProvider">The <see cref="IHttpProvider"/> for sending HTTP requests.</param> /// <param name="webAuthenticationUi">The <see cref="IWebAuthenticationUi"/> for displaying authentication UI to the user.</param> /// <returns>The <see cref="IOneDriveClient"/> for the session.</returns> public static IOneDriveClient GetMicrosoftAccountClient( string appId, string returnUrl, string[] scopes, CredentialCache credentialCache = null, IHttpProvider httpProvider = null, IWebAuthenticationUi webAuthenticationUi = null) { return OneDriveClient.GetMicrosoftAccountClient( appId, returnUrl, scopes, /* clientSecret */ null, credentialCache, httpProvider, new ServiceInfoProvider(webAuthenticationUi)); }
/// <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); }
private void Initialize() { if (Client != null) return; // Load credential cache CredentialCache credentialCache = new CredentialCache(); if (!string.IsNullOrEmpty(Settings.Default.Credentials)) credentialCache.InitializeCacheFromBlob(Convert.FromBase64String(Settings.Default.Credentials)); // Authenticate with OneDrive Client = OneDriveClient.GetMicrosoftAccountClient(applicationId, applicationReturnUrl, applicationScopes, applicationSecret, credentialCache, null, new OneDriveInfoProvider()); Task<AccountSession> oneDriveSessionTask = Client.AuthenticateAsync(); oneDriveSessionTask.Wait(); Session = oneDriveSessionTask.Result; // Save credentials if (Session == null) { Settings.Default.Credentials = null; Settings.Default.Save(); } else if (credentialCache.HasStateChanged) { Settings.Default.Credentials = Convert.ToBase64String(credentialCache.GetCacheBlob()); Settings.Default.Save(); } // Find specified root folder if (!Path.StartsWith("/") || !IsPathValid(Path)) throw new Exception("The specified path is not valid"); Task<Item> task; if (Path.Length == 1) task = Client.Drive.Root.Request().GetAsync(); else task = Client.Drive.Root.ItemWithPath(Path.Trim('/')).Request().GetAsync(); task.Wait(); root = task.Result; }
/// <summary> /// Creates an authenticated 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="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> public static async Task<IOneDriveClient> GetAuthenticatedMicrosoftAccountClient( string appId, string returnUrl, string[] scopes, IServiceInfoProvider serviceInfoProvider, CredentialCache credentialCache = null, IHttpProvider httpProvider = null) { var client = OneDriveClient.GetMicrosoftAccountClient( appId, returnUrl, scopes, /* clientSecret */ null, credentialCache, httpProvider, serviceInfoProvider); await client.AuthenticateAsync(); return client; }
/// <summary> /// Creates an authenticated OneDrive client for use against OneDrive consumer for silent (refresh token) authentication. /// </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="refreshToken">The refresh token for silent authentication.</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> public static async Task<IOneDriveClient> GetSilentlyAuthenticatedMicrosoftAccountClient( string appId, string returnUrl, string[] scopes, string clientSecret, string refreshToken, IServiceInfoProvider serviceInfoProvider, CredentialCache credentialCache = null, IHttpProvider httpProvider = null) { var clientServiceInfoProvider = serviceInfoProvider ?? new ServiceInfoProvider(); var client = OneDriveClient.GetMicrosoftAccountClient( appId, returnUrl, scopes, clientSecret, credentialCache, httpProvider, clientServiceInfoProvider) as OneDriveClient; if (client.ServiceInfo == null) { client.ServiceInfo = await clientServiceInfoProvider.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 OneDrive client for use against OneDrive consumer for silent (refresh token) authentication. /// </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="refreshToken">The refresh token for silent authentication.</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> public static Task<IOneDriveClient> GetSilentlyAuthenticatedMicrosoftAccountClient( string appId, string returnUrl, string[] scopes, string refreshToken, IServiceInfoProvider serviceInfoProvider, CredentialCache credentialCache = null, IHttpProvider httpProvider = null) { return OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient( appId, returnUrl, scopes, /* clientSecret */ null, refreshToken, serviceInfoProvider, credentialCache, httpProvider); }
public void VerifyBlobSerialization() { var accountSessions = new List<AccountSession> { new AccountSession { AccessToken = "token", ClientId = "1", ExpiresOnUtc = DateTimeOffset.Now, RefreshToken = "refresh", Scopes = new string[] { "scope1", "scope2" }, UserId = "1", }, new AccountSession { AccessToken = "token2", ClientId = "2", ExpiresOnUtc = DateTimeOffset.Now, RefreshToken = "refresh2", Scopes = new string[] { "scope" }, UserId = "2", AccountType = AccountType.MicrosoftAccount, } }; foreach (var accountSession in accountSessions) { this.credentialCache.AddToCache(accountSession); } var cacheBlob = this.credentialCache.GetCacheBlob(); var newCredentialCache = new CredentialCache(cacheBlob); Assert.AreEqual(2, newCredentialCache.cacheDictionary.Count, "Unexpected number of cache entries."); foreach (var accountSession in accountSessions) { var accountSessionKey = new CredentialCacheKey { AccountType = accountSession.AccountType, ClientId = accountSession.ClientId, UserId = accountSession.UserId, }; var sessionFromCacheDictionary = newCredentialCache.cacheDictionary[accountSessionKey]; Assert.IsNotNull(sessionFromCacheDictionary, "Unexpected account session returned."); Assert.AreEqual(accountSession.AccessToken, sessionFromCacheDictionary.AccessToken, "Unexpected access token returned."); Assert.AreEqual(accountSession.AccountType, sessionFromCacheDictionary.AccountType, "Unexpected account type returned."); Assert.AreEqual(accountSession.ClientId, sessionFromCacheDictionary.ClientId, "Unexpected client ID returned."); Assert.AreEqual(accountSession.ExpiresOnUtc, sessionFromCacheDictionary.ExpiresOnUtc, "Unexpected expiration returned."); Assert.AreEqual(accountSession.RefreshToken, sessionFromCacheDictionary.RefreshToken, "Unexpected refresh token returned."); Assert.AreEqual(accountSession.UserId, sessionFromCacheDictionary.UserId, "Unexpected access token returned."); Assert.AreEqual(accountSession.Scopes.Length, sessionFromCacheDictionary.Scopes.Length, "Unexpected scopes returned."); for (int i = 0; i < accountSession.Scopes.Length; i++) { Assert.AreEqual(accountSession.Scopes[i], sessionFromCacheDictionary.Scopes[i], "Unexpected scope returned."); } } }
public void Setup() { this.credentialCache = new CredentialCache(); }
/// <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> /// Gets the client. /// </summary> /// <returns></returns> private static async Task<IOneDriveClient> GetClient() { CredentialCache credentialCache = new CredentialCache(); string credentials = OneDriveCloudStorageServiceSettings.Default.Credentials; if (!String.IsNullOrWhiteSpace(credentials)) credentialCache.InitializeCacheFromBlob(Encoding.Default.GetBytes(credentials)); IServiceInfoProvider serviceInfoProvider = new ServiceInfoProvider(new FormsWebAuthenticationUi()) { UserSignInName = OneDriveCloudStorageServiceSettings.Default.UserId }; return await OneDriveClient.GetAuthenticatedMicrosoftAccountClient( Util.Decrypt(OneDriveCloudStorageServiceSettings.Default.AppKey, CultureConstants.InvariantCulture.NativeName), RedirectUri, s_scopes, serviceInfoProvider, credentialCache).ConfigureAwait(false); }