dexie-cloud-addon 4.2.5 → 4.3.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.
Files changed (168) hide show
  1. package/TODO-SOCIALAUTH.md +545 -0
  2. package/dist/modern/DexieCloudAPI.d.ts +4 -0
  3. package/dist/modern/DexieCloudOptions.d.ts +20 -0
  4. package/dist/modern/authentication/exchangeOAuthCode.d.ts +23 -0
  5. package/dist/modern/authentication/fetchAuthProviders.d.ts +14 -0
  6. package/dist/modern/authentication/handleOAuthCallback.d.ts +57 -0
  7. package/dist/modern/authentication/interactWithUser.d.ts +19 -0
  8. package/dist/modern/authentication/oauthLogin.d.ts +37 -0
  9. package/dist/modern/default-ui/AuthProviderButton.d.ts +21 -0
  10. package/dist/modern/default-ui/LoginDialog.d.ts +5 -2
  11. package/dist/modern/default-ui/ProviderSelectionDialog.d.ts +7 -0
  12. package/dist/modern/dexie-cloud-addon.js +577 -5
  13. package/dist/modern/dexie-cloud-addon.js.map +1 -1
  14. package/dist/modern/dexie-cloud-addon.min.js +1 -1
  15. package/dist/modern/dexie-cloud-addon.min.js.gz +0 -0
  16. package/dist/modern/dexie-cloud-addon.min.js.map +1 -1
  17. package/dist/modern/errors/OAuthError.d.ts +10 -0
  18. package/dist/modern/service-worker.js +577 -5
  19. package/dist/modern/service-worker.js.map +1 -1
  20. package/dist/modern/service-worker.min.js +1 -1
  21. package/dist/modern/service-worker.min.js.map +1 -1
  22. package/dist/modern/types/DXCUserInteraction.d.ts +24 -1
  23. package/dist/umd/DISABLE_SERVICEWORKER_STRATEGY.d.ts +1 -0
  24. package/dist/umd/DXCWebSocketStatus.d.ts +1 -0
  25. package/dist/umd/DexieCloudAPI.d.ts +75 -0
  26. package/dist/umd/DexieCloudOptions.d.ts +27 -0
  27. package/dist/umd/DexieCloudSyncOptions.d.ts +4 -0
  28. package/dist/umd/DexieCloudTable.d.ts +18 -0
  29. package/dist/umd/InvalidLicenseError.d.ts +5 -0
  30. package/dist/umd/Invite.d.ts +8 -0
  31. package/dist/umd/PermissionChecker.d.ts +15 -0
  32. package/dist/umd/TSON.d.ts +17 -0
  33. package/dist/umd/WSObservable.d.ts +72 -0
  34. package/dist/umd/associate.d.ts +1 -0
  35. package/dist/umd/authentication/AuthPersistedContext.d.ts +9 -0
  36. package/dist/umd/authentication/TokenErrorResponseError.d.ts +10 -0
  37. package/dist/umd/authentication/TokenExpiredError.d.ts +3 -0
  38. package/dist/umd/authentication/UNAUTHORIZED_USER.d.ts +2 -0
  39. package/dist/umd/authentication/authenticate.d.ts +13 -0
  40. package/dist/umd/authentication/currentUserObservable.d.ts +1 -0
  41. package/dist/umd/authentication/interactWithUser.d.ts +21 -0
  42. package/dist/umd/authentication/login.d.ts +3 -0
  43. package/dist/umd/authentication/logout.d.ts +5 -0
  44. package/dist/umd/authentication/otpFetchTokenCallback.d.ts +3 -0
  45. package/dist/umd/authentication/setCurrentUser.d.ts +14 -0
  46. package/dist/umd/authentication/waitUntil.d.ts +3 -0
  47. package/dist/umd/computeSyncState.d.ts +4 -0
  48. package/dist/umd/createSharedValueObservable.d.ts +3 -0
  49. package/dist/umd/currentUserEmitter.d.ts +3 -0
  50. package/dist/umd/db/DexieCloudDB.d.ts +61 -0
  51. package/dist/umd/db/entities/BaseRevisionMapEntry.d.ts +5 -0
  52. package/dist/umd/db/entities/EntityCommon.d.ts +5 -0
  53. package/dist/umd/db/entities/GuardedJob.d.ts +5 -0
  54. package/dist/umd/db/entities/Member.d.ts +19 -0
  55. package/dist/umd/db/entities/PersistedSyncState.d.ts +22 -0
  56. package/dist/umd/db/entities/Realm.d.ts +14 -0
  57. package/dist/umd/db/entities/Role.d.ts +11 -0
  58. package/dist/umd/db/entities/UserLogin.d.ts +23 -0
  59. package/dist/umd/default-ui/Dialog.d.ts +5 -0
  60. package/dist/umd/default-ui/LoginDialog.d.ts +3 -0
  61. package/dist/umd/default-ui/Styles.d.ts +3 -0
  62. package/dist/umd/default-ui/index.d.ts +24 -0
  63. package/dist/umd/define-ydoc-trigger.d.ts +3 -0
  64. package/dist/umd/dexie-cloud-addon.d.ts +3 -0
  65. package/dist/umd/dexie-cloud-addon.js +578 -6
  66. package/dist/umd/dexie-cloud-addon.js.gz +0 -0
  67. package/dist/umd/dexie-cloud-addon.js.map +1 -1
  68. package/dist/umd/dexie-cloud-addon.min.js +1 -1
  69. package/dist/umd/dexie-cloud-addon.min.js.gz +0 -0
  70. package/dist/umd/dexie-cloud-addon.min.js.map +1 -1
  71. package/dist/umd/dexie-cloud-client.d.ts +23 -0
  72. package/dist/umd/errors/HttpError.d.ts +5 -0
  73. package/dist/umd/extend-dexie-interface.d.ts +23 -0
  74. package/dist/umd/getGlobalRolesObservable.d.ts +5 -0
  75. package/dist/umd/getInternalAccessControlObservable.d.ts +12 -0
  76. package/dist/umd/getInvitesObservable.d.ts +23 -0
  77. package/dist/umd/getPermissionsLookupObservable.d.ts +16 -0
  78. package/dist/umd/getTiedRealmId.d.ts +2 -0
  79. package/dist/umd/helpers/BroadcastedAndLocalEvent.d.ts +8 -0
  80. package/dist/umd/helpers/CancelToken.d.ts +4 -0
  81. package/dist/umd/helpers/IS_SERVICE_WORKER.d.ts +1 -0
  82. package/dist/umd/helpers/SWBroadcastChannel.d.ts +12 -0
  83. package/dist/umd/helpers/allSettled.d.ts +1 -0
  84. package/dist/umd/helpers/bulkUpdate.d.ts +4 -0
  85. package/dist/umd/helpers/computeRealmSetHash.d.ts +2 -0
  86. package/dist/umd/helpers/date-constants.d.ts +5 -0
  87. package/dist/umd/helpers/flatten.d.ts +1 -0
  88. package/dist/umd/helpers/getMutationTable.d.ts +1 -0
  89. package/dist/umd/helpers/getSyncableTables.d.ts +4 -0
  90. package/dist/umd/helpers/getTableFromMutationTable.d.ts +1 -0
  91. package/dist/umd/helpers/makeArray.d.ts +1 -0
  92. package/dist/umd/helpers/randomString.d.ts +1 -0
  93. package/dist/umd/helpers/resolveText.d.ts +16 -0
  94. package/dist/umd/helpers/throwVersionIncrementNeeded.d.ts +1 -0
  95. package/dist/umd/helpers/visibilityState.d.ts +1 -0
  96. package/dist/umd/isEagerSyncDisabled.d.ts +2 -0
  97. package/dist/umd/isFirefox.d.ts +1 -0
  98. package/dist/umd/isSafari.d.ts +2 -0
  99. package/dist/umd/mapValueObservable.d.ts +5 -0
  100. package/dist/umd/mergePermissions.d.ts +2 -0
  101. package/dist/umd/middleware-helpers/guardedTable.d.ts +11 -0
  102. package/dist/umd/middleware-helpers/idGenerationHelpers.d.ts +18 -0
  103. package/dist/umd/middlewares/createIdGenerationMiddleware.d.ts +3 -0
  104. package/dist/umd/middlewares/createImplicitPropSetterMiddleware.d.ts +3 -0
  105. package/dist/umd/middlewares/createMutationTrackingMiddleware.d.ts +17 -0
  106. package/dist/umd/middlewares/outstandingTransaction.d.ts +4 -0
  107. package/dist/umd/overrideParseStoresSpec.d.ts +4 -0
  108. package/dist/umd/performInitialSync.d.ts +4 -0
  109. package/dist/umd/permissions.d.ts +9 -0
  110. package/dist/umd/prodLog.d.ts +9 -0
  111. package/dist/umd/service-worker.d.ts +1 -0
  112. package/dist/umd/service-worker.js +578 -6
  113. package/dist/umd/service-worker.js.map +1 -1
  114. package/dist/umd/service-worker.min.js +1 -1
  115. package/dist/umd/service-worker.min.js.map +1 -1
  116. package/dist/umd/sync/DEXIE_CLOUD_SYNCER_ID.d.ts +1 -0
  117. package/dist/umd/sync/LocalSyncWorker.d.ts +7 -0
  118. package/dist/umd/sync/SyncRequiredError.d.ts +3 -0
  119. package/dist/umd/sync/applyServerChanges.d.ts +3 -0
  120. package/dist/umd/sync/connectWebSocket.d.ts +2 -0
  121. package/dist/umd/sync/encodeIdsForServer.d.ts +4 -0
  122. package/dist/umd/sync/extractRealm.d.ts +2 -0
  123. package/dist/umd/sync/getLatestRevisionsPerTable.d.ts +6 -0
  124. package/dist/umd/sync/getTablesToSyncify.d.ts +3 -0
  125. package/dist/umd/sync/isOnline.d.ts +1 -0
  126. package/dist/umd/sync/isSyncNeeded.d.ts +2 -0
  127. package/dist/umd/sync/listClientChanges.d.ts +9 -0
  128. package/dist/umd/sync/listSyncifiedChanges.d.ts +5 -0
  129. package/dist/umd/sync/messageConsumerIsReady.d.ts +2 -0
  130. package/dist/umd/sync/messagesFromServerQueue.d.ts +8 -0
  131. package/dist/umd/sync/modifyLocalObjectsWithNewUserId.d.ts +4 -0
  132. package/dist/umd/sync/myId.d.ts +1 -0
  133. package/dist/umd/sync/numUnsyncedMutations.d.ts +2 -0
  134. package/dist/umd/sync/old_startSyncingClientChanges.d.ts +39 -0
  135. package/dist/umd/sync/performGuardedJob.d.ts +2 -0
  136. package/dist/umd/sync/ratelimit.d.ts +3 -0
  137. package/dist/umd/sync/registerSyncEvent.d.ts +3 -0
  138. package/dist/umd/sync/sync.d.ts +15 -0
  139. package/dist/umd/sync/syncIfPossible.d.ts +5 -0
  140. package/dist/umd/sync/syncWithServer.d.ts +6 -0
  141. package/dist/umd/sync/triggerSync.d.ts +2 -0
  142. package/dist/umd/sync/updateBaseRevs.d.ts +5 -0
  143. package/dist/umd/types/DXCAlert.d.ts +25 -0
  144. package/dist/umd/types/DXCInputField.d.ts +11 -0
  145. package/dist/umd/types/DXCUserInteraction.d.ts +93 -0
  146. package/dist/umd/types/NewIdOptions.d.ts +3 -0
  147. package/dist/umd/types/SWMessageEvent.d.ts +3 -0
  148. package/dist/umd/types/SWSyncEvent.d.ts +4 -0
  149. package/dist/umd/types/SyncState.d.ts +9 -0
  150. package/dist/umd/types/TXExpandos.d.ts +11 -0
  151. package/dist/umd/updateSchemaFromOptions.d.ts +3 -0
  152. package/dist/umd/userIsActive.d.ts +7 -0
  153. package/dist/umd/verifyConfig.d.ts +2 -0
  154. package/dist/umd/verifySchema.d.ts +2 -0
  155. package/dist/umd/yjs/YDexieCloudSyncState.d.ts +3 -0
  156. package/dist/umd/yjs/YTable.d.ts +3 -0
  157. package/dist/umd/yjs/applyYMessages.d.ts +9 -0
  158. package/dist/umd/yjs/awareness.d.ts +3 -0
  159. package/dist/umd/yjs/createYClientUpdateObservable.d.ts +4 -0
  160. package/dist/umd/yjs/createYHandler.d.ts +2 -0
  161. package/dist/umd/yjs/downloadYDocsFromServer.d.ts +3 -0
  162. package/dist/umd/yjs/getUpdatesTable.d.ts +3 -0
  163. package/dist/umd/yjs/listUpdatesSince.d.ts +3 -0
  164. package/dist/umd/yjs/listYClientMessagesAndStateVector.d.ts +26 -0
  165. package/dist/umd/yjs/reopenDocSignal.d.ts +10 -0
  166. package/dist/umd/yjs/updateYSyncStates.d.ts +6 -0
  167. package/oauth_flow.md +299 -0
  168. package/package.json +3 -3
package/oauth_flow.md ADDED
@@ -0,0 +1,299 @@
1
+ # OAuth Authorization Code Flow for Dexie Cloud SPA Integration
2
+
3
+ ## Actors
4
+
5
+ - **SPA** – Customer's frontend application
6
+ - **Dexie Cloud** – Auth broker + database access control
7
+ - **OAuth Provider** – Google, GitHub, Apple, Microsoft, etc.
8
+ - **Popup Window** – Browser window initiated by the SPA
9
+
10
+ ## Preconditions
11
+
12
+ The SPA:
13
+
14
+ - Generates a persistent public/private keypair
15
+ - Private key stored securely in IndexedDB
16
+ - Public key sent later during token exchange
17
+ - Needs two JWTs after login:
18
+ - Access Token (short-lived)
19
+ - Refresh Token (long-lived)
20
+
21
+ Dexie Cloud acts as OAuth broker and manages tenant + identity linkage.
22
+
23
+ ---
24
+
25
+ ## Flow Overview
26
+
27
+ ### 1. User Initiates Login
28
+
29
+ User clicks "Login", SPA displays list of providers:
30
+
31
+ ```
32
+ Google | GitHub | Apple | Microsoft
33
+ ```
34
+
35
+ No nonce or PKCE is created yet.
36
+
37
+ ---
38
+
39
+ ### 2. User Selects Provider
40
+
41
+ Example: User selects **Google**
42
+
43
+ The client initiates the OAuth flow. There are three ways to do this:
44
+
45
+ #### 2a. Popup Flow (Recommended for Web SPAs)
46
+
47
+ ```js
48
+ popup = window.open('about:blank');
49
+ popup.location = `https://<db>.dexie.cloud/oauth/login/google?redirect_uri=${encodeURIComponent(window.location.origin)}`;
50
+ ```
51
+
52
+ The `redirect_uri` parameter is used to determine the `targetOrigin` for postMessage.
53
+
54
+ #### 2b. Custom URL Scheme (Capacitor / Native Apps)
55
+
56
+ ```js
57
+ // Open in system browser or in-app browser
58
+ Browser.open({
59
+ url: `https://<db>.dexie.cloud/oauth/login/google?redirect_uri=${encodeURIComponent('myapp://oauth-callback')}`
60
+ });
61
+ ```
62
+
63
+ The custom scheme `myapp://` tells Dexie Cloud to redirect back via deep link.
64
+
65
+ #### 2c. Full Page Redirect (Web without Popup)
66
+
67
+ ```js
68
+ window.location.href = `https://<db>.dexie.cloud/oauth/login/google?redirect_uri=${encodeURIComponent('https://myapp.com/oauth-callback')}`;
69
+ ```
70
+
71
+ Used when popups are blocked or for a more traditional OAuth redirect flow.
72
+
73
+ ---
74
+
75
+ ### 3. Dexie Cloud Prepares OAuth
76
+
77
+ Dexie Cloud receives `/oauth/login/google` and generates:
78
+
79
+ - `state` (anti-CSRF)
80
+ - `code_verifier` (PKCE)
81
+ - `code_challenge` (PKCE)
82
+
83
+ Stores these in the challenges table, then redirects popup to provider:
84
+
85
+ ```
86
+ https://accounts.google.com/o/oauth2/v2/auth?
87
+ client_id=...
88
+ redirect_uri=https://<db>.dexie.cloud/oauth/callback/google
89
+ state=STATE
90
+ code_challenge=CHALLENGE
91
+ code_challenge_method=S256
92
+ response_type=code
93
+ scope=openid email profile
94
+ ```
95
+
96
+ Note: `redirect_uri` points to the **Dexie Cloud server** callback endpoint.
97
+
98
+ ---
99
+
100
+ ### 4. Provider Authenticates User
101
+
102
+ Provider authenticates the user and requests consent if needed.
103
+
104
+ ---
105
+
106
+ ### 5. Provider Callback to Dexie Cloud
107
+
108
+ Provider redirects back to popup:
109
+
110
+ ```
111
+ https://<db>.dexie.cloud/oauth/callback/google?code=CODE&state=STATE
112
+ ```
113
+
114
+ Dexie Cloud:
115
+
116
+ 1. Verifies `state`
117
+ 2. Performs token exchange with provider using PKCE
118
+ 3. Extracts identity claims (email/id/name/…)
119
+ 4. Verifies email is verified
120
+ 5. Links identity to tenant/database
121
+ 6. Generates a **single-use Dexie Cloud authorization code**
122
+ 7. Deletes the OAuth state (one-time use)
123
+
124
+ ---
125
+
126
+ ### 6. Dexie Cloud Delivers Auth Code to Client
127
+
128
+ Dexie Cloud returns HTML that delivers the authorization code back to the client.
129
+ The delivery method depends on how the client initiated the login:
130
+
131
+ #### 6a. Popup Flow (Web SPAs)
132
+
133
+ If `window.opener` exists, uses postMessage:
134
+
135
+ ```js
136
+ window.opener.postMessage(
137
+ {
138
+ type: 'dexie:oauthResult',
139
+ code: DEXIE_AUTH_CODE,
140
+ provider: 'google',
141
+ state: STATE
142
+ },
143
+ targetOrigin // Origin captured from redirect_uri or referer
144
+ );
145
+
146
+ window.close();
147
+ ```
148
+
149
+ #### 6b. Custom URL Scheme (Capacitor / Native Apps)
150
+
151
+ If the client passed a `redirect_uri` with a custom scheme (e.g., `myapp://oauth-callback`),
152
+ the callback page redirects to that URL:
153
+
154
+ ```js
155
+ window.location.href =
156
+ 'myapp://oauth-callback?code=DEXIE_AUTH_CODE&provider=google&state=STATE';
157
+ ```
158
+
159
+ The native app intercepts this deep link and extracts the parameters.
160
+
161
+ #### 6c. Full Page Redirect (Web without Popup)
162
+
163
+ If there's no `window.opener` but the client passed an http/https `redirect_uri`,
164
+ the callback page redirects back to the client URL:
165
+
166
+ ```js
167
+ window.location.href =
168
+ 'https://myapp.com/oauth-callback?code=DEXIE_AUTH_CODE&provider=google&state=STATE';
169
+ ```
170
+
171
+ The client page at that URL handles the auth code from query parameters.
172
+
173
+ #### 6d. Error Case
174
+
175
+ If none of the above conditions are met (no opener, no redirect_uri), an error is displayed
176
+ explaining that the auth flow cannot complete.
177
+
178
+ ---
179
+
180
+ ### 7. Client Receives Authorization Code
181
+
182
+ **For Popup Flow (6a):**
183
+
184
+ SPA listens for postMessage and verifies:
185
+
186
+ - `type === "dexie:oauthResult"`
187
+ - origin (implicit via postMessage)
188
+ - provider
189
+ - state (optional)
190
+ - popup lifecycle
191
+
192
+ **For Capacitor/Native Apps (6b):**
193
+
194
+ App registers a deep link handler for the custom URL scheme:
195
+
196
+ ```js
197
+ // Capacitor example
198
+ App.addListener('appUrlOpen', ({ url }) => {
199
+ const params = new URL(url).searchParams;
200
+ const code = params.get('code');
201
+ const provider = params.get('provider');
202
+ const state = params.get('state');
203
+ // Proceed to token exchange
204
+ });
205
+ ```
206
+
207
+ **For Full Page Redirect (6c):**
208
+
209
+ Client page reads parameters from the URL:
210
+
211
+ ```js
212
+ const params = new URLSearchParams(window.location.search);
213
+ const code = params.get('code');
214
+ const provider = params.get('provider');
215
+ const state = params.get('state');
216
+ // Proceed to token exchange
217
+ ```
218
+
219
+ Upon success, client proceeds to token exchange.
220
+
221
+ ---
222
+
223
+ ### 8. Client Performs Token Exchange
224
+
225
+ Client sends:
226
+
227
+ ```http
228
+ POST /token
229
+ Content-Type: application/json
230
+ ```
231
+
232
+ Payload:
233
+
234
+ ```json
235
+ {
236
+ "grant_type": "authorization_code",
237
+ "code": "<DEXIE_AUTH_CODE>",
238
+ "public_key": "<SPA_PUBLIC_KEY>",
239
+ "scopes": ["ACCESS_DB"]
240
+ }
241
+ ```
242
+
243
+ Dexie Cloud validates:
244
+
245
+ - Dexie authorization code integrity
246
+ - TTL (5 minutes)
247
+ - Single-use constraint
248
+ - Database context
249
+ - User identity and claims from stored data
250
+ - Subscription/license status
251
+
252
+ ---
253
+
254
+ ### 9. Dexie Cloud Issues Tokens
255
+
256
+ Dexie Cloud responds with:
257
+
258
+ ```json
259
+ {
260
+ "access_token": "...",
261
+ "refresh_token": "...",
262
+ "expires_in": 3600,
263
+ "token_type": "Bearer"
264
+ }
265
+ ```
266
+
267
+ This completes authentication.
268
+
269
+ ---
270
+
271
+ ## Security Properties Achieved
272
+
273
+ - 🛑 No JWTs exposed via popup or URL
274
+ - 🛑 No refresh tokens in postMessage
275
+ - 🛑 Provider tokens never reach SPA (only Dexie tokens)
276
+ - 🛡 Single-use Dexie authorization code (5 min TTL)
277
+ - 🛡 PKCE prevents provider code interception
278
+ - 🛡 State stored server-side with TTL (30 min)
279
+ - 🛡 CSRF protection via `state` parameter
280
+ - 🛡 OAuth state deleted after use
281
+ - 🛡 Dexie auth code deleted after use
282
+ - 🛡 Email verification enforced by server
283
+ - 🛡 All provider exchanges happen server-side
284
+ - 🛡 CORS + origin protections during `/token` exchange
285
+ - 🛡 Future PoP (Proof-of-Possession) enabled via SPA public key
286
+ - 🛡 Works with Apple, Google, Microsoft, GitHub
287
+
288
+ ---
289
+
290
+ ## Resulting Benefits
291
+
292
+ - Works for SPA / PWA / Capacitor / WebViews
293
+ - Supports multi-tenant architectures
294
+ - Supports native account linking
295
+ - Enables refresh token rotation
296
+ - Supports offline-first/local-first model
297
+
298
+ This aligns with modern OIDC/OAuth best practices (2023+) and matches architectures used by:
299
+ Auth0, Firebase, Supabase, Okta, MSAL, Google Identity Services, Clerk, etc.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dexie-cloud-addon",
3
- "version": "4.2.5",
3
+ "version": "4.3.0",
4
4
  "description": "Dexie addon that syncs with to Dexie Cloud",
5
5
  "type": "module",
6
6
  "module": "dist/modern/dexie-cloud-addon.min.js",
@@ -94,8 +94,8 @@
94
94
  "rxjs": "^7.x",
95
95
  "yjs": "^13.6.27",
96
96
  "y-protocols": "^1.0.6",
97
- "dexie-cloud-common": "^1.0.57",
98
- "y-dexie": "^4.2.2"
97
+ "y-dexie": "^4.2.2",
98
+ "dexie-cloud-common": "^1.0.57"
99
99
  },
100
100
  "peerDependencies": {
101
101
  "dexie": "^4.3.0"