FlyingFox icon indicating copy to clipboard operation
FlyingFox copied to clipboard

Feature Request: Add Support for Basic Auth?

Open blaineam opened this issue 1 year ago • 2 comments

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.

blaineam avatar Sep 16 '24 21:09 blaineam

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)

swhitty avatar Sep 17 '24 04:09 swhitty

Your basic auth branch is working nicely in my app. Had a little bit of trickery with my apps internal web view but was able to find a solution by removing the credential persistence so it re-authenticates every time including right after a credential change and a restart of FlyingFox to immediately start using the new credentials.

Thank you for adding this. Let me know when something like this gets merged back into main.

blaineam avatar Oct 15 '24 14:10 blaineam