soul-cli 0.10.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.env.sample +1 -0
  2. package/.github/workflows/ci.yml +2 -0
  3. package/.prettierrc +4 -0
  4. package/.release-please-manifest.json +1 -1
  5. package/CHANGELOG.md +25 -0
  6. package/README.md +3 -0
  7. package/ROADMAP.md +83 -25
  8. package/docs/api/batch-examples.md +55 -0
  9. package/docs/api/rows-examples.md +10 -2
  10. package/docs/api-examples.md +1 -0
  11. package/docs/auth.md +10 -0
  12. package/package.json +1 -1
  13. package/src/cli.js +6 -0
  14. package/src/config/index.js +7 -0
  15. package/src/config/index.test.js +10 -0
  16. package/src/constants/messages.js +1 -1
  17. package/src/controllers/batch.js +128 -0
  18. package/src/controllers/batch.test.js +180 -0
  19. package/src/controllers/rows.js +223 -213
  20. package/src/controllers/rows.query.test.js +369 -0
  21. package/src/index.js +2 -0
  22. package/src/middlewares/api.js +6 -2
  23. package/src/middlewares/api.test.js +27 -13
  24. package/src/middlewares/auth.js +52 -33
  25. package/src/middlewares/auth.test.js +137 -3
  26. package/src/middlewares/broadcast.js +7 -11
  27. package/src/routes/batch.js +12 -0
  28. package/src/routes/rows.js +1 -0
  29. package/src/schemas/batch.js +27 -0
  30. package/src/schemas/rows.js +4 -1
  31. package/src/services/authService.js +31 -4
  32. package/src/services/authService.test.js +76 -0
  33. package/src/services/rowService.js +12 -6
  34. package/src/services/rowService.test.js +37 -0
  35. package/src/swagger/index.js +14 -16
  36. package/src/swagger/swagger.json +68 -59
  37. package/src/utils/query.js +285 -0
  38. package/src/utils/query.test.js +147 -0
  39. package/src/utils/sql.js +24 -0
  40. package/src/utils/sql.test.js +14 -0
  41. package/src/websocket.js +10 -8
  42. package/src/websocket.test.js +52 -0
  43. package/src/schemas/index.js +0 -30
package/src/websocket.js CHANGED
@@ -54,19 +54,21 @@ wss.on('connection', async function (ws, request) {
54
54
  const cookies = parseCookie(request.headers.cookie || '');
55
55
  const accessToken = cookies.accessToken;
56
56
 
57
- if (!accessToken) {
57
+ if (!accessToken && !config.anonRole) {
58
58
  ws.send(JSON.stringify({ message: 'Missing access token' }));
59
59
  ws.close();
60
60
  return;
61
61
  }
62
62
 
63
- let payload;
64
- try {
65
- payload = await decodeToken(accessToken, config.tokenSecret);
66
- } catch {
67
- ws.send(JSON.stringify({ message: 'Invalid access token' }));
68
- ws.close();
69
- return;
63
+ let payload = null;
64
+ if (accessToken) {
65
+ try {
66
+ payload = await decodeToken(accessToken, config.tokenSecret);
67
+ } catch {
68
+ ws.send(JSON.stringify({ message: 'Invalid access token' }));
69
+ ws.close();
70
+ return;
71
+ }
70
72
  }
71
73
 
72
74
  if (!authService.hasTablePermission({ payload, tableName, verb: 'GET' })) {
@@ -6,6 +6,7 @@ const app = require('./index');
6
6
  const { wss } = require('./websocket');
7
7
  const { generateToken } = require('./utils');
8
8
  const config = require('./config');
9
+ const db = require('./db');
9
10
 
10
11
  describe('websocket', () => {
11
12
  let server;
@@ -118,4 +119,55 @@ describe('websocket', () => {
118
119
 
119
120
  ws.close();
120
121
  });
122
+
123
+ describe('anonymous role', () => {
124
+ let roleId;
125
+
126
+ beforeAll(() => {
127
+ roleId = db
128
+ .prepare(`INSERT INTO _roles (name) VALUES ('ws_public')`)
129
+ .run().lastInsertRowid;
130
+ db.prepare(
131
+ `INSERT INTO _roles_permissions (role_id, table_name, "create", read, "update", "delete") VALUES (?, 'users', 0, 1, 0, 0)`,
132
+ ).run(roleId);
133
+ });
134
+
135
+ afterAll(() => {
136
+ db.prepare(`DELETE FROM _roles_permissions WHERE role_id = ?`).run(
137
+ roleId,
138
+ );
139
+ db.prepare(`DELETE FROM _roles WHERE id = ?`).run(roleId);
140
+ });
141
+
142
+ beforeEach(() => {
143
+ config.anonRole = 'ws_public';
144
+ });
145
+
146
+ afterEach(() => {
147
+ config.anonRole = null;
148
+ });
149
+
150
+ it('lets a client without a token subscribe to a table the anon role can read', async () => {
151
+ const ws = new WebSocket(wsUrl('/ws/tables/users'));
152
+
153
+ const message = await new Promise((resolve) => {
154
+ ws.on('message', (msg) => resolve(JSON.parse(msg.toString())));
155
+ });
156
+
157
+ expect(message.message).toContain('Subscribed to table "users"');
158
+ ws.close();
159
+ });
160
+
161
+ it('rejects a client without a token on a table the anon role cannot read', (done) => {
162
+ const ws = new WebSocket(wsUrl('/ws/tables/posts'));
163
+
164
+ ws.on('message', (message) => {
165
+ expect(JSON.parse(message.toString()).message).toContain(
166
+ 'Not authorized',
167
+ );
168
+ });
169
+
170
+ ws.on('close', () => done());
171
+ });
172
+ });
121
173
  });
@@ -1,30 +0,0 @@
1
- const Joi = require('joi');
2
-
3
- const transaction = Joi.object({
4
- query: Joi.object().required(),
5
- params: Joi.object().required(),
6
- body: Joi.object({
7
- transaction: Joi.array()
8
- .items(
9
- Joi.object({
10
- statement: Joi.string().required(),
11
- values: Joi.object().required(),
12
- }),
13
- Joi.object({
14
- query: Joi.string().required(),
15
- }),
16
- )
17
- .required(),
18
- }).required(),
19
-
20
- // .unknown(true): see the comment in schemas/tables.js -- rejects
21
- // unrelated cookies the browser sends otherwise.
22
- cookies: Joi.object({
23
- refreshToken: Joi.string().optional(),
24
- accessToken: Joi.string().optional(),
25
- }).unknown(true),
26
- });
27
-
28
- module.exports = {
29
- transaction,
30
- };