Skip to content

kemalcr/kemal-basic-auth

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 

kemal-basic-auth

Add basic auth to your Kemal application.

Installation

Add this to your application's shard.yml:

dependencies:
  kemal-basic-auth:
    github: kemalcr/kemal-basic-auth

Usage

Basic example

kemal-basic-auth adds authentication to all routes by default.

require "kemal-basic-auth"

basic_auth "username", "password"
# basic_auth {"username1" => "password1", "username2" => "password2"}

Authentication for specific routes

Kemal::BasicAuth::Handler inherits from Kemal::Handler and it is therefore easy to create a custom handler that adds authentication to specific routes instead of all routes.

class CustomAuthHandler < Kemal::BasicAuth::Handler
  only ["/dashboard", "/admin"]

  def call(context)
    return call_next(context) unless only_match?(context)
    super
  end
end

Now just set Kemal.config.auth_handler = CustomAuthHandler and you are good to go.

kemal_authorized_username

HTTP::Server::Context#kemal_authorized_username? is set when the user is authorized.

basic_auth {"guest" => "123", "admin" => "xyz"}

get "/" do |env|
  "Hi, %s!" % env.kemal_authorized_username?
end

Contributing

  1. Fork it ( https://github.com/kemalcr/kemal-basic-auth/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • sdogruyol Serdar Dogruyol - creator, maintainer

About

Basic auth for your Kemal application

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •