From b573b2859c76c5bc1ac87979591df9075cc6ba5f Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Sat, 5 Mar 2022 22:32:58 +0800 Subject: [PATCH] add fallback for dropbox --- src/remoteForDropbox.ts | 11 +++- src/settings.ts | 121 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 119 insertions(+), 13 deletions(-) diff --git a/src/remoteForDropbox.ts b/src/remoteForDropbox.ts index eb290bf..33154a2 100644 --- a/src/remoteForDropbox.ts +++ b/src/remoteForDropbox.ts @@ -156,13 +156,20 @@ const fixLastModifiedTimeInplace = (allFilesFolders: RemoteItem[]) => { // see https://dropbox.tech/developers/pkce--what-and-why- //////////////////////////////////////////////////////////////////////////////// -export const getAuthUrlAndVerifier = async (appKey: string) => { +export const getAuthUrlAndVerifier = async ( + appKey: string, + needManualPatse: boolean = false +) => { const auth = new DropboxAuth({ clientId: appKey, }); + + const callback = needManualPatse + ? undefined + : `obsidian://${COMMAND_CALLBACK_DROPBOX}`; const authUrl = ( await auth.getAuthenticationUrl( - `obsidian://${COMMAND_CALLBACK_DROPBOX}`, + callback, undefined, "code", "offline", diff --git a/src/settings.ts b/src/settings.ts index 14553b0..23a0fa0 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,4 +1,11 @@ -import { App, Modal, Notice, PluginSettingTab, Setting } from "obsidian"; +import { + App, + Modal, + Notice, + PluginSettingTab, + Setting, + Platform, +} from "obsidian"; import type { SUPPORTED_SERVICES_TYPE, WebdavAuthType } from "./baseTypes"; import { exportVaultSyncPlansToFiles } from "./debugMode"; import { exportQrCodeUri } from "./importExport"; @@ -110,17 +117,45 @@ class DropboxAuthModal extends Modal { async onOpen() { let { contentEl } = this; - const { authUrl, verifier } = await getAuthUrlAndVerifierDropbox( - this.plugin.settings.dropbox.clientID - ); - this.plugin.oauth2Info.verifier = verifier; + let needManualPatse = false; + const userAgent = window.navigator.userAgent.toLocaleLowerCase() || ""; + // some users report that, + // the Linux would open another instance Obsidian if jumping back, + // so fallback to manual paste on Linux + if ( + Platform.isDesktopApp && + !Platform.isMacOS && + (/linux/.test(userAgent) || + /ubuntu/.test(userAgent) || + /debian/.test(userAgent) || + /fedora/.test(userAgent) || + /centos/.test(userAgent)) + ) { + needManualPatse = true; + } - contentEl.createEl("p", { - text: "Visit the address in a browser, and follow the steps.", - }); - contentEl.createEl("p", { - text: "Finally you should be redirected to Obsidian.", - }); + const { authUrl, verifier } = await getAuthUrlAndVerifierDropbox( + this.plugin.settings.dropbox.clientID, + needManualPatse + ); + + if (needManualPatse) { + contentEl.createEl("p", { + text: "Step 1: Visit the address in a browser, and follow the steps.", + }); + contentEl.createEl("p", { + text: 'Step 2: In the end of the web flow, you obtain a long code. Paste it here then click "Submit".', + }); + } else { + this.plugin.oauth2Info.verifier = verifier; + + contentEl.createEl("p", { + text: "Visit the address in a browser, and follow the steps.", + }); + contentEl.createEl("p", { + text: "Finally you should be redirected to Obsidian.", + }); + } const div2 = contentEl.createDiv(); div2.createEl( @@ -140,6 +175,70 @@ class DropboxAuthModal extends Modal { href: authUrl, text: authUrl, }); + + if (needManualPatse) { + let authCode = ""; + new Setting(contentEl) + .setName("Auth Code from web page") + .setDesc('You need to click "Confirm".') + .addText((text) => + text + .setPlaceholder("") + .setValue("") + .onChange((val) => { + authCode = val.trim(); + }) + ) + .addButton(async (button) => { + button.setButtonText("Confirm"); + button.onClick(async () => { + new Notice("Trying to connect to Dropbox"); + try { + const authRes = await sendAuthReqDropbox( + this.plugin.settings.dropbox.clientID, + verifier, + authCode + ); + const self = this; + setConfigBySuccessfullAuthInplace( + this.plugin.settings.dropbox, + authRes, + () => self.plugin.saveSettings() + ); + const client = new RemoteClient( + "dropbox", + undefined, + undefined, + this.plugin.settings.dropbox, + undefined, + this.app.vault.getName(), + () => self.plugin.saveSettings() + ); + const username = await client.getUser(); + this.plugin.settings.dropbox.username = username; + await this.plugin.saveSettings(); + new Notice( + `Good! We've connected to Dropbox as user ${username}!` + ); + this.authDiv.toggleClass( + "dropbox-auth-button-hide", + this.plugin.settings.dropbox.username !== "" + ); + this.revokeAuthDiv.toggleClass( + "dropbox-revoke-auth-button-hide", + this.plugin.settings.dropbox.username === "" + ); + this.revokeAuthSetting.setDesc( + `You've connected as user ${this.plugin.settings.dropbox.username}. If you want to disconnect, click this button.` + ); + this.close(); + } catch (err) { + console.error(err); + new Notice("Something goes wrong while connecting to Dropbox."); + } + }); + }); + } } onClose() {