Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@
"unicorn/max-nested-calls": ["warn", { "max": 5 }],
"eslint/require-await": "off", // overlaps with typescript
"typescript/ban-types": "off",
"eslint/capitalized-comments": ["warn", "always", { "ignoreConsecutiveComments": true }]
"eslint/capitalized-comments": ["warn", "always", { "ignoreConsecutiveComments": true }],
"unicorn/no-array-reduce": "off"
},
"env": { "builtin": true, "browser": true, "vue": true },
"globals": {
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ All notable changes to this project will be documented in this file.
- Supported editing module version in the module editor.
- Fixed the bug that causes module update button to never show when auto-update is disabled.
- Added module management menu item to manually refresh sources.
- Add a loading indicator when module sources are being loaded.
- Added a loading indicator when module sources are being loaded.
- Ensured network response headers are always normalized to lowercase.

### WebDAV Module
Expand Down Expand Up @@ -52,7 +52,7 @@ All notable changes to this project will be documented in this file.
- Removed memory control options since it does not have real effect in current Obsidian environment.
- Made internal operation optimization capturing more precise and simplified its implementation.
- Improved syncing robustness when confronting server rate limits and network fluctuation.
- Improved interval namespace isolation to support double instancing.
- Improved internal namespace isolation to support double instancing.
- Improved internal sync operation construction flow.
- Improved internal file tree rendering process.

Expand Down
2 changes: 2 additions & 0 deletions docs/src/pages/en/development/develop-a-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Passing module metadata to the plugin prepends a magic bytes comment to each com
```TypeScript
plugins: [
syncEngineModule({
id: 'my-module',
name: 'My Module',
icon: 'puzzle',
description: 'An example module.',
Expand All @@ -104,6 +105,7 @@ The output file then starts with:

```js
/*!
id: my-module
name: My Module
icon: puzzle
description: An example module.
Expand Down
4 changes: 2 additions & 2 deletions modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
"id": "gdrive",
"name": "Google Drive",
"version": "1.0.2",
"version": "1.0.3",
"description": "Google Drive backend support.",
"icon": "server",
"main": "https://sync.consensia.cc/modules/gdrive.js",
Expand All @@ -32,7 +32,7 @@
{
"id": "dropbox",
"name": "Dropbox",
"version": "0.1.1",
"version": "0.1.2",
"description": "[Beta] Dropbox backend support.",
"icon": "server",
"main": "https://sync.consensia.cc/modules/dropbox.js",
Expand Down
38 changes: 18 additions & 20 deletions packages/dropbox/src/dropbox/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { requestUrl } from 'obsidian';
import type { Binary, Request, StringKV } from '@hesprs/sync-engine-sdk';
import type { ObsidianProtocolData, SecretStorage } from 'obsidian';

export const AUTH_ERROR_MESSAGE = 'Please authenticate the Dropbox account!';
export const APP_KEY = atob(process.env.DROPBOX_APP_KEY ?? '');
export const REDIRECT_URI = 'obsidian://sync-engine-dropbox-auth';
export const OAUTH_SCOPE = 'files.content.read files.content.write files.metadata.read';
Expand Down Expand Up @@ -121,7 +120,21 @@ export class TokenManager {
readonly setRefreshToken = (token: string) =>
this.secretStorage.setSecret(KEYCHAIN_SECRET_ID, token);

readonly deleteRefreshToken = () => this.secretStorage.deleteSecret(KEYCHAIN_SECRET_ID);
readonly deleteRefreshToken = () => {
const token = this.getRefreshToken();
if (!token) return;
return this.getToken()
.finally(() => this.secretStorage.deleteSecret(KEYCHAIN_SECRET_ID))
.then((access) =>
requestUrl({
headers: { Authorization: `Bearer ${access}` },
method: 'POST',
throw: false,
url: `${API_RPC}/2/auth/token/revoke`,
}),
)
.catch(() => {});
};

readonly setToken = (token: string, expiresIn: number): void => {
this.accessToken = token;
Expand All @@ -135,7 +148,7 @@ export class TokenManager {

private async refresh(): Promise<string> {
const refresh_token = this.getRefreshToken();
if (!refresh_token) throw new Error(AUTH_ERROR_MESSAGE);
if (!refresh_token) throw new Error('Please authenticate the Dropbox account!');
const response = await requestUrl({
body: formEncode({ client_id: APP_KEY, grant_type: 'refresh_token', refresh_token }),
contentType: FORM_CONTENT_TYPE,
Expand Down Expand Up @@ -168,30 +181,15 @@ export function bearerMiddleware(request: Request, manager: TokenManager): Reque
headers: { ...params?.headers, Authorization: `Bearer ${token}` },
});
try {
return await send(await manager.getToken());
return await manager.getToken().then(send);
} catch (error: unknown) {
if (getStatus(error) !== 401) throw error;
manager.invalidate();
return send(await manager.getToken(true));
return manager.getToken(true).then(send);
}
};
}

/** Revokes the token set derived from the current refresh token. Fire-and-forget. */
export async function revokeToken(manager: TokenManager): Promise<void> {
try {
const token = await manager.getToken();
await requestUrl({
headers: { Authorization: `Bearer ${token}` },
method: 'POST',
throw: false,
url: `${API_RPC}/2/auth/token/revoke`,
});
} catch {
// Best-effort; the token also expires on its own.
}
}

/**
* Correlates the `obsidian://sync-engine-dropbox-auth` callback with the pending authorization
* request; callbacks with mismatched or missing state are ignored.
Expand Down
7 changes: 1 addition & 6 deletions packages/dropbox/src/dropbox/check-connection.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { rpc } from './api';
import { AUTH_ERROR_MESSAGE } from './auth';
import { toError } from '@repo/shared/error';
import type { Request } from '@hesprs/sync-engine-sdk';

/** Validates the token by listing the app folder root, the lightest permission-honoring call. */
export default async function checkConnection(
request: Request,
accountId: string,
): Promise<void | Error> {
if (!accountId) return new Error(AUTH_ERROR_MESSAGE);
export default async function checkConnection(request: Request): Promise<void | Error> {
try {
await rpc(request, '/2/files/list_folder', { path: '' });
} catch (error) {
Expand Down
2 changes: 0 additions & 2 deletions packages/dropbox/src/dropbox/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
uidOf,
verifyEntries,
} from './api';
import { AUTH_ERROR_MESSAGE } from './auth';
import UploadSessions from './sessions';
import { SESSION_CHUNK_SIZE, appendSession, concurrentUploadSession, read2xx } from './upload';
import { chunkSize, concurrency } from '@hesprs/sync-engine-sdk';
Expand Down Expand Up @@ -90,7 +89,6 @@ export default class DropboxFs implements RootFs {
private readonly request: Request,
private readonly options: DropboxFsOptions,
) {
if (!options.accountId) throw new Error(AUTH_ERROR_MESSAGE);
this.uploadSessions = new UploadSessions(request);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/dropbox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class Dropbox {
cleanup.push(
registerCss(styles),
registerRemoteFs('dropbox', {
checkConnection: (req) => checkConnection(req, moduleSettings.accountId),
checkConnection,
instantiate: (request) => new DropboxFs(request, moduleSettings),
prettyName: () => translate('dropbox'),
}),
Expand All @@ -103,6 +103,10 @@ export default class Dropbox {
}),
registerObsidianProtocolHandler('sync-engine-dropbox-auth', pending.notify),
);

// The token can exist despite the user ID is missing (e.g. user reinstalled the plugin), force a re-auth to prevent bugs caused by a missing ID
const token = tokenManager.getRefreshToken();
if (!moduleSettings.accountId && token) void tokenManager.deleteRefreshToken();
};

readonly dispose = () => this.cleanup.splice(0).forEach((fn) => fn());
Expand Down
18 changes: 9 additions & 9 deletions packages/dropbox/src/setting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exchangeCodeForTokens, revokeToken, startAuthorization } from './dropbox/auth';
import { exchangeCodeForTokens, startAuthorization } from './dropbox/auth';
import { settingEntry as s } from '@hesprs/sync-engine-sdk';
import { describeError, toError } from '@repo/shared/error';
import { normalizeBaseDir } from '@repo/shared/path';
Expand Down Expand Up @@ -158,7 +158,7 @@ export default function dropboxSetting(
.onClick(() => connectDropbox()),
);
},
visible: () => !settings.accountId || !tokenManager.getRefreshToken(),
visible: () => !tokenManager.getRefreshToken(),
})),
1100: s(() => ({
desc: translate('connectSuccess'),
Expand All @@ -169,17 +169,17 @@ export default function dropboxSetting(
.setButtonText(translate('disconnect'))
.setDestructive()
.onClick(async () => {
if (!tokenManager.getRefreshToken()) return;
await revokeToken(tokenManager);
settings.accountId = '';
tokenManager.deleteRefreshToken();
tokenManager.invalidate();
void saveSettings();
if (tokenManager.getRefreshToken()) {
await tokenManager.deleteRefreshToken();
settings.accountId = '';
tokenManager.invalidate();
void saveSettings();
}
refreshSettingTab();
}),
);
},
visible: () => Boolean(tokenManager.getRefreshToken() && settings.accountId),
visible: () => Boolean(tokenManager.getRefreshToken()),
})),
2000: s(() => ({
desc: translate('baseDirectoryDescription'),
Expand Down
22 changes: 11 additions & 11 deletions packages/dropbox/test/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { API_RPC, OAUTH_TOKEN_URL } from '@/dropbox/api';
import {
APP_KEY,
PendingAuthorization,
TokenManager,
bearerMiddleware,
exchangeCodeForTokens,
startAuthorization,
} from '@/dropbox/auth';
import { testKit } from '@hesprs/sync-engine-sdk/dev';
import ObsidianMock from '@repo/shared/obsidian-mock';
import { expect, mock, test } from 'bun:test';
Expand All @@ -25,16 +33,6 @@ void mock.module('obsidian', () => ({
},
}));

const {
APP_KEY,
PendingAuthorization,
TokenManager,
bearerMiddleware,
exchangeCodeForTokens,
revokeToken,
startAuthorization,
} = await import('@/dropbox/auth');

function reset(...next: Array<HttpResponse>) {
requests.length = 0;
responses = [...next];
Expand Down Expand Up @@ -174,8 +172,10 @@ test('caches tokens and retries bearer requests after a 401', async () => {
test('revokes the current token set derived from the refresh token', async () => {
reset({ json: { access_token: 'access', expires_in: 14_400 } }, {});
const manager = new TokenManager(createSecrets());
await revokeToken(manager);

await manager.deleteRefreshToken();

expect(manager.getRefreshToken()).toBeUndefined();
expect(requests[1]?.url).toBe(`${API_RPC}/2/auth/token/revoke`);
expect(requests[1]?.headers?.Authorization).toBe('Bearer access');
});
Expand Down
4 changes: 2 additions & 2 deletions packages/dropbox/test/fs-dropbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1128,13 +1128,13 @@ test('the commit-ready callback reports ready writes until cleared', async () =>

test('checkConnection validates the token through a root listing', async () => {
const failing = createFs(() => json({ error: { '.tag': 'invalid_access_token' } }, 401));
const failure = await checkConnection(failing.request, 'id');
const failure = await checkConnection(failing.request);
expect(failure).toBeInstanceOf(Error);
expect(String(failure)).toContain('invalid_access_token');

const passing = createFs((url) => {
expect(url).toBe(`${API_RPC}/2/files/list_folder`);
return json({ cursor: '', entries: [], has_more: false });
});
expect(await checkConnection(passing.request, 'id')).toBeUndefined();
expect(await checkConnection(passing.request)).toBeUndefined();
});
10 changes: 5 additions & 5 deletions packages/encryption/src/wrapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class EncryptionFs implements WrappedFs {
);
}

async delete(key: string) {
return this.original.delete(await this.encryptKey(key));
delete(key: string) {
return this.encryptKey(key).then((enc) => this.original.delete(enc));
}

async move(oldKey: string, newKey: string) {
Expand All @@ -142,7 +142,7 @@ class EncryptionFs implements WrappedFs {
}

async stat(key: string) {
const stat = await this.original.stat(await this.encryptKey(key));
const stat = await this.encryptKey(key).then((enc) => this.original.stat(enc));
stat.key = await this.decryptKey(stat.key);
if (stat.isDir) return stat;
const { metaKey } = await this.getKeys();
Expand All @@ -151,8 +151,8 @@ class EncryptionFs implements WrappedFs {
return stat;
}

async exists(key: string): Promise<boolean> {
return this.original.exists(await this.encryptKey(key));
exists(key: string): Promise<boolean> {
return this.encryptKey(key).then((enc) => this.original.exists(enc));
}

async list(key: string, reporter: ListReporter) {
Expand Down
4 changes: 3 additions & 1 deletion packages/encryption/src/wrapper/read-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export default function createDecryptedReadableStream(

const encryptedChunk = pending.slice(expectedSize);
controller.enqueue(
await decryptContentChunk(await fileKeyPromise, encryptedChunk, chunkIndex),
await fileKeyPromise.then((fk) =>
decryptContentChunk(fk, encryptedChunk, chunkIndex),
),
);
chunkIndex += 1;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/gdrive/src/gdrive/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ export function toStat(
uid: md5Checksum ?? `${mtime}~${size}`,
};
}

export function getFileUid(file: DriveFile, key: string) {
const stat = toStat(key, file);
if (stat.isDir) throw new Error(`Google Drive write returned a folder stat for ${key}.`);
return stat.uid;
}
Loading
Loading