//private readonly Dictionary<string, OneDriveApi> _cache = new Dictionary<string, OneDriveApi>(); public static async Task <OneDriveConsumerApi> GetApi(string refreshToken) { var api = new OneDriveConsumerApi(OneDriveClientId, OneDriveClientSecret); await api.AuthenticateUsingRefreshToken(refreshToken); return(api); }
//private readonly Dictionary<string, OneDriveApi> _cache = new Dictionary<string, OneDriveApi>(); public static async Task<OneDriveConsumerApi> GetApi(string refreshToken) { var api = new OneDriveConsumerApi(OneDriveClientId, OneDriveClientSecret); await api.AuthenticateUsingRefreshToken(refreshToken); return api; }
/// <summary> /// Returns an active OneDriveApi instance. If a RefreshToken is available, it will set up an instance based on that, otherwise it will show the login dialog /// </summary> /// <param name="databaseConfig">Configuration of the KeePass database</param> /// <returns>Active OneDrive instance or NULL if unable to get an authenticated instance</returns> public static async Task <OneDriveApi> GetOneDriveApi(Configuration databaseConfig) { OneDriveApi cloudStorage; switch (databaseConfig.CloudStorageType.GetValueOrDefault(CloudStorageType.OneDriveConsumer)) { case CloudStorageType.OneDriveConsumer: cloudStorage = new OneDriveConsumerApi(KoenZomersKeePassOneDriveSyncExt.OneDriveConsumerClientId, KoenZomersKeePassOneDriveSyncExt.OneDriveConsumerClientSecret); break; case CloudStorageType.OneDriveForBusiness: cloudStorage = new OneDriveForBusinessO365Api(KoenZomersKeePassOneDriveSyncExt.OneDriveForBusinessClientId, KoenZomersKeePassOneDriveSyncExt.OneDriveForBusinessClientSecret); break; default: throw new ArgumentOutOfRangeException(string.Format("Cloud storage type {0} is not supported", databaseConfig.CloudStorageType)); } if (string.IsNullOrEmpty(databaseConfig.RefreshToken)) { var oneDriveAuthenticateForm = new OneDriveAuthenticateForm(cloudStorage); var result = oneDriveAuthenticateForm.ShowDialog(); if (result != System.Windows.Forms.DialogResult.OK) { return(null); } // Check if we already know where to store the Refresh Token for this database if (!databaseConfig.RefreshTokenStorage.HasValue) { // We don't know yet where the Refresh Token for this database should be stored, ask the user to choose var oneDriveRefreshTokenStorageForm = new OneDriveRefreshTokenStorageDialog(databaseConfig); oneDriveRefreshTokenStorageForm.ShowDialog(); } // Save the configuration so we keep the Refresh Token var oneDriveApi = oneDriveAuthenticateForm.OneDriveApi; databaseConfig.RefreshToken = oneDriveApi.AccessToken.RefreshToken; Configuration.Save(); return(oneDriveApi); } try { ApplyProxySettings(cloudStorage); await cloudStorage.AuthenticateUsingRefreshToken(databaseConfig.RefreshToken); return(cloudStorage); } catch (WebException) { // Occurs if no connection can be made with the OneDrive service. It will be handled properly in the calling code. return(null); } }
public async Task <OneDriveConsumerApi> GetApi() { if (_api == null) { _api = await OneDriveHelper.GetApi(_account.Secret); } return(_api); }
public OneDriveAuthenticationForm(OneDriveConsumerApi api) { InitializeComponent(); this.Icon = PluginResources.Icon_OneDrive_16x16; m_api = api; SignOut(); }
public async Task<AccountConfiguration> CreateAccount() { _api = new OneDriveConsumerApi(OneDriveHelper.OneDriveClientId, OneDriveHelper.OneDriveClientSecret); var isOk = OAuth2Flow.TryAuthenticate(this); if (!isOk) return null; var drive = await _api.GetDrive(); var account = new AccountConfiguration { Type = StorageType.OneDrive, Name = drive.Owner.User.DisplayName, Id = drive.Id, Secret = _api.AccessToken.RefreshToken }; return account; }
public async Task <AccountConfiguration> CreateAccount() { _api = new OneDriveConsumerApi(OneDriveHelper.OneDriveClientId, OneDriveHelper.OneDriveClientSecret); var isOk = OAuth2Flow.TryAuthenticate(this); if (!isOk) { return(null); } var drive = await _api.GetDrive(); var account = new AccountConfiguration { Type = StorageType.OneDrive, Name = drive.Owner.User.DisplayName, Id = drive.Id, Secret = _api.AccessToken.RefreshToken }; return(account); }
public async Task<OneDriveConsumerApi> GetApi() { if (_api == null) _api = await OneDriveHelper.GetApi(_account.Secret); return _api; }
/// <summary> /// Returns an active OneDriveApi instance. If a RefreshToken is available, it will set up an instance based on that, otherwise it will show the login dialog /// </summary> /// <param name="databaseConfig">Configuration of the KeePass database</param> /// <returns>Active OneDrive instance or NULL if unable to get an authenticated instance</returns> public static async Task <OneDriveApi> GetOneDriveApi(Configuration databaseConfig) { OneDriveApi cloudStorage; switch (databaseConfig.CloudStorageType.GetValueOrDefault(CloudStorageType.OneDriveConsumer)) { case CloudStorageType.OneDriveConsumer: cloudStorage = new OneDriveConsumerApi(KoenZomersKeePassOneDriveSyncExt.OneDriveConsumerClientId, KoenZomersKeePassOneDriveSyncExt.OneDriveConsumerClientSecret); break; case CloudStorageType.OneDriveForBusiness: cloudStorage = new OneDriveForBusinessO365Api(KoenZomersKeePassOneDriveSyncExt.OneDriveForBusinessClientId, KoenZomersKeePassOneDriveSyncExt.OneDriveForBusinessClientSecret); break; case CloudStorageType.MicrosoftGraph: cloudStorage = new OneDriveGraphApi(KoenZomersKeePassOneDriveSyncExt.GraphApiApplicationId); break; default: throw new ArgumentOutOfRangeException(string.Format("Cloud storage type {0} is not supported", databaseConfig.CloudStorageType)); } if (string.IsNullOrEmpty(databaseConfig.RefreshToken)) { var oneDriveAuthenticateForm = new OneDriveAuthenticateForm(cloudStorage); var result = oneDriveAuthenticateForm.ShowDialog(); if (result != System.Windows.Forms.DialogResult.OK) { return(null); } // Check if we already know where to store the Refresh Token for this database if (!databaseConfig.RefreshTokenStorage.HasValue) { // We don't know yet where the Refresh Token for this database should be stored, ask the user to choose var oneDriveRefreshTokenStorageForm = new OneDriveRefreshTokenStorageDialog(databaseConfig); oneDriveRefreshTokenStorageForm.ShowDialog(); } // Save the configuration so we keep the Refresh Token var oneDriveApi = oneDriveAuthenticateForm.OneDriveApi; databaseConfig.RefreshToken = oneDriveApi.AccessToken.RefreshToken; Configuration.Save(); return(oneDriveApi); } try { ApplyProxySettings(cloudStorage); await cloudStorage.AuthenticateUsingRefreshToken(databaseConfig.RefreshToken); return(cloudStorage); } catch (KoenZomers.OneDrive.Api.Exceptions.TokenRetrievalFailedException e) { // Something went wrong with retrieving the access token throw; } catch (Exception e) { // If specifically the HttpRequestException occurred, it likely contains more information on what went wrong, so pass it up to the callstack if (e.GetType().ToString() == "System.Net.Http.HttpRequestException") { throw; } // Occurs if no connection can be made with the OneDrive service. It will be handled properly in the calling code. return(null); } }