The authentication story for Servant is still in the works. Until that's finalized this gist may be useful for anyone who wants to add HTTP Basic Authentication to their Servant clients.
Try it out.
$ stack ghci servant-client servant-server --resolver=lts-3.14
Run from outside a project, using implicit global project config
Using resolver: lts-3.14 specified on command line
Configuring GHCi with the following packages:
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Ok, modules loaded: none.
Load Main.hs
.
Prelude> :load Main
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, modules loaded: Main.
Let's send a GET
request to https://httpbin.org/basic-auth/alice/pass123 without login credentials.
*Main> runEitherT $ http "" "alice" "pass123"
Left (FailureResponse {responseStatus = Status {statusCode = 401, statusMessage = "UNAUTHORIZED"}, responseContentType = application/octet-stream, responseBody = ""})
Fails as expected. Let's try as the user "alice", but with an empty password.
*Main> runEitherT $ http "alice:" "alice" "pass123"
Left (FailureResponse {responseStatus = Status {statusCode = 401, statusMessage = "UNAUTHORIZED"}, responseContentType = application/octet-stream, responseBody = ""})
Also fails as expected. Finally, let's try to login with "alice:pass123".
*Main> runEitherT $ http "alice:pass123" "alice" "pass123"
Right (Response {authenticated = True, user = "alice"})
Success!