Skip to content

Commit c7adf42

Browse files
committed
allow LOGIN_REQUEST_THROTTLER_LIMIT and LOGIN_REQUEST_THROTTLER_TTL variables
1 parent f6cfdad commit c7adf42

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

.env.template

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ GATEWAY_API_KEY=
5656
DEBUG=false
5757
# Whether 'verbose' level logs should be enabled
5858
VERBOSE=false
59-
# Enable rate limitting
59+
# Enable rate limiting
6060
THROTTLER_ENABLED=true
61+
# number of login requests from the same IP permitted in LOGIN_REQUEST_THROTTLER_TTL milliseconds (default: 50)
62+
LOGIN_REQUEST_THROTTLER_LIMIT=
63+
# the duration in milliseconds to enforce LOGIN_REQUEST_THROTTLER_LIMIT (default: 60,000)
64+
LOGIN_REQUEST_THROTTLER_TTL=
65+
6166
# Disable iteration for password hashing (not recommended for production)
6267
# See https://pages.nist.gov/800-63-3/sp800-63b.html
6368
# DANGEROUSLY_DISABLE_PBKDF2_ITERATION=

apps/api/src/auth/auth.controller.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { $NumberLike } from '@douglasneuroinformatics/libjs';
12
import { CurrentUser } from '@douglasneuroinformatics/libnest';
23
import type { RequestUser } from '@douglasneuroinformatics/libnest';
34
import { Body, Controller, Get, HttpCode, HttpStatus, Post } from '@nestjs/common';
45
import { ApiOperation } from '@nestjs/swagger';
56
import { Throttle } from '@nestjs/throttler';
67
import { $LoginCredentials } from '@opendatacapture/schemas/auth';
8+
import z from 'zod/v4';
79

810
import { RouteAccess } from '@/core/decorators/route-access.decorator.js';
911

@@ -25,7 +27,12 @@ export class AuthController {
2527
@HttpCode(HttpStatus.OK)
2628
@Post('login')
2729
@RouteAccess('public')
28-
@Throttle({ long: { limit: 50, ttl: 60_000 } })
30+
@Throttle({
31+
long: {
32+
limit: $NumberLike.pipe(z.number().int().positive()).default(50).parse(process.env.LOGIN_REQUEST_THROTTLER_LIMIT),
33+
ttl: $NumberLike.pipe(z.number().int().positive()).default(60_000).parse(process.env.LOGIN_REQUEST_THROTTLER_TTL)
34+
}
35+
})
2936
async login(@Body() credentials: $LoginCredentials): Promise<{ accessToken: string }> {
3037
return this.authService.login(credentials);
3138
}

docker-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ services:
4343
- GATEWAY_SITE_ADDRESS
4444
- SECRET_KEY
4545
- THROTTLER_ENABLED
46+
- LOGIN_REQUEST_THROTTLER_LIMIT
47+
- LOGIN_REQUEST_THROTTLER_TTL
4648
- VERBOSE
4749
expose:
4850
- 80

0 commit comments

Comments
 (0)