@xeplr/auth 1.7.0 → 1.8.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/README.md +1 -1
- package/lib/apiMatch.js +14 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -631,7 +631,7 @@ var gaps = check.findGaps({ routes, catalog, publicPaths: ['/public/'] });
|
|
|
631
631
|
|
|
632
632
|
**Planned for 2.0:** the gate refuses a route with no row (`NOT_IN_CATALOG`) or no role (`NOT_MAPPED`). It ships as a major version, once every app's test passes.
|
|
633
633
|
|
|
634
|
-
**Which row a request is.** A catalog name is `METHOD path` or a bare path (every method), and a path may hold `:params`: `GET /widget-templates/:id` covers `GET /widget-templates/abc`. When several rows fit, more literal segments win, then a row with a method over one without (`lib/apiMatch.js`). The catalog names are cached under `access:api:catalog`, cleared with the other rules.
|
|
634
|
+
**Which row a request is.** A catalog name is `METHOD path` or a bare path (every method), and a path may hold `:params`: `GET /widget-templates/:id` covers `GET /widget-templates/abc`. A last segment `*` covers one or more segments of any depth, for a route written as a pattern: `GET /factory/files/*` covers `GET /factory/files/a/b/c.png`. When several rows fit, more literal segments win, then a fixed length over `*`, then a row with a method over one without (`lib/apiMatch.js`). In a `catalogCheck` test, give such a route its row's name with `listRoutes(app, { mapPath })`. The catalog names are cached under `access:api:catalog`, cleared with the other rules.
|
|
635
635
|
|
|
636
636
|
**Auth's own routes are checked the same way**, in-process: every signed-in route in `/auth/api` and every route in `/auth/api/admin` passes `accessGate()` (exported from `lib/accessMiddleware`) after the token check. Login, register, refresh and the other signed-out routes carry neither.
|
|
637
637
|
|
package/lib/apiMatch.js
CHANGED
|
@@ -3,11 +3,14 @@
|
|
|
3
3
|
*
|
|
4
4
|
* A row's name is "METHOD path" (that method only) or a bare path (every
|
|
5
5
|
* method), and a path may hold `:params` — "GET /widget-templates/:id"
|
|
6
|
-
* covers GET /widget-templates/abc.
|
|
7
|
-
*
|
|
6
|
+
* covers GET /widget-templates/abc. A last segment `*` covers one or more
|
|
7
|
+
* segments of any depth — "GET /factory/files/*" covers
|
|
8
|
+
* GET /factory/files/a/b/c.png — for a route written as a pattern. When
|
|
9
|
+
* several rows fit, the most exact wins:
|
|
8
10
|
*
|
|
9
11
|
* 1. more literal segments /dashboards/:id/group beats /dashboards/:id/:x
|
|
10
|
-
* 2. a
|
|
12
|
+
* 2. a fixed length over `*` /files/:id beats /files/*
|
|
13
|
+
* 3. a method over none "POST /x" beats "/x"
|
|
11
14
|
*
|
|
12
15
|
* @param names every API name in the catalog
|
|
13
16
|
* @param method 'GET', 'POST', …
|
|
@@ -27,7 +30,13 @@ function parse(name) {
|
|
|
27
30
|
function fit(pattern, path) {
|
|
28
31
|
var a = pattern.split('/');
|
|
29
32
|
var b = path.split('/');
|
|
30
|
-
|
|
33
|
+
var rest = a.length > 1 && a[a.length - 1] === '*';
|
|
34
|
+
if (rest) {
|
|
35
|
+
// `*` takes the remaining segments: at least one, none of them empty.
|
|
36
|
+
a = a.slice(0, -1);
|
|
37
|
+
if (b.length <= a.length || b.slice(a.length).some(function(x) { return x === ''; })) return -1;
|
|
38
|
+
b = b.slice(0, a.length);
|
|
39
|
+
} else if (a.length !== b.length) return -1;
|
|
31
40
|
var literal = 0;
|
|
32
41
|
for (var i = 0; i < a.length; i++) {
|
|
33
42
|
if (a[i].charAt(0) === ':' && b[i] !== '') continue;
|
|
@@ -47,7 +56,7 @@ function matchApi(names, method, path) {
|
|
|
47
56
|
if (row.method && row.method !== verb) return;
|
|
48
57
|
var literal = fit(row.path, target);
|
|
49
58
|
if (literal < 0) return;
|
|
50
|
-
var score = literal * 2 + (row.method ? 1 : 0);
|
|
59
|
+
var score = literal * 4 + (/\/\*$/.test(row.path) ? 0 : 2) + (row.method ? 1 : 0);
|
|
51
60
|
if (score > bestScore) { best = name; bestScore = score; }
|
|
52
61
|
});
|
|
53
62
|
return best;
|