@xeplr/auth 1.1.1 → 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());
@@ -200,6 +208,13 @@ async function boot() {
200
208
 
201
209
  // Configure email (via @xeplr/utils, the engine auth uses) + activation links.
202
210
  require('@xeplr/utils').configureFromEnv();
211
+
212
+ // Whether mail can actually go out — asked, not assumed from a variable
213
+ // being set. Reported in the banner and NEVER fatal: sign-in works without
214
+ // email; only the messages that carry links (activation, invite, password
215
+ // reset) cannot be sent, and the banner says so rather than the first
216
+ // person to register finding out.
217
+ var email = await emailStatus();
203
218
  authService.configureActivation({
204
219
  // Fully qualified, one per link — the token is the only thing appended.
205
220
  // AUTH_ACTIVATION_BASE_URL still works as an origin; see configureActivation.
@@ -219,13 +234,37 @@ async function boot() {
219
234
  }
220
235
  });
221
236
 
222
- banner();
237
+ banner(email);
238
+ if (!email.ok) warnNoEmail(email);
223
239
  return server;
224
240
  }
225
241
 
242
+ async function emailStatus() {
243
+ var utils = require('@xeplr/utils');
244
+ if (typeof utils.checkEmail !== 'function') {
245
+ return { ok: true, unknown: true, detail: 'not checked — @xeplr/utils is too old to check' };
246
+ }
247
+ return utils.checkEmail({ timeoutMs: 5000 });
248
+ }
249
+
250
+ function warnNoEmail(email) {
251
+ console.warn('[xeplr-auth] EMAIL IS NOT WORKING — ' + String(email.detail).replace(/\.$/, '') + '.');
252
+ console.warn(' Sign-in keeps working. Registration, invites and password');
253
+ console.warn(' resets cannot send their links until this is fixed: set');
254
+ console.warn(' EMAIL_PROVIDER (smtp, brevo, aws or azure) and its settings.\n');
255
+ }
256
+
257
+ function describeEmail(email) {
258
+ if (!email) return process.env.EMAIL_PROVIDER || '(not configured)';
259
+ if (email.unknown) return '? ' + email.detail;
260
+ return email.ok
261
+ ? '✓ ' + email.provider + ' — ' + email.detail
262
+ : '✗ NOT WORKING — ' + email.detail;
263
+ }
264
+
226
265
  // Startup summary — the EFFECTIVE config (defaults resolved) so you can see what
227
266
  // the service is actually running with. Secrets are masked, never printed.
228
- function banner() {
267
+ function banner(email) {
229
268
  var set = function (v) { return v ? '✓ set' : '✗ MISSING'; };
230
269
  var rows = [
231
270
  ['port', process.env.AUTH_PORT || '19001'],
@@ -238,7 +277,8 @@ function banner() {
238
277
  ['slide tolerance', (process.env.AUTH_ACCESS_TOKEN_TOLERANCE_SECONDS || '0') + 's'],
239
278
  ['max sessions/user', process.env.AUTH_MAX_SESSIONS_PER_USER || '5'],
240
279
  ['redis', (process.env.REDIS_HOST || 'localhost') + ':' + (process.env.REDIS_PORT || '6379')],
241
- ['email', process.env.EMAIL_PROVIDER || '(not configured)'],
280
+ ['redis prefix', redisPrefix()],
281
+ ['email', describeEmail(email)],
242
282
  // Names the VARIABLE it resolved through, not just "set" — with a shared
243
283
  // default and a per-service override, "which server am I on" is otherwise
244
284
  // a guess.
@@ -326,6 +366,12 @@ var requiredEnv = [
326
366
  // anything starts.
327
367
  'AUTH_SUPER_ADMIN_EMAIL',
328
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',
329
375
  ];
330
376
 
331
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.1",
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": [
@@ -44,7 +44,7 @@
44
44
  "multer": "^2.3.0",
45
45
  "@xeplr/base-apis": "^2.0.0",
46
46
  "@xeplr/db": "^1.0.0",
47
- "@xeplr/utils": "^1.0.0"
47
+ "@xeplr/utils": "^1.0.8"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "express": "^4.0.0 || ^5.0.0"