All Projects → nodes-vapor → gatekeeper

nodes-vapor / gatekeeper

Licence: MIT license
Rate limiting middleware for Vapor 👮

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to gatekeeper

flash
Flash messages between views ⚡️
Stars: ✭ 34 (-37.04%)
Mutual labels:  vapor, server-side-swift, vapor-3
VaporTwilioService
Twilio API provider for all your Vapor needs
Stars: ✭ 19 (-64.81%)
Mutual labels:  vapor, server-side-swift
HomeKitty
A Vapor 3 website to easily browse HomeKit accessories.
Stars: ✭ 75 (+38.89%)
Mutual labels:  vapor, server-side-swift
routing-kit
🚍 High-performance trie-node router.
Stars: ✭ 95 (+75.93%)
Mutual labels:  vapor, server-side-swift
submissions
Provides a common structure to deal with data based API requests
Stars: ✭ 15 (-72.22%)
Mutual labels:  vapor, vapor-3
adaptive throttler
manages multiple throttlers with ability to ramp up and down
Stars: ✭ 31 (-42.59%)
Mutual labels:  rate-limits, rate-limiting
VaporElasticsearch
A Vapor/Swift Elasticsearch client
Stars: ✭ 26 (-51.85%)
Mutual labels:  vapor, vapor-3
Console Kit
💻 APIs for creating interactive CLI tools.
Stars: ✭ 252 (+366.67%)
Mutual labels:  vapor, server-side-swift
fluent-mysql-driver
🖋🐬 Swift ORM (queries, models, relations, etc) built on MySQL.
Stars: ✭ 69 (+27.78%)
Mutual labels:  vapor, server-side-swift
fluent-postgres-driver
🐘 PostgreSQL driver for Fluent.
Stars: ✭ 120 (+122.22%)
Mutual labels:  vapor, server-side-swift
auth
👤 Authentication and Authorization framework for Fluent.
Stars: ✭ 51 (-5.56%)
Mutual labels:  vapor, server-side-swift
ferno
Vapor Firebase Realtime database provider
Stars: ✭ 52 (-3.7%)
Mutual labels:  vapor, vapor-3
Abp.AspNetCoreRateLimit
An Abp module helps you control how often your service is used.
Stars: ✭ 19 (-64.81%)
Mutual labels:  rate-limits, rate-limiting
apns
Vapor APNS for iOS
Stars: ✭ 59 (+9.26%)
Mutual labels:  vapor, vapor-3
go-ratelimit
Ratelimit your methods using Redis
Stars: ✭ 37 (-31.48%)
Mutual labels:  rate-limits, rate-limiting
sqlite-kit
Non-blocking SQLite client library with SQL builder built on SwiftNIO
Stars: ✭ 51 (-5.56%)
Mutual labels:  vapor, server-side-swift
GraphQLRouteCollection
A GraphQL based RouteCollection for Vapor
Stars: ✭ 18 (-66.67%)
Mutual labels:  vapor, server-side-swift
Htmlkit
A type-safe DSL that renders dynamic HTML templates in Swift
Stars: ✭ 229 (+324.07%)
Mutual labels:  vapor, server-side-swift
Telegrammer
Telegram Bot - written with Swift 5.2 / NIO, supports Linux, macOS
Stars: ✭ 248 (+359.26%)
Mutual labels:  vapor, server-side-swift
paginator
Offset pagination for Vapor 🗂
Stars: ✭ 67 (+24.07%)
Mutual labels:  vapor, server-side-swift

Gatekeeper 👮

Swift Version Vapor Version GitHub license

Gatekeeper is a middleware that restricts the number of requests from clients, based on their IP address (can be customized). It works by adding the clients identifier to the cache and count how many requests the clients can make during the Gatekeeper's defined lifespan and give back an HTTP 429(Too Many Requests) if the limit has been reached. The number of requests left will be reset when the defined timespan has been reached.

Please take into consideration that multiple clients can be using the same IP address. eg. public wifi

📦 Installation

Update your Package.swift dependencies:

.package(url: "https://github.com/nodes-vapor/gatekeeper.git", from: "4.0.0"),

as well as to your target (e.g. "App"):

targets: [
    .target(name: "App", dependencies: [..., "Gatekeeper", ...]),
    // ...
]

Getting started 🚀

Configuration

in configure.swift:

import Gatekeeper

// [...]

app.caches.use(.memory)
app.gatekeeper.config = .init(maxRequests: 10, per: .second)

Add to routes

You can add the GatekeeperMiddleware to specific routes or to all.

Specific routes in routes.swift:

let protectedRoutes = router.grouped(GatekeeperMiddleware())
protectedRoutes.get("protected/hello") { req in
    return "Protected Hello, World!"
}

For all requests in configure.swift:

// Register middleware
app.middlewares.use(GatekeeperMiddleware())

Customizing config

By default GatekeeperMiddleware uses app.gatekeeper.config as its configuration. However, you can pass a custom configuration to each GatekeeperMiddleware type via the initializer GatekeeperMiddleware(config:). This allows you to set configuration on a per-route basis.

Key Makers 🔑

By default Gatekeeper uses the client's hostname (IP address) to identify them. This can cause issues where multiple clients are connected from the same network. Therefore, you can customize how Gatekeeper should identify the client by using the GatekeeperKeyMaker protocol.

GatekeeperHostnameKeyMaker is used by default.

You can configure which key maker Gatekeeper should use in configure.swift:

app.gatekeeper.keyMakers.use(.hostname) // default

Custom key maker

This is an example of a key maker that uses the user's ID to identify them.

struct UserIDKeyMaker: GatekeeperKeyMaker {
    public func make(for req: Request) -> EventLoopFuture<String> {
        let userID = try req.auth.require(User.self).requireID()        
        return req.eventLoop.future("gatekeeper_" + userID.uuidString)
    }
}
extension Application.Gatekeeper.KeyMakers.Provider {
    public static var userID: Self {
        .init { app in
            app.gatekeeper.keyMakers.use { _ in UserIDKeyMaker() }
        }
    }
}

configure.swift:

app.gatekeeper.keyMakers.use(.userID)

Cache 🗄

Gatekeeper uses the same cache as configured by app.caches.use() from Vapor, by default. Therefore it is important to set up Vapor's cache if you're using this default behaviour. You can use an in-memory cache for Vapor like so:

configure.swift:

app.cache.use(.memory)

Custom cache

You can override which cache to use by creating your own type that conforms to the Cache protocol from Vapor. Use app.gatekeeper.caches.use() to configure which cache to use.

Credits 🏆

This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Christian. Special thanks goes to madsodgaard for his work on the Vapor 4 version!

License 📄

This package is open-sourced software licensed under the MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].