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>
        /// <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 #2
0
        private static async System.Threading.Tasks.Task t1Async()
        {
            OneDriveApi OneDriveApi;

            OneDriveApi = new OneDriveForBusinessO365Api("62e3021b-5018-40db-9647-03cca79acf8d", "~29933n6rt8C.inIbUQ1egt_nzmcG__0Il");
            await OneDriveApi.AuthenticateUsingRefreshToken("0.ATQA0twCOb8RFECAwaBnGh0s9N0lKdH8ItBBnEqhkzlAwcg0ACo.AgABAAAAAAD--DLA3VO7QrddgJg7WevrAgDs_wQA9P8YSQj_jDdp-nmkDFxSv1HeDBHFK-Vbc71L_FVXBTJcn0byMQiCVnge5U3NExQNsbMnNXtJWi5q8lh_A6GIDbi65GqqYHFdRnmEZH1vs2yF5IBkNmuXFRFkXBC3OfWutUccEqlWWcdxVLT_2lw4Kjl_UbP00RNga4CUwWMQrZaLA7UxJKzd3X-qBPTs5lERpMba6jPyaKyZzY5z4Pc0iZGaC3SdyucZJfWlXus5P3CuhyIhizkq3Nu9yMSO2vhX46IYpAv95xI_yh7Ll5igifbnS94gBva2U-LgT_cmKHxuohkdkWlCvzMhMmKtQvyu3BiIAh8x4esgCgcNBlMq89kYUhVCFPLDRPahblFta3MOKctoBcUJDwsL78UhS2PXcLt2UEQEFp-kNwDw6udXQqTW8Uc_qzDVdIhaDsSKsMF2KuooXBrg8qkCUnuS5KdRihKR1mx_ZzcHpiEa6fPGqVRAIvUApQyG3e-DnIMfNLK1JnBhBpqgJy5-ePSmHn-vp5p06PNte8sw8P0lBBgyiJfjx2T4B7wqR2luAufJ7o-xIPpcYbRZ5Q07vf9rSRy8-uDRbkVEeIskU3HKT6dKba1OOzwrfPMy4BJH9UZ45WkZEbvHovPSSbRDwgzDjaLE_GbdcGVR9Sd9-EoUEAm8rfCFtMVNzga9L8m3QdmYKuAW6GduwLA4Ms3D5-jrW2pzC_D6Q-VeLgDCIGNoJLdUWUuWkNOpxOKsvvRxQuChNiZJsjrDbNLajbUbn4IajDBLGXQBu0lglRJN-un6COOEPCE6tnT-DBw3lCvLzMXR_xnEB3ks-HUUSO5jL8gITOytzsnt3fAk1JEg08-S-dCnkq7TPdjC7dWi5tl58rH-O4xK1WnVfuirSNwkopCENm7RYA8d0OqJnB3TePrjuS-cqYqTBgYrCYeQtyli4MOE-9VeAg");

            await OneDriveApi.GetAccessToken();

            var data = await OneDriveApi.GetDrive();
        }
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>
        /// <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);
            }
        }