swhitty / FlyingFox

Lightweight, HTTP server written in Swift using async/await.
MIT License
450 stars 38 forks source link

Feature Request: Add Support for Basic Auth? #118

Open blaineam opened 1 week ago

blaineam commented 1 week ago

Would it be possible to implement a server configuration option that implements Basic Auth for all http requests? I know it's not really the most secure method but some authentication is better than no authentication for requests made to the FlyingFox HTTP Server. Something as simple as a configurable user name and password that implements the Basic Auth spec would be nice.

swhitty commented 1 week ago

I think that is a nice idea, this really is just another HTTPHandler that acts as a middleware in-between requests and handlers. It may take me a while to workout exactly how to handle this in a generic way but I have pushed a quick branch that shows how you could easily implement it: basic-auth:

var authRoutes = BasicAuthRoutedHTTPHandler(realm: "fox", username: "fish", password: "chips")
authRoutes.appendRoute("/auth/fish") {
    HTTPResponse(statusCode: .ok,
                 headers: [.contentType: "text/plain; charset=UTF-8"],
                 body: "🎣 🍟".data(using: .utf8)!)
}

// all routes under this path require authentication;
await server.appendRoute("/auth/*", to: authRoutes)