Module: AuthlogicConnect::Common::Variables

Includes:
State
Defined in:
lib/authlogic_connect/common/variables.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from State

#auth_controller?, #auth_params?, #auth_session?, #is_auth_session?, #start_authentication?, #validate_password_with_oauth?, #validate_password_with_openid?

Instance Attribute Details

#processing_authenticationObject (readonly)

Returns the value of attribute processing_authentication.



4
5
6
# File 'lib/authlogic_connect/common/variables.rb', line 4

def processing_authentication
  @processing_authentication
end

Instance Method Details

#add_session_key(key, value) ⇒ Object



63
64
65
# File 'lib/authlogic_connect/common/variables.rb', line 63

def add_session_key(key, value)
  
end

#auth_callback_url(options = {}) ⇒ Object



32
33
34
# File 'lib/authlogic_connect/common/variables.rb', line 32

def auth_callback_url(options = {})
  auth_controller.url_for({:controller => auth_controller.controller_name, :action => auth_controller.action_name}.merge(options))
end

#auth_classObject



6
7
8
# File 'lib/authlogic_connect/common/variables.rb', line 6

def auth_class
  is_auth_session? ? self.class : session_class
end

#auth_controllerObject



10
11
12
# File 'lib/authlogic_connect/common/variables.rb', line 10

def auth_controller
  is_auth_session? ? controller : session_class.controller
end

#auth_paramsObject



14
15
16
17
18
19
20
21
# File 'lib/authlogic_connect/common/variables.rb', line 14

def auth_params
  return nil unless auth_controller?
  auth_controller.params.symbolize_keys!
  auth_controller.params.keys.each do |key|
    auth_controller.params[key.to_s] = auth_controller.params.delete(key) if key.to_s =~ /^OpenID/
  end
  auth_controller.params
end

#auth_sessionObject



23
24
25
26
27
28
29
30
# File 'lib/authlogic_connect/common/variables.rb', line 23

def auth_session
  return nil unless auth_controller?
  auth_controller.session.symbolize_keys!
  auth_controller.session.keys.each do |key|
    auth_controller.session[key.to_s] = auth_controller.session.delete(key) if key.to_s =~ /^OpenID/
  end
  auth_controller.session
end

#auth_typeObject

if we’ve said it’s a “user” (registration), or a “session” (login)



37
38
39
# File 'lib/authlogic_connect/common/variables.rb', line 37

def auth_type
  from_session_or_params(:authentication_type)
end

#authenticate_via_protocol(block_given = false, options = {}, &block) ⇒ Object

wraps the call to “save” (in yield). reason being, we need to somehow not allow oauth/openid validations to run when we don’t have a block. We can’t know that using class methods, so we create this property “processing_authentication”, which is used in the validation method. it’s value is set to “block_given”, which is the value of block_given?



77
78
79
80
81
82
# File 'lib/authlogic_connect/common/variables.rb', line 77

def authenticate_via_protocol(block_given = false, options = {}, &block)
  @processing_authentication = auth_controller? && block_given
  saved = yield start_authentication?
  @processing_authentication = false
  saved
end

#authentication_protocol(with, phase) ⇒ Object

returns boolean



85
86
87
88
89
# File 'lib/authlogic_connect/common/variables.rb', line 85

def authentication_protocol(with, phase)
  returning(send("#{phase.to_s}_#{with.to_s}?")) do |ready|
    send("#{phase.to_s}_#{with.to_s}") if ready
  end if send("using_#{with.to_s}?")
end

#auto_register?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/authlogic_connect/common/variables.rb', line 133

def auto_register?
  true
end

#cleanup_authentication_session(options = {}, &block) ⇒ Object

it only reaches this point once it has returned, or you have manually skipped the redirect and save was called directly.



93
94
95
96
97
98
99
# File 'lib/authlogic_connect/common/variables.rb', line 93

def cleanup_authentication_session(options = {}, &block)
  unless (options.has_key?(:keep_session) && options[:keep_session])
    %w(oauth openid).each do |type|
      send("cleanup_#{type.to_s}_session")
    end
  end
end

#correct_request_class?Boolean

because user and session are so closely tied together, I am still uncertain as to how they are saved. So this makes sure if we are logging in, it must be saving the session, otherwise the user.

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File 'lib/authlogic_connect/common/variables.rb', line 53

def correct_request_class?
  return false unless auth_params?
  
  if is_auth_session?
    auth_type.to_s == "session"
  else
    auth_type.to_s == "user"
  end
end

#from_session_or_params(attribute) ⇒ Object

auth_params and auth_session attributes are all String!



42
43
44
45
46
47
48
# File 'lib/authlogic_connect/common/variables.rb', line 42

def from_session_or_params(attribute)
  return nil unless auth_controller?
  key = attribute.is_a?(Symbol) ? attribute : attribute.to_sym
  result = auth_params[key] if (auth_params && auth_params[key])
  result = auth_session[key] if (result.nil? || result.blank?)
  result
end

#log(*methods) ⇒ Object



101
102
103
104
105
# File 'lib/authlogic_connect/common/variables.rb', line 101

def log(*methods)
  methods.each do |method|
    puts "#{method.to_s}: #{send(method).inspect}"
  end
end

#log_stateObject



107
108
109
110
111
112
113
114
# File 'lib/authlogic_connect/common/variables.rb', line 107

def log_state
  log(:correct_request_class?)
  log(:using_oauth?, :start_oauth?, :complete_oauth?)
  log(:oauth_request?, :oauth_response?, :stored_oauth_token_and_secret?)
  log(:using_openid?, :start_openid?, :complete_openid?, :openid_request?, :openid_response?)
  log(:authenticating_with_openid?)
  log(:stored_oauth_token_and_secret)
end

#optimized_session_key(key) ⇒ Object

because we may need to store 6+ session variables, all with pretty lengthy names, might as well just tinify them. just an idea



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/authlogic_connect/common/variables.rb', line 119

def optimized_session_key(key)
  @optimized_session_keys ||= {
    :auth_request_class         => :authcl,
    :authentication_method      => :authme,
    :authentication_type        => :authty,
    :oauth_provider             => :authpr,
    :auth_callback_method       => :authcb,
    :oauth_request_token        => :authtk,
    :oauth_request_token_secret => :authsc,
    :auth_attributes            => :authat
  }
  @optimized_session_keys[key]
end

#remove_session_key(key) ⇒ Object



67
68
69
70
# File 'lib/authlogic_connect/common/variables.rb', line 67

def remove_session_key(key)
  keys = key.is_a?(Symbol) ? [key, key.to_s] : [key, key.to_sym]
  keys.each {|k| auth_session.delete(k)}
end