diff --git a/esbuild.config.mjs b/esbuild.config.mjs index 7707616..c058c22 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -24,6 +24,7 @@ esbuild "obsidian", "electron", "fs", + "crypto", // ...builtins ], format: "cjs", diff --git a/package.json b/package.json index 095defb..3f61d9c 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,7 @@ "browser": { "path": "path-browserify", "process": "process/browser", - "stream": "stream-browserify", - "crypto": "crypto-browserify", - "util": "util/", - "assert": "assert/" + "stream": "stream-browserify" }, "source": "main.ts", "keywords": [], diff --git a/src/main.ts b/src/main.ts index 6c45dc0..b9989a3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -32,8 +32,7 @@ import { WebdavConfig, DEFAULT_WEBDAV_CONFIG, WebdavAuthType } from "./webdav"; import { DropboxConfig, DEFAULT_DROPBOX_CONFIG, - getCodeVerifierAndChallenge, - getAuthUrl, + getAuthUrlAndVerifier, sendAuthReq, setConfigBySuccessfullAuthInplace, } from "./remoteForDropbox"; @@ -296,13 +295,11 @@ export class DropboxAuthModal extends Modal { this.revokeAuthSetting = revokeAuthSetting; } - onOpen() { + async onOpen() { let { contentEl } = this; - const k = getCodeVerifierAndChallenge(); - const authUrl = getAuthUrl( - this.plugin.settings.dropbox.clientID, - k.challenge + const { authUrl, verifier } = await getAuthUrlAndVerifier( + this.plugin.settings.dropbox.clientID ); contentEl.createEl("p", { @@ -336,7 +333,7 @@ export class DropboxAuthModal extends Modal { try { const authRes = await sendAuthReq( this.plugin.settings.dropbox.clientID, - k.verifier, + verifier, authCode ); const self = this; diff --git a/src/remoteForDropbox.ts b/src/remoteForDropbox.ts index 62b1e5a..c9d453c 100644 --- a/src/remoteForDropbox.ts +++ b/src/remoteForDropbox.ts @@ -1,9 +1,7 @@ import * as path from "path"; import { FileStats, Vault } from "obsidian"; -import { Buffer } from "buffer"; -import * as crypto from "crypto"; -import { Dropbox, DropboxResponse, files } from "dropbox"; +import { Dropbox, DropboxAuth, DropboxResponse, files } from "dropbox"; export { Dropbox } from "dropbox"; import { RemoteItem } from "./baseTypes"; import { @@ -162,32 +160,28 @@ const fixLastModifiedTimeInplace = (allFilesFolders: RemoteItem[]) => { // see https://dropbox.tech/developers/pkce--what-and-why- //////////////////////////////////////////////////////////////////////////////// -const specialBase64Encode = (str: Buffer) => { - return str - .toString("base64") - .replace(/\+/g, "-") - .replace(/\//g, "_") - .replace(/=/g, ""); -}; -const sha256 = (buffer: string) => { - return crypto.createHash("sha256").update(buffer).digest(); -}; - -export const getCodeVerifierAndChallenge = () => { - const codeVerifier = specialBase64Encode(crypto.randomBytes(32)); - // console.log(`Client generated code_verifier: ${codeVerifier}`); - const codeChallenge = specialBase64Encode(sha256(codeVerifier)); - // console.log(`Client generated code_challenge: ${codeChallenge}`); +export const getAuthUrlAndVerifier = async (appKey: string) => { + const auth = new DropboxAuth({ + clientId: appKey, + }); + const authUrl = ( + await auth.getAuthenticationUrl( + undefined, + undefined, + "code", + "offline", + undefined, + "none", + true + ) + ).toString(); + const verifier = auth.getCodeVerifier(); return { - verifier: codeVerifier, - challenge: codeChallenge, + authUrl: authUrl, + verifier: verifier, }; }; -export const getAuthUrl = (appKey: string, challenge: string) => { - return `https://www.dropbox.com/oauth2/authorize?client_id=${appKey}&response_type=code&code_challenge=${challenge}&code_challenge_method=S256&token_access_type=offline`; -}; - export interface DropboxSuccessAuthRes { access_token: string; token_type: "bearer"; diff --git a/webpack.config.js b/webpack.config.js index 8103caf..ff4875d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -43,7 +43,8 @@ module.exports = { // buffer: require.resolve("buffer/"), // console: require.resolve("console-browserify"), // constants: require.resolve("constants-browserify"), - crypto: require.resolve("crypto-browserify"), + // crypto: require.resolve("crypto-browserify"), + crypto: false, // domain: require.resolve("domain-browser"), // events: require.resolve("events"), // http: require.resolve("stream-http"),