@xeplr/base-apis 2.0.3 → 2.0.4

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 CHANGED
@@ -114,18 +114,21 @@ createApp(3000, 'myapp', {
114
114
 
115
115
  Why on by default: a service that forgot to configure auth used to boot clean, pass every health check, and serve every route to anybody — nothing distinguished it from a working one.
116
116
 
117
- `remoteAuth` sends the caller's `Authorization: Bearer …` to `url + endpoint` (`/auth/api/me`) and puts the answer's `user` and `access` on `req.user` / `req.access`.
117
+ `remoteAuth` sends the caller's `Authorization: Bearer …` **and the request it is serving** (`x-verify-method`, `x-verify-path`) to `url + endpoint` (`/auth/api/verify`). Auth answers who the caller is and whether they may call that API, so **every request is checked for access, not only for a signed-in token**; the answer's `user` and `access` go on `req.user` / `req.access`. See `@xeplr/auth`'s "Every request is checked".
118
118
 
119
119
  | situation | response |
120
120
  |---|---|
121
121
  | path starts with a `publicPaths` entry | skipped |
122
122
  | no `Bearer` token | 401 `No token provided` |
123
- | auth answers 401 / 403 | same status, `Invalid or expired token` |
123
+ | auth answers `403` with `code` `DISABLED` or `HIDDEN` | 403 with that `code`: signed in, but not allowed this API |
124
+ | auth answers 401 / other 403 | same status, `Invalid or expired token` |
125
+ | auth answers 200 **without** `state: 'enabled'` (an older auth, or an `endpoint` such as `/me` that only says who the caller is) | **503**: "did not say no" is not "said yes" |
124
126
  | auth unreachable, times out (`timeoutMs`, 5000), answers anything else, or answers non-JSON | **503 `Authentication service unavailable` — never let through** |
125
127
 
126
128
  - **Validated over HTTP, not in-process** — so a service needs no `@xeplr/auth` dependency (it would be a cycle), no JWT secret and no Redis. The cost is a hop per request.
127
- - **`cacheSeconds` (default 5)** caches a validated token per process; a revoked token keeps working that long here. `0` checks every request.
128
- - **`X-New-Token` is forwarded** (with `Access-Control-Expose-Headers`) when the auth service slides a token, and that answer is **not cached** — otherwise the next requests would be answered from memory without telling the client to switch, and it would be logged out when the window closed.
129
+ - **A burst makes one call.** Requests with the same token, method and path that arrive while a check is on its way share its answer: ten at once, one call to auth. This holds with `cacheSeconds: 0` too.
130
+ - **`cacheSeconds` (default 5)** caches each answer (allowed or refused) per token, method and path, per process; a revoked token or a changed mapping takes that long to reach here. `0` checks every request.
131
+ - **`X-New-Token` is forwarded** (with `Access-Control-Expose-Headers`) when the auth service slides a token, to every request in the burst that shared the check, so none is rejected because the token changed. That answer is **not cached** — otherwise the next requests would be answered from memory without telling the client to switch, and it would be logged out when the window closed.
129
132
 
130
133
  ### Uploads
131
134
 
package/lib/remoteAuth.js CHANGED
@@ -18,7 +18,10 @@
18
18
  // everything — deliberately, because the alternative to "auth is down so
19
19
  // nothing serves" is "auth is down so everything serves unauthenticated".
20
20
 
21
- var DEFAULT_ENDPOINT = '/auth/api/me';
21
+ // Who the caller is AND whether they may call the API being served, in one
22
+ // answer. Every request asks both: there is no identity-only check, so an app
23
+ // cannot forget to ask about access. See @xeplr/auth's GET /verify.
24
+ var DEFAULT_ENDPOINT = '/auth/api/verify';
22
25
 
23
26
  // Five seconds. Long enough that a busy endpoint stops making a round trip per
24
27
  // request; short enough that a logout takes effect before anyone notices.
@@ -40,7 +43,9 @@ var DEFAULT_TIMEOUT_MS = 5000;
40
43
  * @param {string[]} [config.publicPaths] - path PREFIXES that skip the gate.
41
44
  * @param {number} [config.cacheSeconds=5] - per-process cache of validated
42
45
  * tokens. 0 disables.
43
- * @param {string} [config.endpoint='/auth/api/me'] - the validation route.
46
+ * @param {string} [config.endpoint='/auth/api/verify'] - the validation route.
47
+ * It must answer the access state; one that does not (an older auth, or
48
+ * /me) is refused, never taken as "allowed".
44
49
  * @param {number} [config.timeoutMs=5000]
45
50
  * @returns {Function} express middleware
46
51
  */
@@ -56,11 +61,18 @@ function remoteAuth(config) {
56
61
  var timeoutMs = config.timeoutMs || DEFAULT_TIMEOUT_MS;
57
62
  var cacheSeconds = config.cacheSeconds == null ? DEFAULT_CACHE_SECONDS : config.cacheSeconds;
58
63
 
59
- // token → { expires, user, access }. Per-process and deliberately not
64
+ // token + request → { expires, outcome }. Per-process and deliberately not
60
65
  // shared: a cache in Redis would put back the dependency this exists to
61
- // remove, to save a hop it already saves.
66
+ // remove, to save a hop it already saves. Keyed by the request too, because
67
+ // the answer is about one API, not just about the token.
62
68
  var cache = new Map();
63
69
 
70
+ // The same key while its answer is on the way → the same promise. A burst
71
+ // (a dashboard firing ten widget requests at once) makes ONE call to auth,
72
+ // and all ten get its answer: the same user, the same state, and the same
73
+ // replacement token if it slid.
74
+ var pending = new Map();
75
+
64
76
  if (cacheSeconds > 0) {
65
77
  // Bounded growth. Without this a process that sees many distinct tokens
66
78
  // (every user, every rotation) keeps every one of them forever.
@@ -81,28 +93,12 @@ function remoteAuth(config) {
81
93
  return false;
82
94
  }
83
95
 
84
- return async function remoteAuthMiddleware(req, res, next) {
85
- if (isPublic(req.path)) return next();
86
-
87
- var header = req.headers.authorization;
88
- if (!header || header.indexOf('Bearer ') !== 0) {
89
- return res.status(401).json({ error: 'No token provided' });
90
- }
91
- var token = header.slice('Bearer '.length);
92
-
93
- if (cacheSeconds > 0) {
94
- var hit = cache.get(token);
95
- if (hit && hit.expires > Date.now()) {
96
- req.user = hit.user;
97
- req.access = hit.access;
98
- return next();
99
- }
100
- }
101
-
96
+ // One call to auth → an outcome every waiter can apply.
97
+ async function ask(header, method, path) {
102
98
  var response;
103
99
  try {
104
100
  response = await fetch(url + endpoint, {
105
- headers: { authorization: header },
101
+ headers: { authorization: header, 'x-verify-method': method, 'x-verify-path': path },
106
102
  signal: AbortSignal.timeout(timeoutMs)
107
103
  });
108
104
  } catch (err) {
@@ -110,34 +106,9 @@ function remoteAuth(config) {
110
106
  // establish who this is — and "cannot establish" must never resolve to
111
107
  // "let them in". Failing open here would turn one service's outage into
112
108
  // an authentication bypass across every app that trusts this gate.
113
- if (req.log) req.log.error('auth unreachable: ' + err.message);
114
- return res.status(503).json({ error: 'Authentication service unavailable' });
109
+ return { kind: 'unavailable', why: 'auth unreachable: ' + err.message };
115
110
  }
116
111
 
117
- if (!response.ok) {
118
- // The auth service's own answer, passed through unchanged. A 401 is a
119
- // bad token; anything else is the auth service having a problem, and
120
- // relabelling that as 401 would send the caller looking at their token.
121
- if (response.status === 401 || response.status === 403) {
122
- return res.status(response.status).json({ error: 'Invalid or expired token' });
123
- }
124
- if (req.log) req.log.error('auth returned ' + response.status);
125
- return res.status(503).json({ error: 'Authentication service unavailable' });
126
- }
127
-
128
- var body;
129
- try {
130
- body = await response.json();
131
- } catch (err) {
132
- return res.status(503).json({ error: 'Authentication service unavailable' });
133
- }
134
-
135
- req.user = body.user;
136
- // Permissions arrive with identity, in the same answer — so a route that
137
- // needs to know what this caller may do does not make a second call for
138
- // it.
139
- req.access = body.access;
140
-
141
112
  // SLIDING REFRESH, FORWARDED.
142
113
  //
143
114
  // With a tolerance configured, the auth service serves an expired-but-
@@ -150,27 +121,95 @@ function remoteAuth(config) {
150
121
  // tolerance window closes, and then the user is logged out mid-session
151
122
  // with nothing in any log to connect the two.
152
123
  var newToken = response.headers.get('x-new-token');
153
- if (newToken) {
154
- res.setHeader('X-New-Token', newToken);
124
+
125
+ var body = null;
126
+ try { body = await response.json(); } catch (err) { body = null; }
127
+
128
+ if (response.status === 403 && body && (body.code === 'DISABLED' || body.code === 'HIDDEN')) {
129
+ // Signed in, but not allowed this API. The code says which, so a UI can
130
+ // grey a disabled item out rather than treat it as missing.
131
+ return { kind: 'refused', newToken: newToken, body: { error: body.error || 'Access denied', code: body.code } };
132
+ }
133
+
134
+ if (!response.ok) {
135
+ // The auth service's own answer, passed through unchanged. A 401 is a
136
+ // bad token; anything else is the auth service having a problem, and
137
+ // relabelling that as 401 would send the caller looking at their token.
138
+ if (response.status === 401 || response.status === 403) {
139
+ return { kind: 'invalid', status: response.status };
140
+ }
141
+ return { kind: 'unavailable', why: 'auth returned ' + response.status };
142
+ }
143
+
144
+ // 200 but no access answer: this auth does not check access (an older
145
+ // version, or an endpoint that only says who the caller is). Refused,
146
+ // because "did not say no" is not "said yes".
147
+ if (!body || body.state !== 'enabled') {
148
+ return { kind: 'unavailable', why: 'auth answered without an access state; update @xeplr/auth' };
149
+ }
150
+
151
+ return { kind: 'ok', newToken: newToken, user: body.user, access: body.access };
152
+ }
153
+
154
+ function apply(outcome, req, res, next) {
155
+ if (outcome.newToken) {
156
+ res.setHeader('X-New-Token', outcome.newToken);
155
157
  // Without this a browser cannot READ the header, so forwarding it would
156
158
  // be invisible anyway — same pair @xeplr/auth sets locally.
157
159
  res.setHeader('Access-Control-Expose-Headers', 'X-New-Token');
158
160
  }
161
+ if (outcome.kind === 'ok') {
162
+ req.user = outcome.user;
163
+ // Permissions arrive with identity, in the same answer — so a route that
164
+ // needs to know what this caller may do does not make a second call for
165
+ // it.
166
+ req.access = outcome.access;
167
+ return next();
168
+ }
169
+ if (outcome.kind === 'refused') return res.status(403).json(outcome.body);
170
+ if (outcome.kind === 'invalid') return res.status(outcome.status).json({ error: 'Invalid or expired token' });
171
+ if (req.log) req.log.error(outcome.why);
172
+ return res.status(503).json({ error: 'Authentication service unavailable' });
173
+ }
159
174
 
160
- // NOT CACHED WHEN THE TOKEN SLID. The caller is being told to switch to a
161
- // replacement, and caching the old one would answer its next few requests
162
- // from memory — without the header telling it to switch. A client that
163
- // missed the first one would never be told again, and would be cut off
164
- // when the window closed.
165
- if (cacheSeconds > 0 && !newToken) {
166
- cache.set(token, {
167
- user: body.user,
168
- access: body.access,
169
- expires: Date.now() + cacheSeconds * 1000
170
- });
175
+ return async function remoteAuthMiddleware(req, res, next) {
176
+ if (isPublic(req.path)) return next();
177
+
178
+ var header = req.headers.authorization;
179
+ if (!header || header.indexOf('Bearer ') !== 0) {
180
+ return res.status(401).json({ error: 'No token provided' });
171
181
  }
182
+ var token = header.slice('Bearer '.length);
183
+ var method = req.method;
184
+ var path = (req.baseUrl || '') + req.path;
185
+ var key = token + '\n' + method + ' ' + path;
172
186
 
173
- next();
187
+ if (cacheSeconds > 0) {
188
+ var hit = cache.get(key);
189
+ if (hit && hit.expires > Date.now()) return apply(hit.outcome, req, res, next);
190
+ }
191
+
192
+ var inFlight = pending.get(key);
193
+ if (!inFlight) {
194
+ inFlight = ask(header, method, path);
195
+ pending.set(key, inFlight);
196
+ inFlight.then(function (outcome) {
197
+ pending.delete(key);
198
+ // NOT CACHED WHEN THE TOKEN SLID. The caller is being told to switch
199
+ // to a replacement, and caching the old one would answer its next few
200
+ // requests from memory — without the header telling it to switch. A
201
+ // client that missed the first one would never be told again, and
202
+ // would be cut off when the window closed.
203
+ //
204
+ // Only a real answer is cached: an outage or a bad token is asked
205
+ // again next time.
206
+ var answer = outcome.kind === 'ok' || outcome.kind === 'refused';
207
+ if (cacheSeconds > 0 && answer && !outcome.newToken) {
208
+ cache.set(key, { outcome: outcome, expires: Date.now() + cacheSeconds * 1000 });
209
+ }
210
+ });
211
+ }
212
+ apply(await inFlight, req, res, next);
174
213
  };
175
214
  }
176
215
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/base-apis",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "Base API bootstrapper — Express and plain HTTP server factories with middleware injection",
5
5
  "main": "index.js",
6
6
  "bin": {