Adds some basic auth in to your lucky app using a pipe instead of HTTP Handler
Requires Lucky v0.17 or later
- Add the dependency to your
shard.yml
:
dependencies:
lucky-basic-auth:
github: jwoertink/lucky-basic-auth
- Run
shards install
Require the shard
# in src/shards.cr
require "lucky-basic-auth"
Include it in your main BrowserAction
# in src/actions/browser_action.cr
abstract class BrowserAction < Lucky::Action
include Lucky::ProtectFromForgery
# include it
include Lucky::BasicAuthPipe
# use it
basic_auth
end
Or, you can also make a subclass to handle auths
# src/actions/authorized_action.cr
abstract class AuthorizedAction < BrowserAction
include Lucky::BasicAuthPipe
basic_auth
end
class MyAction < AuthorizedAction
get "/admin/my_action" do
plain_text "I'm secure!"
end
end
Call the basic_auth
to enable.
# in src/actions/whatever/your_action.cr
class Whatever::YourAction < BrowserAction
basic_auth
#...
end
This method requires 2 ENV
variables to exist. This will check for the username and password to match these variables.
ENV["AUTH_USERNAME"]
ENV["AUTH_PASSWORD"]
If you need more control over how the values are checked (e.g. lookup from a database, or check role authorization), you can pass a proc.
# in src/actions/whatever/your_action.cr
class Whatever::YourAction < BrowserAction
basic_auth ->(user, pass) {
user == MY_USERNAME_THING && pass == MY_PASSWORD_CHECKER
}
#...
end
This gives you the option to set different user and pass per action if you want.
write specs
- Fork it (https://github.com/jwoertink/lucky-basic-auth/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Jeremy Woertink - creator and maintainer