Beispiel #1
0
        /// <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>
        /// <param name="oneDriveClientId">ClientID to get access to OneDrive</param>
        /// <param name="oneDriveClientSecret">ClientSecret to get access to OneDrive</param>
        /// <returns>Active OneDrive instance or NULL if unable to get an authenticated instance</returns>
        public static async Task<OneDriveApi> GetOneDriveApi(Configuration databaseConfig, string oneDriveClientId, string oneDriveClientSecret)
        {
            if (string.IsNullOrEmpty(databaseConfig.RefreshToken))
            {                
                var oneDriveAuthenticateForm = new OneDriveAuthenticateForm(oneDriveClientId, oneDriveClientSecret);
                var result = oneDriveAuthenticateForm.ShowDialog();

                if (result != DialogResult.OK)
                {
                    return null;
                }                

                // Save the configuration so we keep the Refresh Token
                var oneDriveApi = oneDriveAuthenticateForm.OneDriveApi;
                databaseConfig.RefreshToken = oneDriveApi.AccessToken.RefreshToken;

                Configuration.Save();
                
                return oneDriveApi;
            }

            try
            {
                return await OneDriveApi.GetOneDriveApiFromRefreshToken(oneDriveClientId, oneDriveClientSecret, databaseConfig.RefreshToken);
            }
            catch (WebException)
            {
                // Occurs if no connection can be made with the OneDrive service. It will be handled properly in the calling code.
                return null;
            }
        }
Beispiel #2
0
        /// <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);
            }
        }
Beispiel #3
0
        /// <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>
        /// <param name="oneDriveClientId">ClientID to get access to OneDrive</param>
        /// <param name="oneDriveClientSecret">ClientSecret to get access to OneDrive</param>
        /// <returns>Active OneDrive instance or NULL if unable to get an authenticated instance</returns>
        public static async Task <OneDriveApi> GetOneDriveApi(Configuration databaseConfig, string oneDriveClientId, string oneDriveClientSecret)
        {
            if (string.IsNullOrEmpty(databaseConfig.RefreshToken))
            {
                var oneDriveAuthenticateForm = new OneDriveAuthenticateForm(oneDriveClientId, oneDriveClientSecret);
                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
            {
                var oneDriveApi = new OneDriveApi(oneDriveClientId, oneDriveClientSecret);
                ApplyProxySettings(oneDriveApi);
                await oneDriveApi.AuthenticateUsingRefreshToken(databaseConfig.RefreshToken);

                return(oneDriveApi);
            }
            catch (WebException)
            {
                // Occurs if no connection can be made with the OneDrive service. It will be handled properly in the calling code.
                return(null);
            }
        }
        /// <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>
        /// <param name="oneDriveClientId">ClientID to get access to OneDrive</param>
        /// <param name="oneDriveClientSecret">ClientSecret to get access to OneDrive</param>
        /// <returns>Active OneDrive instance or NULL if unable to get an authenticated instance</returns>
        public static async Task<OneDriveApi> GetOneDriveApi(Configuration databaseConfig, string oneDriveClientId, string oneDriveClientSecret)
        {
            if (string.IsNullOrEmpty(databaseConfig.RefreshToken))
            {
                var oneDriveAuthenticateForm = new OneDriveAuthenticateForm(oneDriveClientId, oneDriveClientSecret);
                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
            {
                var oneDriveApi = new OneDriveApi(oneDriveClientId, oneDriveClientSecret);
                ApplyProxySettings(oneDriveApi);
                await oneDriveApi.AuthenticateUsingRefreshToken(databaseConfig.RefreshToken);
                return oneDriveApi;
            }
            catch (WebException)
            {
                // Occurs if no connection can be made with the OneDrive service. It will be handled properly in the calling code.
                return null;
            }
        }
Beispiel #5
0
        /// <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);
            }
        }