@xeplr/auth 1.1.2 → 1.1.3

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/bin/server.js CHANGED
@@ -7,7 +7,8 @@
7
7
  *
8
8
  * env: ENCRYPTION_KEY · AUTH_DB_CONNECTION_INFO_ENCRYPTED · AUTH_DB_NAME ·
9
9
  * AUTH_JWT_SECRET · AUTH_PORT · AUTH_ACTIVATION_BASE_URL ·
10
- * AUTH_ACCESS_TOKEN_TTL_MINUTES · XEPLR_AUTH_MIGRATIONS · email vars · REDIS_*
10
+ * AUTH_ACCESS_TOKEN_TTL_MINUTES · XEPLR_AUTH_MIGRATIONS · REDIS_PREFIX ·
11
+ * email vars · REDIS_*
11
12
  */
12
13
  require('../index').boot().then(function () {
13
14
  if (process.send) process.send({ status: 'ready' });
package/index.js CHANGED
@@ -9,6 +9,7 @@ var AUTH_CONN_VAR = 'AUTH_DB_CONNECTION_INFO_ENCRYPTED';
9
9
  function authConnection() { return resolveDbConnection(AUTH_CONN_VAR); }
10
10
  const { createApp } = require('@xeplr/base-apis');
11
11
  const authHelper = require('./lib/authHelper');
12
+ const { redisPrefix } = require('./lib/redisPrefix');
12
13
  const { seedSuperAdmin } = require('./lib/seed');
13
14
  const authMiddleware = require('./lib/authMiddleware');
14
15
  const authService = require('./lib/authService');
@@ -35,6 +36,10 @@ let _initialized = false;
35
36
  * @param {string} [config.resetBaseUrl] - Base URL for password reset links
36
37
  */
37
38
  async function init(config = {}) {
39
+ // Before anything touches Redis. Here rather than only in boot() so a host
40
+ // that mounts auth's routers in its own process is held to it too.
41
+ redisPrefix();
42
+
38
43
  // One call: decrypt the app-supplied connection, connect, and bind the models.
39
44
  const dbName = config.database || process.env.AUTH_DB_NAME || 'auth';
40
45
  const connection = await getConnection(dbName, config.connection || config.db, {
@@ -175,6 +180,9 @@ async function boot() {
175
180
  var connName = 'auth';
176
181
  var database = process.env.AUTH_DB_NAME || 'auth';
177
182
 
183
+ // Ahead of the session-store probe, which would otherwise write its first
184
+ // key under the shared default.
185
+ redisPrefix();
178
186
  await checkSessionStore();
179
187
 
180
188
  await resolveConfig(connName, authConnection());
@@ -269,6 +277,7 @@ function banner(email) {
269
277
  ['slide tolerance', (process.env.AUTH_ACCESS_TOKEN_TOLERANCE_SECONDS || '0') + 's'],
270
278
  ['max sessions/user', process.env.AUTH_MAX_SESSIONS_PER_USER || '5'],
271
279
  ['redis', (process.env.REDIS_HOST || 'localhost') + ':' + (process.env.REDIS_PORT || '6379')],
280
+ ['redis prefix', redisPrefix()],
272
281
  ['email', describeEmail(email)],
273
282
  // Names the VARIABLE it resolved through, not just "set" — with a shared
274
283
  // default and a per-service override, "which server am I on" is otherwise
@@ -357,6 +366,12 @@ var requiredEnv = [
357
366
  // anything starts.
358
367
  'AUTH_SUPER_ADMIN_EMAIL',
359
368
  'AUTH_SUPER_ADMIN_PASSWORD',
369
+
370
+ // Every auth key in Redis is written under it. Two apps left on the shared
371
+ // 'xeplr:' default serve each other's menus and API permissions — see
372
+ // lib/redisPrefix.js. init() also refuses the shared value itself, which a
373
+ // presence check cannot.
374
+ 'REDIS_PREFIX',
360
375
  ];
361
376
 
362
377
  // Read at ACCESS time so it reflects the .env the app has already loaded — the
@@ -0,0 +1,46 @@
1
+ /**
2
+ * REDIS_PREFIX — the namespace every auth key is written under: sessions, the
3
+ * token whitelist, and the access cache. Required, and never the old shared
4
+ * default.
5
+ *
6
+ * WHY IT IS NOT DEFAULTED. @xeplr/utils' cache falls back to 'xeplr:', so two
7
+ * apps on one Redis (every developer machine running more than one of them)
8
+ * read and write the same keys. Most of those keys carry a token or a random
9
+ * user id and so never meet, but the access cache has keys named by content:
10
+ *
11
+ * access:public the menus/pages everyone may see
12
+ * access:api:<name> whether an API is open, and to which roles
13
+ *
14
+ * One app fills them from ITS database and the other serves them as its own.
15
+ * The visible half is a drawer showing another product's menus and dropping
16
+ * its own. The half nobody sees is worse: an API the other app has never
17
+ * registered is cached as "open to everyone", so a guarded route — publishing
18
+ * a screen, which alters tables — answers any signed-in caller for up to ten
19
+ * minutes. No error, no log line.
20
+ *
21
+ * 'xeplr:' is refused even when written out explicitly: it is exactly the
22
+ * value an env file copied from another project would carry, and accepting it
23
+ * would bring the collision back looking deliberate.
24
+ *
25
+ * @param {object} [env=process.env]
26
+ * @returns {string} the prefix to use
27
+ * @throws {Error} naming REDIS_PREFIX when it is missing or the shared default
28
+ */
29
+ var SHARED_DEFAULT = 'xeplr:';
30
+
31
+ function redisPrefix(env) {
32
+ env = env || process.env;
33
+ var value = (env.REDIS_PREFIX || '').trim();
34
+
35
+ if (!value) {
36
+ throw new Error('REDIS_PREFIX is not set. Give this app its own, e.g. REDIS_PREFIX=myapp: — ' +
37
+ 'apps sharing a Redis without one serve each other\'s sessions, menus and API permissions.');
38
+ }
39
+ if (value === SHARED_DEFAULT) {
40
+ throw new Error('REDIS_PREFIX is "' + SHARED_DEFAULT + '", the value every app shares by default. ' +
41
+ 'Give this app its own, e.g. REDIS_PREFIX=myapp:');
42
+ }
43
+ return value;
44
+ }
45
+
46
+ module.exports = { redisPrefix: redisPrefix, SHARED_DEFAULT: SHARED_DEFAULT };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/auth",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Pluggable authentication service: register, login, forgot/reset password, RBAC, multi-tenancy",
5
5
  "main": "index.js",
6
6
  "files": [