@xeplr/auth 1.0.8 → 1.1.1
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/index.js +59 -0
- package/migrations/0008_super_admin.sql +58 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -126,10 +126,57 @@ async function start(config = {}) {
|
|
|
126
126
|
// connection AUTH_DB_CONNECTION_INFO_ENCRYPTED · db AUTH_DB_NAME · AUTH_JWT_SECRET
|
|
127
127
|
// AUTH_PORT · AUTH_ACTIVATION_BASE_URL · AUTH_ACCESS_TOKEN_TTL_MINUTES
|
|
128
128
|
// XEPLR_AUTH_MIGRATIONS · email vars · REDIS_*
|
|
129
|
+
/**
|
|
130
|
+
* REDIS HOLDS THE SESSIONS, so this service cannot authenticate anybody
|
|
131
|
+
* without it. Checked here, out loud, because the way this fails otherwise is
|
|
132
|
+
* genuinely cruel:
|
|
133
|
+
*
|
|
134
|
+
* • login SUCCEEDS — minting a token does not need Redis to work
|
|
135
|
+
* • the session write fails silently
|
|
136
|
+
* • every later request finds no live session and answers
|
|
137
|
+
* "Invalid or expired token"
|
|
138
|
+
*
|
|
139
|
+
* So a brand-new token is rejected as expired, and the message sends whoever
|
|
140
|
+
* is debugging it towards clocks, token lifetimes and JWT secrets — anywhere
|
|
141
|
+
* but the service that is not running. It cost two people an afternoon before
|
|
142
|
+
* this check existed.
|
|
143
|
+
*
|
|
144
|
+
* Refuses rather than warns. An auth service that cannot keep a session is not
|
|
145
|
+
* degraded, it is unable to do the one thing it is for, and starting anyway
|
|
146
|
+
* only moves the failure somewhere less obvious.
|
|
147
|
+
*/
|
|
148
|
+
async function checkSessionStore() {
|
|
149
|
+
var host = process.env.REDIS_HOST || 'localhost';
|
|
150
|
+
var port = process.env.REDIS_PORT || '6379';
|
|
151
|
+
var probe = '__xeplr_auth_startup_probe__';
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
var { cache } = require('@xeplr/utils');
|
|
155
|
+
await cache.set(probe, { ok: true }, 5);
|
|
156
|
+
var back = await cache.get(probe);
|
|
157
|
+
await cache.del(probe);
|
|
158
|
+
if (!back || !back.ok) throw new Error('the session store did not return what was written to it');
|
|
159
|
+
} catch (err) {
|
|
160
|
+
console.error('\n[xeplr-auth] Cannot reach the session store (Redis) at ' + host + ':' + port + '.');
|
|
161
|
+
console.error(' ' + (err && err.message ? err.message : err) + '\n');
|
|
162
|
+
console.error(' Sign-in sessions are kept there, so without it every login');
|
|
163
|
+
console.error(' succeeds and then every request is refused as "Invalid or');
|
|
164
|
+
console.error(' expired token" — which looks like a token problem and is not.\n');
|
|
165
|
+
console.error(' Start Redis, or point REDIS_HOST / REDIS_PORT at one:\n');
|
|
166
|
+
console.error(' macOS brew services start redis');
|
|
167
|
+
console.error(' Linux sudo systemctl start redis');
|
|
168
|
+
console.error(' Windows docker run -d -p 6379:6379 redis');
|
|
169
|
+
console.error(' (Redis has no native Windows build — Docker or WSL)\n');
|
|
170
|
+
process.exit(1);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
129
174
|
async function boot() {
|
|
130
175
|
var connName = 'auth';
|
|
131
176
|
var database = process.env.AUTH_DB_NAME || 'auth';
|
|
132
177
|
|
|
178
|
+
await checkSessionStore();
|
|
179
|
+
|
|
133
180
|
await resolveConfig(connName, authConnection());
|
|
134
181
|
|
|
135
182
|
// Self-migrate: auth's own .sql migrations (schema + base data), then the
|
|
@@ -267,6 +314,18 @@ var requiredEnv = [
|
|
|
267
314
|
'AUTH_PORT',
|
|
268
315
|
'AUTH_DB_NAME', // auth db (api reaches it via attach())
|
|
269
316
|
'XEPLR_AUTH_MIGRATIONS', // migrations run via migrate:up
|
|
317
|
+
|
|
318
|
+
// THE FIRST ACCOUNT — consumed by migrations/0008_super_admin.sql, which
|
|
319
|
+
// this package now ships so that every app does not hand-write the same
|
|
320
|
+
// insert against columns this package owns.
|
|
321
|
+
//
|
|
322
|
+
// Required, not optional: an install that migrates cleanly and has nobody
|
|
323
|
+
// who can sign in is not a working install, and the failure shows up much
|
|
324
|
+
// later than the cause. The migrator refuses by name if either is unset,
|
|
325
|
+
// so this listing only moves the same complaint to `check-env`, before
|
|
326
|
+
// anything starts.
|
|
327
|
+
'AUTH_SUPER_ADMIN_EMAIL',
|
|
328
|
+
'AUTH_SUPER_ADMIN_PASSWORD',
|
|
270
329
|
];
|
|
271
330
|
|
|
272
331
|
// Read at ACCESS time so it reflects the .env the app has already loaded — the
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
-- 0008_super_admin.sql
|
|
2
|
+
-- THE FIRST ACCOUNT. Every install needs one, so this package ships it rather
|
|
3
|
+
-- than leaving each app to hand-write the same SQL.
|
|
4
|
+
--
|
|
5
|
+
-- It used to be the app's job: 0006 creates the "Super Admin" ROLE but nothing
|
|
6
|
+
-- created a super admin USER, so every consumer copied a version of this file
|
|
7
|
+
-- into its own migrations-auth/. That copy restated things this package owns —
|
|
8
|
+
-- the users columns, that normalizedEmail is the lowercased one, that the role
|
|
9
|
+
-- is spelled exactly 'Super Admin', that passwords are bcrypt — so a change
|
|
10
|
+
-- here silently broke every copy out there. And it broke as "invalid email or
|
|
11
|
+
-- password", which sends somebody hunting their password rather than their
|
|
12
|
+
-- migration.
|
|
13
|
+
--
|
|
14
|
+
-- WHO is still the app's decision; only HOW lives here. Both values come from
|
|
15
|
+
-- the environment and are substituted at apply time, so the password is never
|
|
16
|
+
-- written into a file. The migrator REFUSES to run when either is unset,
|
|
17
|
+
-- naming the variable — deliberately, because the alternative is an install
|
|
18
|
+
-- that migrates cleanly and has no way to sign in.
|
|
19
|
+
--
|
|
20
|
+
-- No quotes, spaces, $ or # in the password: these are substituted into SQL
|
|
21
|
+
-- text, the same constraint every other env-substituted migration here has.
|
|
22
|
+
--
|
|
23
|
+
-- Insert-if-absent on both statements, so re-running changes nothing — and an
|
|
24
|
+
-- app that already seeded its own super admin under the same address keeps the
|
|
25
|
+
-- one it has, password included.
|
|
26
|
+
|
|
27
|
+
INSERT INTO "users" (
|
|
28
|
+
id, email, "normalizedEmail", name, pwd, "isActive", "isActivated",
|
|
29
|
+
"activatedOn", "activatedBy", "mtId1", "recordCreatedDate", "recordModifiedDate"
|
|
30
|
+
)
|
|
31
|
+
SELECT
|
|
32
|
+
encode(gen_random_bytes(12), 'hex'),
|
|
33
|
+
'${AUTH_SUPER_ADMIN_EMAIL}',
|
|
34
|
+
lower('${AUTH_SUPER_ADMIN_EMAIL}'),
|
|
35
|
+
'Super Admin',
|
|
36
|
+
crypt('${AUTH_SUPER_ADMIN_PASSWORD}', gen_salt('bf')),
|
|
37
|
+
true,
|
|
38
|
+
true,
|
|
39
|
+
now(),
|
|
40
|
+
'system',
|
|
41
|
+
-- '*' — belongs to every tenant rather than to one company, so the first
|
|
42
|
+
-- account can still sign in before any company exists.
|
|
43
|
+
'*',
|
|
44
|
+
now(),
|
|
45
|
+
now()
|
|
46
|
+
WHERE NOT EXISTS (
|
|
47
|
+
SELECT 1 FROM "users" WHERE "normalizedEmail" = lower('${AUTH_SUPER_ADMIN_EMAIL}')
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
INSERT INTO "userRolesMapping" (id, "userId", "roleId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
|
|
51
|
+
SELECT encode(gen_random_bytes(12), 'hex'), u.id, r.id, true, '*', now(), now()
|
|
52
|
+
FROM "users" u
|
|
53
|
+
CROSS JOIN "roles" r
|
|
54
|
+
WHERE u."normalizedEmail" = lower('${AUTH_SUPER_ADMIN_EMAIL}')
|
|
55
|
+
AND r.name = 'Super Admin'
|
|
56
|
+
AND NOT EXISTS (
|
|
57
|
+
SELECT 1 FROM "userRolesMapping" m WHERE m."userId" = u.id AND m."roleId" = r.id
|
|
58
|
+
);
|