nbibler (owner)

Revisions

gist: 230043 Download_button fork
public
Public Clone URL: git://gist.github.com/230043.git
Embed All Files: show embed
basic.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Warden::Strategies.add(:site_basic) do
  
  def valid?
    authorization.present?
  end
  
  def authenticate!
    if authorization.present?
      site = Site.find_by_identifier_and_token(*user_name_and_password)
      site ? success!(site) : fail!("Unauthorized")
    else
      pass
    end
  end
  
  
  protected
  
  
  def authorization
    request.env['HTTP_AUTHORIZATION'] ||
    request.env['X-HTTP_AUTHORIZATION'] ||
    request.env['X_HTTP_AUTHORIZATION'] ||
    request.env['REDIRECT_X_HTTP_AUTHORIZATION']
  end
  
  def user_name_and_password
    decode_credentials.split(/:/, 2)
  end
  
  def decode_credentials
    Base64.decode64(authorization.split(' ', 2).last || '')
  end
  
end