Skip to content

Commit 9f90a78

Browse files
committed
chore: extract get user IP address as standalone function
1 parent b8c5c1b commit 9f90a78

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

src/routes/v2/auth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const logger = require("@logger/logger").default
99
const { attachReadRouteHandlerWrapper } = require("@middleware/routeHandler")
1010

1111
const FRONTEND_URL = config.get("app.frontendUrl")
12-
const { isSecure } = require("@utils/auth-utils")
12+
const { isSecure, getUserIPAddress } = require("@utils/auth-utils")
1313

1414
const {
1515
EmailSchema,
@@ -106,7 +106,7 @@ class AuthRouter {
106106
message: `Invalid request format: ${error.message}`,
107107
})
108108
const email = rawEmail.toLowerCase()
109-
const userIp = isSecure ? req.get("cf-connecting-ip") : req.ip
109+
const userIp = getUserIPAddress(req)
110110
const userInfo = (
111111
await this.authService.verifyOtp({ email, otp, clientIp: userIp })
112112
).value

src/routes/v2/authenticated/users.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import UserSessionData from "@classes/UserSessionData"
1414
import DatabaseError from "@root/errors/DatabaseError"
1515
import { isError, RequestHandler } from "@root/types"
1616
import { nameAnonymousMethods } from "@root/utils/apm-utils"
17-
import { isSecure } from "@root/utils/auth-utils"
17+
import { getUserIPAddress } from "@root/utils/auth-utils"
1818
import {
1919
VerifyEmailOtpSchema,
2020
VerifyMobileNumberOtpSchema,
@@ -84,7 +84,7 @@ export class UsersRouter {
8484
const userId = userSessionData.isomerUserId
8585
const parsedEmail = email.toLowerCase()
8686

87-
const userIp = isSecure ? req.get("cf-connecting-ip") : req.ip
87+
const userIp = getUserIPAddress(req)
8888
return this.usersService
8989
.verifyEmailOtp(parsedEmail, otp, userIp)
9090
.andThen(() =>
@@ -144,7 +144,7 @@ export class UsersRouter {
144144
const { userSessionData } = res.locals
145145
const userId = userSessionData.isomerUserId
146146

147-
const userIp = isSecure ? req.get("cf-connecting-ip") : req.ip
147+
const userIp = getUserIPAddress(req)
148148
return this.usersService
149149
.verifyMobileOtp(mobile, otp, userIp)
150150
.andThen(() =>

src/services/utilServices/RateLimiter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import rateLimit from "express-rate-limit"
22

33
import { BaseIsomerError } from "@root/errors/BaseError"
4-
import { isSecure } from "@root/utils/auth-utils"
4+
import { getUserIPAddress } from "@root/utils/auth-utils"
55

66
const DEFAULT_AUTH_TOKEN_EXPIRY_MILLISECONDS = 900000
77

@@ -21,7 +21,7 @@ export const rateLimiter = rateLimit({
2121
// We know that this key exists in a secure env (Cloudflare)
2222
// See https://developers.cloudflare.com/fundamentals/reference/http-request-headers/#cf-connecting-ip
2323
keyGenerator: (req) => {
24-
const userIp = isSecure ? req.get("cf-connecting-ip") : req.ip
24+
const userIp = getUserIPAddress(req)
2525
if (!userIp) {
2626
// This should never happen, but if it does, we should know about it
2727
throw new BaseIsomerError({

src/utils/auth-utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const NODE_ENV = config.get("env")
44

55
const isSecure = NODE_ENV !== "dev" && NODE_ENV !== "test"
66

7+
// FIXME: This makes a strong assumption that the app is always behind
8+
// Cloudflare, but may not necessarily be the case when Cloudflare is disabled.
9+
// Fix this to fallback to other headers or req.ip if Cloudflare headers are not
10+
// present.
11+
const getUserIPAddress = (req) =>
12+
isSecure ? req.get("cf-connecting-ip") : req.ip
13+
714
module.exports = {
815
isSecure,
16+
getUserIPAddress,
917
}

0 commit comments

Comments
 (0)