@n24q02m/workspace 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +46 -0
- package/README.md +342 -2
- package/bin/cli.mjs +144 -0
- package/build/src/auth/account-context.d.ts +3 -0
- package/build/src/auth/account-context.d.ts.map +1 -0
- package/build/src/auth/account-context.js +17 -0
- package/build/src/auth/account-context.js.map +1 -0
- package/build/src/auth/account-context.test.d.ts +2 -0
- package/build/src/auth/account-context.test.d.ts.map +1 -0
- package/build/src/auth/account-context.test.js +30 -0
- package/build/src/auth/account-context.test.js.map +1 -0
- package/build/src/auth/account-store.d.ts +87 -0
- package/build/src/auth/account-store.d.ts.map +1 -0
- package/build/src/auth/account-store.js +187 -0
- package/build/src/auth/account-store.js.map +1 -0
- package/build/src/auth/account-store.test.d.ts +2 -0
- package/build/src/auth/account-store.test.d.ts.map +1 -0
- package/build/src/auth/account-store.test.js +264 -0
- package/build/src/auth/account-store.test.js.map +1 -0
- package/build/src/auth/add-account.d.ts +12 -0
- package/build/src/auth/add-account.d.ts.map +1 -0
- package/build/src/auth/add-account.js +95 -0
- package/build/src/auth/add-account.js.map +1 -0
- package/build/src/auth/add-account.test.d.ts +2 -0
- package/build/src/auth/add-account.test.d.ts.map +1 -0
- package/build/src/auth/add-account.test.js +155 -0
- package/build/src/auth/add-account.test.js.map +1 -0
- package/build/src/auth/cli-auth.d.ts +2 -0
- package/build/src/auth/cli-auth.d.ts.map +1 -0
- package/build/src/auth/cli-auth.js +47 -0
- package/build/src/auth/cli-auth.js.map +1 -0
- package/build/src/auth/cli-auth.test.d.ts +2 -0
- package/build/src/auth/cli-auth.test.d.ts.map +1 -0
- package/build/src/auth/cli-auth.test.js +78 -0
- package/build/src/auth/cli-auth.test.js.map +1 -0
- package/build/src/auth/consent-server.d.ts +43 -0
- package/build/src/auth/consent-server.d.ts.map +1 -0
- package/build/src/auth/consent-server.js +56 -0
- package/build/src/auth/consent-server.js.map +1 -0
- package/build/src/auth/credential-state.d.ts +7 -0
- package/build/src/auth/credential-state.d.ts.map +1 -0
- package/build/src/auth/credential-state.js +64 -0
- package/build/src/auth/credential-state.js.map +1 -0
- package/build/src/auth/credential-state.test.d.ts +2 -0
- package/build/src/auth/credential-state.test.d.ts.map +1 -0
- package/build/src/auth/credential-state.test.js +142 -0
- package/build/src/auth/credential-state.test.js.map +1 -0
- package/build/src/auth/loopback-consent.d.ts +23 -0
- package/build/src/auth/loopback-consent.d.ts.map +1 -0
- package/build/src/auth/loopback-consent.js +145 -0
- package/build/src/auth/loopback-consent.js.map +1 -0
- package/build/src/auth/loopback-consent.test.d.ts +2 -0
- package/build/src/auth/loopback-consent.test.d.ts.map +1 -0
- package/build/src/auth/loopback-consent.test.js +131 -0
- package/build/src/auth/loopback-consent.test.js.map +1 -0
- package/build/src/auth/oauth-setup.d.ts +10 -0
- package/build/src/auth/oauth-setup.d.ts.map +1 -0
- package/build/src/auth/oauth-setup.js +92 -0
- package/build/src/auth/oauth-setup.js.map +1 -0
- package/build/src/auth/oauth-setup.test.d.ts +2 -0
- package/build/src/auth/oauth-setup.test.d.ts.map +1 -0
- package/build/src/auth/oauth-setup.test.js +164 -0
- package/build/src/auth/oauth-setup.test.js.map +1 -0
- package/build/src/auth/subject-context.d.ts +2 -0
- package/build/src/auth/subject-context.d.ts.map +1 -0
- package/build/src/auth/subject-context.js +16 -0
- package/build/src/auth/subject-context.js.map +1 -0
- package/build/src/auth/subject-context.test.d.ts +2 -0
- package/build/src/auth/subject-context.test.d.ts.map +1 -0
- package/build/src/auth/subject-context.test.js +34 -0
- package/build/src/auth/subject-context.test.js.map +1 -0
- package/build/src/auth/workspace-auth.d.ts +47 -0
- package/build/src/auth/workspace-auth.d.ts.map +1 -0
- package/build/src/auth/workspace-auth.js +123 -0
- package/build/src/auth/workspace-auth.js.map +1 -0
- package/build/src/auth/workspace-auth.test.d.ts +2 -0
- package/build/src/auth/workspace-auth.test.d.ts.map +1 -0
- package/build/src/auth/workspace-auth.test.js +242 -0
- package/build/src/auth/workspace-auth.test.js.map +1 -0
- package/build/src/constants.d.ts +6 -0
- package/build/src/constants.d.ts.map +1 -0
- package/build/src/constants.js +11 -0
- package/build/src/constants.js.map +1 -0
- package/build/src/create-server.d.ts +13 -0
- package/build/src/create-server.d.ts.map +1 -0
- package/build/src/create-server.js +34 -0
- package/build/src/create-server.js.map +1 -0
- package/build/src/create-server.test.d.ts +2 -0
- package/build/src/create-server.test.d.ts.map +1 -0
- package/build/src/create-server.test.js +13 -0
- package/build/src/create-server.test.js.map +1 -0
- package/build/src/docs/calendar.md +15 -0
- package/build/src/docs/chat.md +15 -0
- package/build/src/docs/config.md +117 -0
- package/build/src/docs/docs.md +36 -0
- package/build/src/docs/drive.md +15 -0
- package/build/src/docs/forms.md +119 -0
- package/build/src/docs/gmail.md +18 -0
- package/build/src/docs/overview.md +52 -0
- package/build/src/docs/people.md +10 -0
- package/build/src/docs/sheets.md +10 -0
- package/build/src/docs/slides.md +26 -0
- package/build/src/docs/tasks.md +13 -0
- package/build/src/docs/time.md +11 -0
- package/build/src/init-server.d.ts +10 -0
- package/build/src/init-server.d.ts.map +1 -0
- package/build/src/init-server.js +13 -0
- package/build/src/init-server.js.map +1 -0
- package/build/src/init-server.test.d.ts +2 -0
- package/build/src/init-server.test.d.ts.map +1 -0
- package/build/src/init-server.test.js +15 -0
- package/build/src/init-server.test.js.map +1 -0
- package/build/src/main.d.ts +33 -0
- package/build/src/main.d.ts.map +1 -0
- package/build/src/main.js +120 -0
- package/build/src/main.js.map +1 -0
- package/build/src/main.test.d.ts +2 -0
- package/build/src/main.test.d.ts.map +1 -0
- package/build/src/main.test.js +204 -0
- package/build/src/main.test.js.map +1 -0
- package/build/src/services/FormsService.d.ts +89 -0
- package/build/src/services/FormsService.d.ts.map +1 -0
- package/build/src/services/FormsService.js +140 -0
- package/build/src/services/FormsService.js.map +1 -0
- package/build/src/services/FormsService.test.d.ts +2 -0
- package/build/src/services/FormsService.test.d.ts.map +1 -0
- package/build/src/services/FormsService.test.js +173 -0
- package/build/src/services/FormsService.test.js.map +1 -0
- package/build/src/tools/config.d.ts +19 -0
- package/build/src/tools/config.d.ts.map +1 -0
- package/build/src/tools/config.js +100 -0
- package/build/src/tools/config.js.map +1 -0
- package/build/src/tools/config.test.d.ts +2 -0
- package/build/src/tools/config.test.d.ts.map +1 -0
- package/build/src/tools/config.test.js +201 -0
- package/build/src/tools/config.test.js.map +1 -0
- package/build/src/tools/domains/calendar.d.ts +3 -0
- package/build/src/tools/domains/calendar.d.ts.map +1 -0
- package/build/src/tools/domains/calendar.js +14 -0
- package/build/src/tools/domains/calendar.js.map +1 -0
- package/build/src/tools/domains/chat.d.ts +3 -0
- package/build/src/tools/domains/chat.d.ts.map +1 -0
- package/build/src/tools/domains/chat.js +16 -0
- package/build/src/tools/domains/chat.js.map +1 -0
- package/build/src/tools/domains/docs.d.ts +8 -0
- package/build/src/tools/domains/docs.d.ts.map +1 -0
- package/build/src/tools/domains/docs.js +11 -0
- package/build/src/tools/domains/docs.js.map +1 -0
- package/build/src/tools/domains/docs.test.d.ts +2 -0
- package/build/src/tools/domains/docs.test.d.ts.map +1 -0
- package/build/src/tools/domains/docs.test.js +111 -0
- package/build/src/tools/domains/docs.test.js.map +1 -0
- package/build/src/tools/domains/drive.d.ts +3 -0
- package/build/src/tools/domains/drive.d.ts.map +1 -0
- package/build/src/tools/domains/drive.js +14 -0
- package/build/src/tools/domains/drive.js.map +1 -0
- package/build/src/tools/domains/factory.d.ts +24 -0
- package/build/src/tools/domains/factory.d.ts.map +1 -0
- package/build/src/tools/domains/factory.js +43 -0
- package/build/src/tools/domains/factory.js.map +1 -0
- package/build/src/tools/domains/factory.test.d.ts +2 -0
- package/build/src/tools/domains/factory.test.d.ts.map +1 -0
- package/build/src/tools/domains/factory.test.js +48 -0
- package/build/src/tools/domains/factory.test.js.map +1 -0
- package/build/src/tools/domains/forms.d.ts +9 -0
- package/build/src/tools/domains/forms.d.ts.map +1 -0
- package/build/src/tools/domains/forms.js +12 -0
- package/build/src/tools/domains/forms.js.map +1 -0
- package/build/src/tools/domains/forms.test.d.ts +2 -0
- package/build/src/tools/domains/forms.test.d.ts.map +1 -0
- package/build/src/tools/domains/forms.test.js +93 -0
- package/build/src/tools/domains/forms.test.js.map +1 -0
- package/build/src/tools/domains/gmail.d.ts +3 -0
- package/build/src/tools/domains/gmail.d.ts.map +1 -0
- package/build/src/tools/domains/gmail.js +17 -0
- package/build/src/tools/domains/gmail.js.map +1 -0
- package/build/src/tools/domains/index.d.ts +20 -0
- package/build/src/tools/domains/index.d.ts.map +1 -0
- package/build/src/tools/domains/index.js +230 -0
- package/build/src/tools/domains/index.js.map +1 -0
- package/build/src/tools/domains/people.d.ts +3 -0
- package/build/src/tools/domains/people.d.ts.map +1 -0
- package/build/src/tools/domains/people.js +5 -0
- package/build/src/tools/domains/people.js.map +1 -0
- package/build/src/tools/domains/sheets.d.ts +3 -0
- package/build/src/tools/domains/sheets.d.ts.map +1 -0
- package/build/src/tools/domains/sheets.js +5 -0
- package/build/src/tools/domains/sheets.js.map +1 -0
- package/build/src/tools/domains/slides.d.ts +3 -0
- package/build/src/tools/domains/slides.d.ts.map +1 -0
- package/build/src/tools/domains/slides.js +25 -0
- package/build/src/tools/domains/slides.js.map +1 -0
- package/build/src/tools/domains/tasks.d.ts +3 -0
- package/build/src/tools/domains/tasks.d.ts.map +1 -0
- package/build/src/tools/domains/tasks.js +14 -0
- package/build/src/tools/domains/tasks.js.map +1 -0
- package/build/src/tools/domains/time.d.ts +3 -0
- package/build/src/tools/domains/time.d.ts.map +1 -0
- package/build/src/tools/domains/time.js +6 -0
- package/build/src/tools/domains/time.js.map +1 -0
- package/build/src/tools/helpers/errors.d.ts +48 -0
- package/build/src/tools/helpers/errors.d.ts.map +1 -0
- package/build/src/tools/helpers/errors.js +290 -0
- package/build/src/tools/helpers/errors.js.map +1 -0
- package/build/src/tools/helpers/errors.test.d.ts +2 -0
- package/build/src/tools/helpers/errors.test.d.ts.map +1 -0
- package/build/src/tools/helpers/errors.test.js +220 -0
- package/build/src/tools/helpers/errors.test.js.map +1 -0
- package/build/src/tools/registry.d.ts +14 -0
- package/build/src/tools/registry.d.ts.map +1 -0
- package/build/src/tools/registry.js +276 -0
- package/build/src/tools/registry.js.map +1 -0
- package/build/src/tools/registry.test.d.ts +2 -0
- package/build/src/tools/registry.test.d.ts.map +1 -0
- package/build/src/tools/registry.test.js +321 -0
- package/build/src/tools/registry.test.js.map +1 -0
- package/build/src/transports/http.d.ts +7 -0
- package/build/src/transports/http.d.ts.map +1 -0
- package/build/src/transports/http.js +56 -0
- package/build/src/transports/http.js.map +1 -0
- package/build/src/transports/http.test.d.ts +2 -0
- package/build/src/transports/http.test.d.ts.map +1 -0
- package/build/src/transports/http.test.js +76 -0
- package/build/src/transports/http.test.js.map +1 -0
- package/build/src/vendored/auth/AuthManager.d.ts +7 -0
- package/build/src/vendored/auth/AuthManager.d.ts.map +1 -0
- package/build/src/vendored/auth/AuthManager.js +11 -0
- package/build/src/vendored/auth/AuthManager.js.map +1 -0
- package/build/src/vendored/services/CalendarService.d.ts +164 -0
- package/build/src/vendored/services/CalendarService.d.ts.map +1 -0
- package/build/src/vendored/services/CalendarService.js +726 -0
- package/build/src/vendored/services/CalendarService.js.map +1 -0
- package/build/src/vendored/services/CalendarValidation.d.ts +9 -0
- package/build/src/vendored/services/CalendarValidation.d.ts.map +1 -0
- package/build/src/vendored/services/CalendarValidation.js +125 -0
- package/build/src/vendored/services/CalendarValidation.js.map +1 -0
- package/build/src/vendored/services/ChatService.d.ts +90 -0
- package/build/src/vendored/services/ChatService.d.ts.map +1 -0
- package/build/src/vendored/services/ChatService.js +451 -0
- package/build/src/vendored/services/ChatService.js.map +1 -0
- package/build/src/vendored/services/DocsService.d.ts +109 -0
- package/build/src/vendored/services/DocsService.d.ts.map +1 -0
- package/build/src/vendored/services/DocsService.js +663 -0
- package/build/src/vendored/services/DocsService.js.map +1 -0
- package/build/src/vendored/services/DocsService.test.d.ts +2 -0
- package/build/src/vendored/services/DocsService.test.d.ts.map +1 -0
- package/build/src/vendored/services/DocsService.test.js +77 -0
- package/build/src/vendored/services/DocsService.test.js.map +1 -0
- package/build/src/vendored/services/DriveService.d.ts +117 -0
- package/build/src/vendored/services/DriveService.d.ts.map +1 -0
- package/build/src/vendored/services/DriveService.js +527 -0
- package/build/src/vendored/services/DriveService.js.map +1 -0
- package/build/src/vendored/services/DriveService.test.d.ts +2 -0
- package/build/src/vendored/services/DriveService.test.d.ts.map +1 -0
- package/build/src/vendored/services/DriveService.test.js +115 -0
- package/build/src/vendored/services/DriveService.test.js.map +1 -0
- package/build/src/vendored/services/GmailService.d.ts +137 -0
- package/build/src/vendored/services/GmailService.d.ts.map +1 -0
- package/build/src/vendored/services/GmailService.js +681 -0
- package/build/src/vendored/services/GmailService.js.map +1 -0
- package/build/src/vendored/services/PeopleService.d.ts +42 -0
- package/build/src/vendored/services/PeopleService.d.ts.map +1 -0
- package/build/src/vendored/services/PeopleService.js +168 -0
- package/build/src/vendored/services/PeopleService.js.map +1 -0
- package/build/src/vendored/services/SheetsService.d.ts +52 -0
- package/build/src/vendored/services/SheetsService.d.ts.map +1 -0
- package/build/src/vendored/services/SheetsService.js +262 -0
- package/build/src/vendored/services/SheetsService.js.map +1 -0
- package/build/src/vendored/services/SheetsService.test.d.ts +2 -0
- package/build/src/vendored/services/SheetsService.test.d.ts.map +1 -0
- package/build/src/vendored/services/SheetsService.test.js +104 -0
- package/build/src/vendored/services/SheetsService.test.js.map +1 -0
- package/build/src/vendored/services/SlidesService.d.ts +167 -0
- package/build/src/vendored/services/SlidesService.d.ts.map +1 -0
- package/build/src/vendored/services/SlidesService.js +887 -0
- package/build/src/vendored/services/SlidesService.js.map +1 -0
- package/build/src/vendored/services/TasksService.d.ts +97 -0
- package/build/src/vendored/services/TasksService.d.ts.map +1 -0
- package/build/src/vendored/services/TasksService.js +234 -0
- package/build/src/vendored/services/TasksService.js.map +1 -0
- package/build/src/vendored/services/TimeService.d.ts +29 -0
- package/build/src/vendored/services/TimeService.d.ts.map +1 -0
- package/build/src/vendored/services/TimeService.js +71 -0
- package/build/src/vendored/services/TimeService.js.map +1 -0
- package/build/src/vendored/utils/DriveQueryBuilder.d.ts +15 -0
- package/build/src/vendored/utils/DriveQueryBuilder.d.ts.map +1 -0
- package/build/src/vendored/utils/DriveQueryBuilder.js +17 -0
- package/build/src/vendored/utils/DriveQueryBuilder.js.map +1 -0
- package/build/src/vendored/utils/GaxiosConfig.d.ts +9 -0
- package/build/src/vendored/utils/GaxiosConfig.d.ts.map +1 -0
- package/build/src/vendored/utils/GaxiosConfig.js +30 -0
- package/build/src/vendored/utils/GaxiosConfig.js.map +1 -0
- package/build/src/vendored/utils/IdUtils.d.ts +13 -0
- package/build/src/vendored/utils/IdUtils.d.ts.map +1 -0
- package/build/src/vendored/utils/IdUtils.js +29 -0
- package/build/src/vendored/utils/IdUtils.js.map +1 -0
- package/build/src/vendored/utils/MimeHelper.d.ts +64 -0
- package/build/src/vendored/utils/MimeHelper.d.ts.map +1 -0
- package/build/src/vendored/utils/MimeHelper.js +174 -0
- package/build/src/vendored/utils/MimeHelper.js.map +1 -0
- package/build/src/vendored/utils/MimeHelper.test.d.ts +2 -0
- package/build/src/vendored/utils/MimeHelper.test.d.ts.map +1 -0
- package/build/src/vendored/utils/MimeHelper.test.js +47 -0
- package/build/src/vendored/utils/MimeHelper.test.js.map +1 -0
- package/build/src/vendored/utils/constants.d.ts +9 -0
- package/build/src/vendored/utils/constants.d.ts.map +1 -0
- package/build/src/vendored/utils/constants.js +9 -0
- package/build/src/vendored/utils/constants.js.map +1 -0
- package/build/src/vendored/utils/logger.d.ts +3 -0
- package/build/src/vendored/utils/logger.d.ts.map +1 -0
- package/build/src/vendored/utils/logger.js +15 -0
- package/build/src/vendored/utils/logger.js.map +1 -0
- package/build/src/vendored/utils/paths.d.ts +2 -0
- package/build/src/vendored/utils/paths.d.ts.map +1 -0
- package/build/src/vendored/utils/paths.js +19 -0
- package/build/src/vendored/utils/paths.js.map +1 -0
- package/build/src/vendored/utils/validation.d.ts +100 -0
- package/build/src/vendored/utils/validation.d.ts.map +1 -0
- package/build/src/vendored/utils/validation.js +161 -0
- package/build/src/vendored/utils/validation.js.map +1 -0
- package/build/src/version.d.ts +2 -0
- package/build/src/version.d.ts.map +1 -0
- package/build/src/version.js +29 -0
- package/build/src/version.js.map +1 -0
- package/build/src/version.test.d.ts +2 -0
- package/build/src/version.test.d.ts.map +1 -0
- package/build/src/version.test.js +19 -0
- package/build/src/version.test.js.map +1 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/package.json +69 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-context.d.ts","sourceRoot":"","sources":["../../../src/auth/account-context.ts"],"names":[],"mappings":"AAgBA,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAE/F;AAED,wBAAgB,cAAc,IAAI,MAAM,GAAG,SAAS,CAEnD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account đang được chọn cho request hiện tại.
|
|
3
|
+
*
|
|
4
|
+
* Vì các service vendored được khởi tạo singleton lúc module-load (xem
|
|
5
|
+
* factory.ts) và chữ ký upstream `getAuthenticatedClient()` không nhận tham
|
|
6
|
+
* số, account không thể đi xuống bằng đường tham số mà không sửa code
|
|
7
|
+
* vendored. AsyncLocalStorage giữ nguyên chữ ký đó.
|
|
8
|
+
*/
|
|
9
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
10
|
+
const storage = new AsyncLocalStorage();
|
|
11
|
+
export function runWithAccount(account, fn) {
|
|
12
|
+
return storage.run({ account }, fn);
|
|
13
|
+
}
|
|
14
|
+
export function currentAccount() {
|
|
15
|
+
return storage.getStore()?.account;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=account-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-context.js","sourceRoot":"","sources":["../../../src/auth/account-context.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAMpD,MAAM,OAAO,GAAG,IAAI,iBAAiB,EAAgB,CAAA;AAErD,MAAM,UAAU,cAAc,CAAI,OAA2B,EAAE,EAAoB;IACjF,OAAO,OAAO,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAA;AACrC,CAAC;AAED,MAAM,UAAU,cAAc;IAC5B,OAAO,OAAO,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAA;AACpC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-context.test.d.ts","sourceRoot":"","sources":["../../../src/auth/account-context.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { currentAccount, runWithAccount } from './account-context.js';
|
|
3
|
+
describe('account-context', () => {
|
|
4
|
+
it('returns undefined outside any scope', () => {
|
|
5
|
+
expect(currentAccount()).toBeUndefined();
|
|
6
|
+
});
|
|
7
|
+
it('exposes the account inside the scope and restores after', async () => {
|
|
8
|
+
const inside = await runWithAccount('a@example.com', async () => currentAccount());
|
|
9
|
+
expect(inside).toBe('a@example.com');
|
|
10
|
+
expect(currentAccount()).toBeUndefined();
|
|
11
|
+
});
|
|
12
|
+
it('keeps concurrent scopes isolated', async () => {
|
|
13
|
+
const slow = runWithAccount('slow@example.com', async () => {
|
|
14
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
15
|
+
return currentAccount();
|
|
16
|
+
});
|
|
17
|
+
const fast = runWithAccount('fast@example.com', async () => currentAccount());
|
|
18
|
+
expect(await fast).toBe('fast@example.com');
|
|
19
|
+
expect(await slow).toBe('slow@example.com');
|
|
20
|
+
});
|
|
21
|
+
it('lets an inner scope shadow an outer one', async () => {
|
|
22
|
+
const seen = await runWithAccount('outer@example.com', async () => runWithAccount('inner@example.com', async () => currentAccount()));
|
|
23
|
+
expect(seen).toBe('inner@example.com');
|
|
24
|
+
});
|
|
25
|
+
it('treats an explicit undefined as "no account selected"', async () => {
|
|
26
|
+
const seen = await runWithAccount(undefined, async () => currentAccount());
|
|
27
|
+
expect(seen).toBeUndefined();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
//# sourceMappingURL=account-context.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-context.test.js","sourceRoot":"","sources":["../../../src/auth/account-context.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAErE,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAC/B,EAAE,CAAC,qCAAqC,EAAE,GAAG,EAAE;QAC7C,MAAM,CAAC,cAAc,EAAE,CAAC,CAAC,aAAa,EAAE,CAAA;IAC1C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,yDAAyD,EAAE,KAAK,IAAI,EAAE;QACvE,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,eAAe,EAAE,KAAK,IAAI,EAAE,CAAC,cAAc,EAAE,CAAC,CAAA;QAClF,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,CAAA;QACpC,MAAM,CAAC,cAAc,EAAE,CAAC,CAAC,aAAa,EAAE,CAAA;IAC1C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,kCAAkC,EAAE,KAAK,IAAI,EAAE;QAChD,MAAM,IAAI,GAAG,cAAc,CAAC,kBAAkB,EAAE,KAAK,IAAI,EAAE;YACzD,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;YAC3C,OAAO,cAAc,EAAE,CAAA;QACzB,CAAC,CAAC,CAAA;QACF,MAAM,IAAI,GAAG,cAAc,CAAC,kBAAkB,EAAE,KAAK,IAAI,EAAE,CAAC,cAAc,EAAE,CAAC,CAAA;QAC7E,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAA;QAC3C,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAA;IAC7C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,yCAAyC,EAAE,KAAK,IAAI,EAAE;QACvD,MAAM,IAAI,GAAG,MAAM,cAAc,CAAC,mBAAmB,EAAE,KAAK,IAAI,EAAE,CAChE,cAAc,CAAC,mBAAmB,EAAE,KAAK,IAAI,EAAE,CAAC,cAAc,EAAE,CAAC,CAClE,CAAA;QACD,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,uDAAuD,EAAE,KAAK,IAAI,EAAE;QACrE,MAAM,IAAI,GAAG,MAAM,cAAc,CAAC,SAAS,EAAE,KAAK,IAAI,EAAE,CAAC,cAAc,EAAE,CAAC,CAAA;QAC1E,MAAM,CAAC,IAAI,CAAC,CAAC,aAAa,EAAE,CAAA;IAC9B,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
export interface AccountRecord {
|
|
2
|
+
access_token: string;
|
|
3
|
+
refresh_token?: string;
|
|
4
|
+
expiry_date?: number;
|
|
5
|
+
scope?: string;
|
|
6
|
+
token_type?: string;
|
|
7
|
+
id_token?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface AccountsBlob {
|
|
10
|
+
version: 2;
|
|
11
|
+
accounts: Record<string, AccountRecord>;
|
|
12
|
+
primary: string;
|
|
13
|
+
}
|
|
14
|
+
/** Đọc claim `email` từ id_token. Không xác thực chữ ký: token do chính Google trả về trong luồng đã hoàn tất. */
|
|
15
|
+
export declare function emailFromIdToken(idToken: string | undefined): string | undefined;
|
|
16
|
+
/** Blob phẳng của M1: token nằm ngay ở cấp trên, chưa có `version`. */
|
|
17
|
+
export declare function isLegacyBlob(raw: Record<string, unknown> | null): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Key giữ blob M1 không xác định được chủ, thay vì xoá nó đi. Không phải email
|
|
20
|
+
* nên không đụng vào không gian tên account thật, và vẫn hiện ra ở
|
|
21
|
+
* `account_list` để người dùng xoá được bằng `account_remove`.
|
|
22
|
+
*/
|
|
23
|
+
export declare const UNIDENTIFIED_ACCOUNT = "(unidentified)";
|
|
24
|
+
export declare class AccountStore {
|
|
25
|
+
private store;
|
|
26
|
+
/**
|
|
27
|
+
* `sub` = namespace của người dùng (chế độ HTTP multi-user). Không truyền =
|
|
28
|
+
* `null` = một bucket duy nhất, đúng cho stdio single-user.
|
|
29
|
+
*
|
|
30
|
+
* PerPluginStore đổi credKey theo sub: `better-workspace/subs/<sub>/config` so
|
|
31
|
+
* với `better-workspace/config`, và khoá mã hoá cũng được derive theo sub
|
|
32
|
+
* (khoá theo user thay cho khoá máy). Nên hai
|
|
33
|
+
* sub khác nhau không đọc được blob của nhau kể cả khi chạm được cùng backend.
|
|
34
|
+
*
|
|
35
|
+
* Nhánh sub cần env `CREDENTIAL_SECRET`; thiếu nó thì lần đọc/ghi đầu tiên ném.
|
|
36
|
+
* Một `sub` chứa ký tự ngoài `[a-zA-Z0-9._-]` bị `credPath()` từ chối NGAY trong
|
|
37
|
+
* constructor này -- đó là hàng rào chặn path traversal, giữ nguyên đừng bọc lại.
|
|
38
|
+
*/
|
|
39
|
+
constructor(sub?: string | null);
|
|
40
|
+
/**
|
|
41
|
+
* Trả blob v2. Nếu trên đĩa còn là blob phẳng M1 thì tự migrate KHI suy ra
|
|
42
|
+
* được email từ id_token; nếu không suy ra được thì trả null và để nguyên
|
|
43
|
+
* blob cũ (không được làm mất token của người dùng) — Task 3 sẽ hỏi Google
|
|
44
|
+
* lấy email rồi gọi `put`.
|
|
45
|
+
*/
|
|
46
|
+
load(): Promise<AccountsBlob | null>;
|
|
47
|
+
/** Blob phẳng M1 chưa migrate được, hoặc null. */
|
|
48
|
+
loadLegacy(): Promise<AccountRecord | null>;
|
|
49
|
+
/**
|
|
50
|
+
* Nhận blob phẳng M1 về dưới tên `email` -- cửa ra hợp lệ duy nhất của guard
|
|
51
|
+
* trong `put()`. Tách hẳn khỏi `put()` vì hai ý định khác nhau: `put` là
|
|
52
|
+
* "thêm một account MỚI" (và phải từ chối khi làm vậy sẽ đè mất blob cũ),
|
|
53
|
+
* còn đây là "blob cũ đó CHÍNH LÀ account này, ghi lại đúng chỗ".
|
|
54
|
+
*
|
|
55
|
+
* Ghi trong MỘT lần `save()`, không `clear()` rồi `save()`: một cú crash
|
|
56
|
+
* giữa hai bước sẽ xoá sạch refresh_token mà người dùng không lấy lại được
|
|
57
|
+
* mà không OAuth lại.
|
|
58
|
+
*
|
|
59
|
+
* Trả về record đã nhận, hoặc null nếu trên đĩa không (còn) là blob phẳng --
|
|
60
|
+
* nhờ đó gọi lại lần hai là no-op thay vì làm hỏng blob v2 đang có.
|
|
61
|
+
*/
|
|
62
|
+
adoptLegacy(email: string): Promise<AccountRecord | null>;
|
|
63
|
+
/**
|
|
64
|
+
* `absorbLegacy` dành cho caller đứng ngay sau một lần consent người dùng vừa
|
|
65
|
+
* hoàn tất. Không có cờ đó, một blob M1 chưa nhận về sẽ khiến `put` từ chối.
|
|
66
|
+
*/
|
|
67
|
+
put(email: string, record: AccountRecord, opts?: {
|
|
68
|
+
makePrimary?: boolean;
|
|
69
|
+
absorbLegacy?: boolean;
|
|
70
|
+
}): Promise<void>;
|
|
71
|
+
/** `email` không truyền = primary. Account lạ trả null (KHÔNG âm thầm rơi về primary). */
|
|
72
|
+
get(email?: string): Promise<{
|
|
73
|
+
email: string;
|
|
74
|
+
record: AccountRecord;
|
|
75
|
+
} | null>;
|
|
76
|
+
list(): Promise<{
|
|
77
|
+
accounts: string[];
|
|
78
|
+
primary: string | null;
|
|
79
|
+
}>;
|
|
80
|
+
remove(email: string): Promise<{
|
|
81
|
+
removed: boolean;
|
|
82
|
+
newPrimary: string | null;
|
|
83
|
+
}>;
|
|
84
|
+
setPrimary(email: string): Promise<void>;
|
|
85
|
+
clear(): Promise<void>;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=account-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-store.d.ts","sourceRoot":"","sources":["../../../src/auth/account-store.ts"],"names":[],"mappings":"AAWA,MAAM,WAAW,aAAa;IAC5B,YAAY,EAAE,MAAM,CAAA;IACpB,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,CAAC,CAAA;IACV,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;IACvC,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,kHAAkH;AAClH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAUhF;AAED,uEAAuE;AACvE,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,GAAG,OAAO,CAEzE;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,mBAAmB,CAAA;AAIpD,qBAAa,YAAY;IAC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Key cho mỗi
|
|
5
|
+
* (plugin, sub) — nên layout "một key mỗi account" mà spec §4.2
|
|
6
|
+
* mô tả không dùng được. Gộp vào một blob cũng khiến mỗi lần ghi là atomic:
|
|
7
|
+
* `accounts` và `primary` không bao giờ lệch nhau.
|
|
8
|
+
*/
|
|
9
|
+
import { PerPluginStore } from '@n24q02m/hull/storage';
|
|
10
|
+
import { STORE_PLUGIN } from '../constants.js';
|
|
11
|
+
/** Đọc claim `email` từ id_token. Không xác thực chữ ký: token do chính Google trả về trong luồng đã hoàn tất. */
|
|
12
|
+
export function emailFromIdToken(idToken) {
|
|
13
|
+
if (typeof idToken !== 'string')
|
|
14
|
+
return undefined;
|
|
15
|
+
const payload = idToken.split('.')[1];
|
|
16
|
+
if (!payload)
|
|
17
|
+
return undefined;
|
|
18
|
+
try {
|
|
19
|
+
const claims = JSON.parse(Buffer.from(payload, 'base64url').toString('utf8'));
|
|
20
|
+
return typeof claims.email === 'string' ? claims.email.toLowerCase() : undefined;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Blob phẳng của M1: token nằm ngay ở cấp trên, chưa có `version`. */
|
|
27
|
+
export function isLegacyBlob(raw) {
|
|
28
|
+
return raw !== null && raw.version === undefined && typeof raw.access_token === 'string';
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Key giữ blob M1 không xác định được chủ, thay vì xoá nó đi. Không phải email
|
|
32
|
+
* nên không đụng vào không gian tên account thật, và vẫn hiện ra ở
|
|
33
|
+
* `account_list` để người dùng xoá được bằng `account_remove`.
|
|
34
|
+
*/
|
|
35
|
+
export const UNIDENTIFIED_ACCOUNT = '(unidentified)';
|
|
36
|
+
const normalize = (email) => email.trim().toLowerCase();
|
|
37
|
+
export class AccountStore {
|
|
38
|
+
/**
|
|
39
|
+
* `sub` = namespace của người dùng (chế độ HTTP multi-user). Không truyền =
|
|
40
|
+
* `null` = một bucket duy nhất, đúng cho stdio single-user.
|
|
41
|
+
*
|
|
42
|
+
* PerPluginStore đổi credKey theo sub: `better-workspace/subs/<sub>/config` so
|
|
43
|
+
* với `better-workspace/config`, và khoá mã hoá cũng được derive theo sub
|
|
44
|
+
* (khoá theo user thay cho khoá máy). Nên hai
|
|
45
|
+
* sub khác nhau không đọc được blob của nhau kể cả khi chạm được cùng backend.
|
|
46
|
+
*
|
|
47
|
+
* Nhánh sub cần env `CREDENTIAL_SECRET`; thiếu nó thì lần đọc/ghi đầu tiên ném.
|
|
48
|
+
* Một `sub` chứa ký tự ngoài `[a-zA-Z0-9._-]` bị `credPath()` từ chối NGAY trong
|
|
49
|
+
* constructor này -- đó là hàng rào chặn path traversal, giữ nguyên đừng bọc lại.
|
|
50
|
+
*/
|
|
51
|
+
constructor(sub = null) {
|
|
52
|
+
this.store = new PerPluginStore(STORE_PLUGIN, sub);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Trả blob v2. Nếu trên đĩa còn là blob phẳng M1 thì tự migrate KHI suy ra
|
|
56
|
+
* được email từ id_token; nếu không suy ra được thì trả null và để nguyên
|
|
57
|
+
* blob cũ (không được làm mất token của người dùng) — Task 3 sẽ hỏi Google
|
|
58
|
+
* lấy email rồi gọi `put`.
|
|
59
|
+
*/
|
|
60
|
+
async load() {
|
|
61
|
+
const raw = await this.store.load();
|
|
62
|
+
if (raw === null)
|
|
63
|
+
return null;
|
|
64
|
+
if (!isLegacyBlob(raw)) {
|
|
65
|
+
const blob = raw;
|
|
66
|
+
return blob.version === 2 && blob.accounts ? blob : null;
|
|
67
|
+
}
|
|
68
|
+
const legacy = raw;
|
|
69
|
+
const email = emailFromIdToken(legacy.id_token);
|
|
70
|
+
if (!email)
|
|
71
|
+
return null;
|
|
72
|
+
const migrated = { version: 2, accounts: { [email]: legacy }, primary: email };
|
|
73
|
+
await this.store.save(migrated);
|
|
74
|
+
return migrated;
|
|
75
|
+
}
|
|
76
|
+
/** Blob phẳng M1 chưa migrate được, hoặc null. */
|
|
77
|
+
async loadLegacy() {
|
|
78
|
+
const raw = await this.store.load();
|
|
79
|
+
return isLegacyBlob(raw) ? raw : null;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Nhận blob phẳng M1 về dưới tên `email` -- cửa ra hợp lệ duy nhất của guard
|
|
83
|
+
* trong `put()`. Tách hẳn khỏi `put()` vì hai ý định khác nhau: `put` là
|
|
84
|
+
* "thêm một account MỚI" (và phải từ chối khi làm vậy sẽ đè mất blob cũ),
|
|
85
|
+
* còn đây là "blob cũ đó CHÍNH LÀ account này, ghi lại đúng chỗ".
|
|
86
|
+
*
|
|
87
|
+
* Ghi trong MỘT lần `save()`, không `clear()` rồi `save()`: một cú crash
|
|
88
|
+
* giữa hai bước sẽ xoá sạch refresh_token mà người dùng không lấy lại được
|
|
89
|
+
* mà không OAuth lại.
|
|
90
|
+
*
|
|
91
|
+
* Trả về record đã nhận, hoặc null nếu trên đĩa không (còn) là blob phẳng --
|
|
92
|
+
* nhờ đó gọi lại lần hai là no-op thay vì làm hỏng blob v2 đang có.
|
|
93
|
+
*/
|
|
94
|
+
async adoptLegacy(email) {
|
|
95
|
+
const legacy = await this.loadLegacy();
|
|
96
|
+
if (!legacy)
|
|
97
|
+
return null;
|
|
98
|
+
const key = normalize(email);
|
|
99
|
+
await this.store.save({
|
|
100
|
+
version: 2,
|
|
101
|
+
accounts: { [key]: legacy },
|
|
102
|
+
primary: key
|
|
103
|
+
});
|
|
104
|
+
return legacy;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* `absorbLegacy` dành cho caller đứng ngay sau một lần consent người dùng vừa
|
|
108
|
+
* hoàn tất. Không có cờ đó, một blob M1 chưa nhận về sẽ khiến `put` từ chối.
|
|
109
|
+
*/
|
|
110
|
+
async put(email, record, opts = {}) {
|
|
111
|
+
const key = normalize(email);
|
|
112
|
+
const existing = await this.load();
|
|
113
|
+
let carried = {};
|
|
114
|
+
if (!existing) {
|
|
115
|
+
// load() trả null cho CẢ "chưa có gì" lẫn "có blob M1 nhưng chưa nhận về
|
|
116
|
+
// được". Ghi đè ở trường hợp thứ hai sẽ xoá refresh_token của người dùng,
|
|
117
|
+
// nên dừng lại thật to thay vì mất dữ liệu: caller nhận blob cũ về trước
|
|
118
|
+
// (WorkspaceAuth.getAuthenticatedClient làm việc đó), hoặc người dùng chủ
|
|
119
|
+
// động bỏ nó bằng config(action="setup_reset").
|
|
120
|
+
const legacy = await this.loadLegacy();
|
|
121
|
+
if (legacy) {
|
|
122
|
+
if (!opts.absorbLegacy) {
|
|
123
|
+
throw new Error('Refusing to overwrite credentials stored in the older single-account layout. Adopt them first, or clear them deliberately with config(action="setup_reset") before adding a new account.');
|
|
124
|
+
}
|
|
125
|
+
// Người dùng vừa hoàn tất consent, nên ghi là đúng ý họ -- nhưng vẫn không
|
|
126
|
+
// xoá blob cũ: giữ nó dưới UNIDENTIFIED_ACCOUNT để account_list thấy được và
|
|
127
|
+
// account_remove xoá được. Chặn ở đây thay vì hấp thụ sẽ làm startup không
|
|
128
|
+
// thoát được: guard chặn chính lần ghi đó, mà setup_reset lại cần server đang chạy.
|
|
129
|
+
carried = { [UNIDENTIFIED_ACCOUNT]: legacy };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const base = existing ?? { version: 2, accounts: carried, primary: key };
|
|
133
|
+
const accounts = { ...base.accounts, [key]: record };
|
|
134
|
+
const primary = opts.makePrimary || !base.accounts[base.primary] ? key : base.primary;
|
|
135
|
+
await this.store.save({ version: 2, accounts, primary });
|
|
136
|
+
}
|
|
137
|
+
/** `email` không truyền = primary. Account lạ trả null (KHÔNG âm thầm rơi về primary). */
|
|
138
|
+
async get(email) {
|
|
139
|
+
const blob = await this.load();
|
|
140
|
+
if (!blob)
|
|
141
|
+
return null;
|
|
142
|
+
const key = email ? normalize(email) : blob.primary;
|
|
143
|
+
const record = blob.accounts[key];
|
|
144
|
+
return record ? { email: key, record } : null;
|
|
145
|
+
}
|
|
146
|
+
async list() {
|
|
147
|
+
const blob = await this.load();
|
|
148
|
+
if (!blob)
|
|
149
|
+
return { accounts: [], primary: null };
|
|
150
|
+
return { accounts: Object.keys(blob.accounts).sort(), primary: blob.primary };
|
|
151
|
+
}
|
|
152
|
+
async remove(email) {
|
|
153
|
+
const blob = await this.load();
|
|
154
|
+
if (!blob)
|
|
155
|
+
return { removed: false, newPrimary: null };
|
|
156
|
+
const key = normalize(email);
|
|
157
|
+
if (!blob.accounts[key])
|
|
158
|
+
return { removed: false, newPrimary: blob.primary };
|
|
159
|
+
const accounts = { ...blob.accounts };
|
|
160
|
+
delete accounts[key];
|
|
161
|
+
const remaining = Object.keys(accounts);
|
|
162
|
+
if (remaining.length === 0) {
|
|
163
|
+
await this.store.clear();
|
|
164
|
+
return { removed: true, newPrimary: null };
|
|
165
|
+
}
|
|
166
|
+
// Đề account khác lên trước UNIDENTIFIED_ACCOUNT: nó là credential không biết
|
|
167
|
+
// của ai, thường đã hết hiệu lực. Để nó thành primary là âm thầm đưa mọi lời
|
|
168
|
+
// gọi không truyền `account` sang một mailbox không xác định -- đúng cái mà
|
|
169
|
+
// "account lạ = lỗi, không rơi về primary" tồn tại để tránh.
|
|
170
|
+
const promoted = remaining.find((k) => k !== UNIDENTIFIED_ACCOUNT) ?? remaining[0];
|
|
171
|
+
const primary = blob.primary === key ? promoted : blob.primary;
|
|
172
|
+
await this.store.save({ version: 2, accounts, primary });
|
|
173
|
+
return { removed: true, newPrimary: primary };
|
|
174
|
+
}
|
|
175
|
+
async setPrimary(email) {
|
|
176
|
+
const blob = await this.load();
|
|
177
|
+
const key = normalize(email);
|
|
178
|
+
if (!blob?.accounts[key]) {
|
|
179
|
+
throw new Error(`Account ${key} is not configured.`);
|
|
180
|
+
}
|
|
181
|
+
await this.store.save({ ...blob, primary: key });
|
|
182
|
+
}
|
|
183
|
+
async clear() {
|
|
184
|
+
await this.store.clear();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
//# sourceMapping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AAC,UAAU,CAAC,KAAa;QAC5B,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,CAAA;QAC9B,MAAM,GAAG,GAAG,SAAS,CAAC,KAAK,CAAC,CAAA;QAC5B,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,WAAW,GAAG,qBAAqB,CAAC,CAAA;QACtD,CAAC;QACD,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,EAAwC,CAAC,CAAA;IACxF,CAAC;IAED,KAAK,CAAC,KAAK;QACT,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;IAC1B,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-store.test.d.ts","sourceRoot":"","sources":["../../../src/auth/account-store.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { PerPluginStore, setHomeDirForTesting } from '@n24q02m/hull/storage';
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
6
|
+
import { STORE_PLUGIN } from '../constants.js';
|
|
7
|
+
import { AccountStore, emailFromIdToken, isLegacyBlob, UNIDENTIFIED_ACCOUNT } from './account-store.js';
|
|
8
|
+
// id_token là JWT không cần chữ ký hợp lệ ở đây: chỉ payload được đọc.
|
|
9
|
+
function fakeIdToken(claims) {
|
|
10
|
+
const b64 = (o) => Buffer.from(JSON.stringify(o), 'utf8').toString('base64url');
|
|
11
|
+
return `${b64({ alg: 'none' })}.${b64(claims)}.sig`;
|
|
12
|
+
}
|
|
13
|
+
const REC = { access_token: 'at', refresh_token: 'rt', expiry_date: 4_000_000_000_000 };
|
|
14
|
+
describe('emailFromIdToken', () => {
|
|
15
|
+
it('reads the email claim', () => {
|
|
16
|
+
expect(emailFromIdToken(fakeIdToken({ sub: '1', email: 'A@Example.com' }))).toBe('a@example.com');
|
|
17
|
+
});
|
|
18
|
+
it('returns undefined for a token without an email claim', () => {
|
|
19
|
+
expect(emailFromIdToken(fakeIdToken({ sub: '1' }))).toBeUndefined();
|
|
20
|
+
});
|
|
21
|
+
it('returns undefined for undefined or malformed input', () => {
|
|
22
|
+
expect(emailFromIdToken(undefined)).toBeUndefined();
|
|
23
|
+
expect(emailFromIdToken('not-a-jwt')).toBeUndefined();
|
|
24
|
+
expect(emailFromIdToken('a.!!!.c')).toBeUndefined();
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
describe('isLegacyBlob', () => {
|
|
28
|
+
it('detects the flat M1 shape', () => {
|
|
29
|
+
expect(isLegacyBlob({ access_token: 'at' })).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
it('rejects the v2 shape and null', () => {
|
|
32
|
+
expect(isLegacyBlob({ version: 2, accounts: {}, primary: '' })).toBe(false);
|
|
33
|
+
expect(isLegacyBlob(null)).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
describe('AccountStore', () => {
|
|
37
|
+
let home;
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
home = mkdtempSync(join(tmpdir(), 'bws-accounts-test-'));
|
|
40
|
+
setHomeDirForTesting(home);
|
|
41
|
+
});
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
setHomeDirForTesting(null);
|
|
44
|
+
rmSync(home, { recursive: true, force: true });
|
|
45
|
+
});
|
|
46
|
+
it('returns null before anything is stored', async () => {
|
|
47
|
+
expect(await new AccountStore().load()).toBeNull();
|
|
48
|
+
expect(await new AccountStore().get()).toBeNull();
|
|
49
|
+
});
|
|
50
|
+
it('stores the first account and makes it primary automatically', async () => {
|
|
51
|
+
const store = new AccountStore();
|
|
52
|
+
await store.put('First@Example.com', REC);
|
|
53
|
+
expect(await store.list()).toEqual({ accounts: ['first@example.com'], primary: 'first@example.com' });
|
|
54
|
+
const got = await store.get();
|
|
55
|
+
expect(got?.email).toBe('first@example.com');
|
|
56
|
+
expect(got?.record.access_token).toBe('at');
|
|
57
|
+
});
|
|
58
|
+
it('keeps the existing primary when a second account is added without makePrimary', async () => {
|
|
59
|
+
const store = new AccountStore();
|
|
60
|
+
await store.put('one@example.com', REC);
|
|
61
|
+
await store.put('two@example.com', { ...REC, access_token: 'at2' });
|
|
62
|
+
expect((await store.list()).primary).toBe('one@example.com');
|
|
63
|
+
expect((await store.get('two@example.com'))?.record.access_token).toBe('at2');
|
|
64
|
+
});
|
|
65
|
+
it('switches primary when makePrimary is set', async () => {
|
|
66
|
+
const store = new AccountStore();
|
|
67
|
+
await store.put('one@example.com', REC);
|
|
68
|
+
await store.put('two@example.com', REC, { makePrimary: true });
|
|
69
|
+
expect((await store.list()).primary).toBe('two@example.com');
|
|
70
|
+
});
|
|
71
|
+
it('resolves an unknown account to null rather than falling back to primary', async () => {
|
|
72
|
+
const store = new AccountStore();
|
|
73
|
+
await store.put('one@example.com', REC);
|
|
74
|
+
expect(await store.get('nobody@example.com')).toBeNull();
|
|
75
|
+
});
|
|
76
|
+
it('removes an account and promotes a remaining one when the primary goes away', async () => {
|
|
77
|
+
const store = new AccountStore();
|
|
78
|
+
await store.put('one@example.com', REC);
|
|
79
|
+
await store.put('two@example.com', REC);
|
|
80
|
+
expect(await store.remove('one@example.com')).toEqual({ removed: true, newPrimary: 'two@example.com' });
|
|
81
|
+
expect((await store.list()).primary).toBe('two@example.com');
|
|
82
|
+
});
|
|
83
|
+
it('clears the blob when the last account is removed', async () => {
|
|
84
|
+
const store = new AccountStore();
|
|
85
|
+
await store.put('only@example.com', REC);
|
|
86
|
+
expect(await store.remove('only@example.com')).toEqual({ removed: true, newPrimary: null });
|
|
87
|
+
expect(await store.load()).toBeNull();
|
|
88
|
+
});
|
|
89
|
+
it('reports removed:false for an account that was never there', async () => {
|
|
90
|
+
const store = new AccountStore();
|
|
91
|
+
await store.put('one@example.com', REC);
|
|
92
|
+
expect(await store.remove('ghost@example.com')).toEqual({ removed: false, newPrimary: 'one@example.com' });
|
|
93
|
+
});
|
|
94
|
+
it('refuses to set primary to an unknown account', async () => {
|
|
95
|
+
const store = new AccountStore();
|
|
96
|
+
await store.put('one@example.com', REC);
|
|
97
|
+
await expect(store.setPrimary('ghost@example.com')).rejects.toThrow(/not configured/i);
|
|
98
|
+
});
|
|
99
|
+
it('migrates a flat M1 blob in place when it carries an id_token with an email', async () => {
|
|
100
|
+
await new PerPluginStore(STORE_PLUGIN).save({ ...REC, id_token: fakeIdToken({ email: 'legacy@example.com' }) });
|
|
101
|
+
const store = new AccountStore();
|
|
102
|
+
expect(await store.load()).toEqual({
|
|
103
|
+
version: 2,
|
|
104
|
+
accounts: { 'legacy@example.com': expect.objectContaining({ access_token: 'at' }) },
|
|
105
|
+
primary: 'legacy@example.com'
|
|
106
|
+
});
|
|
107
|
+
// migration được ghi lại xuống đĩa, không phải chỉ trong bộ nhớ
|
|
108
|
+
expect(isLegacyBlob(await new PerPluginStore(STORE_PLUGIN).load())).toBe(false);
|
|
109
|
+
});
|
|
110
|
+
it('leaves a flat blob untouched and exposes it via loadLegacy when the email cannot be derived', async () => {
|
|
111
|
+
await new PerPluginStore(STORE_PLUGIN).save({ ...REC });
|
|
112
|
+
const store = new AccountStore();
|
|
113
|
+
expect(await store.load()).toBeNull();
|
|
114
|
+
expect((await store.loadLegacy())?.access_token).toBe('at');
|
|
115
|
+
expect(isLegacyBlob(await new PerPluginStore(STORE_PLUGIN).load())).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
it('refuses to overwrite an unadopted legacy blob instead of losing its tokens', async () => {
|
|
118
|
+
// Blob phẳng M1 không có id_token -> load() trả null, nhưng dữ liệu vẫn ở đó.
|
|
119
|
+
await new PerPluginStore(STORE_PLUGIN).save({ access_token: 'legacy-at', refresh_token: 'legacy-rt' });
|
|
120
|
+
const store = new AccountStore();
|
|
121
|
+
await expect(store.put('new@example.com', REC)).rejects.toThrow(/older single-account layout/i);
|
|
122
|
+
// Token cũ còn nguyên, không bị ghi đè.
|
|
123
|
+
expect((await store.loadLegacy())?.access_token).toBe('legacy-at');
|
|
124
|
+
expect((await store.loadLegacy())?.refresh_token).toBe('legacy-rt');
|
|
125
|
+
});
|
|
126
|
+
describe('absorbLegacy', () => {
|
|
127
|
+
it('absorbs an unidentified legacy blob instead of deadlocking the next consent', async () => {
|
|
128
|
+
await new PerPluginStore(STORE_PLUGIN).save({ access_token: 'legacy-at', refresh_token: 'legacy-rt' });
|
|
129
|
+
const store = new AccountStore();
|
|
130
|
+
await store.put('new@example.com', REC, { absorbLegacy: true });
|
|
131
|
+
const { accounts, primary } = await store.list();
|
|
132
|
+
expect(accounts).toContain('new@example.com');
|
|
133
|
+
expect(accounts).toContain(UNIDENTIFIED_ACCOUNT);
|
|
134
|
+
expect(primary).toBe('new@example.com');
|
|
135
|
+
// token cũ vẫn còn, xoá được bằng account_remove
|
|
136
|
+
expect((await store.get(UNIDENTIFIED_ACCOUNT))?.record.refresh_token).toBe('legacy-rt');
|
|
137
|
+
});
|
|
138
|
+
it('still refuses when the caller did not just complete a consent', async () => {
|
|
139
|
+
await new PerPluginStore(STORE_PLUGIN).save({ access_token: 'legacy-at' });
|
|
140
|
+
const store = new AccountStore();
|
|
141
|
+
await expect(store.put('new@example.com', REC)).rejects.toThrow(/older single-account layout/i);
|
|
142
|
+
});
|
|
143
|
+
it('lets account_remove clear the absorbed blob, leaving the real account alone', async () => {
|
|
144
|
+
await new PerPluginStore(STORE_PLUGIN).save({ access_token: 'legacy-at', refresh_token: 'legacy-rt' });
|
|
145
|
+
const store = new AccountStore();
|
|
146
|
+
await store.put('new@example.com', REC, { absorbLegacy: true });
|
|
147
|
+
expect(await store.remove(UNIDENTIFIED_ACCOUNT)).toEqual({ removed: true, newPrimary: 'new@example.com' });
|
|
148
|
+
expect(await store.list()).toEqual({ accounts: ['new@example.com'], primary: 'new@example.com' });
|
|
149
|
+
});
|
|
150
|
+
it('promotes a real account over the unidentified blob when the primary is removed', async () => {
|
|
151
|
+
// (unidentified) là credential không biết của ai và thường đã hết hiệu lực.
|
|
152
|
+
// Để nó lên primary là âm thầm đưa mọi lời gọi không truyền `account` sang
|
|
153
|
+
// một mailbox không xác định.
|
|
154
|
+
await new PerPluginStore(STORE_PLUGIN).save({ access_token: 'legacy-at', refresh_token: 'legacy-rt' });
|
|
155
|
+
const store = new AccountStore();
|
|
156
|
+
await store.put('first@example.com', REC, { absorbLegacy: true });
|
|
157
|
+
await store.put('second@example.com', REC);
|
|
158
|
+
expect(await store.remove('first@example.com')).toEqual({ removed: true, newPrimary: 'second@example.com' });
|
|
159
|
+
expect((await store.list()).primary).toBe('second@example.com');
|
|
160
|
+
});
|
|
161
|
+
it('falls back to the unidentified blob only when nothing else is left', async () => {
|
|
162
|
+
await new PerPluginStore(STORE_PLUGIN).save({ access_token: 'legacy-at', refresh_token: 'legacy-rt' });
|
|
163
|
+
const store = new AccountStore();
|
|
164
|
+
await store.put('only@example.com', REC, { absorbLegacy: true });
|
|
165
|
+
expect(await store.remove('only@example.com')).toEqual({ removed: true, newPrimary: UNIDENTIFIED_ACCOUNT });
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
describe('adoptLegacy', () => {
|
|
169
|
+
it('turns the flat blob into a v2 blob under the given email, keeping its tokens', async () => {
|
|
170
|
+
await new PerPluginStore(STORE_PLUGIN).save({ access_token: 'legacy-at', refresh_token: 'legacy-rt' });
|
|
171
|
+
const store = new AccountStore();
|
|
172
|
+
expect(await store.adoptLegacy('Adopted@Example.com')).toMatchObject({ access_token: 'legacy-at' });
|
|
173
|
+
expect(await store.load()).toEqual({
|
|
174
|
+
version: 2,
|
|
175
|
+
accounts: {
|
|
176
|
+
'adopted@example.com': expect.objectContaining({ access_token: 'legacy-at', refresh_token: 'legacy-rt' })
|
|
177
|
+
},
|
|
178
|
+
primary: 'adopted@example.com'
|
|
179
|
+
});
|
|
180
|
+
expect(isLegacyBlob(await new PerPluginStore(STORE_PLUGIN).load())).toBe(false);
|
|
181
|
+
expect(await store.loadLegacy()).toBeNull();
|
|
182
|
+
});
|
|
183
|
+
it('unblocks put() -- the guard only fires while the legacy blob is unadopted', async () => {
|
|
184
|
+
await new PerPluginStore(STORE_PLUGIN).save({ access_token: 'legacy-at', refresh_token: 'legacy-rt' });
|
|
185
|
+
const store = new AccountStore();
|
|
186
|
+
await store.adoptLegacy('adopted@example.com');
|
|
187
|
+
await store.put('new@example.com', REC);
|
|
188
|
+
expect((await store.list()).accounts).toEqual(['adopted@example.com', 'new@example.com']);
|
|
189
|
+
// adoption giữ ngôi primary, thêm account mới không cướp nó
|
|
190
|
+
expect((await store.list()).primary).toBe('adopted@example.com');
|
|
191
|
+
});
|
|
192
|
+
it('returns null when there is no flat blob to adopt', async () => {
|
|
193
|
+
const store = new AccountStore();
|
|
194
|
+
expect(await store.adoptLegacy('nobody@example.com')).toBeNull();
|
|
195
|
+
await store.put('one@example.com', REC);
|
|
196
|
+
expect(await store.adoptLegacy('one@example.com')).toBeNull();
|
|
197
|
+
// blob v2 đang có không bị adoptLegacy làm hỏng
|
|
198
|
+
expect((await store.list()).accounts).toEqual(['one@example.com']);
|
|
199
|
+
});
|
|
200
|
+
it('is a no-op the second time, leaving every account of the v2 blob in place', async () => {
|
|
201
|
+
// Bảo hiểm cho tuyên bố "gọi lần hai là no-op" trong doc-comment: nếu
|
|
202
|
+
// adoptLegacy ghi vô điều kiện thì nó thu blob v2 về đúng MỘT account và
|
|
203
|
+
// xoá sạch những account thêm sau lần adopt -- mất token thật.
|
|
204
|
+
await new PerPluginStore(STORE_PLUGIN).save({ access_token: 'legacy-at', refresh_token: 'legacy-rt' });
|
|
205
|
+
const store = new AccountStore();
|
|
206
|
+
await store.adoptLegacy('adopted@example.com');
|
|
207
|
+
await store.put('second@example.com', { ...REC, access_token: 'at2' });
|
|
208
|
+
expect(await store.adoptLegacy('adopted@example.com')).toBeNull();
|
|
209
|
+
expect(await store.list()).toEqual({
|
|
210
|
+
accounts: ['adopted@example.com', 'second@example.com'],
|
|
211
|
+
primary: 'adopted@example.com'
|
|
212
|
+
});
|
|
213
|
+
expect((await store.get('second@example.com'))?.record.access_token).toBe('at2');
|
|
214
|
+
expect((await store.get('adopted@example.com'))?.record.refresh_token).toBe('legacy-rt');
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
describe('per-subject keying', () => {
|
|
218
|
+
// Bucket theo sub cần CREDENTIAL_SECRET: PerPluginStore derive khoá mã hoá
|
|
219
|
+
// qua `deriveMultiUserKey` (per-plugin-store.ts:85) và NÉM khi thiếu biến
|
|
220
|
+
// đó, khác hẳn nhánh sub=null vốn tự sinh khoá máy trên đĩa. Đặt ở đây chứ
|
|
221
|
+
// không ở beforeEach ngoài, để những test stdio phía trên vẫn chạy đúng cái
|
|
222
|
+
// môi trường chúng vẫn chạy.
|
|
223
|
+
const originalSecret = process.env.CREDENTIAL_SECRET;
|
|
224
|
+
beforeEach(() => {
|
|
225
|
+
process.env.CREDENTIAL_SECRET = 'test-credential-secret-at-least-32-chars';
|
|
226
|
+
});
|
|
227
|
+
afterEach(() => {
|
|
228
|
+
if (originalSecret === undefined)
|
|
229
|
+
delete process.env.CREDENTIAL_SECRET;
|
|
230
|
+
else
|
|
231
|
+
process.env.CREDENTIAL_SECRET = originalSecret;
|
|
232
|
+
});
|
|
233
|
+
it('keeps two subjects in separate credential buckets', async () => {
|
|
234
|
+
const alice = new AccountStore('sub-alice');
|
|
235
|
+
const bob = new AccountStore('sub-bob');
|
|
236
|
+
await alice.put('shared@example.com', { ...REC, access_token: 'alice-token' });
|
|
237
|
+
await bob.put('shared@example.com', { ...REC, access_token: 'bob-token' });
|
|
238
|
+
expect((await alice.get('shared@example.com'))?.record.access_token).toBe('alice-token');
|
|
239
|
+
expect((await bob.get('shared@example.com'))?.record.access_token).toBe('bob-token');
|
|
240
|
+
});
|
|
241
|
+
it('does not let one subject see another subject accounts', async () => {
|
|
242
|
+
const alice = new AccountStore('sub-alice');
|
|
243
|
+
const bob = new AccountStore('sub-bob');
|
|
244
|
+
await alice.put('only-alice@example.com', REC);
|
|
245
|
+
expect((await bob.list()).accounts).toEqual([]);
|
|
246
|
+
expect(await bob.get('only-alice@example.com')).toBeNull();
|
|
247
|
+
});
|
|
248
|
+
it('defaults to the single-user bucket when no sub is given (stdio behavior unchanged)', async () => {
|
|
249
|
+
const stdio = new AccountStore();
|
|
250
|
+
await stdio.put('local@example.com', REC);
|
|
251
|
+
expect((await new AccountStore().list()).accounts).toEqual(['local@example.com']);
|
|
252
|
+
// và bucket đó KHÔNG lẫn với bucket của một sub
|
|
253
|
+
expect((await new AccountStore('sub-x').list()).accounts).toEqual([]);
|
|
254
|
+
});
|
|
255
|
+
it('rejects a sub that would escape the per-subject directory', async () => {
|
|
256
|
+
// credPath() của hull validate sub (per-plugin-store.ts:34-40) và ném
|
|
257
|
+
// ngay trong constructor. Ghi lại ở đây vì `getAuth()` dựng AccountStore từ
|
|
258
|
+
// một giá trị lấy từ JWT: đường ném đó là đường bảo vệ, không phải tai nạn.
|
|
259
|
+
expect(() => new AccountStore('../escape')).toThrow(/Invalid sub/i);
|
|
260
|
+
expect(() => new AccountStore('has/slash')).toThrow(/Invalid sub/i);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
//# sourceMappingURL=account-store.test.js.map
|