Skip to content

Commit

Permalink
move redis object in config, and disconnect after test
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianboros committed Sep 25, 2024
1 parent e1c77aa commit 0fd372b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
18 changes: 18 additions & 0 deletions packages/wallet/backend/src/config/redis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { Env } from '@/config/env'
import { Redis } from 'ioredis'
import { RedisClient } from '@shared/backend'
let redisClient: Redis | null = null

export const createRedisClient = (env: Env): Redis => {
redisClient = new Redis(env.REDIS_URL)

redisClient.on('error', (err) => {
console.error('Redis error:', err)
})

return redisClient
}

export const getRedisClient = (env: Env): Redis | null => {
if (!redisClient) {
return createRedisClient(env)
}
return redisClient
}

export function createRedis(env: Env) {
const redis = new Redis(env.REDIS_URL)
Expand Down
62 changes: 30 additions & 32 deletions packages/wallet/backend/src/middleware/rateLimit.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
import { env } from '@/config/env'
import Redis from 'ioredis'
import { RateLimiterRedisHelper } from '@/rateLimit/service'
import { NextFunction, Request, Response } from 'express'

const redisClient = new Redis(env.REDIS_URL)

const sendEmailLimiter = new RateLimiterRedisHelper({
storeClient: redisClient,
keyPrefix: 'send_email',
points: env.SEND_EMAIL_RATE_LIMIT,
duration: env.SEND_EMAIL_RATE_LIMIT_RESET_INTERVAL_IN_SECONDS,
blockDuration: env.SEND_EMAIL_RATE_LIMIT_PAUSE_IN_SECONDS
})
const loginAttemptLimiter = new RateLimiterRedisHelper({
storeClient: redisClient,
keyPrefix: 'login_email',
points: env.LOGIN_RATE_LIMIT,
duration: env.LOGIN_RATE_LIMIT_RESET_INTERVAL_IN_SECONDS,
blockDuration: env.LOGIN_RATE_LIMIT_PAUSE_IN_SECONDS
})
const loginIPLimiter = new RateLimiterRedisHelper({
storeClient: redisClient,
keyPrefix: 'login_ip',
points: env.LOGIN_IP_RATE_LIMIT,
duration: env.LOGIN_IP_RATE_LIMIT_RESET_INTERVAL_IN_SECONDS,
blockDuration: env.LOGIN_IP_RATE_LIMIT_PAUSE_IN_SECONDS
})
const loginBlockIPLimiter = new RateLimiterRedisHelper({
storeClient: redisClient,
keyPrefix: 'login_block_ip',
points: env.LOGIN_IP_BLOCK_RATE_LIMIT,
duration: env.LOGIN_IP_BLOCK_RATE_LIMIT_RESET_INTERVAL_IN_SECONDS,
blockDuration: env.LOGIN_IP_BLOCK_RATE_LIMIT_PAUSE_IN_SECONDS
})
import { getRedisClient } from '@/config/redis'

export const rateLimiterEmail = async (
req: Request,
_res: Response,
next: NextFunction
): Promise<void> => {
try {
const sendEmailLimiter = new RateLimiterRedisHelper({
storeClient: getRedisClient(env),
keyPrefix: 'send_email',
points: env.SEND_EMAIL_RATE_LIMIT,
duration: env.SEND_EMAIL_RATE_LIMIT_RESET_INTERVAL_IN_SECONDS,
blockDuration: env.SEND_EMAIL_RATE_LIMIT_PAUSE_IN_SECONDS
})
await sendEmailLimiter.checkAttempts(req.body.email)
await sendEmailLimiter.useAttempt(req.body.email)
} catch (e) {
Expand All @@ -55,6 +31,28 @@ export const rateLimiterLogin = async (
): Promise<void> => {
try {
const userIp = `${req.ip}`
const loginAttemptLimiter = new RateLimiterRedisHelper({
storeClient: getRedisClient(env),
keyPrefix: 'login_email',
points: env.LOGIN_RATE_LIMIT,
duration: env.LOGIN_RATE_LIMIT_RESET_INTERVAL_IN_SECONDS,
blockDuration: env.LOGIN_RATE_LIMIT_PAUSE_IN_SECONDS
})
const loginIPLimiter = new RateLimiterRedisHelper({
storeClient: getRedisClient(env),
keyPrefix: 'login_ip',
points: env.LOGIN_IP_RATE_LIMIT,
duration: env.LOGIN_IP_RATE_LIMIT_RESET_INTERVAL_IN_SECONDS,
blockDuration: env.LOGIN_IP_RATE_LIMIT_PAUSE_IN_SECONDS
})
const loginBlockIPLimiter = new RateLimiterRedisHelper({
storeClient: getRedisClient(env),
keyPrefix: 'login_block_ip',
points: env.LOGIN_IP_BLOCK_RATE_LIMIT,
duration: env.LOGIN_IP_BLOCK_RATE_LIMIT_RESET_INTERVAL_IN_SECONDS,
blockDuration: env.LOGIN_IP_BLOCK_RATE_LIMIT_PAUSE_IN_SECONDS
})

await loginBlockIPLimiter.checkAttempts(userIp)
await loginBlockIPLimiter.useAttempt(userIp)

Expand Down
3 changes: 3 additions & 0 deletions packages/wallet/backend/tests/auth/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { AuthController } from '@/auth/controller'
import type { AuthService } from '@/auth/service'
import { applyMiddleware } from '@/tests/utils'
import { withSession } from '@/middleware/withSession'
import { getRedisClient } from '@/config/redis'
import { rateLimiterLogin, rateLimiterEmail } from '@/middleware/rateLimit'
import type { UserService } from '@/user/service'
import {
Expand Down Expand Up @@ -63,6 +64,8 @@ describe('Authentication Controller', (): void => {
afterAll(async (): Promise<void> => {
await appContainer.stop()
await knex.destroy()
const redisClient = getRedisClient(env)
redisClient?.disconnect()
})

afterEach(async (): Promise<void> => {
Expand Down

0 comments on commit 0fd372b

Please sign in to comment.