Swift package for adding API Key requirement to vapor backends.

Overview

APIKeyMiddleware



Minimum Supported Vapor Version is 4

Swift package for adding API Key requirement to vapor backends.

Features

  • Supports an array of keys
  • Supports overriding thrown errors
  • 100% Test Coverage

Usage

Requests are expected to have the header api-key.

Simple

public func configure(_ app: Application) throws {
    app.middleware.use(APIKeyMiddleware(keys: ["123"]))
}

See Vapor Docs if wanting to add to individual routes.

Override Errors

The following errors are used by default:

  • Missing API key: Abort(.badRequest, reason: "Missing api-key.")
  • Unauthorized API Key: Abort(.unauthorized, reason: "Unauthorized api-key.")

To override these errors, assign a delegate to the APIKeyMiddleware. Returning nil uses defaults.

class Delegate: APIKeyMiddlewareDelegate {

    static let shared = Delegate()

    init() {}

    func errorForMissingKey(in middleware: APIKeyMiddleware) -> Error? {
        Abort(.imATeapot, reason: "Missing api-key was found by teapot.")
    }

    func errorForUnauthorizedKey(in middleware: APIKeyMiddleware) -> Error? {
        Abort(.imATeapot, reason: "Unauthorized api-key was found by teapot.")
    }
}

public func configure(_ app: Application) throws {
    let middleware = APIKeyMiddleware(keys: ["123"], delegate: Delegate.shared)
    app.middleware.use(middleware)
}

Installation

SPM

Add the following to your project:

https://github.com/ptrkstr/APIKeyMiddleware

Possible Future Features

Raise an issue if you need these

  • Allow overriding API-Key header name
You might also like...
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)

Perfect: Server-Side Swift 简体中文 Perfect: Server-Side Swift Perfect is a complete and powerful toolbox, framework, and application server for Linux, iO

Quickstart-ios-swift-grpc - Quickstart for integrating Approov with iOS apps in Swift that make API requests you wish to protect using GRPC
Access Xcode Server API with native Swift objects.

Xcode Server SDK Use Xcode Server's API with native Swift objects. First brought to you in Buildasaur, now in an independent project. This is an unoff

A WIP Swift wrapper for Discord's Bot API.

Quickcord Quickcord is a WIP Swift wrapper for Discord's Bot API. Features (As of right now) Connecting the the gateway and identifying. Literally eve

An unofficial supported Swift client library for accessing News API.

An unofficial supported Swift client library for accessing News API.

A modern Swift wrapper for Instagram Private API.
A modern Swift wrapper for Instagram Private API.

Swiftagram is a wrapper for Instagram Private API, written entirely in (modern) Swift. Instagram's official APIs, both the Instagram Basic Display API

A lightweight generic networking API written purely in Swift

SwiftyNetworking SwiftyNetworking library is a generic networking library writte

A web API client in Swift built using Async/Await

Get A modern web API client in Swift built using Async/Await and Actors. let cli

Elegant API Abstraction for Swift

Endpoint (Deprecated) ⚠️ This project has been deprecated. Consider using Moya and MoyaSugar instead. 🚀 Elegant API Abstraction for Swift. At a Glanc

Comments
  • Feature Request: Store API Key in DB and validate in middleware

    Feature Request: Store API Key in DB and validate in middleware

    It would be great if we can store an API Key in the db (with a migration using bcrypt) and then check if the api-key header is valid against multiple api-keys stored in the DB. Maybe do something like what Fluent has in ModelAuthenticatable or in ModelTokenAuthenticatable.

    Having multiple api-key allows controlling which client can access the data.

    enhancement 
    opened by Axort 3
Owner
null
APIProvider - API Provider Package for easier API management inspired by abstraction

APIProvider Using APIProvider you can easily communicate with all API endpoints

null 1 Apr 21, 2022
This package is meant to make http request of an easy way inspiren in the architecture of Moya package

NetworkAgent This package is meant to make http request of an easy way inspiren in the architecture of Moya package. This package is 100% free of depe

Angel Rada 19 Sep 8, 2022
UnsplashProvider - A package that can use the Unsplash API. It was developed as a SwiftUI

UnsplashProvider It is a package that can use the Unsplash API. It was developed

jasu 12 Dec 20, 2022
Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux.

BlueSocket Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux. Prerequisites Swift Swift Open Source swift-5.1

Kitura 1.3k Dec 26, 2022
A Swift Package that allows iOS apps to communicate with AltServer.

AltKit allows apps to communicate with AltServers on the same WiFi network and enable features such as JIT compilation.

Riley Testut 45 Sep 8, 2022
This is my attempt to make this repo available on swift package manager

Parse LiveQuery Client for iOS/OSX PFQuery is one of the key concepts for Parse. It allows you to retrieve PFObjects by specifying some conditions, ma

Benji 0 Nov 3, 2021
Create xcframework zip archive for Swift binary package.

xczip xczip - create xcframework zip archive for swift binary package. Creates an xcframework archive with a stable checksum that does not change when

Dmitriy Borovikov 0 Jan 8, 2022
NotionDrive - A swift package that can upload files to Notion.so or download files from Notion.so

NotionDrive NotionDrive is a swift package that can upload files to Notion.so or

Underthestars-zhy 4 Apr 9, 2022
UDPReader : a simple package for reading udp packets

UDPReader is a simple package for reading udp packets

leroy 2 Aug 7, 2022
AsyncWebSocketClient - A package that contains a client behaving as a wrapper for the URLSessionWebSocketTask

(WORK IN PROGRESS) AsyncWebSocketClient This is a package that contains a client

null 3 Dec 30, 2022