Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Vapor 4 support + custom cache keys #20

Merged
merged 9 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
67 changes: 0 additions & 67 deletions .circleci/config.yml

This file was deleted.

2 changes: 0 additions & 2 deletions .codebeatignore

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: test
on:
pull_request:
push:
branches:
- master
jobs:
linux:
runs-on: ubuntu-latest
container: swift:5.3-focal
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Run tests with Thread Sanitizer
run: swift test --enable-test-discovery --sanitize=thread
macOS:
runs-on: macos-latest
steps:
- name: Select latest available Xcode
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest
- name: Check out code
uses: actions/checkout@v2
- name: Run tests with Thread Sanitizer
run: swift test --enable-test-discovery --sanitize=thread
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Config/secrets/
.DS_Store
.swift-version
CMakeLists.txt
Package.resolved
Package.resolved
.swiftpm
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017-2019 Nodes
Copyright (c) 2017-2021 Nodes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
16 changes: 11 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
// swift-tools-version:4.2
// swift-tools-version:5.3
import PackageDescription

let package = Package(
name: "Gatekeeper",
name: "gatekeeper",
platforms: [
.macOS(.v10_15),
],
products: [
.library(
name: "Gatekeeper",
targets: ["Gatekeeper"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.38.0"),
],
targets: [
.target(
name: "Gatekeeper",
dependencies: [
"Vapor"
.product(name: "Vapor", package: "vapor")
]),
.testTarget(
name: "GatekeeperTests",
dependencies: ["Gatekeeper"]),
dependencies: [
"Gatekeeper",
.product(name: "XCTVapor", package: "vapor")
]),
]
)
87 changes: 64 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Gatekeeper 👮
[![Swift Version](https://img.shields.io/badge/Swift-4.2-brightgreen.svg)](http://swift.org)
[![Vapor Version](https://img.shields.io/badge/Vapor-3-30B6FC.svg)](http://vapor.codes)
[![Circle CI](https://circleci.com/gh/nodes-vapor/gatekeeper/tree/master.svg?style=shield)](https://circleci.com/gh/nodes-vapor/gatekeeper)
[![codebeat badge](https://codebeat.co/badges/35c7b0bb-1662-44ae-b953-ab1d4aaf231f)](https://codebeat.co/projects/github-com-nodes-vapor-gatekeeper-master)
[![codecov](https://codecov.io/gh/nodes-vapor/gatekeeper/branch/master/graph/badge.svg)](https://codecov.io/gh/nodes-vapor/gatekeeper)
[![Readme Score](http://readme-score-api.herokuapp.com/score.svg?url=https://github.com/nodes-vapor/gatekeeper)](http://clayallsopp.github.io/readme-score?url=https://github.com/nodes-vapor/gatekeeper)
[![Swift Version](https://img.shields.io/badge/Swift-5.3-brightgreen.svg)](http://swift.org)
[![Vapor Version](https://img.shields.io/badge/Vapor-4-30B6FC.svg)](http://vapor.codes)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nodes-vapor/gatekeeper/master/LICENSE)

Gatekeeper is a middleware that restricts the number of requests from clients, based on their IP address.
It works by adding the clients IP address 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.
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**

Expand All @@ -18,15 +14,15 @@ It works by adding the clients IP address to the cache and count how many reques
Update your `Package.swift` dependencies:

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

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

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

Expand All @@ -40,15 +36,8 @@ import Gatekeeper

// [...]

// Register providers first
try services.register(
GatekeeperProvider(
config: GatekeeperConfig(maxRequests: 10, per: .second),
cacheFactory: { container -> KeyedCache in
return try container.make()
}
)
)
app.caches.use(.memory)
app.gatekeeper.config = .init(maxRequests: 10, per: .second)
```

### Add to routes
Expand All @@ -58,7 +47,7 @@ You can add the `GatekeeperMiddleware` to specific routes or to all.
**Specific routes**
in routes.swift:
```swift
let protectedRoutes = router.grouped(GatekeeperMiddleware.self)
let protectedRoutes = router.grouped(GatekeeperMiddleware())
protectedRoutes.get("protected/hello") { req in
return "Protected Hello, World!"
}
Expand All @@ -68,15 +57,67 @@ protectedRoutes.get("protected/hello") { req in
in configure.swift:
```swift
// Register middleware
var middlewares = MiddlewareConfig() // Create _empty_ middleware config
middlewares.use(GatekeeperMiddleware.self)
services.register(middlewares)
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`:
```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.
```swift
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)
}
}
```

```swift
extension Application.Gatekeeper.KeyMakers.Provider {
public static var userID: Self {
.init { app in
app.gatekeeper.keyMakers.use { _ in UserIDKeyMaker() }
}
}
}
```
**configure.swift:**
```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**:
```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](https://www.nodesagency.com).
madsodgaard marked this conversation as resolved.
Show resolved Hide resolved
The package owner for this project is [Christian](https://github.com/cweinberger).
Special thanks goes to [madsodgaard](https://github.com/madsodgaard) for his work on the Vapor 4 version!

## License 📄

Expand Down
Loading