acebase-server 1.11.0 → 1.13.1

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 (342) hide show
  1. package/.vscode/launch.json +24 -0
  2. package/dist/cjs/index.d.ts +1 -0
  3. package/dist/cjs/logger.js +74 -0
  4. package/dist/cjs/middleware/admin-only.js +29 -0
  5. package/dist/cjs/middleware/user.js +4 -4
  6. package/dist/cjs/routes/auth-change-password.js +7 -4
  7. package/dist/cjs/routes/auth-delete.js +9 -7
  8. package/dist/cjs/routes/auth-forgot-password.js +9 -6
  9. package/dist/cjs/routes/auth-reset-password.js +5 -3
  10. package/dist/cjs/routes/auth-signout.js +6 -4
  11. package/dist/cjs/routes/auth-signup.js +17 -15
  12. package/dist/cjs/routes/auth-update.js +9 -6
  13. package/dist/cjs/routes/auth-verify-email.js +6 -3
  14. package/dist/cjs/routes/data-index-create.js +3 -5
  15. package/dist/cjs/routes/data-index-delete.js +2 -4
  16. package/dist/cjs/routes/data-index-list.js +2 -4
  17. package/dist/cjs/routes/data-reflect.js +2 -4
  18. package/dist/cjs/routes/data-schema-get.js +2 -4
  19. package/dist/cjs/routes/data-schema-set.js +2 -4
  20. package/dist/cjs/routes/data-schema-test.js +2 -4
  21. package/dist/cjs/routes/data-schemas-list.js +2 -4
  22. package/dist/cjs/routes/data-set.js +8 -7
  23. package/dist/cjs/routes/data-transaction.js +15 -8
  24. package/dist/cjs/routes/data-update.js +8 -6
  25. package/dist/cjs/routes/meta-logs.js +50 -0
  26. package/dist/cjs/routes/meta.js +3 -0
  27. package/dist/cjs/routes/oauth2-signin.js +9 -6
  28. package/dist/cjs/server.js +3 -1
  29. package/dist/cjs/shared/rootpath.js +2 -0
  30. package/dist/cjs/shared/signin.js +8 -3
  31. package/dist/cjs/start.js +3 -0
  32. package/dist/cjs/websocket/index.js +42 -23
  33. package/dist/esm/index.d.ts +1 -0
  34. package/dist/esm/logger.js +52 -0
  35. package/dist/esm/middleware/admin-only.js +25 -0
  36. package/dist/esm/middleware/user.js +4 -4
  37. package/dist/esm/routes/auth-change-password.js +6 -3
  38. package/dist/esm/routes/auth-delete.js +7 -5
  39. package/dist/esm/routes/auth-forgot-password.js +8 -6
  40. package/dist/esm/routes/auth-reset-password.js +4 -2
  41. package/dist/esm/routes/auth-signout.js +4 -2
  42. package/dist/esm/routes/auth-signup.js +12 -10
  43. package/dist/esm/routes/auth-update.js +8 -6
  44. package/dist/esm/routes/auth-verify-email.js +6 -2
  45. package/dist/esm/routes/data-index-create.js +4 -6
  46. package/dist/esm/routes/data-index-delete.js +3 -5
  47. package/dist/esm/routes/data-index-list.js +3 -5
  48. package/dist/esm/routes/data-reflect.js +1 -4
  49. package/dist/esm/routes/data-schema-get.js +3 -5
  50. package/dist/esm/routes/data-schema-set.js +3 -5
  51. package/dist/esm/routes/data-schema-test.js +3 -5
  52. package/dist/esm/routes/data-schemas-list.js +3 -5
  53. package/dist/esm/routes/data-set.js +6 -5
  54. package/dist/esm/routes/data-transaction.js +12 -5
  55. package/dist/esm/routes/data-update.js +6 -4
  56. package/dist/esm/routes/meta-logs.js +36 -0
  57. package/dist/esm/routes/meta.js +3 -0
  58. package/dist/esm/routes/oauth2-signin.js +8 -5
  59. package/dist/esm/server.js +3 -1
  60. package/dist/esm/shared/rootpath.js +2 -0
  61. package/dist/esm/shared/signin.js +7 -3
  62. package/dist/esm/start.js +3 -0
  63. package/dist/esm/websocket/index.js +37 -22
  64. package/dist/types/logger.d.ts +35 -0
  65. package/dist/types/middleware/admin-only.d.ts +19 -0
  66. package/dist/types/routes/meta-logs.d.ts +20 -0
  67. package/dist/types/rules.d.ts +2 -2
  68. package/dist/types/shared/env.d.ts +3 -1
  69. package/dist/types/shared/signin.d.ts +1 -1
  70. package/dist/types/websocket/index.d.ts +4 -0
  71. package/package.json +5 -5
  72. package/webmanager/acebase-client-browser.min.js +16 -16
  73. package/create-package-files +0 -12
  74. package/dist/cjs/auth.js.map +0 -1
  75. package/dist/cjs/cluster.js.map +0 -1
  76. package/dist/cjs/index.js.map +0 -1
  77. package/dist/cjs/middleware/404.js.map +0 -1
  78. package/dist/cjs/middleware/cache.js.map +0 -1
  79. package/dist/cjs/middleware/connection.js.map +0 -1
  80. package/dist/cjs/middleware/context.js.map +0 -1
  81. package/dist/cjs/middleware/cors.js.map +0 -1
  82. package/dist/cjs/middleware/swagger.js.map +0 -1
  83. package/dist/cjs/middleware/user.js.map +0 -1
  84. package/dist/cjs/oauth-providers/dropbox.js.map +0 -1
  85. package/dist/cjs/oauth-providers/facebook.js.map +0 -1
  86. package/dist/cjs/oauth-providers/google.js.map +0 -1
  87. package/dist/cjs/oauth-providers/index.js.map +0 -1
  88. package/dist/cjs/oauth-providers/instagram.js.map +0 -1
  89. package/dist/cjs/oauth-providers/oauth-provider.js.map +0 -1
  90. package/dist/cjs/oauth-providers/spotify.js.map +0 -1
  91. package/dist/cjs/routes/auth-change-password.js.map +0 -1
  92. package/dist/cjs/routes/auth-delete.js.map +0 -1
  93. package/dist/cjs/routes/auth-forgot-password.js.map +0 -1
  94. package/dist/cjs/routes/auth-reset-password.js.map +0 -1
  95. package/dist/cjs/routes/auth-signin.js.map +0 -1
  96. package/dist/cjs/routes/auth-signout.js.map +0 -1
  97. package/dist/cjs/routes/auth-signup.js.map +0 -1
  98. package/dist/cjs/routes/auth-state.js.map +0 -1
  99. package/dist/cjs/routes/auth-update.js.map +0 -1
  100. package/dist/cjs/routes/auth-verify-email.js.map +0 -1
  101. package/dist/cjs/routes/auth.js.map +0 -1
  102. package/dist/cjs/routes/data-exists.js.map +0 -1
  103. package/dist/cjs/routes/data-export.js.map +0 -1
  104. package/dist/cjs/routes/data-get.js.map +0 -1
  105. package/dist/cjs/routes/data-import.js.map +0 -1
  106. package/dist/cjs/routes/data-index-create.js.map +0 -1
  107. package/dist/cjs/routes/data-index-delete.js.map +0 -1
  108. package/dist/cjs/routes/data-index-list.js.map +0 -1
  109. package/dist/cjs/routes/data-query.js.map +0 -1
  110. package/dist/cjs/routes/data-reflect.js.map +0 -1
  111. package/dist/cjs/routes/data-schema-get.js.map +0 -1
  112. package/dist/cjs/routes/data-schema-set.js.map +0 -1
  113. package/dist/cjs/routes/data-schema-test.js.map +0 -1
  114. package/dist/cjs/routes/data-schemas-list.js.map +0 -1
  115. package/dist/cjs/routes/data-set.js.map +0 -1
  116. package/dist/cjs/routes/data-sync-changes.js.map +0 -1
  117. package/dist/cjs/routes/data-sync-mutations.js.map +0 -1
  118. package/dist/cjs/routes/data-transaction.js.map +0 -1
  119. package/dist/cjs/routes/data-update.js.map +0 -1
  120. package/dist/cjs/routes/data.js.map +0 -1
  121. package/dist/cjs/routes/docs.js.map +0 -1
  122. package/dist/cjs/routes/meta-info.js.map +0 -1
  123. package/dist/cjs/routes/meta-ping.js.map +0 -1
  124. package/dist/cjs/routes/meta-stats.js.map +0 -1
  125. package/dist/cjs/routes/meta.js.map +0 -1
  126. package/dist/cjs/routes/oauth2-init.js.map +0 -1
  127. package/dist/cjs/routes/oauth2-refresh.js.map +0 -1
  128. package/dist/cjs/routes/oauth2-signin.js.map +0 -1
  129. package/dist/cjs/routes/webmanager.js.map +0 -1
  130. package/dist/cjs/rules.js.map +0 -1
  131. package/dist/cjs/schema/user.js.map +0 -1
  132. package/dist/cjs/server.js.map +0 -1
  133. package/dist/cjs/settings/email.js.map +0 -1
  134. package/dist/cjs/settings/index.js.map +0 -1
  135. package/dist/cjs/shared/clients.js.map +0 -1
  136. package/dist/cjs/shared/email.js.map +0 -1
  137. package/dist/cjs/shared/env.js.map +0 -1
  138. package/dist/cjs/shared/error.js.map +0 -1
  139. package/dist/cjs/shared/http.js.map +0 -1
  140. package/dist/cjs/shared/password.js.map +0 -1
  141. package/dist/cjs/shared/rootpath.js.map +0 -1
  142. package/dist/cjs/shared/signin.js.map +0 -1
  143. package/dist/cjs/shared/simple-fetch.js.map +0 -1
  144. package/dist/cjs/shared/tokens.js.map +0 -1
  145. package/dist/cjs/shared/validate.js.map +0 -1
  146. package/dist/cjs/start.js.map +0 -1
  147. package/dist/cjs/test.js.map +0 -1
  148. package/dist/cjs/websocket/index.js.map +0 -1
  149. package/dist/cjs/websocket/manager.js.map +0 -1
  150. package/dist/cjs/websocket/socket.io.js.map +0 -1
  151. package/dist/esm/auth.js.map +0 -1
  152. package/dist/esm/cluster.js.map +0 -1
  153. package/dist/esm/index.js.map +0 -1
  154. package/dist/esm/middleware/404.js.map +0 -1
  155. package/dist/esm/middleware/cache.js.map +0 -1
  156. package/dist/esm/middleware/connection.js.map +0 -1
  157. package/dist/esm/middleware/context.js.map +0 -1
  158. package/dist/esm/middleware/cors.js.map +0 -1
  159. package/dist/esm/middleware/swagger.js.map +0 -1
  160. package/dist/esm/middleware/user.js.map +0 -1
  161. package/dist/esm/oauth-providers/dropbox.js.map +0 -1
  162. package/dist/esm/oauth-providers/facebook.js.map +0 -1
  163. package/dist/esm/oauth-providers/google.js.map +0 -1
  164. package/dist/esm/oauth-providers/index.js.map +0 -1
  165. package/dist/esm/oauth-providers/instagram.js.map +0 -1
  166. package/dist/esm/oauth-providers/oauth-provider.js.map +0 -1
  167. package/dist/esm/oauth-providers/spotify.js.map +0 -1
  168. package/dist/esm/routes/auth-change-password.js.map +0 -1
  169. package/dist/esm/routes/auth-delete.js.map +0 -1
  170. package/dist/esm/routes/auth-forgot-password.js.map +0 -1
  171. package/dist/esm/routes/auth-reset-password.js.map +0 -1
  172. package/dist/esm/routes/auth-signin.js.map +0 -1
  173. package/dist/esm/routes/auth-signout.js.map +0 -1
  174. package/dist/esm/routes/auth-signup.js.map +0 -1
  175. package/dist/esm/routes/auth-state.js.map +0 -1
  176. package/dist/esm/routes/auth-update.js.map +0 -1
  177. package/dist/esm/routes/auth-verify-email.js.map +0 -1
  178. package/dist/esm/routes/auth.js.map +0 -1
  179. package/dist/esm/routes/data-exists.js.map +0 -1
  180. package/dist/esm/routes/data-export.js.map +0 -1
  181. package/dist/esm/routes/data-get.js.map +0 -1
  182. package/dist/esm/routes/data-import.js.map +0 -1
  183. package/dist/esm/routes/data-index-create.js.map +0 -1
  184. package/dist/esm/routes/data-index-delete.js.map +0 -1
  185. package/dist/esm/routes/data-index-list.js.map +0 -1
  186. package/dist/esm/routes/data-query.js.map +0 -1
  187. package/dist/esm/routes/data-reflect.js.map +0 -1
  188. package/dist/esm/routes/data-schema-get.js.map +0 -1
  189. package/dist/esm/routes/data-schema-set.js.map +0 -1
  190. package/dist/esm/routes/data-schema-test.js.map +0 -1
  191. package/dist/esm/routes/data-schemas-list.js.map +0 -1
  192. package/dist/esm/routes/data-set.js.map +0 -1
  193. package/dist/esm/routes/data-sync-changes.js.map +0 -1
  194. package/dist/esm/routes/data-sync-mutations.js.map +0 -1
  195. package/dist/esm/routes/data-transaction.js.map +0 -1
  196. package/dist/esm/routes/data-update.js.map +0 -1
  197. package/dist/esm/routes/data.js.map +0 -1
  198. package/dist/esm/routes/docs.js.map +0 -1
  199. package/dist/esm/routes/meta-info.js.map +0 -1
  200. package/dist/esm/routes/meta-ping.js.map +0 -1
  201. package/dist/esm/routes/meta-stats.js.map +0 -1
  202. package/dist/esm/routes/meta.js.map +0 -1
  203. package/dist/esm/routes/oauth2-init.js.map +0 -1
  204. package/dist/esm/routes/oauth2-refresh.js.map +0 -1
  205. package/dist/esm/routes/oauth2-signin.js.map +0 -1
  206. package/dist/esm/routes/webmanager.js.map +0 -1
  207. package/dist/esm/rules.js.map +0 -1
  208. package/dist/esm/schema/user.js.map +0 -1
  209. package/dist/esm/server.js.map +0 -1
  210. package/dist/esm/settings/email.js.map +0 -1
  211. package/dist/esm/settings/index.js.map +0 -1
  212. package/dist/esm/shared/clients.js.map +0 -1
  213. package/dist/esm/shared/email.js.map +0 -1
  214. package/dist/esm/shared/env.js.map +0 -1
  215. package/dist/esm/shared/error.js.map +0 -1
  216. package/dist/esm/shared/http.js.map +0 -1
  217. package/dist/esm/shared/password.js.map +0 -1
  218. package/dist/esm/shared/rootpath.js.map +0 -1
  219. package/dist/esm/shared/signin.js.map +0 -1
  220. package/dist/esm/shared/simple-fetch.js.map +0 -1
  221. package/dist/esm/shared/tokens.js.map +0 -1
  222. package/dist/esm/shared/validate.js.map +0 -1
  223. package/dist/esm/start.js.map +0 -1
  224. package/dist/esm/test.js.map +0 -1
  225. package/dist/esm/websocket/index.js.map +0 -1
  226. package/dist/esm/websocket/manager.js.map +0 -1
  227. package/dist/esm/websocket/socket.io.js.map +0 -1
  228. package/src/auth.ts +0 -87
  229. package/src/cluster.ts +0 -62
  230. package/src/index.ts +0 -4
  231. package/src/middleware/404.ts +0 -13
  232. package/src/middleware/cache.ts +0 -15
  233. package/src/middleware/connection.ts +0 -48
  234. package/src/middleware/context.ts +0 -23
  235. package/src/middleware/cors.ts +0 -49
  236. package/src/middleware/swagger.ts +0 -19
  237. package/src/middleware/user.ts +0 -51
  238. package/src/oauth-providers/dropbox.ts +0 -144
  239. package/src/oauth-providers/facebook.ts +0 -103
  240. package/src/oauth-providers/google.ts +0 -147
  241. package/src/oauth-providers/index.ts +0 -21
  242. package/src/oauth-providers/instagram.ts +0 -98
  243. package/src/oauth-providers/oauth-provider.ts +0 -65
  244. package/src/oauth-providers/spotify.ts +0 -127
  245. package/src/placeholder.d.ts +0 -12
  246. package/src/routes/auth-change-password.ts +0 -89
  247. package/src/routes/auth-change-password.yaml +0 -74
  248. package/src/routes/auth-delete.ts +0 -47
  249. package/src/routes/auth-delete.yaml +0 -78
  250. package/src/routes/auth-forgot-password.ts +0 -73
  251. package/src/routes/auth-forgot-password.yaml +0 -50
  252. package/src/routes/auth-reset-password.ts +0 -93
  253. package/src/routes/auth-reset-password.yaml +0 -54
  254. package/src/routes/auth-signin.ts +0 -55
  255. package/src/routes/auth-signin.yaml +0 -130
  256. package/src/routes/auth-signout.ts +0 -61
  257. package/src/routes/auth-signout.yaml +0 -46
  258. package/src/routes/auth-signup.ts +0 -161
  259. package/src/routes/auth-signup.yaml +0 -136
  260. package/src/routes/auth-state.ts +0 -20
  261. package/src/routes/auth-state.yaml +0 -29
  262. package/src/routes/auth-update.ts +0 -147
  263. package/src/routes/auth-update.yaml +0 -152
  264. package/src/routes/auth-verify-email.ts +0 -67
  265. package/src/routes/auth-verify-email.yaml +0 -53
  266. package/src/routes/auth.ts +0 -67
  267. package/src/routes/data-exists.ts +0 -31
  268. package/src/routes/data-exists.yaml +0 -44
  269. package/src/routes/data-export.ts +0 -51
  270. package/src/routes/data-export.yaml +0 -64
  271. package/src/routes/data-get.ts +0 -68
  272. package/src/routes/data-get.yaml +0 -54
  273. package/src/routes/data-import.ts +0 -56
  274. package/src/routes/data-import.yaml +0 -75
  275. package/src/routes/data-index-create.ts +0 -85
  276. package/src/routes/data-index-create.yaml +0 -105
  277. package/src/routes/data-index-delete.ts +0 -39
  278. package/src/routes/data-index-delete.yaml +0 -51
  279. package/src/routes/data-index-list.ts +0 -42
  280. package/src/routes/data-index-list.yaml +0 -80
  281. package/src/routes/data-query.ts +0 -97
  282. package/src/routes/data-query.yaml +0 -166
  283. package/src/routes/data-reflect.ts +0 -94
  284. package/src/routes/data-reflect.yaml +0 -124
  285. package/src/routes/data-schema-get.ts +0 -38
  286. package/src/routes/data-schema-get.yaml +0 -53
  287. package/src/routes/data-schema-set.ts +0 -38
  288. package/src/routes/data-schema-set.yaml +0 -60
  289. package/src/routes/data-schema-test.ts +0 -53
  290. package/src/routes/data-schema-test.yaml +0 -66
  291. package/src/routes/data-schemas-list.ts +0 -33
  292. package/src/routes/data-schemas-list.yaml +0 -41
  293. package/src/routes/data-set.ts +0 -80
  294. package/src/routes/data-set.yaml +0 -77
  295. package/src/routes/data-sync-changes.ts +0 -78
  296. package/src/routes/data-sync-changes.yaml +0 -67
  297. package/src/routes/data-sync-mutations.ts +0 -78
  298. package/src/routes/data-sync-mutations.yaml +0 -67
  299. package/src/routes/data-transaction-finish.yaml +0 -82
  300. package/src/routes/data-transaction-start.yaml +0 -58
  301. package/src/routes/data-transaction.ts +0 -159
  302. package/src/routes/data-update.ts +0 -81
  303. package/src/routes/data-update.yaml +0 -77
  304. package/src/routes/data.ts +0 -79
  305. package/src/routes/docs.ts +0 -249
  306. package/src/routes/meta-info.ts +0 -80
  307. package/src/routes/meta-info.yaml +0 -41
  308. package/src/routes/meta-ping.ts +0 -15
  309. package/src/routes/meta-ping.yaml +0 -23
  310. package/src/routes/meta-stats.ts +0 -31
  311. package/src/routes/meta-stats.yaml +0 -41
  312. package/src/routes/meta.ts +0 -18
  313. package/src/routes/oauth2-init.ts +0 -44
  314. package/src/routes/oauth2-init.yaml +0 -63
  315. package/src/routes/oauth2-refresh.ts +0 -39
  316. package/src/routes/oauth2-refresh.yaml +0 -65
  317. package/src/routes/oauth2-signin.ts +0 -282
  318. package/src/routes/oauth2-signin.yaml +0 -66
  319. package/src/routes/webmanager.ts +0 -29
  320. package/src/rules.ts +0 -196
  321. package/src/schema/user.ts +0 -99
  322. package/src/server.ts +0 -418
  323. package/src/settings/email.ts +0 -18
  324. package/src/settings/index.ts +0 -241
  325. package/src/shared/clients.ts +0 -31
  326. package/src/shared/email.ts +0 -33
  327. package/src/shared/env.ts +0 -38
  328. package/src/shared/error.ts +0 -37
  329. package/src/shared/http.ts +0 -26
  330. package/src/shared/password.ts +0 -41
  331. package/src/shared/rootpath.ts +0 -15
  332. package/src/shared/signin.ts +0 -143
  333. package/src/shared/simple-fetch.ts +0 -65
  334. package/src/shared/tokens.ts +0 -77
  335. package/src/shared/validate.ts +0 -53
  336. package/src/start.ts +0 -45
  337. package/src/test.ts +0 -10
  338. package/src/websocket/index.ts +0 -320
  339. package/src/websocket/manager.ts +0 -46
  340. package/src/websocket/socket.io.ts +0 -90
  341. package/tsconfig-cjs.json +0 -10
  342. package/tsconfig.json +0 -21
@@ -13,8 +13,11 @@ export const addRoutes = (env) => {
13
13
  // Start transaction endpoint:
14
14
  env.app.post(`/transaction/${env.db.name}/start`, (req, res) => {
15
15
  const data = req.body;
16
+ const LOG_ACTION = 'data.transaction.start';
17
+ const LOG_DETAILS = { ip: req.ip, uid: req.user?.uid ?? null, path: data.path };
16
18
  const access = env.rules.userHasAccess(req.user, data.path, true);
17
19
  if (!access.allow) {
20
+ env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null }, access.details);
18
21
  return sendUnauthorizedError(res, access.code, access.message);
19
22
  }
20
23
  // Start transaction
@@ -50,15 +53,18 @@ export const addRoutes = (env) => {
50
53
  }
51
54
  catch (err) {
52
55
  env.debug.error(`failed to start transaction on "${tx.path}":`, err);
53
- env.logRef?.push({ action: 'tx_start', success: false, code: err.code ?? 'unknown_error', path: tx.path, error: err.message, ip: req.ip, uid: req.user?.uid ?? null });
56
+ env.log.error(LOG_ACTION, err.code ?? 'unexpected', LOG_DETAILS, typeof err.code === 'undefined' ? err : null);
54
57
  sendUnexpectedError(res, err);
55
58
  }
56
59
  });
57
60
  // Finish transaction endpoint:
58
61
  env.app.post(`/transaction/${env.db.name}/finish`, async (req, res) => {
59
62
  const data = req.body;
63
+ const LOG_ACTION = 'data.transaction.finish';
64
+ const LOG_DETAILS = { ip: req.ip, uid: req.user?.uid ?? null, path: data.path };
60
65
  const tx = _transactions.get(data.id);
61
66
  if (!tx || tx.path !== data.path) {
67
+ env.log.error(LOG_ACTION, tx ? 'wrong_path' : 'not_found', { ...LOG_DETAILS, id: data.id, tx_path: tx?.path ?? null });
62
68
  res.statusCode = 410; // Gone
63
69
  res.send(`transaction not found`);
64
70
  return;
@@ -67,6 +73,7 @@ export const addRoutes = (env) => {
67
73
  _transactions.delete(tx.id);
68
74
  const access = env.rules.userHasAccess(req.user, tx.path, true);
69
75
  if (!access.allow) {
76
+ env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null }, access.details);
70
77
  return sendUnauthorizedError(res, access.code, access.message);
71
78
  }
72
79
  // Finish transaction
@@ -98,16 +105,16 @@ export const addRoutes = (env) => {
98
105
  catch (err) {
99
106
  tx.finish(); // Finish without value cancels the transaction
100
107
  if (err instanceof SchemaValidationError) {
101
- env.logRef?.push({ action: 'tx_finish', success: false, code: 'schema_validation_failed', path: tx.path, error: err.reason, ip: req.ip, uid: req.user?.uid ?? null });
108
+ env.log.error(LOG_ACTION, 'schema_validation_failed', { ...LOG_DETAILS, reason: err.reason });
102
109
  res.status(422).send({ code: 'schema_validation_failed', message: err.message });
103
110
  }
104
111
  else if (err instanceof DataTransactionError) {
105
- env.logRef?.push({ action: 'tx_finish', success: false, code: err.code, path: tx.path, ip: req.ip, uid: req.user?.uid ?? null });
112
+ env.log.error(LOG_ACTION, err.code, { ...LOG_DETAILS, message: err.message });
106
113
  sendBadRequestError(res, err);
107
114
  }
108
115
  else {
109
- env.debug.error(`failed to finsih transaction on "${tx.path}":`, err);
110
- env.logRef?.push({ action: 'tx_finish', success: false, code: 'unknown_error', path: tx.path, error: err.message, ip: req.ip, uid: req.user?.uid ?? null });
116
+ env.debug.error(`failed to finish transaction on "${tx.path}":`, err);
117
+ env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
111
118
  sendError(res, err);
112
119
  }
113
120
  }
@@ -9,10 +9,12 @@ export class UpdateDataError extends Error {
9
9
  }
10
10
  export const addRoute = (env) => {
11
11
  env.app.post(`/data/${env.db.name}/*`, async (req, res) => {
12
- // update data
13
12
  const path = req.path.slice(env.db.name.length + 7);
13
+ const LOG_ACTION = 'data.update';
14
+ const LOG_DETAILS = { ip: req.ip, uid: req.user?.uid ?? null, path };
14
15
  const access = env.rules.userHasAccess(req.user, path, true);
15
16
  if (!access.allow) {
17
+ env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null, rule_error: access.details?.message ?? null });
16
18
  return sendUnauthorizedError(res, access.code, access.message);
17
19
  }
18
20
  try {
@@ -40,16 +42,16 @@ export const addRoute = (env) => {
40
42
  }
41
43
  catch (err) {
42
44
  if (err instanceof SchemaValidationError) {
43
- env.logRef?.push({ action: 'update_data', success: false, code: 'schema_validation_failed', path, error: err.reason, ip: req.ip, uid: req.user?.uid ?? null });
45
+ env.log.error(LOG_ACTION, 'schema_validation_failed', { ...LOG_DETAILS, reason: err.reason });
44
46
  res.status(422).send({ code: 'schema_validation_failed', message: err.message });
45
47
  }
46
48
  else if (err instanceof UpdateDataError) {
47
- env.logRef?.push({ action: 'update_data', success: false, code: err.code, path, ip: req.ip, uid: req.user?.uid ?? null });
49
+ env.log.error(LOG_ACTION, err.code, { ...LOG_DETAILS, message: err.message });
48
50
  sendBadRequestError(res, err);
49
51
  }
50
52
  else {
51
53
  env.debug.error(`failed to update "${path}":`, err);
52
- env.logRef?.push({ action: 'update_data', success: false, code: `unknown_error`, path, error: err.message, ip: req.ip, uid: req.user?.uid ?? null });
54
+ env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
53
55
  sendError(res, err);
54
56
  }
55
57
  }
@@ -0,0 +1,36 @@
1
+ import adminOnly from '../middleware/admin-only.js';
2
+ export const addRoute = (env) => {
3
+ env.app.get(`/logs/${env.db.name}`, adminOnly(env), async (req, res) => {
4
+ // Get database logs
5
+ // Create indexes if not there yet
6
+ const db = env.log.ref.db;
7
+ const createIndexes = [
8
+ db.indexes.create(env.log.ref.path, 'date')
9
+ ];
10
+ if (req.query.filter_col === 'action') {
11
+ createIndexes.push(db.indexes.create(env.log.ref.path, 'action', { include: ['date'] }));
12
+ }
13
+ if (req.query.filter_col === 'code') {
14
+ createIndexes.push(db.indexes.create(env.log.ref.path, 'code', { include: ['date'] }));
15
+ }
16
+ await Promise.all(createIndexes);
17
+ try {
18
+ const query = env.log.query()
19
+ .take(parseInt(req.query.take ?? '100'))
20
+ .skip(parseInt(req.query.skip ?? '0'))
21
+ .sort(req.query.sort ?? 'date', false);
22
+ if (req.query.filter_col) {
23
+ query.filter(req.query.filter_col, req.query.filter_op, req.query.filter_val);
24
+ }
25
+ const snaps = await query.get();
26
+ const logs = snaps.getValues();
27
+ res.send(logs);
28
+ }
29
+ catch (err) {
30
+ res.statusCode = 500;
31
+ res.send(err.message);
32
+ }
33
+ });
34
+ };
35
+ export default addRoute;
36
+ //# sourceMappingURL=meta-logs.js.map
@@ -1,6 +1,7 @@
1
1
  import addInfoRoute from './meta-info.js';
2
2
  import addPingRoute from './meta-ping.js';
3
3
  import addStatsRoute from './meta-stats.js';
4
+ import addLogsRoute from './meta-logs.js';
4
5
  export const addRoutes = (env) => {
5
6
  // Add info endpoint
6
7
  addInfoRoute(env);
@@ -8,6 +9,8 @@ export const addRoutes = (env) => {
8
9
  addPingRoute(env);
9
10
  // Add database stats endpoint
10
11
  addStatsRoute(env);
12
+ // Add logs endpoint (admin only)
13
+ addLogsRoute(env);
11
14
  };
12
15
  export default addRoutes;
13
16
  //# sourceMappingURL=meta.js.map
@@ -7,6 +7,8 @@ const socketSignInFailed = `<html><script>window.close()</script><body>Failed to
7
7
  export const addRoute = (env) => {
8
8
  env.app.get(`/oauth2/${env.db.name}/signin`, async (req, res) => {
9
9
  // This is where the user is redirected to by the provider after signin or error
10
+ const LOG_ACTION = 'oauth2.signin';
11
+ const LOG_DETAILS = { ip: req.ip, provider: null };
10
12
  try {
11
13
  const state = parseSignedPublicToken(req.query.state, env.tokenSalt);
12
14
  if (req.query.error) {
@@ -23,6 +25,7 @@ export const addRoute = (env) => {
23
25
  // Got authorization code
24
26
  const authCode = req.query.code;
25
27
  const provider = env.authProviders[state.provider];
28
+ LOG_DETAILS.provider = state.provider;
26
29
  // Get access & refresh tokens
27
30
  const tokens = await provider.getAccessToken({ type: 'auth', auth_code: authCode, redirect_url: `${req.protocol}://${req.headers.host}/oauth2/${env.db.name}/signin` });
28
31
  let user_details;
@@ -129,7 +132,7 @@ export const addRoute = (env) => {
129
132
  // Add provider details
130
133
  await env.authRef.child(uid).child('settings').update(getProviderSettings());
131
134
  // Log success
132
- env.logRef.push({ action: 'oauth2_signin', success: true, ip: req.ip, date: new Date(), uid });
135
+ env.log.event(LOG_ACTION, { ...LOG_DETAILS, uid });
133
136
  // Cache the user
134
137
  env.authCache.set(user.uid, user);
135
138
  // Request signin e-mail to be sent
@@ -149,13 +152,13 @@ export const addRoute = (env) => {
149
152
  provider: state.provider
150
153
  };
151
154
  env.config.email?.send(request).catch(err => {
152
- env.logRef.push({ action: 'oauth2_login_email', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, request });
155
+ env.log.error(LOG_ACTION + '.email', 'unexpected', { ...LOG_DETAILS, uid, request }, err);
153
156
  });
154
157
  }
155
158
  else if (snaps.length === 0) {
156
159
  // User does not exist, create
157
160
  if (!env.config.auth.allowUserSignup) {
158
- env.logRef.push({ action: 'oauth2_signup', success: false, code: 'user_signup_disabled', provider: state.provider, email: user_details.email, date: new Date() });
161
+ env.log.error(LOG_ACTION, 'user_signup_disabled', { ...LOG_DETAILS, email: user_details.email });
159
162
  res.statusCode = 403; // Forbidden
160
163
  return res.send({ code: 'admin_only', message: 'Only admin is allowed to create users' });
161
164
  }
@@ -182,7 +185,7 @@ export const addRoute = (env) => {
182
185
  const uid = userRef.key;
183
186
  user.uid = uid;
184
187
  // Log success
185
- env.logRef.push({ action: 'oauth2_signup', success: true, ip: req.ip, date: new Date(), uid });
188
+ env.log.event(LOG_ACTION, { ...LOG_DETAILS, uid });
186
189
  // Cache the user
187
190
  env.authCache.set(user.uid, user);
188
191
  // Request welcome e-mail to be sent
@@ -202,7 +205,7 @@ export const addRoute = (env) => {
202
205
  provider: state.provider
203
206
  };
204
207
  env.config.email?.send(request).catch(err => {
205
- env.logRef.push({ action: 'oauth2_signup_email', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, request });
208
+ env.log.error(LOG_ACTION + '.email', 'unexpected', { ...LOG_DETAILS, uid, request }, err);
206
209
  });
207
210
  }
208
211
  else {
@@ -17,6 +17,7 @@ import addWebManagerRoutes from './routes/webmanager.js';
17
17
  import addMetadataRoutes from './routes/meta.js';
18
18
  import addSwaggerMiddleware from './middleware/swagger.js';
19
19
  import addCacheMiddleware from './middleware/cache.js';
20
+ import { DatabaseLog } from './logger.js';
20
21
  // type PrivateLocalSettings = AceBaseLocalSettings & { storage: PrivateStorageSettings };
21
22
  export class AceBaseServerNotReadyError extends Error {
22
23
  constructor() { super('Server is not ready yet'); }
@@ -100,6 +101,7 @@ export class AceBaseServer extends SimpleEventEmitter {
100
101
  const securityRef = authDb ? authDb === db ? db.ref('__auth__/security') : authDb.ref('security') : null;
101
102
  const authRef = authDb ? authDb === db ? db.ref('__auth__/accounts') : authDb.ref('accounts') : null;
102
103
  const logRef = authDb ? authDb === db ? db.ref('__log__') : authDb.ref('log') : null;
104
+ const logger = new DatabaseLog(logRef);
103
105
  // Setup rules
104
106
  const rulesFilePath = `${this.config.path}/${this.db.name}.acebase/rules.json`;
105
107
  const rules = new PathBasedRules(rulesFilePath, config.auth.defaultAccessRule, { db, debug: this.debug, authEnabled: this.config.auth.enabled });
@@ -112,7 +114,7 @@ export class AceBaseServer extends SimpleEventEmitter {
112
114
  debug: this.debug,
113
115
  securityRef,
114
116
  authRef,
115
- logRef,
117
+ log: logger,
116
118
  tokenSalt: null,
117
119
  clients,
118
120
  authCache: null,
@@ -7,6 +7,8 @@ if (process.platform === 'win32' && currentDir.startsWith('/')) {
7
7
  // tsc-esm-fix does not handle win32 file urls correctly, the drive letter in import.meta.url is prefixed with a slash: file:///C:/dir/file.js
8
8
  currentDir = currentDir.slice(1);
9
9
  }
10
+ // tsc-esm-fix also does not use decodeURI to remove encoded characters (such as %20 for spaces)
11
+ currentDir = decodeURI(currentDir);
10
12
  export const packageRootPath = resolve(currentDir, '../../..');
11
13
  export default packageRootPath;
12
14
  //# sourceMappingURL=rootpath.js.map
@@ -17,6 +17,8 @@ export class SignInError extends Error {
17
17
  * @returns
18
18
  */
19
19
  export const signIn = async (credentials, env, req) => {
20
+ const LOG_ACTION = 'auth.signin';
21
+ const LOG_DETAILS = { ip: req.ip, method: credentials.method };
20
22
  try {
21
23
  const query = env.authRef.query();
22
24
  let tokenDetails;
@@ -34,7 +36,7 @@ export const signIn = async (credentials, env, req) => {
34
36
  }
35
37
  break;
36
38
  }
37
- case 'private_token': {
39
+ case 'internal': {
38
40
  // Method used internally: uses the access token extracted from a public access token (see tokenDetails.access_token in above 'token' case)
39
41
  if (typeof credentials.access_token !== 'string') {
40
42
  throw new SignInError('invalid_details', 'sign in request has invalid arguments');
@@ -109,7 +111,9 @@ export const signIn = async (credentials, env, req) => {
109
111
  // Update db
110
112
  await snap.ref.update(updates);
111
113
  // Log history item
112
- env.logRef.push({ action: `signin`, type: credentials.method, [credentials.method]: credentials[credentials.method], ip: req.ip, date: new Date(), success: true });
114
+ if (credentials.method !== 'internal') {
115
+ env.log.event(LOG_ACTION, LOG_DETAILS);
116
+ }
113
117
  // Add to cache
114
118
  env.authCache.set(user.uid, user);
115
119
  // Bind user to current request
@@ -118,7 +122,7 @@ export const signIn = async (credentials, env, req) => {
118
122
  }
119
123
  catch (err) {
120
124
  // Log error
121
- env.logRef.push({ action: 'signin', type: credentials.method, [credentials.method]: credentials[credentials.method], success: false, code: err.code || 'unexpected', message: err.code ? null : err.message, ip: req.ip, date: new Date(), ...err.details });
125
+ env.log.error(LOG_ACTION, err.code ?? 'unexpected', LOG_DETAILS, typeof err.code === 'undefined' ? err : null);
122
126
  throw err;
123
127
  }
124
128
  };
package/dist/esm/start.js CHANGED
@@ -40,6 +40,9 @@ if (+getVariable('TXLOG', 0) === 1) {
40
40
  maxAge: +getVariable('TXDAYS', 30)
41
41
  };
42
42
  }
43
+ if (+getVariable('AUTH', 1) === 0) {
44
+ options.authentication = { enabled: false };
45
+ }
43
46
  const server = new AceBaseServer(dbname, options);
44
47
  server.once("ready", () => {
45
48
  server.debug.log(`AceBase server running`);
@@ -2,6 +2,12 @@ import { ColorStyle, Transport } from "acebase-core";
2
2
  import { ConnectedClient } from "../shared/clients.js";
3
3
  import { decodePublicAccessToken } from "../shared/tokens.js";
4
4
  import { createServer } from "./socket.io.js";
5
+ export class SocketRequestError extends Error {
6
+ constructor(code, message) {
7
+ super(message);
8
+ this.code = code;
9
+ }
10
+ }
5
11
  export const addWebsocketServer = (env) => {
6
12
  // TODO: Allow using uWebSockets.js server instead of Socket.IO
7
13
  const serverManager = createServer(env);
@@ -94,7 +100,7 @@ export const addWebsocketServer = (env) => {
94
100
  serverManager.send(event.socket, 'oauth2-signin', { error: err.message });
95
101
  }
96
102
  });
97
- const acknowlegdeRequest = (socket, requestId) => {
103
+ const acknowledgeRequest = (socket, requestId) => {
98
104
  // Send acknowledgement
99
105
  serverManager.send(socket, 'result', {
100
106
  success: true,
@@ -119,12 +125,12 @@ export const addWebsocketServer = (env) => {
119
125
  const subscriptionPath = event.data.path;
120
126
  const isSubscribed = () => subscriptionPath in client.subscriptions && client.subscriptions[subscriptionPath].some(s => s.event === event.data.event);
121
127
  if (isSubscribed()) {
122
- return acknowlegdeRequest(event.socket, event.data.req_id);
128
+ return acknowledgeRequest(event.socket, event.data.req_id);
123
129
  }
124
130
  // Get client
125
131
  // const client = clients.get(socket.id);
126
132
  if (!env.rules.userHasAccess(client.user, subscriptionPath, false)) {
127
- env.logRef.push({ action: 'subscribe', success: false, code: 'access_denied', uid: client.user ? client.user.uid : '-', path: subscriptionPath });
133
+ env.log.error('event.subscribe', 'access_denied', { uid: client.user?.uid ?? 'anonymous', path: subscriptionPath });
128
134
  return failRequest(event.socket, event.data.req_id, 'access_denied');
129
135
  }
130
136
  const callback = (err, path, currentValue, previousValue, context) => {
@@ -165,7 +171,7 @@ export const addWebsocketServer = (env) => {
165
171
  let subscr = { path: subscriptionPath, event: event.data.event, callback };
166
172
  pathSubs.push(subscr);
167
173
  env.db.api.subscribe(subscriptionPath, event.data.event, callback);
168
- acknowlegdeRequest(event.socket, event.data.req_id);
174
+ acknowledgeRequest(event.socket, event.data.req_id);
169
175
  });
170
176
  serverManager.on('unsubscribe', event => {
171
177
  // Client unsubscribes from events on a node
@@ -178,7 +184,7 @@ export const addWebsocketServer = (env) => {
178
184
  let pathSubs = client.subscriptions[event.data.path];
179
185
  if (!pathSubs) {
180
186
  // We have no knowledge of any active subscriptions on this path
181
- return acknowlegdeRequest(event.socket, event.data.req_id);
187
+ return acknowledgeRequest(event.socket, event.data.req_id);
182
188
  }
183
189
  let remove = pathSubs;
184
190
  if (event.data.event) {
@@ -195,7 +201,7 @@ export const addWebsocketServer = (env) => {
195
201
  // No subscriptions left on this path, remove the path entry
196
202
  delete client.subscriptions[event.data.path];
197
203
  }
198
- return acknowlegdeRequest(event.socket, event.data.req_id);
204
+ return acknowledgeRequest(event.socket, event.data.req_id);
199
205
  });
200
206
  serverManager.on('query-unsubscribe', event => {
201
207
  // Client unsubscribing from realtime query events
@@ -206,15 +212,17 @@ export const addWebsocketServer = (env) => {
206
212
  env.debug.verbose(`Client ${event.socket_id} is unsubscribing from realtime query "${event.data.query_id}"`);
207
213
  // const client = clients.get(socket.id);
208
214
  delete client.realtimeQueries[event.data.query_id];
209
- acknowlegdeRequest(event.socket, event.data.req_id);
215
+ acknowledgeRequest(event.socket, event.data.req_id);
210
216
  });
211
217
  const TRANSACTION_TIMEOUT_MS = 10000; // 10s to finish a started transaction
212
218
  serverManager.on('transaction-start', event => {
213
219
  // Start transaction
214
220
  const client = getClientBySocketId(event.socket_id, 'transaction-start');
215
- if (!client) {
221
+ if (!client || !event.data) {
216
222
  return;
217
223
  }
224
+ const LOG_ACTION = 'socket.transaction.start';
225
+ const LOG_DETAILS = { ip: event.socket.conn.remoteAddress, uid: client.user?.uid ?? null, path: event.data.path };
218
226
  env.debug.verbose(`Client ${event.socket_id} is sending transaction start request on path "${event.data.path}"`);
219
227
  const data = event.data;
220
228
  const tx = {
@@ -226,11 +234,14 @@ export const addWebsocketServer = (env) => {
226
234
  timeout: setTimeout(() => {
227
235
  delete client.transactions[tx.id];
228
236
  tx.finish(); // Finish without value cancels the transaction
237
+ env.log.error(LOG_ACTION, 'timeout', LOG_DETAILS);
229
238
  serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: 'timeout' });
230
239
  }, TRANSACTION_TIMEOUT_MS)
231
240
  };
232
- if (!env.rules.userHasAccess(client.user, data.path, true)) {
241
+ const access = env.rules.userHasAccess(client.user, data.path, true);
242
+ if (!access.allow) {
233
243
  // throw new AccessDeniedError(`access_denied`);
244
+ env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null }, access.details);
234
245
  return serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: 'access_denied' });
235
246
  }
236
247
  // Bind to client
@@ -254,32 +265,36 @@ export const addWebsocketServer = (env) => {
254
265
  serverManager.on('transaction-finish', async (event) => {
255
266
  // Finish transaction
256
267
  const client = getClientBySocketId(event.socket_id, 'transaction-finish');
257
- if (!client) {
268
+ if (!client || !event.data) {
258
269
  return;
259
270
  }
271
+ const LOG_ACTION = 'socket.transaction.finish';
272
+ const LOG_DETAILS = { ip: event.socket.conn.remoteAddress, uid: client.user?.uid ?? null, path: event.data.path };
260
273
  const data = event.data;
261
274
  const tx = client.transactions[data.id];
262
275
  try {
263
276
  if (!tx || data.path !== tx.path) {
264
- throw new Error('transaction_not_found');
277
+ env.log.error(LOG_ACTION, tx ? 'wrong_path' : 'not_found', { ...LOG_DETAILS, id: data.id, tx_path: tx?.path ?? null });
278
+ throw new SocketRequestError('transaction_not_found', 'Transaction not found');
265
279
  }
266
280
  clearTimeout(tx.timeout);
267
281
  delete client.transactions[data.id];
268
- if (!env.rules.userHasAccess(client.user, data.path, true)) {
269
- throw new Error('access_denied');
282
+ const access = env.rules.userHasAccess(client.user, data.path, true);
283
+ if (!access.allow) {
284
+ env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null }, access.details);
285
+ throw new SocketRequestError('access_denied', 'Access denied');
270
286
  }
271
287
  const newValue = 'val' in data.value ? Transport.deserialize(data.value) : undefined;
272
- try {
273
- const { cursor } = await tx.finish(newValue);
274
- env.debug.verbose(`Transaction ${tx.id} finished`);
275
- serverManager.send(event.socket, 'tx_completed', { id: tx.id, context: { cursor } });
276
- }
277
- catch (err) {
278
- serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: err.message });
279
- }
288
+ const { cursor } = await tx.finish(newValue);
289
+ env.debug.verbose(`Transaction ${tx.id} finished`);
290
+ serverManager.send(event.socket, 'tx_completed', { id: tx.id, context: { cursor } });
280
291
  }
281
292
  catch (err) {
282
- serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: err.message, data });
293
+ if (!(err instanceof SocketRequestError)) {
294
+ // Other errors have been logged already
295
+ env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
296
+ }
297
+ serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: err.code ?? err.message, data });
283
298
  tx.finish(); // Finish with undefined, canceling the transaction
284
299
  }
285
300
  });
@@ -0,0 +1,35 @@
1
+ import { DataReference } from "acebase-core";
2
+ export declare class DatabaseLog {
3
+ private logRef;
4
+ constructor(logRef: DataReference);
5
+ /**
6
+ * Logs an action initiated by a client/user with a positive or negative outcome. Used by `success` and `failure`
7
+ * @param action action identifier
8
+ * @param success whether the action was successful
9
+ * @param details any additional details to be logged
10
+ */
11
+ event(action: string, details: any): Promise<void>;
12
+ /**
13
+ * Logs a system warning
14
+ * @param action action identifier
15
+ * @param code warning code
16
+ * @param details any additional details to be logged
17
+ */
18
+ warning(action: string, code: string, details: any): Promise<void>;
19
+ /**
20
+ * Logs a system or client/user error
21
+ * @param action action identifier
22
+ * @param code error code
23
+ * @param details any additional details to be logged
24
+ */
25
+ error(action: string, code: string, details: any, unexpectedError?: any): Promise<void>;
26
+ /**
27
+ * Query the logs
28
+ * @returns
29
+ */
30
+ query(): import("acebase-core").DataReferenceQuery;
31
+ /**
32
+ * Reference to the logs database collection
33
+ */
34
+ get ref(): DataReference;
35
+ }
@@ -0,0 +1,19 @@
1
+ import { NextFunction } from "express";
2
+ import { RouteInitEnvironment, RouteRequest } from "../shared/env";
3
+ import type { Response } from "../shared/http";
4
+ /**
5
+ * Middleware function that checks if the current user is `admin`. An 403 Forbidden error will be sent in the response if
6
+ * authentication is enabled on the server and the user is not signed in as admin.
7
+ *
8
+ * Can be applied to any route by adding it in the router chain.
9
+ * @example
10
+ * app.get('/endpoint', adminOnly(env), (req, res) => {
11
+ * // If we get here we are either an admin,
12
+ * // or the server doesn't have authentication enabled.
13
+ * })
14
+ * @param env Middle
15
+ * @param errorMessage
16
+ * @returns
17
+ */
18
+ export declare const adminOnly: (env: RouteInitEnvironment, errorMessage?: string) => (req: RouteRequest, res: Response, next: NextFunction) => void;
19
+ export default adminOnly;
@@ -0,0 +1,20 @@
1
+ import { QueryOperator } from 'acebase-core/types/data-reference';
2
+ import { RouteInitEnvironment, RouteRequest } from '../shared/env';
3
+ declare type AceBaseLogLine = {
4
+ action: string;
5
+ ip: string;
6
+ date: string;
7
+ };
8
+ export declare type RequestQuery = {
9
+ take?: string;
10
+ skip?: string;
11
+ sort?: 'date';
12
+ filter_col?: string;
13
+ filter_op?: QueryOperator;
14
+ filter_val?: string;
15
+ };
16
+ export declare type RequestBody = null;
17
+ export declare type ResponseBody = AceBaseLogLine[];
18
+ export declare type Request = RouteRequest<any, ResponseBody, RequestBody, RequestQuery>;
19
+ export declare const addRoute: (env: RouteInitEnvironment) => void;
20
+ export default addRoute;
@@ -9,7 +9,7 @@ declare type HasAccessResult = {
9
9
  message?: string;
10
10
  rule?: string | boolean;
11
11
  rulePath?: string;
12
- details?: any;
12
+ details?: Error;
13
13
  } & ({
14
14
  allow: true;
15
15
  } | {
@@ -18,7 +18,7 @@ declare type HasAccessResult = {
18
18
  message: string;
19
19
  rule?: string | boolean;
20
20
  rulePath?: string;
21
- details?: any;
21
+ details?: Error;
22
22
  });
23
23
  export declare class PathBasedRules {
24
24
  private authEnabled;
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  /// <reference types="express" />
3
4
  import type { HttpApp, Request } from './http';
4
5
  import type { AceBase, DataReference } from 'acebase';
@@ -11,6 +12,7 @@ import type { Server as HttpServer } from 'http';
11
12
  import type { Server as SecureHttpServer } from 'https';
12
13
  import type { PathBasedRules } from '../rules';
13
14
  import { Api } from 'acebase-core/src/api';
15
+ import { DatabaseLog } from '../logger';
14
16
  export interface RouteInitEnvironment {
15
17
  server: HttpServer | SecureHttpServer;
16
18
  app: HttpApp;
@@ -22,7 +24,7 @@ export interface RouteInitEnvironment {
22
24
  debug: DebugLogger;
23
25
  securityRef: DataReference;
24
26
  authRef: DataReference;
25
- logRef: DataReference;
27
+ log: DatabaseLog;
26
28
  tokenSalt: string;
27
29
  clients: Map<string, ConnectedClient>;
28
30
  authCache: SimpleCache<string, DbUserAccountDetails>;
@@ -4,7 +4,7 @@ export declare type SignInCredentials = {
4
4
  method: 'token';
5
5
  access_token: string;
6
6
  } | {
7
- method: 'private_token';
7
+ method: 'internal';
8
8
  access_token: string;
9
9
  } | {
10
10
  method: 'email';
@@ -1,2 +1,6 @@
1
1
  import { RouteInitEnvironment } from "../shared/env";
2
+ export declare class SocketRequestError extends Error {
3
+ code: string;
4
+ constructor(code: string, message: string);
5
+ }
2
6
  export declare const addWebsocketServer: (env: RouteInitEnvironment) => import("./socket.io").SocketIOManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "acebase-server",
3
- "version": "1.11.0",
3
+ "version": "1.13.1",
4
4
  "description": "AceBase realtime database server (webserver endpoint to allow remote connections)",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -14,7 +14,7 @@
14
14
  "private": false,
15
15
  "repository": "github:appy-one/acebase-server",
16
16
  "scripts": {
17
- "start": "node dist/esm/start.js DBNAME=default DBPATH=.. HOST=localhost PORT=3000 TXLOG=1",
17
+ "start": "node dist/esm/start.js DBNAME=default DBPATH=.. HOST=localhost PORT=3000 TXLOG=1 AUTH=1",
18
18
  "test": "set NODE_ENV=development && npm run start",
19
19
  "build": "npm run build:clean && npm run build:esm && npm run build:cjs && npm run build:packages && echo Done!",
20
20
  "build:clean": "rm -fr dist/*",
@@ -57,8 +57,8 @@
57
57
  "author": "Ewout Stortenbeker <me@appy.one> (http://appy.one)",
58
58
  "license": "MIT",
59
59
  "dependencies": {
60
- "acebase": "^1.21.1",
61
- "acebase-core": "^1.18.0",
60
+ "acebase": "^1.24.2",
61
+ "acebase-core": "^1.22.3",
62
62
  "express": "^4.17.1",
63
63
  "socket.io": "^4.5.0",
64
64
  "swagger-jsdoc": "^6.1.0",
@@ -69,6 +69,6 @@
69
69
  "@types/node": "^10.17.60",
70
70
  "@types/socket.io": "^3.0.2",
71
71
  "tsc-esm-fix": "^2.12.4",
72
- "typescript": "^4.6.3"
72
+ "typescript": "^4.7.4"
73
73
  }
74
74
  }