No commit activity in last 3 years
No release in over 3 years
Middleware for handling Bearer type Authentication
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
~> 0.9
~> 0.6
~> 12.3
~> 3.0
 Project Readme

rack/authentication_bearer

  • Build
  • Dependencies
  • Version

A middleware for parsing Bearer tokens from the HTTP request header Authentication (or Authorization if you're old school).

Using

This gem just provides you an easy way to take the header value and turn it into something meaningful for your rack-based application. This is largely helpful for things like PEST or JWT (though we think it's a bad idea). So lets talk about what we can do:

First, if you don't want to care at all just don't provide a proc:

rack.use(Rack::AuthenticationBearer)

The middleware will do nothing! This is valuable for certain environments. Okay, so here is an example of simple token decoding:

rack.use(
  Rack::AuthenticationBearer,
  -> (bearer) do
    Base64.urlsafe_decode64(value)
  end
)

This will take a header like Authentication: Bearer VGhpcyBJcyBBIFNlY3JldA== and returns "This Is A Secret", which you will have access to in your rack env rack.authentication. Now a little more complex JWT version:

rack.use(
  Rack::AuthenticationBearer,
  -> (bearer) do
    begin
      JWT.decode(token, SOMESECRET).first
    rescue JWT::DecodeError
      {}
    end
  end
)

Okay, so what happens if the client is sending a malformed authentication value? Well, we don't freak out, instead we give you the tools to freak out:

rack.call({"HTTP_AUTHORIZATION" => "Bearer "})
# {
#   "HTTP_AUTHORIZATION" => "Bearer ",
#   "rack.authentication" => Rack::AuthenticationBearer::InvalidBearerTokenError
# }

Okay, now what if they don't have a value at all?

rack.call({})
# {
#   "rack.authentication" => Rack::AuthenticationBearer::MissingBearerTokenError
# }

These exception classes have a status property that you can bubble up to the server.

Installing

Add this line to your application's Gemfile:

gem "rack-authentication_bearer", "~> 2.0"

And then execute:

$ bundle

Or install it yourself with:

$ gem install rack-authentication_bearer

Contributing

  1. Fork it
  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 new Pull Request

Licensing

Copyright (c) 2018 Kurtis Rainbolt-Greene

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.