8000 Example basic authentication · Issue #112 · wookay/Bukdu.jl · GitHub
[go: up one dir, main page]

Skip to content

Example basic authentication #112

Open

Description

@rbontekoe

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.

Activity

  1. wookay commented on Feb 3, 2021

    @wookay
    Owner

    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")
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0