ソースを参照

refactored code extraction

Former-commit-id: df9253efaf815c73d0a97e3e4c711f05108372a1
Gildas 6 年 前
コミット
a6769b6e76
3 ファイル変更23 行追加29 行削除
  1. 5 25
      extension/core/bg/downloads.js
  2. 1 1
      extension/core/bg/tabs.js
  3. 17 3
      extension/lib/gdrive/gdrive.js

+ 5 - 25
extension/core/bg/downloads.js

@@ -127,38 +127,18 @@ singlefile.extension.core.bg.downloads = (() => {
 	}
 	}
 
 
 	async function getAuthInfo(uploadOptions, force) {
 	async function getAuthInfo(uploadOptions, force) {
-		let code, cancelled, authInfo = await singlefile.extension.core.bg.config.getAuthInfo();
+		let authInfo = await singlefile.extension.core.bg.config.getAuthInfo();
 		const options = {
 		const options = {
 			interactive: true,
 			interactive: true,
 			auto: true,
 			auto: true,
 			forceWebAuthFlow: uploadOptions.forceWebAuthFlow,
 			forceWebAuthFlow: uploadOptions.forceWebAuthFlow,
-			requestPermissionIdentity
+			requestPermissionIdentity,
+			extractAuthCode: () => singlefile.extension.core.bg.tabs.extractAuthCode(gDrive.getAuthURL(options)),
+			promptAuthCode: () => singlefile.extension.core.bg.tabs.promptValue("Please enter the access code for Google Drive")
 		};
 		};
 		gDrive.setAuthInfo(authInfo, options);
 		gDrive.setAuthInfo(authInfo, options);
 		if (!authInfo || force || gDrive.managedToken(options)) {
 		if (!authInfo || force || gDrive.managedToken(options)) {
-			try {
-				if (!gDrive.managedToken(options)) {
-					singlefile.extension.core.bg.tabs.getAuthCode(gDrive.getAuthURL(options))
-						.then(authCode => code = authCode)
-						.catch(() => { cancelled = true; });
-				}
-				authInfo = await gDrive.auth(options);
-			} catch (error) {
-				if (!cancelled && error.message == "code_required" && !code) {
-					if (options.auto) {
-						options.auto = false;
-						return getAuthInfo(uploadOptions, force);
-					} else {
-						code = await singlefile.extension.core.bg.tabs.promptValue("Please enter the access code for Google Drive");
-					}
-				}
-				if (code) {
-					options.code = code;
-					authInfo = await gDrive.auth(options);
-				} else {
-					throw error;
-				}
-			}
+			authInfo = await gDrive.auth(options);
 			if (authInfo) {
 			if (authInfo) {
 				await singlefile.extension.core.bg.config.setAuthInfo(authInfo);
 				await singlefile.extension.core.bg.config.setAuthInfo(authInfo);
 			} else {
 			} else {

+ 1 - 1
extension/core/bg/tabs.js

@@ -75,7 +75,7 @@ singlefile.extension.core.bg.tabs = (() => {
 				}
 				}
 			});
 			});
 		},
 		},
-		getAuthCode: authURL => {
+		extractAuthCode: authURL => {
 			return new Promise((resolve, reject) => {
 			return new Promise((resolve, reject) => {
 				let authTabId;
 				let authTabId;
 				browser.tabs.onUpdated.addListener(onTabUpdated);
 				browser.tabs.onUpdated.addListener(onTabUpdated);

+ 17 - 3
extension/lib/gdrive/gdrive.js

@@ -214,8 +214,14 @@ this.GDrive = this.GDrive || (() => {
 	}
 	}
 
 
 	async function initAuth(gdrive, options) {
 	async function initAuth(gdrive, options) {
+		let code, cancelled;
+		if (options.extractAuthCode) {
+			options.extractAuthCode()
+				.then(authCode => code = authCode)
+				.catch(() => { cancelled = true; });
+		}
 		try {
 		try {
-			if (gdrive.managedToken(options)) {
+			if (browser.identity && browser.identity.launchWebAuthFlow && !options.forceWebAuthFlow) {
 				return await browser.identity.launchWebAuthFlow({
 				return await browser.identity.launchWebAuthFlow({
 					interactive: options.interactive,
 					interactive: options.interactive,
 					url: gdrive.authURL
 					url: gdrive.authURL
@@ -227,8 +233,16 @@ this.GDrive = this.GDrive || (() => {
 			}
 			}
 		}
 		}
 		catch (error) {
 		catch (error) {
-			if (error.message && error.message.includes("access")) {
-				throw new Error("code_required");
+			if (error.message && (error.message == "code_required" || error.message.includes("access"))) {
+				if (!options.auto && !cancelled && !code && options.promptAuthCode) {
+					code = await options.promptAuthCode();
+				}
+				if (code) {
+					options.code = code;
+					return await authFromCode(gdrive, options);
+				} else {
+					throw new Error("code_required");
+				}
 			} else {
 			} else {
 				throw error;
 				throw error;
 			}
 			}