Class: Egalite::Auth::Basic

Inherits:
Object show all
Defined in:
lib/egalite/auth/basic.rb

Class Method Summary collapse

Class Method Details

.authorize(req, realm) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/egalite/auth/basic.rb', line 5

def self.authorize(req,realm)
  auth = req.authorization
  return unauthorized(realm) if auth.blank?
  (method,credentials) = auth.split(' ', 2)
  return bad_request if method.downcase != "basic"
  (username,password) = credentials.unpack("m*").first.split(/:/,2)
  return unauthorized(realm) unless yield(username,password)
  true
end

.bad_requestObject



22
23
24
25
26
27
28
# File 'lib/egalite/auth/basic.rb', line 22

def self.bad_request
  return [ 400,
    { 'Content-Type' => 'text/plain',
      'Content-Length' => '0' },
    []
  ]
end

.unauthorized(realm) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/egalite/auth/basic.rb', line 14

def self.unauthorized(realm)
  return [ 401,
    { 'Content-Type' => 'text/plain',
      'Content-Length' => '0',
      'WWW-Authenticate' => 'Basic realm="%s"' % realm },
    []
  ]
end