浏览代码

removed gDrive.managedToken

Former-commit-id: 5be37f23a949423f128a6f733003d91a7e1eff16
Gildas 6 年之前
父节点
当前提交
bf3ecc4f28
共有 2 个文件被更改,包括 32 次插入34 次删除
  1. 3 3
      extension/core/bg/downloads.js
  2. 29 31
      extension/lib/gdrive/gdrive.js

+ 3 - 3
extension/core/bg/downloads.js

@@ -42,7 +42,6 @@ singlefile.extension.core.bg.downloads = (() => {
 	const manifest = browser.runtime.getManifest();
 	const requestPermissionIdentity = manifest.optional_permissions && manifest.optional_permissions.includes("identity");
 	const gDrive = new GDrive(CLIENT_ID, SCOPES);
-	gDrive.launchWebAuthFlow = async options => singlefile.extension.core.bg.tabs.launchWebAuthFlow(options);
 	return {
 		onMessage,
 		download,
@@ -133,11 +132,12 @@ singlefile.extension.core.bg.downloads = (() => {
 			auto: true,
 			forceWebAuthFlow: uploadOptions.forceWebAuthFlow,
 			requestPermissionIdentity,
-			extractAuthCode: () => singlefile.extension.core.bg.tabs.extractAuthCode(gDrive.getAuthURL(options)),
+			launchWebAuthFlow: options => singlefile.extension.core.bg.tabs.launchWebAuthFlow(options),
+			extractAuthCode: authURL => singlefile.extension.core.bg.tabs.extractAuthCode(authURL),
 			promptAuthCode: () => singlefile.extension.core.bg.tabs.promptValue("Please enter the access code for Google Drive")
 		};
 		gDrive.setAuthInfo(authInfo, options);
-		if (!authInfo || force || gDrive.managedToken(options)) {
+		if (!authInfo || force) {
 			authInfo = await gDrive.auth(options);
 			if (authInfo) {
 				await singlefile.extension.core.bg.config.setAuthInfo(authInfo);

+ 29 - 31
extension/lib/gdrive/gdrive.js

@@ -43,31 +43,24 @@ this.GDrive = this.GDrive || (() => {
 			setInterval(() => this.folderIds.clear(), 60 * 1000);
 		}
 		async auth(options = { interactive: true, auto: true }) {
-			if (this.managedToken(options)) {
-				if (options.requestPermissionIdentity && requestPermissionIdentityNeeded) {
-					try {
-						await browser.permissions.request({ permissions: ["identity"] });
-						requestPermissionIdentityNeeded = false;
-					}
-					catch (error) {
-						// ignored;
-					}
+			if (options.requestPermissionIdentity && requestPermissionIdentityNeeded) {
+				try {
+					await browser.permissions.request({ permissions: ["identity"] });
+					requestPermissionIdentityNeeded = false;
 				}
-				const token = await browser.identity.getAuthToken({ interactive: options.interactive });
-				if (token) {
-					this.accessToken = token;
-					return { accessToken: this.accessToken };
+				catch (error) {
+					// ignored;
 				}
+			}
+			if (nativeAuth(options)) {
+				this.accessToken = await browser.identity.getAuthToken({ interactive: options.interactive });
 			} else {
-				this.getAuthURL(options);
+				getAuthURL(this, options);
 				return options.code ? authFromCode(this, options) : initAuth(this, options);
 			}
 		}
-		managedToken(options) {
-			return Boolean(browser.identity && browser.identity.getAuthToken) && !options.forceWebAuthFlow;
-		}
 		setAuthInfo(authInfo, options) {
-			if (!this.managedToken(options)) {
+			if (!nativeAuth(options)) {
 				if (authInfo) {
 					this.accessToken = authInfo.accessToken;
 					this.refreshToken = authInfo.refreshToken;
@@ -79,16 +72,6 @@ this.GDrive = this.GDrive || (() => {
 				}
 			}
 		}
-		getAuthURL(options = {}) {
-			this.redirectURI = encodeURIComponent("urn:ietf:wg:oauth:2.0:oob" + (options.auto ? ":auto" : ""));
-			this.authURL = AUTH_URL +
-				"?client_id=" + this.clientId +
-				"&response_type=code" +
-				"&access_type=offline" +
-				"&redirect_uri=" + this.redirectURI +
-				"&scope=" + this.scopes.join(" ");
-			return this.authURL;
-		}
 		async refreshAuthToken() {
 			if (this.clientId && this.refreshToken) {
 				const httpResponse = await fetch(TOKEN_URL, {
@@ -216,7 +199,7 @@ this.GDrive = this.GDrive || (() => {
 	async function initAuth(gdrive, options) {
 		let code, cancelled;
 		if (options.extractAuthCode) {
-			options.extractAuthCode()
+			options.extractAuthCode(getAuthURL(gdrive, options))
 				.then(authCode => code = authCode)
 				.catch(() => { cancelled = true; });
 		}
@@ -226,8 +209,8 @@ this.GDrive = this.GDrive || (() => {
 					interactive: options.interactive,
 					url: gdrive.authURL
 				});
-			} else if (gdrive.launchWebAuthFlow) {
-				return await gdrive.launchWebAuthFlow({ url: gdrive.authURL });
+			} else if (options.launchWebAuthFlow) {
+				return await options.launchWebAuthFlow({ url: gdrive.authURL });
 			} else {
 				throw new Error("auth_not_supported");
 			}
@@ -249,6 +232,21 @@ this.GDrive = this.GDrive || (() => {
 		}
 	}
 
+	function getAuthURL(gdrive, options = {}) {
+		gdrive.redirectURI = encodeURIComponent("urn:ietf:wg:oauth:2.0:oob" + (options.auto ? ":auto" : ""));
+		gdrive.authURL = AUTH_URL +
+			"?client_id=" + gdrive.clientId +
+			"&response_type=code" +
+			"&access_type=offline" +
+			"&redirect_uri=" + gdrive.redirectURI +
+			"&scope=" + gdrive.scopes.join(" ");
+		return gdrive.authURL;
+	}
+
+	function nativeAuth(options) {
+		return Boolean(browser.identity && browser.identity.getAuthToken) && !options.forceWebAuthFlow;
+	}
+
 	async function getParentFolderId(gdrive, filename, retry = true) {
 		const fileParts = filename.split("/");
 		fileParts.pop();