@galadrim/galadmin 0.2.6 → 0.2.7
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/.github/workflows/main.yml +2 -2
- package/dist/index.js +24 -2
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/config.d.ts +6 -1
- package/package.json +1 -1
- package/src/index.ts +25 -2
- package/src/types/config.ts +13 -1
- package/tests/index.test.ts +49 -0
package/dist/types/config.d.ts
CHANGED
|
@@ -359,12 +359,17 @@ export type AuthRole = {
|
|
|
359
359
|
filters?: string;
|
|
360
360
|
useArgon2?: boolean;
|
|
361
361
|
};
|
|
362
|
+
export type TrustedUser = {
|
|
363
|
+
role: string;
|
|
364
|
+
userId?: string | number;
|
|
365
|
+
};
|
|
362
366
|
export type Config = {
|
|
363
367
|
enriched?: boolean;
|
|
364
368
|
name: string;
|
|
365
369
|
sessionSecret: string;
|
|
366
370
|
useMysql2?: boolean;
|
|
367
|
-
|
|
371
|
+
trustedAuth?: (req: any) => TrustedUser | null | Promise<TrustedUser | null>;
|
|
372
|
+
auth?: {
|
|
368
373
|
username: string;
|
|
369
374
|
password: string;
|
|
370
375
|
};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -65,6 +65,26 @@ export default (config: Config): any => {
|
|
|
65
65
|
name: 'galadmin.sid'
|
|
66
66
|
}));
|
|
67
67
|
|
|
68
|
+
// trustedAuth: the host app (e.g. behind its own SSO) says who the user is, on every request.
|
|
69
|
+
// galadmin then has no login of its own: /api/login is refused, config.auth and authRoles are unused.
|
|
70
|
+
if (config.trustedAuth) {
|
|
71
|
+
app.use(async (req, res, next) => {
|
|
72
|
+
try {
|
|
73
|
+
const user = await config.trustedAuth(req);
|
|
74
|
+
if (user) {
|
|
75
|
+
if (JSON.stringify(req.session.user) !== JSON.stringify(user)) {
|
|
76
|
+
req.session.user = user;
|
|
77
|
+
}
|
|
78
|
+
} else if (req.session.user) {
|
|
79
|
+
delete req.session.user;
|
|
80
|
+
}
|
|
81
|
+
next();
|
|
82
|
+
} catch (err) {
|
|
83
|
+
next(err);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
68
88
|
const passwordMatch = async (password: string, hash: string, useArgon2: boolean) => {
|
|
69
89
|
if (password === hash || bcrypt.compareSync(password, hash)) {
|
|
70
90
|
return true;
|
|
@@ -81,7 +101,10 @@ export default (config: Config): any => {
|
|
|
81
101
|
}
|
|
82
102
|
|
|
83
103
|
app.post('/api/login', async (req, res) => {
|
|
84
|
-
if (
|
|
104
|
+
if (config.trustedAuth) {
|
|
105
|
+
return res.send({ success: false })
|
|
106
|
+
}
|
|
107
|
+
if (config.auth && req.body.username === config.auth.username && req.body.password === config.auth.password) {
|
|
85
108
|
req.session.user = { role: "root" }
|
|
86
109
|
res.send({ success: true })
|
|
87
110
|
} else if (config.authRoles) {
|
|
@@ -126,7 +149,7 @@ export default (config: Config): any => {
|
|
|
126
149
|
|
|
127
150
|
app.use("/api", async (req, res, next) => {
|
|
128
151
|
if (req.method === "OPTIONS" || req.session.user) {
|
|
129
|
-
if (req.session?.user?.userId) {
|
|
152
|
+
if (req.session?.user?.userId && !config.trustedAuth) {
|
|
130
153
|
// we check that the user still exists
|
|
131
154
|
let stillExists = false;
|
|
132
155
|
for (const authRole of config.authRoles) {
|
package/src/types/config.ts
CHANGED
|
@@ -428,6 +428,11 @@ export type AuthRole = {
|
|
|
428
428
|
useArgon2?: boolean;
|
|
429
429
|
};
|
|
430
430
|
|
|
431
|
+
export type TrustedUser = {
|
|
432
|
+
role: string;
|
|
433
|
+
userId?: string | number;
|
|
434
|
+
};
|
|
435
|
+
|
|
431
436
|
export type Config = {
|
|
432
437
|
/** Ne pas renseigner !!! */
|
|
433
438
|
enriched?: boolean;
|
|
@@ -440,8 +445,15 @@ export type Config = {
|
|
|
440
445
|
|
|
441
446
|
useMysql2?: boolean;
|
|
442
447
|
|
|
448
|
+
/**
|
|
449
|
+
* Authentification déléguée à l'application hôte (par exemple derrière son SSO) : appelée à chaque
|
|
450
|
+
* requête, renvoie l'utilisateur (`role`, et `userId` pour la traçabilité) ou null. Quand elle est
|
|
451
|
+
* définie, galadmin n'a plus de login propre : `auth` et `authRoles` ne servent plus.
|
|
452
|
+
*/
|
|
453
|
+
trustedAuth?: (req: any) => TrustedUser | null | Promise<TrustedUser | null>;
|
|
454
|
+
|
|
443
455
|
/** Identifiants permettant d'accéder au back-office */
|
|
444
|
-
auth
|
|
456
|
+
auth?: {
|
|
445
457
|
/** Nom d'utilisateur */
|
|
446
458
|
username: string;
|
|
447
459
|
|
package/tests/index.test.ts
CHANGED
|
@@ -258,6 +258,55 @@ describe("index middleware (src/index.ts)", () => {
|
|
|
258
258
|
});
|
|
259
259
|
});
|
|
260
260
|
|
|
261
|
+
describe("trustedAuth", () => {
|
|
262
|
+
const trusted = (user: any) =>
|
|
263
|
+
mysqlConfig({ trustedAuth: jest.fn(async () => user) });
|
|
264
|
+
|
|
265
|
+
it("authenticates from the host app, without a galadmin login", async () => {
|
|
266
|
+
const { app } = buildAppWithDb(trusted({ role: "root", userId: 7 }), withSchema());
|
|
267
|
+
const auth = await request(app).get("/api/isAuthenticated").expect(200);
|
|
268
|
+
expect(auth.body).toEqual({ isAuthenticated: true });
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("refuses the api when the host app returns no user", async () => {
|
|
272
|
+
const { app } = buildAppWithDb(trusted(null), withSchema());
|
|
273
|
+
await request(app).get("/api/config").expect(401);
|
|
274
|
+
const auth = await request(app).get("/api/isAuthenticated").expect(200);
|
|
275
|
+
expect(auth.body).toEqual({ isAuthenticated: false });
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("refuses the password login, shared account included", async () => {
|
|
279
|
+
const { app } = buildAppWithDb(trusted(null), withSchema());
|
|
280
|
+
const res = await request(app)
|
|
281
|
+
.post("/api/login")
|
|
282
|
+
.send({ username: "admin", password: "admin-pass" })
|
|
283
|
+
.expect(200);
|
|
284
|
+
expect(res.body).toEqual({ success: false });
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("drops the access as soon as the host app stops vouching for the user", async () => {
|
|
288
|
+
let user: any = { role: "user", userId: 3 };
|
|
289
|
+
const config = mysqlConfig({ trustedAuth: jest.fn(async () => user) });
|
|
290
|
+
const { app } = buildAppWithDb(config, withSchema());
|
|
291
|
+
const agent = request.agent(app);
|
|
292
|
+
expect((await agent.get("/api/isAuthenticated")).body).toEqual({ isAuthenticated: true });
|
|
293
|
+
user = null;
|
|
294
|
+
expect((await agent.get("/api/isAuthenticated")).body).toEqual({ isAuthenticated: false });
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("never runs the authRoles re-check against the database", async () => {
|
|
298
|
+
const config = mysqlConfig({
|
|
299
|
+
trustedAuth: jest.fn(async () => ({ role: "editor", userId: 3 })),
|
|
300
|
+
authRoles: [
|
|
301
|
+
{ role: "editor", table: "users", usernameField: "u", passwordField: "p", idField: "id" },
|
|
302
|
+
],
|
|
303
|
+
});
|
|
304
|
+
const { app, instance } = buildAppWithDb(config, withSchema());
|
|
305
|
+
await request(app).get("/api/isAuthenticated").expect(200);
|
|
306
|
+
expect(instance.query.mock.calls.some(([sql]: any[]) => /FROM \?\? WHERE \?\? = \?/.test(sql))).toBe(false);
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
|
|
261
310
|
describe("GET /api/isAuthenticated and /api/logout", () => {
|
|
262
311
|
it("reports unauthenticated state before login", async () => {
|
|
263
312
|
const { app } = buildAppWithDb(mysqlConfig(), withSchema(fruitSchema()));
|