@astratra/security 1.4.0 → 1.5.0
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 +12 -0
- package/package.json +1 -1
- package/src/index.d.ts +7 -0
- package/src/jwtAuth.js +39 -5
package/README.md
CHANGED
|
@@ -103,6 +103,18 @@ app.delete('/api/projects/:id', authorizeRoles('owner', 'admin'), handler);
|
|
|
103
103
|
Par défaut, le middleware limite la vérification à `HS256`. Si ton application
|
|
104
104
|
utilise un autre algorithme, configure explicitement `algorithms`.
|
|
105
105
|
|
|
106
|
+
**Session par cookie sans `cookieParserMiddleware()` monté en amont** :
|
|
107
|
+
`createAuthMiddleware` a besoin de `req.cookies` pour lire un cookie de
|
|
108
|
+
session. Si aucun token n'est trouvé (ni cookie, ni header `Authorization`)
|
|
109
|
+
**et** que `req.cookies` est `undefined` — c'est-à-dire que
|
|
110
|
+
`cookieParserMiddleware()` (section suivante) n'a jamais tourné — le
|
|
111
|
+
middleware ne renvoie pas un 401 silencieux : il transmet une
|
|
112
|
+
`AuthConfigurationError` à `next(error)`, pour distinguer une vraie
|
|
113
|
+
absence de session d'un pipeline mal monté. Un routeur monté séparément de
|
|
114
|
+
`createSaasApp()` (au lieu de passer par `options.extendRoutes`) est le cas
|
|
115
|
+
le plus courant où ça arrive — voir
|
|
116
|
+
[`docs/guides/custom-routes-wiring.md`](../../docs/guides/custom-routes-wiring.md).
|
|
117
|
+
|
|
106
118
|
## Sessions cookie HttpOnly + CSRF
|
|
107
119
|
|
|
108
120
|
```js
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -141,6 +141,13 @@ export interface AuthMiddlewareOptions<TDecoded = Record<string, unknown>> {
|
|
|
141
141
|
export function createAuthMiddleware<TDecoded = Record<string, unknown>>(options: AuthMiddlewareOptions<TDecoded>): RequestHandler;
|
|
142
142
|
export function authorizeRoles(...roles: string[]): RequestHandler;
|
|
143
143
|
|
|
144
|
+
/**
|
|
145
|
+
* Thrown by the default token extractor — and passed to `next(error)`,
|
|
146
|
+
* never swallowed into a 401 — when `req.cookies` is `undefined` (no
|
|
147
|
+
* cookie parser mounted) and no token was found by any other means either.
|
|
148
|
+
*/
|
|
149
|
+
export class AuthConfigurationError extends Error {}
|
|
150
|
+
|
|
144
151
|
export interface RevocationStore {
|
|
145
152
|
revoke(jti: string, expiresAt: number): Awaitable<void>;
|
|
146
153
|
isRevoked(jti?: string): Awaitable<boolean>;
|
package/src/jwtAuth.js
CHANGED
|
@@ -13,16 +13,46 @@ const DEFAULT_FORBIDDEN = {
|
|
|
13
13
|
|
|
14
14
|
const DEFAULT_ALGORITHMS = ['HS256'];
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Thrown by the default token extractor when `req.cookies` is `undefined` —
|
|
18
|
+
* meaning `cookieParserMiddleware()` was never mounted ahead of this
|
|
19
|
+
* middleware. That misconfiguration must never be swallowed into a plain
|
|
20
|
+
* 401 (indistinguishable from "no valid session"): it is a wiring bug, not
|
|
21
|
+
* an authentication failure, and otherwise stays silent until someone
|
|
22
|
+
* traces a session that never sticks. See docs/guides/custom-routes-wiring.md.
|
|
23
|
+
*/
|
|
24
|
+
class AuthConfigurationError extends Error {
|
|
25
|
+
constructor(message) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.name = 'AuthConfigurationError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
16
31
|
const defaultExtractToken = (req) => {
|
|
17
|
-
const cookies = req.cookies
|
|
18
|
-
|
|
19
|
-
|
|
32
|
+
const cookies = req.cookies;
|
|
33
|
+
if (cookies !== undefined) {
|
|
34
|
+
const cookieToken = cookies[DEFAULT_SESSION_COOKIE_NAME] || cookies.token;
|
|
35
|
+
if (cookieToken) return cookieToken;
|
|
36
|
+
}
|
|
20
37
|
|
|
21
38
|
const authorization = req.headers && req.headers.authorization;
|
|
22
39
|
if (authorization && authorization.startsWith('Bearer ')) {
|
|
23
40
|
return authorization.slice('Bearer '.length);
|
|
24
41
|
}
|
|
25
42
|
|
|
43
|
+
// Only now, having found no token by any means, does an unparsed
|
|
44
|
+
// req.cookies become suspicious rather than merely "no cookie sent" —
|
|
45
|
+
// a mounted parser always leaves at least `{}` behind (see cookies.js),
|
|
46
|
+
// so `undefined` here can only mean the middleware never ran.
|
|
47
|
+
if (cookies === undefined) {
|
|
48
|
+
throw new AuthConfigurationError(
|
|
49
|
+
'createAuthMiddleware: req.cookies is undefined. Mount cookieParserMiddleware() ' +
|
|
50
|
+
'(from @astratra/security) before this middleware so cookie-based sessions can be read — ' +
|
|
51
|
+
'see the "Cookies" section of the @astratra/security README, or pass a custom ' +
|
|
52
|
+
'options.extractToken if this app never authenticates via cookie.'
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
26
56
|
return null;
|
|
27
57
|
};
|
|
28
58
|
|
|
@@ -90,7 +120,10 @@ const createAuthMiddleware = (options = {}) => {
|
|
|
90
120
|
|
|
91
121
|
req.user = decoded;
|
|
92
122
|
return next();
|
|
93
|
-
} catch (
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (error instanceof AuthConfigurationError) {
|
|
125
|
+
return next(error);
|
|
126
|
+
}
|
|
94
127
|
return res.status(401).json(unauthorizedMessage);
|
|
95
128
|
}
|
|
96
129
|
};
|
|
@@ -106,5 +139,6 @@ const authorizeRoles = (...allowedRoles) => (req, res, next) => {
|
|
|
106
139
|
|
|
107
140
|
module.exports = {
|
|
108
141
|
createAuthMiddleware,
|
|
109
|
-
authorizeRoles
|
|
142
|
+
authorizeRoles,
|
|
143
|
+
AuthConfigurationError
|
|
110
144
|
};
|