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
@@ -10,13 +10,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.addRoute = void 0;
13
+ const admin_only_1 = require("../middleware/admin-only");
13
14
  const error_1 = require("../shared/error");
14
15
  const addRoute = (env) => {
15
- env.app.post(`/schema/${env.db.name}`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
16
+ env.app.post(`/schema/${env.db.name}`, (0, admin_only_1.default)(env), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
16
17
  // defines a schema
17
- if (!req.user || req.user.username !== 'admin') {
18
- return (0, error_1.sendUnauthorizedError)(res, 'admin_only', 'only admin can perform schema operations');
19
- }
20
18
  try {
21
19
  const data = req.body;
22
20
  const { path, schema } = data;
@@ -11,14 +11,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.addRoute = void 0;
13
13
  const acebase_core_1 = require("acebase-core");
14
+ const admin_only_1 = require("../middleware/admin-only");
14
15
  const error_1 = require("../shared/error");
15
16
  const addRoute = (env) => {
16
- env.app.post(`/schema/${env.db.name}/test`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
17
+ env.app.post(`/schema/${env.db.name}/test`, (0, admin_only_1.default)(env), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
17
18
  var _a, _b;
18
19
  // tests a schema
19
- if (!req.user || req.user.username !== 'admin') {
20
- return (0, error_1.sendUnauthorizedError)(res, 'admin_only', 'only admin can perform schema operations');
21
- }
22
20
  try {
23
21
  const data = req.body;
24
22
  if (typeof ((_a = data.value) === null || _a === void 0 ? void 0 : _a.val) === 'undefined' || !['string', 'object', 'undefined'].includes(typeof ((_b = data.value) === null || _b === void 0 ? void 0 : _b.map))) {
@@ -10,13 +10,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.addRoute = void 0;
13
+ const admin_only_1 = require("../middleware/admin-only");
13
14
  const error_1 = require("../shared/error");
14
15
  const addRoute = (env) => {
15
- env.app.get(`/schema/${env.db.name}`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
16
+ env.app.get(`/schema/${env.db.name}`, (0, admin_only_1.default)(env), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
16
17
  // Get all defined schemas
17
- if (!req.user || req.user.username !== 'admin') {
18
- return (0, error_1.sendUnauthorizedError)(res, 'admin_only', 'only admin can perform schema operations');
19
- }
20
18
  try {
21
19
  const schemas = yield env.db.schema.all();
22
20
  res.contentType('application/json').send(schemas.map(schema => ({
@@ -22,11 +22,13 @@ class SetDataError extends Error {
22
22
  exports.SetDataError = SetDataError;
23
23
  const addRoute = (env) => {
24
24
  env.app.put(`/data/${env.db.name}/*`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
25
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
26
- // Set data
25
+ var _a, _b, _c, _d, _e, _f;
27
26
  const path = req.path.slice(env.db.name.length + 7);
27
+ const LOG_ACTION = 'data.set';
28
+ const LOG_DETAILS = { ip: req.ip, uid: (_b = (_a = req.user) === null || _a === void 0 ? void 0 : _a.uid) !== null && _b !== void 0 ? _b : null, path };
28
29
  const access = env.rules.userHasAccess(req.user, path, true);
29
30
  if (!access.allow) {
31
+ env.log.error(LOG_ACTION, 'unauthorized', Object.assign(Object.assign({}, LOG_DETAILS), { rule_code: access.code, rule_path: (_c = access.rulePath) !== null && _c !== void 0 ? _c : null, rule_error: (_e = (_d = access.details) === null || _d === void 0 ? void 0 : _d.message) !== null && _e !== void 0 ? _e : null }));
30
32
  return (0, error_1.sendUnauthorizedError)(res, access.code, access.message);
31
33
  }
32
34
  try {
@@ -35,7 +37,7 @@ const addRoute = (env) => {
35
37
  throw new SetDataError('invalid_serialized_value', 'The sent value is not properly serialized');
36
38
  }
37
39
  const val = acebase_core_1.Transport.deserialize(data);
38
- if (path === '' && ((_a = req.user) === null || _a === void 0 ? void 0 : _a.uid) !== 'admin' && val !== null && typeof val === 'object') {
40
+ if (path === '' && ((_f = req.user) === null || _f === void 0 ? void 0 : _f.uid) !== 'admin' && val !== null && typeof val === 'object') {
39
41
  // Non-admin user: remove any private properties from the update object
40
42
  Object.keys(val).filter(key => key.startsWith('__')).forEach(key => delete val[key]);
41
43
  }
@@ -54,20 +56,19 @@ const addRoute = (env) => {
54
56
  }
55
57
  catch (err) {
56
58
  if (err instanceof acebase_1.SchemaValidationError) {
57
- (_b = env.logRef) === null || _b === void 0 ? void 0 : _b.push({ action: 'set_data', success: false, code: 'schema_validation_failed', path, error: err.reason, ip: req.ip, uid: (_d = (_c = req.user) === null || _c === void 0 ? void 0 : _c.uid) !== null && _d !== void 0 ? _d : null });
59
+ env.log.error(LOG_ACTION, 'schema_validation_failed', Object.assign(Object.assign({}, LOG_DETAILS), { reason: err.reason }));
58
60
  res.status(422).send({ code: 'schema_validation_failed', message: err.message });
59
61
  }
60
62
  else if (err instanceof SetDataError) {
61
- (_e = env.logRef) === null || _e === void 0 ? void 0 : _e.push({ action: 'set_data', success: false, code: err.code, path, ip: req.ip, uid: (_g = (_f = req.user) === null || _f === void 0 ? void 0 : _f.uid) !== null && _g !== void 0 ? _g : null });
63
+ env.log.error(LOG_ACTION, err.code, Object.assign(Object.assign({}, LOG_DETAILS), { message: err.message }));
62
64
  (0, error_1.sendBadRequestError)(res, err);
63
65
  }
64
66
  else {
65
67
  env.debug.error(`failed to set "${path}":`, err);
66
- (_h = env.logRef) === null || _h === void 0 ? void 0 : _h.push({ action: 'set_data', success: false, code: 'unknown_error', path, error: err.message, ip: req.ip, uid: (_k = (_j = req.user) === null || _j === void 0 ? void 0 : _j.uid) !== null && _k !== void 0 ? _k : null });
68
+ env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
67
69
  (0, error_1.sendError)(res, err);
68
70
  }
69
71
  }
70
- ;
71
72
  }));
72
73
  };
73
74
  exports.addRoute = addRoute;
@@ -27,8 +27,11 @@ const addRoutes = (env) => {
27
27
  env.app.post(`/transaction/${env.db.name}/start`, (req, res) => {
28
28
  var _a, _b, _c, _d;
29
29
  const data = req.body;
30
+ const LOG_ACTION = 'data.transaction.start';
31
+ const LOG_DETAILS = { ip: req.ip, uid: (_b = (_a = req.user) === null || _a === void 0 ? void 0 : _a.uid) !== null && _b !== void 0 ? _b : null, path: data.path };
30
32
  const access = env.rules.userHasAccess(req.user, data.path, true);
31
33
  if (!access.allow) {
34
+ env.log.error(LOG_ACTION, 'unauthorized', Object.assign(Object.assign({}, LOG_DETAILS), { rule_code: access.code, rule_path: (_c = access.rulePath) !== null && _c !== void 0 ? _c : null }), access.details);
32
35
  return (0, error_1.sendUnauthorizedError)(res, access.code, access.message);
33
36
  }
34
37
  // Start transaction
@@ -64,16 +67,19 @@ const addRoutes = (env) => {
64
67
  }
65
68
  catch (err) {
66
69
  env.debug.error(`failed to start transaction on "${tx.path}":`, err);
67
- (_a = env.logRef) === null || _a === void 0 ? void 0 : _a.push({ action: 'tx_start', success: false, code: (_b = err.code) !== null && _b !== void 0 ? _b : 'unknown_error', path: tx.path, error: err.message, ip: req.ip, uid: (_d = (_c = req.user) === null || _c === void 0 ? void 0 : _c.uid) !== null && _d !== void 0 ? _d : null });
70
+ env.log.error(LOG_ACTION, (_d = err.code) !== null && _d !== void 0 ? _d : 'unexpected', LOG_DETAILS, typeof err.code === 'undefined' ? err : null);
68
71
  (0, error_1.sendUnexpectedError)(res, err);
69
72
  }
70
73
  });
71
74
  // Finish transaction endpoint:
72
75
  env.app.post(`/transaction/${env.db.name}/finish`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
73
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
76
+ var _a, _b, _c, _d, _e, _f, _g;
74
77
  const data = req.body;
78
+ const LOG_ACTION = 'data.transaction.finish';
79
+ const LOG_DETAILS = { ip: req.ip, uid: (_b = (_a = req.user) === null || _a === void 0 ? void 0 : _a.uid) !== null && _b !== void 0 ? _b : null, path: data.path };
75
80
  const tx = _transactions.get(data.id);
76
81
  if (!tx || tx.path !== data.path) {
82
+ env.log.error(LOG_ACTION, tx ? 'wrong_path' : 'not_found', Object.assign(Object.assign({}, LOG_DETAILS), { id: data.id, tx_path: (_c = tx === null || tx === void 0 ? void 0 : tx.path) !== null && _c !== void 0 ? _c : null }));
77
83
  res.statusCode = 410; // Gone
78
84
  res.send(`transaction not found`);
79
85
  return;
@@ -82,6 +88,7 @@ const addRoutes = (env) => {
82
88
  _transactions.delete(tx.id);
83
89
  const access = env.rules.userHasAccess(req.user, tx.path, true);
84
90
  if (!access.allow) {
91
+ env.log.error(LOG_ACTION, 'unauthorized', Object.assign(Object.assign({}, LOG_DETAILS), { rule_code: access.code, rule_path: (_d = access.rulePath) !== null && _d !== void 0 ? _d : null }), access.details);
85
92
  return (0, error_1.sendUnauthorizedError)(res, access.code, access.message);
86
93
  }
87
94
  // Finish transaction
@@ -93,11 +100,11 @@ const addRoutes = (env) => {
93
100
  // then is sent to the server as an empty object: {}
94
101
  cancel = true;
95
102
  }
96
- else if (typeof ((_a = data.value) === null || _a === void 0 ? void 0 : _a.val) === 'undefined' || !['string', 'object', 'undefined'].includes(typeof ((_b = data.value) === null || _b === void 0 ? void 0 : _b.map))) {
103
+ else if (typeof ((_e = data.value) === null || _e === void 0 ? void 0 : _e.val) === 'undefined' || !['string', 'object', 'undefined'].includes(typeof ((_f = data.value) === null || _f === void 0 ? void 0 : _f.map))) {
97
104
  throw new DataTransactionError('invalid_serialized_value', 'The sent value is not properly serialized');
98
105
  }
99
106
  const newValue = cancel ? undefined : acebase_core_1.Transport.deserialize(data.value);
100
- if (tx.path === '' && ((_c = req.user) === null || _c === void 0 ? void 0 : _c.uid) !== 'admin' && newValue !== null && typeof newValue === 'object') {
107
+ if (tx.path === '' && ((_g = req.user) === null || _g === void 0 ? void 0 : _g.uid) !== 'admin' && newValue !== null && typeof newValue === 'object') {
101
108
  // Non-admin user: remove any private properties from the update object
102
109
  Object.keys(newValue).filter(key => key.startsWith('__')).forEach(key => delete newValue[key]);
103
110
  }
@@ -113,16 +120,16 @@ const addRoutes = (env) => {
113
120
  catch (err) {
114
121
  tx.finish(); // Finish without value cancels the transaction
115
122
  if (err instanceof acebase_1.SchemaValidationError) {
116
- (_d = env.logRef) === null || _d === void 0 ? void 0 : _d.push({ action: 'tx_finish', success: false, code: 'schema_validation_failed', path: tx.path, error: err.reason, ip: req.ip, uid: (_f = (_e = req.user) === null || _e === void 0 ? void 0 : _e.uid) !== null && _f !== void 0 ? _f : null });
123
+ env.log.error(LOG_ACTION, 'schema_validation_failed', Object.assign(Object.assign({}, LOG_DETAILS), { reason: err.reason }));
117
124
  res.status(422).send({ code: 'schema_validation_failed', message: err.message });
118
125
  }
119
126
  else if (err instanceof DataTransactionError) {
120
- (_g = env.logRef) === null || _g === void 0 ? void 0 : _g.push({ action: 'tx_finish', success: false, code: err.code, path: tx.path, ip: req.ip, uid: (_j = (_h = req.user) === null || _h === void 0 ? void 0 : _h.uid) !== null && _j !== void 0 ? _j : null });
127
+ env.log.error(LOG_ACTION, err.code, Object.assign(Object.assign({}, LOG_DETAILS), { message: err.message }));
121
128
  (0, error_1.sendBadRequestError)(res, err);
122
129
  }
123
130
  else {
124
- env.debug.error(`failed to finsih transaction on "${tx.path}":`, err);
125
- (_k = env.logRef) === null || _k === void 0 ? void 0 : _k.push({ action: 'tx_finish', success: false, code: 'unknown_error', path: tx.path, error: err.message, ip: req.ip, uid: (_m = (_l = req.user) === null || _l === void 0 ? void 0 : _l.uid) !== null && _m !== void 0 ? _m : null });
131
+ env.debug.error(`failed to finish transaction on "${tx.path}":`, err);
132
+ env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
126
133
  (0, error_1.sendError)(res, err);
127
134
  }
128
135
  }
@@ -22,11 +22,13 @@ class UpdateDataError extends Error {
22
22
  exports.UpdateDataError = UpdateDataError;
23
23
  const addRoute = (env) => {
24
24
  env.app.post(`/data/${env.db.name}/*`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
25
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
26
- // update data
25
+ var _a, _b, _c, _d, _e, _f;
27
26
  const path = req.path.slice(env.db.name.length + 7);
27
+ const LOG_ACTION = 'data.update';
28
+ const LOG_DETAILS = { ip: req.ip, uid: (_b = (_a = req.user) === null || _a === void 0 ? void 0 : _a.uid) !== null && _b !== void 0 ? _b : null, path };
28
29
  const access = env.rules.userHasAccess(req.user, path, true);
29
30
  if (!access.allow) {
31
+ env.log.error(LOG_ACTION, 'unauthorized', Object.assign(Object.assign({}, LOG_DETAILS), { rule_code: access.code, rule_path: (_c = access.rulePath) !== null && _c !== void 0 ? _c : null, rule_error: (_e = (_d = access.details) === null || _d === void 0 ? void 0 : _d.message) !== null && _e !== void 0 ? _e : null }));
30
32
  return (0, error_1.sendUnauthorizedError)(res, access.code, access.message);
31
33
  }
32
34
  try {
@@ -35,7 +37,7 @@ const addRoute = (env) => {
35
37
  throw new UpdateDataError('invalid_serialized_value', 'The sent value is not properly serialized');
36
38
  }
37
39
  const val = acebase_core_1.Transport.deserialize(data);
38
- if (path === '' && ((_a = req.user) === null || _a === void 0 ? void 0 : _a.uid) !== 'admin' && val !== null && typeof val === 'object') {
40
+ if (path === '' && ((_f = req.user) === null || _f === void 0 ? void 0 : _f.uid) !== 'admin' && val !== null && typeof val === 'object') {
39
41
  // Non-admin user: remove any private properties from the update object
40
42
  Object.keys(val).filter(key => key.startsWith('__')).forEach(key => delete val[key]);
41
43
  }
@@ -54,16 +56,16 @@ const addRoute = (env) => {
54
56
  }
55
57
  catch (err) {
56
58
  if (err instanceof acebase_1.SchemaValidationError) {
57
- (_b = env.logRef) === null || _b === void 0 ? void 0 : _b.push({ action: 'update_data', success: false, code: 'schema_validation_failed', path, error: err.reason, ip: req.ip, uid: (_d = (_c = req.user) === null || _c === void 0 ? void 0 : _c.uid) !== null && _d !== void 0 ? _d : null });
59
+ env.log.error(LOG_ACTION, 'schema_validation_failed', Object.assign(Object.assign({}, LOG_DETAILS), { reason: err.reason }));
58
60
  res.status(422).send({ code: 'schema_validation_failed', message: err.message });
59
61
  }
60
62
  else if (err instanceof UpdateDataError) {
61
- (_e = env.logRef) === null || _e === void 0 ? void 0 : _e.push({ action: 'update_data', success: false, code: err.code, path, ip: req.ip, uid: (_g = (_f = req.user) === null || _f === void 0 ? void 0 : _f.uid) !== null && _g !== void 0 ? _g : null });
63
+ env.log.error(LOG_ACTION, err.code, Object.assign(Object.assign({}, LOG_DETAILS), { message: err.message }));
62
64
  (0, error_1.sendBadRequestError)(res, err);
63
65
  }
64
66
  else {
65
67
  env.debug.error(`failed to update "${path}":`, err);
66
- (_h = env.logRef) === null || _h === void 0 ? void 0 : _h.push({ action: 'update_data', success: false, code: `unknown_error`, path, error: err.message, ip: req.ip, uid: (_k = (_j = req.user) === null || _j === void 0 ? void 0 : _j.uid) !== null && _k !== void 0 ? _k : null });
68
+ env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
67
69
  (0, error_1.sendError)(res, err);
68
70
  }
69
71
  }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.addRoute = void 0;
13
+ const admin_only_1 = require("../middleware/admin-only");
14
+ const addRoute = (env) => {
15
+ env.app.get(`/logs/${env.db.name}`, (0, admin_only_1.default)(env), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
16
+ // Get database logs
17
+ var _a, _b, _c;
18
+ // Create indexes if not there yet
19
+ const db = env.log.ref.db;
20
+ const createIndexes = [
21
+ db.indexes.create(env.log.ref.path, 'date')
22
+ ];
23
+ if (req.query.filter_col === 'action') {
24
+ createIndexes.push(db.indexes.create(env.log.ref.path, 'action', { include: ['date'] }));
25
+ }
26
+ if (req.query.filter_col === 'code') {
27
+ createIndexes.push(db.indexes.create(env.log.ref.path, 'code', { include: ['date'] }));
28
+ }
29
+ yield Promise.all(createIndexes);
30
+ try {
31
+ const query = env.log.query()
32
+ .take(parseInt((_a = req.query.take) !== null && _a !== void 0 ? _a : '100'))
33
+ .skip(parseInt((_b = req.query.skip) !== null && _b !== void 0 ? _b : '0'))
34
+ .sort((_c = req.query.sort) !== null && _c !== void 0 ? _c : 'date', false);
35
+ if (req.query.filter_col) {
36
+ query.filter(req.query.filter_col, req.query.filter_op, req.query.filter_val);
37
+ }
38
+ const snaps = yield query.get();
39
+ const logs = snaps.getValues();
40
+ res.send(logs);
41
+ }
42
+ catch (err) {
43
+ res.statusCode = 500;
44
+ res.send(err.message);
45
+ }
46
+ }));
47
+ };
48
+ exports.addRoute = addRoute;
49
+ exports.default = exports.addRoute;
50
+ //# sourceMappingURL=meta-logs.js.map
@@ -4,6 +4,7 @@ exports.addRoutes = void 0;
4
4
  const meta_info_1 = require("./meta-info");
5
5
  const meta_ping_1 = require("./meta-ping");
6
6
  const meta_stats_1 = require("./meta-stats");
7
+ const meta_logs_1 = require("./meta-logs");
7
8
  const addRoutes = (env) => {
8
9
  // Add info endpoint
9
10
  (0, meta_info_1.default)(env);
@@ -11,6 +12,8 @@ const addRoutes = (env) => {
11
12
  (0, meta_ping_1.default)(env);
12
13
  // Add database stats endpoint
13
14
  (0, meta_stats_1.default)(env);
15
+ // Add logs endpoint (admin only)
16
+ (0, meta_logs_1.default)(env);
14
17
  };
15
18
  exports.addRoutes = addRoutes;
16
19
  exports.default = exports.addRoutes;
@@ -18,8 +18,10 @@ const socketSignInSuccess = `<html><script>window.close()</script><body>Signed i
18
18
  const socketSignInFailed = `<html><script>window.close()</script><body>Failed to sign in. You can <a href="javascript:window.close()">close</a> this page</body></html>`;
19
19
  const addRoute = (env) => {
20
20
  env.app.get(`/oauth2/${env.db.name}/signin`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
21
- var _a, _b;
22
21
  // This is where the user is redirected to by the provider after signin or error
22
+ var _a, _b;
23
+ const LOG_ACTION = 'oauth2.signin';
24
+ const LOG_DETAILS = { ip: req.ip, provider: null };
23
25
  try {
24
26
  const state = (0, tokens_1.parseSignedPublicToken)(req.query.state, env.tokenSalt);
25
27
  if (req.query.error) {
@@ -36,6 +38,7 @@ const addRoute = (env) => {
36
38
  // Got authorization code
37
39
  const authCode = req.query.code;
38
40
  const provider = env.authProviders[state.provider];
41
+ LOG_DETAILS.provider = state.provider;
39
42
  // Get access & refresh tokens
40
43
  const tokens = yield provider.getAccessToken({ type: 'auth', auth_code: authCode, redirect_url: `${req.protocol}://${req.headers.host}/oauth2/${env.db.name}/signin` });
41
44
  let user_details;
@@ -142,7 +145,7 @@ const addRoute = (env) => {
142
145
  // Add provider details
143
146
  yield env.authRef.child(uid).child('settings').update(getProviderSettings());
144
147
  // Log success
145
- env.logRef.push({ action: 'oauth2_signin', success: true, ip: req.ip, date: new Date(), uid });
148
+ env.log.event(LOG_ACTION, Object.assign(Object.assign({}, LOG_DETAILS), { uid }));
146
149
  // Cache the user
147
150
  env.authCache.set(user.uid, user);
148
151
  // Request signin e-mail to be sent
@@ -162,13 +165,13 @@ const addRoute = (env) => {
162
165
  provider: state.provider
163
166
  };
164
167
  (_a = env.config.email) === null || _a === void 0 ? void 0 : _a.send(request).catch(err => {
165
- env.logRef.push({ action: 'oauth2_login_email', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, request });
168
+ env.log.error(LOG_ACTION + '.email', 'unexpected', Object.assign(Object.assign({}, LOG_DETAILS), { uid, request }), err);
166
169
  });
167
170
  }
168
171
  else if (snaps.length === 0) {
169
172
  // User does not exist, create
170
173
  if (!env.config.auth.allowUserSignup) {
171
- env.logRef.push({ action: 'oauth2_signup', success: false, code: 'user_signup_disabled', provider: state.provider, email: user_details.email, date: new Date() });
174
+ env.log.error(LOG_ACTION, 'user_signup_disabled', Object.assign(Object.assign({}, LOG_DETAILS), { email: user_details.email }));
172
175
  res.statusCode = 403; // Forbidden
173
176
  return res.send({ code: 'admin_only', message: 'Only admin is allowed to create users' });
174
177
  }
@@ -195,7 +198,7 @@ const addRoute = (env) => {
195
198
  const uid = userRef.key;
196
199
  user.uid = uid;
197
200
  // Log success
198
- env.logRef.push({ action: 'oauth2_signup', success: true, ip: req.ip, date: new Date(), uid });
201
+ env.log.event(LOG_ACTION, Object.assign(Object.assign({}, LOG_DETAILS), { uid }));
199
202
  // Cache the user
200
203
  env.authCache.set(user.uid, user);
201
204
  // Request welcome e-mail to be sent
@@ -215,7 +218,7 @@ const addRoute = (env) => {
215
218
  provider: state.provider
216
219
  };
217
220
  (_b = env.config.email) === null || _b === void 0 ? void 0 : _b.send(request).catch(err => {
218
- env.logRef.push({ action: 'oauth2_signup_email', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, request });
221
+ env.log.error(LOG_ACTION + '.email', 'unexpected', Object.assign(Object.assign({}, LOG_DETAILS), { uid, request }), err);
219
222
  });
220
223
  }
221
224
  else {
@@ -29,6 +29,7 @@ const webmanager_1 = require("./routes/webmanager");
29
29
  const meta_1 = require("./routes/meta");
30
30
  const swagger_1 = require("./middleware/swagger");
31
31
  const cache_1 = require("./middleware/cache");
32
+ const logger_1 = require("./logger");
32
33
  // type PrivateLocalSettings = AceBaseLocalSettings & { storage: PrivateStorageSettings };
33
34
  class AceBaseServerNotReadyError extends Error {
34
35
  constructor() { super('Server is not ready yet'); }
@@ -117,6 +118,7 @@ class AceBaseServer extends acebase_core_1.SimpleEventEmitter {
117
118
  const securityRef = authDb ? authDb === db ? db.ref('__auth__/security') : authDb.ref('security') : null;
118
119
  const authRef = authDb ? authDb === db ? db.ref('__auth__/accounts') : authDb.ref('accounts') : null;
119
120
  const logRef = authDb ? authDb === db ? db.ref('__log__') : authDb.ref('log') : null;
121
+ const logger = new logger_1.DatabaseLog(logRef);
120
122
  // Setup rules
121
123
  const rulesFilePath = `${this.config.path}/${this.db.name}.acebase/rules.json`;
122
124
  const rules = new rules_1.PathBasedRules(rulesFilePath, config.auth.defaultAccessRule, { db, debug: this.debug, authEnabled: this.config.auth.enabled });
@@ -129,7 +131,7 @@ class AceBaseServer extends acebase_core_1.SimpleEventEmitter {
129
131
  debug: this.debug,
130
132
  securityRef,
131
133
  authRef,
132
- logRef,
134
+ log: logger,
133
135
  tokenSalt: null,
134
136
  clients,
135
137
  authCache: null,
@@ -10,6 +10,8 @@ if (process.platform === 'win32' && currentDir.startsWith('/')) {
10
10
  // 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
11
11
  currentDir = currentDir.slice(1);
12
12
  }
13
+ // tsc-esm-fix also does not use decodeURI to remove encoded characters (such as %20 for spaces)
14
+ currentDir = decodeURI(currentDir);
13
15
  exports.packageRootPath = (0, path_1.resolve)(currentDir, '../../..');
14
16
  exports.default = exports.packageRootPath;
15
17
  //# sourceMappingURL=rootpath.js.map
@@ -30,6 +30,9 @@ exports.SignInError = SignInError;
30
30
  * @returns
31
31
  */
32
32
  const signIn = (credentials, env, req) => __awaiter(void 0, void 0, void 0, function* () {
33
+ var _a;
34
+ const LOG_ACTION = 'auth.signin';
35
+ const LOG_DETAILS = { ip: req.ip, method: credentials.method };
33
36
  try {
34
37
  const query = env.authRef.query();
35
38
  let tokenDetails;
@@ -47,7 +50,7 @@ const signIn = (credentials, env, req) => __awaiter(void 0, void 0, void 0, func
47
50
  }
48
51
  break;
49
52
  }
50
- case 'private_token': {
53
+ case 'internal': {
51
54
  // Method used internally: uses the access token extracted from a public access token (see tokenDetails.access_token in above 'token' case)
52
55
  if (typeof credentials.access_token !== 'string') {
53
56
  throw new SignInError('invalid_details', 'sign in request has invalid arguments');
@@ -122,7 +125,9 @@ const signIn = (credentials, env, req) => __awaiter(void 0, void 0, void 0, func
122
125
  // Update db
123
126
  yield snap.ref.update(updates);
124
127
  // Log history item
125
- env.logRef.push({ action: `signin`, type: credentials.method, [credentials.method]: credentials[credentials.method], ip: req.ip, date: new Date(), success: true });
128
+ if (credentials.method !== 'internal') {
129
+ env.log.event(LOG_ACTION, LOG_DETAILS);
130
+ }
126
131
  // Add to cache
127
132
  env.authCache.set(user.uid, user);
128
133
  // Bind user to current request
@@ -131,7 +136,7 @@ const signIn = (credentials, env, req) => __awaiter(void 0, void 0, void 0, func
131
136
  }
132
137
  catch (err) {
133
138
  // Log error
134
- env.logRef.push(Object.assign({ 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));
139
+ env.log.error(LOG_ACTION, (_a = err.code) !== null && _a !== void 0 ? _a : 'unexpected', LOG_DETAILS, typeof err.code === 'undefined' ? err : null);
135
140
  throw err;
136
141
  }
137
142
  });
package/dist/cjs/start.js CHANGED
@@ -42,6 +42,9 @@ if (+getVariable('TXLOG', 0) === 1) {
42
42
  maxAge: +getVariable('TXDAYS', 30)
43
43
  };
44
44
  }
45
+ if (+getVariable('AUTH', 1) === 0) {
46
+ options.authentication = { enabled: false };
47
+ }
45
48
  const server = new server_1.AceBaseServer(dbname, options);
46
49
  server.once("ready", () => {
47
50
  var _a;
@@ -9,11 +9,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.addWebsocketServer = void 0;
12
+ exports.addWebsocketServer = exports.SocketRequestError = void 0;
13
13
  const acebase_core_1 = require("acebase-core");
14
14
  const clients_1 = require("../shared/clients");
15
15
  const tokens_1 = require("../shared/tokens");
16
16
  const socket_io_1 = require("./socket.io");
17
+ class SocketRequestError extends Error {
18
+ constructor(code, message) {
19
+ super(message);
20
+ this.code = code;
21
+ }
22
+ }
23
+ exports.SocketRequestError = SocketRequestError;
17
24
  const addWebsocketServer = (env) => {
18
25
  // TODO: Allow using uWebSockets.js server instead of Socket.IO
19
26
  const serverManager = (0, socket_io_1.createServer)(env);
@@ -106,7 +113,7 @@ const addWebsocketServer = (env) => {
106
113
  serverManager.send(event.socket, 'oauth2-signin', { error: err.message });
107
114
  }
108
115
  }));
109
- const acknowlegdeRequest = (socket, requestId) => {
116
+ const acknowledgeRequest = (socket, requestId) => {
110
117
  // Send acknowledgement
111
118
  serverManager.send(socket, 'result', {
112
119
  success: true,
@@ -122,6 +129,7 @@ const addWebsocketServer = (env) => {
122
129
  });
123
130
  };
124
131
  serverManager.on('subscribe', event => {
132
+ var _a, _b;
125
133
  // Client wants to subscribe to events on a node
126
134
  const client = getClientBySocketId(event.socket_id, 'subscribe');
127
135
  if (!client) {
@@ -131,12 +139,12 @@ const addWebsocketServer = (env) => {
131
139
  const subscriptionPath = event.data.path;
132
140
  const isSubscribed = () => subscriptionPath in client.subscriptions && client.subscriptions[subscriptionPath].some(s => s.event === event.data.event);
133
141
  if (isSubscribed()) {
134
- return acknowlegdeRequest(event.socket, event.data.req_id);
142
+ return acknowledgeRequest(event.socket, event.data.req_id);
135
143
  }
136
144
  // Get client
137
145
  // const client = clients.get(socket.id);
138
146
  if (!env.rules.userHasAccess(client.user, subscriptionPath, false)) {
139
- env.logRef.push({ action: 'subscribe', success: false, code: 'access_denied', uid: client.user ? client.user.uid : '-', path: subscriptionPath });
147
+ env.log.error('event.subscribe', 'access_denied', { uid: (_b = (_a = client.user) === null || _a === void 0 ? void 0 : _a.uid) !== null && _b !== void 0 ? _b : 'anonymous', path: subscriptionPath });
140
148
  return failRequest(event.socket, event.data.req_id, 'access_denied');
141
149
  }
142
150
  const callback = (err, path, currentValue, previousValue, context) => {
@@ -177,7 +185,7 @@ const addWebsocketServer = (env) => {
177
185
  let subscr = { path: subscriptionPath, event: event.data.event, callback };
178
186
  pathSubs.push(subscr);
179
187
  env.db.api.subscribe(subscriptionPath, event.data.event, callback);
180
- acknowlegdeRequest(event.socket, event.data.req_id);
188
+ acknowledgeRequest(event.socket, event.data.req_id);
181
189
  });
182
190
  serverManager.on('unsubscribe', event => {
183
191
  // Client unsubscribes from events on a node
@@ -190,7 +198,7 @@ const addWebsocketServer = (env) => {
190
198
  let pathSubs = client.subscriptions[event.data.path];
191
199
  if (!pathSubs) {
192
200
  // We have no knowledge of any active subscriptions on this path
193
- return acknowlegdeRequest(event.socket, event.data.req_id);
201
+ return acknowledgeRequest(event.socket, event.data.req_id);
194
202
  }
195
203
  let remove = pathSubs;
196
204
  if (event.data.event) {
@@ -207,7 +215,7 @@ const addWebsocketServer = (env) => {
207
215
  // No subscriptions left on this path, remove the path entry
208
216
  delete client.subscriptions[event.data.path];
209
217
  }
210
- return acknowlegdeRequest(event.socket, event.data.req_id);
218
+ return acknowledgeRequest(event.socket, event.data.req_id);
211
219
  });
212
220
  serverManager.on('query-unsubscribe', event => {
213
221
  // Client unsubscribing from realtime query events
@@ -218,15 +226,18 @@ const addWebsocketServer = (env) => {
218
226
  env.debug.verbose(`Client ${event.socket_id} is unsubscribing from realtime query "${event.data.query_id}"`);
219
227
  // const client = clients.get(socket.id);
220
228
  delete client.realtimeQueries[event.data.query_id];
221
- acknowlegdeRequest(event.socket, event.data.req_id);
229
+ acknowledgeRequest(event.socket, event.data.req_id);
222
230
  });
223
231
  const TRANSACTION_TIMEOUT_MS = 10000; // 10s to finish a started transaction
224
232
  serverManager.on('transaction-start', event => {
233
+ var _a, _b, _c;
225
234
  // Start transaction
226
235
  const client = getClientBySocketId(event.socket_id, 'transaction-start');
227
- if (!client) {
236
+ if (!client || !event.data) {
228
237
  return;
229
238
  }
239
+ const LOG_ACTION = 'socket.transaction.start';
240
+ const LOG_DETAILS = { ip: event.socket.conn.remoteAddress, uid: (_b = (_a = client.user) === null || _a === void 0 ? void 0 : _a.uid) !== null && _b !== void 0 ? _b : null, path: event.data.path };
230
241
  env.debug.verbose(`Client ${event.socket_id} is sending transaction start request on path "${event.data.path}"`);
231
242
  const data = event.data;
232
243
  const tx = {
@@ -238,11 +249,14 @@ const addWebsocketServer = (env) => {
238
249
  timeout: setTimeout(() => {
239
250
  delete client.transactions[tx.id];
240
251
  tx.finish(); // Finish without value cancels the transaction
252
+ env.log.error(LOG_ACTION, 'timeout', LOG_DETAILS);
241
253
  serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: 'timeout' });
242
254
  }, TRANSACTION_TIMEOUT_MS)
243
255
  };
244
- if (!env.rules.userHasAccess(client.user, data.path, true)) {
256
+ const access = env.rules.userHasAccess(client.user, data.path, true);
257
+ if (!access.allow) {
245
258
  // throw new AccessDeniedError(`access_denied`);
259
+ env.log.error(LOG_ACTION, 'unauthorized', Object.assign(Object.assign({}, LOG_DETAILS), { rule_code: access.code, rule_path: (_c = access.rulePath) !== null && _c !== void 0 ? _c : null }), access.details);
246
260
  return serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: 'access_denied' });
247
261
  }
248
262
  // Bind to client
@@ -264,34 +278,39 @@ const addWebsocketServer = (env) => {
264
278
  }, { context: tx.context });
265
279
  });
266
280
  serverManager.on('transaction-finish', (event) => __awaiter(void 0, void 0, void 0, function* () {
281
+ var _a, _b, _c, _d, _e;
267
282
  // Finish transaction
268
283
  const client = getClientBySocketId(event.socket_id, 'transaction-finish');
269
- if (!client) {
284
+ if (!client || !event.data) {
270
285
  return;
271
286
  }
287
+ const LOG_ACTION = 'socket.transaction.finish';
288
+ const LOG_DETAILS = { ip: event.socket.conn.remoteAddress, uid: (_b = (_a = client.user) === null || _a === void 0 ? void 0 : _a.uid) !== null && _b !== void 0 ? _b : null, path: event.data.path };
272
289
  const data = event.data;
273
290
  const tx = client.transactions[data.id];
274
291
  try {
275
292
  if (!tx || data.path !== tx.path) {
276
- throw new Error('transaction_not_found');
293
+ env.log.error(LOG_ACTION, tx ? 'wrong_path' : 'not_found', Object.assign(Object.assign({}, LOG_DETAILS), { id: data.id, tx_path: (_c = tx === null || tx === void 0 ? void 0 : tx.path) !== null && _c !== void 0 ? _c : null }));
294
+ throw new SocketRequestError('transaction_not_found', 'Transaction not found');
277
295
  }
278
296
  clearTimeout(tx.timeout);
279
297
  delete client.transactions[data.id];
280
- if (!env.rules.userHasAccess(client.user, data.path, true)) {
281
- throw new Error('access_denied');
298
+ const access = env.rules.userHasAccess(client.user, data.path, true);
299
+ if (!access.allow) {
300
+ env.log.error(LOG_ACTION, 'unauthorized', Object.assign(Object.assign({}, LOG_DETAILS), { rule_code: access.code, rule_path: (_d = access.rulePath) !== null && _d !== void 0 ? _d : null }), access.details);
301
+ throw new SocketRequestError('access_denied', 'Access denied');
282
302
  }
283
303
  const newValue = 'val' in data.value ? acebase_core_1.Transport.deserialize(data.value) : undefined;
284
- try {
285
- const { cursor } = yield tx.finish(newValue);
286
- env.debug.verbose(`Transaction ${tx.id} finished`);
287
- serverManager.send(event.socket, 'tx_completed', { id: tx.id, context: { cursor } });
288
- }
289
- catch (err) {
290
- serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: err.message });
291
- }
304
+ const { cursor } = yield tx.finish(newValue);
305
+ env.debug.verbose(`Transaction ${tx.id} finished`);
306
+ serverManager.send(event.socket, 'tx_completed', { id: tx.id, context: { cursor } });
292
307
  }
293
308
  catch (err) {
294
- serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: err.message, data });
309
+ if (!(err instanceof SocketRequestError)) {
310
+ // Other errors have been logged already
311
+ env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
312
+ }
313
+ serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: (_e = err.code) !== null && _e !== void 0 ? _e : err.message, data });
295
314
  tx.finish(); // Finish with undefined, canceling the transaction
296
315
  }
297
316
  }));
@@ -0,0 +1 @@
1
+ export * from '../types';