Class: Rack::SimpleAuth
- Inherits:
-
Object
- Object
- Rack::SimpleAuth
- Defined in:
- lib/rack-simple-auth.rb
Instance Method Summary collapse
- #authenticated?(cookies) ⇒ Boolean
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ SimpleAuth
constructor
A new instance of SimpleAuth.
Constructor Details
#initialize(app, options = {}) ⇒ SimpleAuth
Returns a new instance of SimpleAuth.
5 6 7 8 9 10 11 12 |
# File 'lib/rack-simple-auth.rb', line 5 def initialize(app, = {}) @app = app @key = [:key] @secret = [:secret] @login_url = [:login_url] @authenticated_with = [:authenticated_with] || Proc.new { |value| true } @except = [:except] || Proc.new { false } end |
Instance Method Details
#authenticated?(cookies) ⇒ Boolean
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rack-simple-auth.rb', line 23 def authenticated?() if data = [@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) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/rack-simple-auth.rb', line 14 def call(env) request = Request.new(env) if authenticated?(request.) || @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 |