class Rack::SimpleAuth

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack-simple-auth.rb, line 5
def initialize(app, options = {})
  @app = app
  @key = options[:key]
  @secret = options[:secret]
  @login_url = options[:login_url]
  @authenticated_with = options[:authenticated_with] || Proc.new { |value| true }
  @except = options[:except] || Proc.new { false }
end

Public Instance Methods

authenticated?(cookies) click to toggle source
# File lib/rack-simple-auth.rb, line 23
def authenticated?(cookies)
  if data = cookies[@key]
    packed_data, digest = data.split('--')
    hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, @secret, packed_data)
    begin
      # false if tampering going on
      digest == hmac && @authenticated_with.call(packed_data.unpack("m*").first)
    rescue
      false
    end
  else
    false
  end
end
call(env) click to toggle source
# File lib/rack-simple-auth.rb, line 14
def call(env)
  request = Request.new(env)
  if authenticated?(request.cookies) || @except.call(request)
    @app.call(env)
  else
    [302, {'Content-Type' => 'text/plain', 'Location' => "#{@login_url}?return_to=#{request.url}"}, ['You must be logged in to see this.']]
  end
end