Module: ProxES::Helpers::Authentication

Included in:
Security
Defined in:
lib/proxes/helpers/authentication.rb

Instance Method Summary collapse

Instance Method Details

#authenticateObject



16
17
18
# File 'lib/proxes/helpers/authentication.rb', line 16

def authenticate
  authenticated?
end

#authenticate!Object

Raises:



24
25
26
27
# File 'lib/proxes/helpers/authentication.rb', line 24

def authenticate!
  raise NotAuthenticated unless current_user
  true
end

#authenticated?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/proxes/helpers/authentication.rb', line 20

def authenticated?
  current_user.nil?
end

#check_basicObject

Raises:



33
34
35
36
37
38
39
40
41
42
# File 'lib/proxes/helpers/authentication.rb', line 33

def check_basic
  auth = Rack::Auth::Basic::Request.new(env)
  return unless auth.provided?
  return unless auth.basic?

  identity = ::ProxES::Identity.find(username: auth.credentials[0])
  identity = ::ProxES::Identity.find(username: URI.unescape(auth.credentials[0])) unless identity
  raise NotAuthenticated unless identity
  self.current_user = identity.user if identity.authenticate(auth.credentials[1])
end

#current_userObject



6
7
8
9
10
# File 'lib/proxes/helpers/authentication.rb', line 6

def current_user
  return nil unless env['rack.session'] && env['rack.session']['user_id']
  @users ||= Hash.new { |h, k| h[k] = User[k] }
  @users[env['rack.session']['user_id']]
end

#current_user=(user) ⇒ Object



12
13
14
# File 'lib/proxes/helpers/authentication.rb', line 12

def current_user=(user)
  env['rack.session']['user_id'] = user.id
end

#logoutObject



29
30
31
# File 'lib/proxes/helpers/authentication.rb', line 29

def logout
  env['rack.session'].delete('user_id')
end