Class: Sinatra::Helpers::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/basic_auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, request) ⇒ Authentication



25
26
27
28
# File 'lib/sinatra/basic_auth.rb', line 25

def initialize( app, request )
  self.app = app
  self.request = request
end

Instance Attribute Details

#appObject

Returns the value of attribute app



24
25
26
# File 'lib/sinatra/basic_auth.rb', line 24

def app
  @app
end

#requestObject

Returns the value of attribute request



24
25
26
# File 'lib/sinatra/basic_auth.rb', line 24

def request
  @request
end

Instance Method Details

#authObject



30
31
32
# File 'lib/sinatra/basic_auth.rb', line 30

def auth
  @basic_auth ||= Rack::Auth::Basic::Request.new( request.env )
end

#authorize(username, password) ⇒ Object



47
48
49
50
# File 'lib/sinatra/basic_auth.rb', line 47

def authorize( username, password )
  settings.basic_auth_username == username &&
  settings.basic_auth_password == password
end

#authorized?Boolean



43
44
45
# File 'lib/sinatra/basic_auth.rb', line 43

def authorized?
  request.env['REMOTE_USER']
end

#bad_request!Object



39
40
41
# File 'lib/sinatra/basic_auth.rb', line 39

def bad_request!
  throw :halt, [ 400, 'Bad Request' ]
end

#unauthorized!Object



34
35
36
37
# File 'lib/sinatra/basic_auth.rb', line 34

def unauthorized!
  app.headers 'WWW-Authenticate' => %(Basic realm="#{settings.basic_auth_realm}")
  throw :halt, [ 401, 'Authorization Required' ]
end