Class: Carpool::Seatbelt

Inherits:
Object
  • Object
show all
Includes:
Mixins::Core
Defined in:
lib/carpool/seatbelt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Core

included

Constructor Details

#initialize(env) ⇒ Seatbelt

Returns a new instance of Seatbelt.



8
9
10
# File 'lib/carpool/seatbelt.rb', line 8

def initialize(env)
  @env = env
end

Instance Attribute Details

#current_passengerObject

Returns the value of attribute current_passenger.



6
7
8
# File 'lib/carpool/seatbelt.rb', line 6

def current_passenger
  @current_passenger
end

#current_userObject

Returns the value of attribute current_user.



6
7
8
# File 'lib/carpool/seatbelt.rb', line 6

def current_user
  @current_user
end

#envObject

Returns the value of attribute env.



6
7
8
# File 'lib/carpool/seatbelt.rb', line 6

def env
  @env
end

#redirect_toObject

Returns the value of attribute redirect_to.



6
7
8
# File 'lib/carpool/seatbelt.rb', line 6

def redirect_to
  @redirect_to
end

Instance Method Details

#auth_request!Object



35
36
37
38
39
# File 'lib/carpool/seatbelt.rb', line 35

def auth_request!
  return if auth_request?
  carpool_cookies['passenger_uri'] = @env['HTTP_REFERER']
  carpool_cookies['requesting_authentication'] = true
end

#auth_request?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/carpool/seatbelt.rb', line 41

def auth_request?
  carpool_cookies['requesting_authentication'] && carpool_cookies['requesting_authentication'] == true
end

#authenticate!Object



16
17
18
# File 'lib/carpool/seatbelt.rb', line 16

def authenticate!
  throw(:carpool, Carpool::Responder.authenticate) unless authentication_exists?
end

#authentication_exists?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/carpool/seatbelt.rb', line 12

def authentication_exists?
  !carpool_passenger_tokens.empty?
end

#authorize!(user = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/carpool/seatbelt.rb', line 20

def authorize!(user = nil)
  unless Carpool.acts_as?(:passenger)
    return false unless auth_request?
    update_authentication!(passenger_for_auth[:secret])
    token   = Carpool::Encryptor.generate_token(user.encrypted_credentials, passenger_for_auth[:secret])
    payload = Carpool::Encryptor.generate_payload(current_passenger, token)        
    throw(:carpool, Carpool::Responder.passenger_redirect(current_passenger, payload))        
  else
    seatbelt = Carpool::Encryptor.process_seatbelt(request.params['seatbelt'])
    throw(:carpool, Carpool::Responder.invalid) and return unless seatbelt[:user].is_a?(Hash)
    @current_user = seatbelt[:user]
    @redirect_to  = seatbelt[:redirect_to]
  end
end

#revoke!Object



49
50
51
# File 'lib/carpool/seatbelt.rb', line 49

def revoke!
  destroy_session!
end

#success!Object



53
54
55
# File 'lib/carpool/seatbelt.rb', line 53

def success!
  throw(:carpool, [303, {"Location" => @redirect_to.to_s}, "Authorized!"])
end