No description, website, or topics provided.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Source
Tests
.gitignore
.swift-version
.travis.yml
LICENSE
Package.swift
README.md

README.md

BasicAuthMiddleware

Swift Zewo Platform License Slack Travis Codebeat

BasicAuthMiddleware provides basic authentication for server and client.

Usage

Server

import BasicAuthMiddleware
import HTTPServer
import Router

let basicAuth = BasicAuthMiddleware { username, password in
	if username == "admin" && password == "password" {
		return .Authenticated
	}

	return .AccessDenied
}

let router = Router(middleware: basicAuth) { route in
	route.get("/") { _ in
		return Response(status: .OK, body: "Authenticated")
	}
}

try Server(responder: router).start()

If you want to pass forward any custom data in the Request storage, you can return a Payload with a key and a value.

let basicAuth = BasicAuthMiddleware { username, password in
	if let user = User.withUsername(username, password: password) {
		return .Payload(key: "user", value: user)
	}

	return .AccessDenied
}

Then you can retrieve the value in the route.

let router = Router(middleware: basicAuth) { route in
	route.get("/") { request in
		let user = request.storage["user"]!
		// Do what you want with the user
		return Response(status: .OK, body: "Authenticated")
	}
}

If you want the browser to show a login prompt after receiving Access Denied, you can set an optional Basic realm for the WWW-Authenticate header.

let basicAuth = BasicAuthMiddleware(realm: "Password Protected Realm") { username, password in
    if username == "admin" && password == "password" {
        return .Authenticated
    }

    return .AccessDenied
}

Client

import BasicAuthMiddleware
import HTTPClient

let basicAuth = BasicAuthMiddleware(
	username: "API_KEY",
	password: "API_SECRET"
)

let client = try Client(host: "your.api.com", port: 80)
let response = try client.get("/", middleware: basicAuth)

Installation

  • Add BasicAuthMiddleware to your Package.swift
import PackageDescription

let package = Package(
	dependencies: [
		.Package(url: "https://github.com/Zewo/BasicAuthMiddleware.git", majorVersion: 0, minor: 5)
	]
)

Support

If you need any help you can join our Slack and go to the #help channel. Or you can create a Github issue in our main repository. When stating your issue be sure to add enough details, specify what module is causing the problem and reproduction steps.

Community

Slack

The entire Zewo code base is licensed under MIT. By contributing to Zewo you are contributing to an open and engaged community of brilliant Swift programmers. Join us on Slack to get to know us!

License

This project is released under the MIT license. See LICENSE for details.