soul-cli 0.11.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.
- package/.env.sample +1 -0
- package/.prettierrc +4 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +13 -0
- package/README.md +3 -0
- package/docs/api/batch-examples.md +55 -0
- package/docs/api-examples.md +1 -0
- package/docs/auth.md +10 -0
- package/package.json +1 -1
- package/src/cli.js +6 -0
- package/src/config/index.js +7 -0
- package/src/config/index.test.js +10 -0
- package/src/constants/messages.js +1 -1
- package/src/controllers/batch.js +128 -0
- package/src/controllers/batch.test.js +180 -0
- package/src/controllers/rows.js +108 -121
- package/src/index.js +2 -0
- package/src/middlewares/api.js +6 -2
- package/src/middlewares/api.test.js +27 -13
- package/src/middlewares/auth.js +52 -33
- package/src/middlewares/auth.test.js +137 -3
- package/src/middlewares/broadcast.js +7 -11
- package/src/routes/batch.js +12 -0
- package/src/routes/rows.js +1 -0
- package/src/schemas/batch.js +27 -0
- package/src/services/authService.js +31 -4
- package/src/services/authService.test.js +76 -0
- package/src/swagger/index.js +14 -16
- package/src/swagger/swagger.json +48 -57
- package/src/websocket.js +10 -8
- package/src/websocket.test.js +52 -0
- package/src/schemas/index.js +0 -30
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const Joi = require('joi');
|
|
2
|
+
|
|
3
|
+
const MAX_OPERATIONS = 100;
|
|
4
|
+
|
|
5
|
+
const batch = Joi.object({
|
|
6
|
+
query: Joi.object().required(),
|
|
7
|
+
params: Joi.object().required(),
|
|
8
|
+
body: Joi.object({
|
|
9
|
+
operations: Joi.array()
|
|
10
|
+
.items(
|
|
11
|
+
Joi.object({
|
|
12
|
+
method: Joi.string().valid('POST', 'PUT', 'DELETE').required(),
|
|
13
|
+
path: Joi.string().required(),
|
|
14
|
+
body: Joi.object(),
|
|
15
|
+
}),
|
|
16
|
+
)
|
|
17
|
+
.min(1)
|
|
18
|
+
.max(MAX_OPERATIONS)
|
|
19
|
+
.required(),
|
|
20
|
+
}).required(),
|
|
21
|
+
cookies: Joi.object({
|
|
22
|
+
refreshToken: Joi.string().optional(),
|
|
23
|
+
accessToken: Joi.string().optional(),
|
|
24
|
+
}).unknown(true),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
module.exports = { batch };
|
|
@@ -2,6 +2,7 @@ const buildRowService = require('./rowService');
|
|
|
2
2
|
|
|
3
3
|
const { constantRoles, dbConstants, apiConstants } = require('../constants');
|
|
4
4
|
const { toBoolean } = require('../utils');
|
|
5
|
+
const config = require('../config');
|
|
5
6
|
|
|
6
7
|
const {
|
|
7
8
|
USERS_TABLE,
|
|
@@ -109,10 +110,26 @@ module.exports = (db) => {
|
|
|
109
110
|
return result;
|
|
110
111
|
},
|
|
111
112
|
|
|
113
|
+
getAnonRoleId() {
|
|
114
|
+
if (!config.anonRole) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const [role] = rowService.get({
|
|
119
|
+
tableName: ROLES_TABLE,
|
|
120
|
+
whereString: `WHERE ${tableFields.ROLE_NAME}=?`,
|
|
121
|
+
whereStringValues: [config.anonRole],
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
return role ? role.id : null;
|
|
125
|
+
},
|
|
126
|
+
|
|
112
127
|
// Shared by the REST `hasAccess` middleware and the WebSocket connection
|
|
113
128
|
// handler so the two auth surfaces can't drift apart.
|
|
129
|
+
// A null payload is an anonymous request. The anon role also applies to
|
|
130
|
+
// authenticated users, but never to system (`_`-prefixed) tables.
|
|
114
131
|
hasTablePermission({ payload, tableName, verb }) {
|
|
115
|
-
if (toBoolean(payload.isSuperuser)) {
|
|
132
|
+
if (payload && toBoolean(payload.isSuperuser)) {
|
|
116
133
|
return true;
|
|
117
134
|
}
|
|
118
135
|
|
|
@@ -120,9 +137,19 @@ module.exports = (db) => {
|
|
|
120
137
|
return false;
|
|
121
138
|
}
|
|
122
139
|
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
140
|
+
const roleIds = [...(payload?.roleIds || [])];
|
|
141
|
+
if (!tableName.startsWith('_')) {
|
|
142
|
+
const anonRoleId = this.getAnonRoleId();
|
|
143
|
+
if (anonRoleId !== null) {
|
|
144
|
+
roleIds.push(anonRoleId);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (roleIds.length === 0) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const rolePermissions = this.getPermissionByRoleIds({ roleIds });
|
|
126
153
|
const resourcePermission = rolePermissions.filter(
|
|
127
154
|
(row) => row.table_name === tableName,
|
|
128
155
|
);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const Database = require('better-sqlite3');
|
|
2
2
|
|
|
3
3
|
const buildAuthService = require('./authService');
|
|
4
|
+
const config = require('../config');
|
|
4
5
|
|
|
5
6
|
// hasTablePermission is exercised in the rest of the suite only via
|
|
6
7
|
// superuser tokens (which short-circuit at the very first check) or via
|
|
@@ -113,3 +114,78 @@ describe('authService.hasTablePermission', () => {
|
|
|
113
114
|
).toBe(true);
|
|
114
115
|
});
|
|
115
116
|
});
|
|
117
|
+
|
|
118
|
+
describe('authService anonymous role', () => {
|
|
119
|
+
let db;
|
|
120
|
+
let authService;
|
|
121
|
+
|
|
122
|
+
beforeEach(() => {
|
|
123
|
+
db = new Database(':memory:');
|
|
124
|
+
db.exec(`
|
|
125
|
+
CREATE TABLE _roles (id INTEGER PRIMARY KEY, name TEXT);
|
|
126
|
+
CREATE TABLE _roles_permissions (
|
|
127
|
+
id INTEGER PRIMARY KEY,
|
|
128
|
+
role_id INTEGER,
|
|
129
|
+
table_name TEXT,
|
|
130
|
+
"create" BOOLEAN DEFAULT 0,
|
|
131
|
+
read BOOLEAN DEFAULT 0,
|
|
132
|
+
"update" BOOLEAN DEFAULT 0,
|
|
133
|
+
"delete" BOOLEAN DEFAULT 0
|
|
134
|
+
);
|
|
135
|
+
INSERT INTO _roles (id, name) VALUES (1, 'public'), (2, 'editor');
|
|
136
|
+
INSERT INTO _roles_permissions (role_id, table_name, read) VALUES (1, 'items', 1);
|
|
137
|
+
INSERT INTO _roles_permissions (role_id, table_name, read) VALUES (1, '_users', 1);
|
|
138
|
+
INSERT INTO _roles_permissions (role_id, table_name, "update") VALUES (2, 'items', 1);
|
|
139
|
+
`);
|
|
140
|
+
authService = buildAuthService(db);
|
|
141
|
+
config.anonRole = 'public';
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
afterEach(() => {
|
|
145
|
+
config.anonRole = null;
|
|
146
|
+
db.close();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const check = (payload, tableName, verb) =>
|
|
150
|
+
authService.hasTablePermission({ payload, tableName, verb });
|
|
151
|
+
|
|
152
|
+
it('getAnonRoleId returns the id of the configured role', () => {
|
|
153
|
+
expect(authService.getAnonRoleId()).toBe(1);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('getAnonRoleId returns null when unset or when the role does not exist', () => {
|
|
157
|
+
config.anonRole = null;
|
|
158
|
+
expect(authService.getAnonRoleId()).toBeNull();
|
|
159
|
+
|
|
160
|
+
config.anonRole = 'missing';
|
|
161
|
+
expect(authService.getAnonRoleId()).toBeNull();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('allows an anonymous request only for what the anon role grants', () => {
|
|
165
|
+
expect(check(null, 'items', 'GET')).toBe(true);
|
|
166
|
+
expect(check(null, 'items', 'POST')).toBe(false);
|
|
167
|
+
expect(check(null, 'other', 'GET')).toBe(false);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('denies an anonymous request when no anon role is configured', () => {
|
|
171
|
+
config.anonRole = null;
|
|
172
|
+
expect(check(null, 'items', 'GET')).toBe(false);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('denies an anonymous request when the configured role does not exist', () => {
|
|
176
|
+
config.anonRole = 'missing';
|
|
177
|
+
expect(check(null, 'items', 'GET')).toBe(false);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('never applies the anon role to system tables', () => {
|
|
181
|
+
expect(check(null, '_users', 'GET')).toBe(false);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('lets an authenticated user fall back to the anon role permissions', () => {
|
|
185
|
+
const payload = { isSuperuser: false, roleIds: [2] };
|
|
186
|
+
|
|
187
|
+
expect(check(payload, 'items', 'PUT')).toBe(true);
|
|
188
|
+
expect(check(payload, 'items', 'GET')).toBe(true);
|
|
189
|
+
expect(check(payload, 'items', 'DELETE')).toBe(false);
|
|
190
|
+
});
|
|
191
|
+
});
|
package/src/swagger/index.js
CHANGED
|
@@ -42,20 +42,6 @@ const doc = {
|
|
|
42
42
|
name: 'users',
|
|
43
43
|
},
|
|
44
44
|
Row: {},
|
|
45
|
-
Query: {
|
|
46
|
-
query: 'SELECT * FROM users',
|
|
47
|
-
},
|
|
48
|
-
Statement: {
|
|
49
|
-
statement:
|
|
50
|
-
'INSERT INTO users (id, firstName, lastName) VALUES (:id, :firstName, :lastName)',
|
|
51
|
-
values: { id: 1, firstName: 'John', lastName: 'Doe' },
|
|
52
|
-
},
|
|
53
|
-
Transaction: {
|
|
54
|
-
transaction: [
|
|
55
|
-
{ $ref: '#/definitions/Query' },
|
|
56
|
-
{ $ref: '#/definitions/Statement' },
|
|
57
|
-
],
|
|
58
|
-
},
|
|
59
45
|
ForeignKey: {
|
|
60
46
|
table: 'users',
|
|
61
47
|
column: 'id',
|
|
@@ -123,8 +109,20 @@ const doc = {
|
|
|
123
109
|
pks: [1, 2, 3],
|
|
124
110
|
},
|
|
125
111
|
|
|
126
|
-
|
|
127
|
-
|
|
112
|
+
BatchRequestBody: {
|
|
113
|
+
operations: [
|
|
114
|
+
{
|
|
115
|
+
method: 'POST',
|
|
116
|
+
path: '/api/tables/users/rows',
|
|
117
|
+
body: { fields: { firstName: 'John', lastName: 'Doe' } },
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
method: 'PUT',
|
|
121
|
+
path: '/api/tables/users/rows/1',
|
|
122
|
+
body: { fields: { lastName: 'Smith' } },
|
|
123
|
+
},
|
|
124
|
+
{ method: 'DELETE', path: '/api/tables/users/rows/2' },
|
|
125
|
+
],
|
|
128
126
|
},
|
|
129
127
|
ObtainAccessTokenRequestBody: {
|
|
130
128
|
fields: {
|
package/src/swagger/swagger.json
CHANGED
|
@@ -54,6 +54,34 @@
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
},
|
|
57
|
+
"/api/batch": {
|
|
58
|
+
"post": {
|
|
59
|
+
"tags": ["Rows"],
|
|
60
|
+
"summary": "Batch",
|
|
61
|
+
"description": "Run row inserts, updates and deletes atomically: if one fails, none are applied.",
|
|
62
|
+
"parameters": [
|
|
63
|
+
{
|
|
64
|
+
"name": "body",
|
|
65
|
+
"in": "body",
|
|
66
|
+
"required": true,
|
|
67
|
+
"schema": {
|
|
68
|
+
"$ref": "#/definitions/BatchRequestBody"
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
],
|
|
72
|
+
"responses": {
|
|
73
|
+
"200": {
|
|
74
|
+
"description": "OK"
|
|
75
|
+
},
|
|
76
|
+
"400": {
|
|
77
|
+
"description": "Bad Request"
|
|
78
|
+
},
|
|
79
|
+
"401": {
|
|
80
|
+
"description": "Unauthorized"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
57
85
|
"/api/tables/": {
|
|
58
86
|
"get": {
|
|
59
87
|
"tags": ["Tables"],
|
|
@@ -433,9 +461,6 @@
|
|
|
433
461
|
}
|
|
434
462
|
],
|
|
435
463
|
"responses": {
|
|
436
|
-
"200": {
|
|
437
|
-
"description": "OK"
|
|
438
|
-
},
|
|
439
464
|
"400": {
|
|
440
465
|
"description": "Bad Request"
|
|
441
466
|
},
|
|
@@ -475,9 +500,6 @@
|
|
|
475
500
|
}
|
|
476
501
|
],
|
|
477
502
|
"responses": {
|
|
478
|
-
"200": {
|
|
479
|
-
"description": "OK"
|
|
480
|
-
},
|
|
481
503
|
"400": {
|
|
482
504
|
"description": "Bad Request"
|
|
483
505
|
},
|
|
@@ -486,9 +508,6 @@
|
|
|
486
508
|
},
|
|
487
509
|
"403": {
|
|
488
510
|
"description": "Forbidden"
|
|
489
|
-
},
|
|
490
|
-
"404": {
|
|
491
|
-
"description": "Not Found"
|
|
492
511
|
}
|
|
493
512
|
}
|
|
494
513
|
}
|
|
@@ -554,52 +573,6 @@
|
|
|
554
573
|
"type": "object",
|
|
555
574
|
"properties": {}
|
|
556
575
|
},
|
|
557
|
-
"Query": {
|
|
558
|
-
"type": "object",
|
|
559
|
-
"properties": {
|
|
560
|
-
"query": {
|
|
561
|
-
"type": "string",
|
|
562
|
-
"example": "SELECT * FROM users"
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
},
|
|
566
|
-
"Statement": {
|
|
567
|
-
"type": "object",
|
|
568
|
-
"properties": {
|
|
569
|
-
"statement": {
|
|
570
|
-
"type": "string",
|
|
571
|
-
"example": "INSERT INTO users (id, firstName, lastName) VALUES (:id, :firstName, :lastName)"
|
|
572
|
-
},
|
|
573
|
-
"values": {
|
|
574
|
-
"type": "object",
|
|
575
|
-
"properties": {
|
|
576
|
-
"id": {
|
|
577
|
-
"type": "number",
|
|
578
|
-
"example": 1
|
|
579
|
-
},
|
|
580
|
-
"firstName": {
|
|
581
|
-
"type": "string",
|
|
582
|
-
"example": "John"
|
|
583
|
-
},
|
|
584
|
-
"lastName": {
|
|
585
|
-
"type": "string",
|
|
586
|
-
"example": "Doe"
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
},
|
|
592
|
-
"Transaction": {
|
|
593
|
-
"type": "object",
|
|
594
|
-
"properties": {
|
|
595
|
-
"transaction": {
|
|
596
|
-
"type": "array",
|
|
597
|
-
"items": {
|
|
598
|
-
"$ref": "#/definitions/Statement"
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
},
|
|
603
576
|
"ForeignKey": {
|
|
604
577
|
"type": "object",
|
|
605
578
|
"properties": {
|
|
@@ -811,8 +784,26 @@
|
|
|
811
784
|
}
|
|
812
785
|
}
|
|
813
786
|
},
|
|
814
|
-
"
|
|
815
|
-
"
|
|
787
|
+
"BatchRequestBody": {
|
|
788
|
+
"type": "object",
|
|
789
|
+
"properties": {
|
|
790
|
+
"operations": {
|
|
791
|
+
"type": "array",
|
|
792
|
+
"items": {
|
|
793
|
+
"type": "object",
|
|
794
|
+
"properties": {
|
|
795
|
+
"method": {
|
|
796
|
+
"type": "string",
|
|
797
|
+
"example": "DELETE"
|
|
798
|
+
},
|
|
799
|
+
"path": {
|
|
800
|
+
"type": "string",
|
|
801
|
+
"example": "/api/tables/users/rows/2"
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
}
|
|
816
807
|
},
|
|
817
808
|
"ObtainAccessTokenRequestBody": {
|
|
818
809
|
"type": "object",
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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' })) {
|
package/src/websocket.test.js
CHANGED
|
@@ -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
|
});
|
package/src/schemas/index.js
DELETED
|
@@ -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
|
-
};
|