@astratra/security 1.6.0 → 1.7.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 +30 -0
- package/package.json +1 -1
- package/src/index.d.ts +13 -0
- package/src/rateLimiters.js +7 -1
package/README.md
CHANGED
|
@@ -249,6 +249,36 @@ seule fois par processus) signale ce cas. Fournir son propre `keyGenerator`
|
|
|
249
249
|
contourne complètement ce point.
|
|
250
250
|
```
|
|
251
251
|
|
|
252
|
+
### Store Redis namespacé
|
|
253
|
+
|
|
254
|
+
Par défaut, `redisUrl` seul suffit à activer un store Redis partagé entre
|
|
255
|
+
tous les limiteurs. Pour isoler les compteurs par domaine (connexion,
|
|
256
|
+
réservation, avis...) sur la même instance Redis, construis le store avec
|
|
257
|
+
un préfixe et passe-le via `options.store` :
|
|
258
|
+
|
|
259
|
+
```js
|
|
260
|
+
const { createApiLimiter, createLoginLimiter, createRedisRateLimitStore } = require('@astratra/security');
|
|
261
|
+
|
|
262
|
+
const loginStore = createRedisRateLimitStore({
|
|
263
|
+
redisUrl: process.env.REDIS_URL,
|
|
264
|
+
prefix: 'mon-app:rate:connexion:'
|
|
265
|
+
});
|
|
266
|
+
app.use('/auth', createLoginLimiter({ store: loginStore }));
|
|
267
|
+
|
|
268
|
+
const bookingStore = createRedisRateLimitStore({
|
|
269
|
+
redisUrl: process.env.REDIS_URL,
|
|
270
|
+
prefix: 'mon-app:rate:reservation:'
|
|
271
|
+
});
|
|
272
|
+
app.use('/bookings', createApiLimiter({ store: bookingStore }));
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
`prefix` est entièrement libre et défini par le projet consommateur — Astratra
|
|
276
|
+
ne connaît aucun nom de domaine métier. Si Redis n'est pas joignable au
|
|
277
|
+
démarrage ou perd la connexion, le store bascule automatiquement sur un
|
|
278
|
+
store en mémoire process (mêmes garanties que le store par défaut sans
|
|
279
|
+
`prefix`), donc aucune configuration additionnelle n'est nécessaire pour
|
|
280
|
+
gérer l'indisponibilité de Redis.
|
|
281
|
+
|
|
252
282
|
## WAF
|
|
253
283
|
|
|
254
284
|
```js
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -247,6 +247,19 @@ export interface RateLimitOptions {
|
|
|
247
247
|
redisUrl?: string;
|
|
248
248
|
}
|
|
249
249
|
|
|
250
|
+
export interface RedisRateLimitStoreOptions {
|
|
251
|
+
/** URL de connexion Redis. */
|
|
252
|
+
redisUrl?: string;
|
|
253
|
+
/** Préfixe Redis librement défini par le projet consommateur. */
|
|
254
|
+
prefix?: string;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Crée un store express-rate-limit Redis avec bascule mémoire si Redis est
|
|
259
|
+
* indisponible. Le préfixe est optionnel et entièrement défini par le projet.
|
|
260
|
+
*/
|
|
261
|
+
export function createRedisRateLimitStore(options?: RedisRateLimitStoreOptions): RateLimitStore | undefined;
|
|
262
|
+
|
|
250
263
|
export interface LoginBlockedContext {
|
|
251
264
|
ip: string;
|
|
252
265
|
req: RequestLike;
|
package/src/rateLimiters.js
CHANGED
|
@@ -98,7 +98,7 @@ const createFailoverStore = (redisStore, memoryStore, connectPromise) => {
|
|
|
98
98
|
};
|
|
99
99
|
};
|
|
100
100
|
|
|
101
|
-
const createRedisStore = (redisUrl) => {
|
|
101
|
+
const createRedisStore = (redisUrl, prefix) => {
|
|
102
102
|
if (!redisUrl) return undefined;
|
|
103
103
|
|
|
104
104
|
let RedisModule;
|
|
@@ -122,6 +122,7 @@ const createRedisStore = (redisUrl) => {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
const redisStore = new RedisStore({
|
|
125
|
+
...(prefix ? { prefix } : {}),
|
|
125
126
|
sendCommand: (...args) => client.sendCommand(args)
|
|
126
127
|
});
|
|
127
128
|
const memoryStore = createMemoryStore();
|
|
@@ -138,6 +139,10 @@ const createRedisStore = (redisUrl) => {
|
|
|
138
139
|
}
|
|
139
140
|
};
|
|
140
141
|
|
|
142
|
+
// Store Redis générique : le namespace est choisi par le projet consommateur.
|
|
143
|
+
// Astratra ne connaît donc aucun domaine métier (auth, commandes, réservations…).
|
|
144
|
+
const createRedisRateLimitStore = ({ redisUrl, prefix } = {}) => createRedisStore(redisUrl, prefix);
|
|
145
|
+
|
|
141
146
|
const resolveStore = (options) => {
|
|
142
147
|
if (options.store) return options.store;
|
|
143
148
|
return createRedisStore(options.redisUrl);
|
|
@@ -235,6 +240,7 @@ const createAccountLimiter = (options = {}) => {
|
|
|
235
240
|
};
|
|
236
241
|
|
|
237
242
|
module.exports = {
|
|
243
|
+
createRedisRateLimitStore,
|
|
238
244
|
createApiLimiter,
|
|
239
245
|
createLoginLimiter,
|
|
240
246
|
createAccountLimiter,
|