Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admission check endpoint #338

Merged
merged 8 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/controllers/invoices/get-invoice-pubkey-check-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Request, Response } from 'express'
import { createLogger } from '../../factories/logger-factory'
import { getRemoteAddress } from '../../utils/http'
import { IController } from '../../@types/controllers'
import { IRateLimiter } from '../../@types/utils'
import { IUserRepository } from '../../@types/repositories'
import { path } from 'ramda'
import { Settings } from '../../@types/settings'

const debug = createLogger('get-invoice-pubkey-check-controller')

export class GetInvoicePubkeyCheckController implements IController {
public constructor(
private readonly userRepository: IUserRepository,
private readonly settings: () => Settings,
private readonly rateLimiter: () => IRateLimiter,
){}

public async handleRequest(request: Request, response: Response): Promise<void> {
const currentSettings = this.settings()

const limited = await this.isRateLimited(request, currentSettings)
if (limited) {
response
.status(429)
.setHeader('content-type', 'text/plain; charset=utf8')
.send('Too many requests')
return
}

const pubkey = request.params.pubkey
const user = await this.userRepository.findByPubkey(pubkey)

console.log('user', user)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove


let userAdmitted = false

const minBalance = currentSettings.limits?.event?.pubkey?.minBalance
if (user && user.isAdmitted && (!minBalance || user.balance >= minBalance)) {
userAdmitted = true
}

response
.status(200)
.setHeader('content-type', 'application/json; charset=utf8')
.send({ userAdmitted })

return
}

public async isRateLimited(request: Request, settings: Settings) {
const rateLimits = path(['limits', 'invoice', 'rateLimits'], settings)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a different rate limit for this endpoint

if (!Array.isArray(rateLimits) || !rateLimits.length) {
return false
}

const ipWhitelist = path(['limits', 'invoice', 'ipWhitelist'], settings)
const remoteAddress = getRemoteAddress(request, settings)

let limited = false
if (Array.isArray(ipWhitelist) && !ipWhitelist.includes(remoteAddress)) {
const rateLimiter = this.rateLimiter()
for (const { rate, period } of rateLimits) {
if (await rateLimiter.hit(`${remoteAddress}:invoice:${period}`, 1, { period, rate })) {
debug('rate limited %s: %d in %d milliseconds', remoteAddress, rate, period)
limited = true
}
}
}
return limited
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createSettings } from '../settings-factory'
import { GetInvoicePubkeyCheckController } from '../../controllers/invoices/get-invoice-pubkey-check-controller'
import { getMasterDbClient } from '../../database/client'
import { slidingWindowRateLimiterFactory } from '../rate-limiter-factory'
import { UserRepository } from '../../repositories/user-repository'

export const createGetInvoicePubkeyCheckController = () => {
const dbClient = getMasterDbClient()
const userRepository = new UserRepository(dbClient)

return new GetInvoicePubkeyCheckController(
userRepository,
createSettings,
slidingWindowRateLimiterFactory
)
}
4 changes: 3 additions & 1 deletion src/routes/invoices/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router, urlencoded } from 'express'

import { createGetInvoiceController } from '../../factories/controllers/get-invoice-controller-factory'
import { createGetInvoicePubkeyCheckController } from '../../factories/controllers/get-invoice-pubkey-check-controller-factory'
import { createGetInvoiceStatusController } from '../../factories/controllers/get-invoice-status-controller-factory'
import { createPostInvoiceController } from '../../factories/controllers/post-invoice-controller-factory'
import { withController } from '../../handlers/request-handlers/with-controller-request-handler'
Expand All @@ -10,6 +11,7 @@ const invoiceRouter = Router()
invoiceRouter
.get('/', withController(createGetInvoiceController))
.get('/:invoiceId/status', withController(createGetInvoiceStatusController))
.get('/check/:pubkey', withController(createGetInvoicePubkeyCheckController))
.post('/', urlencoded({ extended: true }), withController(createPostInvoiceController))

export default invoiceRouter
export default invoiceRouter