Module: Backstage::Authentication

Defined in:
lib/authentication.rb

Instance Method Summary collapse

Instance Method Details

#authObject



23
24
25
# File 'lib/authentication.rb', line 23

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

#authenticate(username, password) ⇒ Object



44
45
46
47
48
# File 'lib/authentication.rb', line 44

def authenticate(username, password)
  return false if username.nil? || password.nil?
  authenticator = TorqueBox::Authentication['backstage']
  authenticator.authenticate(username, password)
end

#authenticated?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/authentication.rb', line 40

def authenticated?
  !request.env['REMOTE_USER'].nil?
end

#bad_request!Object



32
33
34
# File 'lib/authentication.rb', line 32

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

#login_pathObject



36
37
38
# File 'lib/authentication.rb', line 36

def 
  "#{request.script_name}/login"
end

#require_authenticationObject



54
55
56
57
58
59
60
61
# File 'lib/authentication.rb', line 54

def require_authentication
  return if request.env['SKIP_AUTH']
  return if authenticated?
  unauthorized! unless auth.provided?
  bad_request! unless auth.basic?
  unauthorized! unless authenticate(*auth.credentials)
  request.env['REMOTE_USER'] = auth.username
end

#skip_authenticationObject



50
51
52
# File 'lib/authentication.rb', line 50

def skip_authentication
  request.env['SKIP_AUTH'] = true
end

#unauthorized!(realm = request.host) ⇒ Object



27
28
29
30
# File 'lib/authentication.rb', line 27

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