mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-27 14:15:50 +02:00
use redis store in rate limiters
This commit is contained in:
parent
0e87b6e48b
commit
fb5010a2b5
3 changed files with 17 additions and 28 deletions
|
@ -214,21 +214,6 @@ export const configSchema = z
|
||||||
no_reply: z.string().email().optional()
|
no_reply: z.string().email().optional()
|
||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
users: z.object({
|
|
||||||
server_admin: z.object({
|
|
||||||
email: z
|
|
||||||
.string()
|
|
||||||
.email()
|
|
||||||
.optional()
|
|
||||||
.transform(getEnvOrYaml("USERS_SERVERADMIN_EMAIL"))
|
|
||||||
.pipe(z.string().email())
|
|
||||||
.transform((v) => v.toLowerCase()),
|
|
||||||
password: passwordSchema
|
|
||||||
.optional()
|
|
||||||
.transform(getEnvOrYaml("USERS_SERVERADMIN_PASSWORD"))
|
|
||||||
.pipe(passwordSchema)
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
flags: z
|
flags: z
|
||||||
.object({
|
.object({
|
||||||
require_email_verification: z.boolean().optional(),
|
require_email_verification: z.boolean().optional(),
|
||||||
|
|
|
@ -7,7 +7,8 @@ import config from "@server/lib/config";
|
||||||
import { RedisStore } from "rate-limit-redis";
|
import { RedisStore } from "rate-limit-redis";
|
||||||
import redisManager from "@server/db/redis";
|
import redisManager from "@server/db/redis";
|
||||||
|
|
||||||
export let rateLimitStore: Store = new MemoryStore();
|
export function createStore(): Store {
|
||||||
|
let rateLimitStore: Store = new MemoryStore();
|
||||||
if (config.getRawConfig().flags?.enable_redis) {
|
if (config.getRawConfig().flags?.enable_redis) {
|
||||||
const client = redisManager.client!;
|
const client = redisManager.client!;
|
||||||
rateLimitStore = new RedisStore({
|
rateLimitStore = new RedisStore({
|
||||||
|
@ -15,6 +16,8 @@ if (config.getRawConfig().flags?.enable_redis) {
|
||||||
(await client.call(command, args)) as any
|
(await client.call(command, args)) as any
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
return rateLimitStore;
|
||||||
|
}
|
||||||
|
|
||||||
export function rateLimitMiddleware({
|
export function rateLimitMiddleware({
|
||||||
windowMin,
|
windowMin,
|
||||||
|
@ -44,7 +47,7 @@ export function rateLimitMiddleware({
|
||||||
createHttpError(HttpCode.TOO_MANY_REQUESTS, message)
|
createHttpError(HttpCode.TOO_MANY_REQUESTS, message)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
store: rateLimitStore
|
store: createStore()
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return rateLimit({
|
return rateLimit({
|
||||||
|
@ -57,7 +60,8 @@ export function rateLimitMiddleware({
|
||||||
return next(
|
return next(
|
||||||
createHttpError(HttpCode.TOO_MANY_REQUESTS, message)
|
createHttpError(HttpCode.TOO_MANY_REQUESTS, message)
|
||||||
);
|
);
|
||||||
}
|
},
|
||||||
|
store: createStore()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ import {
|
||||||
verifyIsLoggedInUser,
|
verifyIsLoggedInUser,
|
||||||
verifyClientAccess,
|
verifyClientAccess,
|
||||||
verifyApiKeyAccess,
|
verifyApiKeyAccess,
|
||||||
rateLimitStore,
|
createStore,
|
||||||
} from "@server/middlewares";
|
} from "@server/middlewares";
|
||||||
import { verifyUserHasAction } from "../middlewares/verifyUserHasAction";
|
import { verifyUserHasAction } from "../middlewares/verifyUserHasAction";
|
||||||
import { ActionsEnum } from "@server/auth/actions";
|
import { ActionsEnum } from "@server/auth/actions";
|
||||||
|
@ -784,7 +784,7 @@ authRouter.post(
|
||||||
const message = `You can only request an email verification code ${3} times every ${15} minutes. Please try again later.`;
|
const message = `You can only request an email verification code ${3} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
},
|
},
|
||||||
store: rateLimitStore
|
store: createStore()
|
||||||
}),
|
}),
|
||||||
auth.requestEmailVerificationCode
|
auth.requestEmailVerificationCode
|
||||||
);
|
);
|
||||||
|
@ -805,7 +805,7 @@ authRouter.post(
|
||||||
const message = `You can only request a password reset ${3} times every ${15} minutes. Please try again later.`;
|
const message = `You can only request a password reset ${3} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
},
|
},
|
||||||
store: rateLimitStore
|
store: createStore()
|
||||||
}),
|
}),
|
||||||
auth.requestPasswordReset
|
auth.requestPasswordReset
|
||||||
);
|
);
|
||||||
|
@ -825,7 +825,7 @@ authRouter.post(
|
||||||
const message = `You can only request an email OTP ${10} times every ${15} minutes. Please try again later.`;
|
const message = `You can only request an email OTP ${10} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
},
|
},
|
||||||
store: rateLimitStore
|
store: createStore()
|
||||||
}),
|
}),
|
||||||
resource.authWithWhitelist
|
resource.authWithWhitelist
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue