attempt to reduce size and use native crypto

This commit is contained in:
fyears 2021-12-04 00:55:31 +08:00
parent 789d07261e
commit 62936c4473
5 changed files with 28 additions and 38 deletions

View File

@ -24,6 +24,7 @@ esbuild
"obsidian",
"electron",
"fs",
"crypto",
// ...builtins
],
format: "cjs",

View File

@ -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": [],

View File

@ -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;

View File

@ -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";

View File

@ -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"),