Bukdu.jl
Bukdu.jl copied to clipboard
Example basic authentication
A basic authentication example would be great. I saw the example auth.jl but like to know how I can return a 401 Unauthorized response to force the user to login.
Here's example like AuthHandler from HTTP.jl docs:
https://juliaweb.github.io/HTTP.jl/stable/public_interface/#HTTP.Handlers
using Bukdu
using HTTP
struct ExampleController <: ApplicationController
conn::Conn
end
const ANIMALS = Dict("uuid" => "ok")
function authenticate(c::ExampleController)
req = c.conn.request
if HTTP.hasheader(req, "Animal-UUID")
uuid = HTTP.header(req, "Animal-UUID")
if haskey(ANIMALS, uuid)
return render(Text, "ok")
end
end
req.response.status = 401 # 401 Unauthorized
return render(Text, "Authentication failed")
end
routes() do
post("/auth", ExampleController, authenticate)
end
Bukdu.start(8190, host="127.0.0.1")
test requests:
using HTTP
HTTP.post("http://127.0.0.1:8190/auth", ["Animal-UUID"=>"uuid"])
HTTP.post("http://127.0.0.1:8190/auth", ["Animal-UUID"=>"wrong"])
HTTP.post("http://127.0.0.1:8190/auth")