Class: Stratagem::Crawler::Authentication::Base
- Inherits:
-
Object
- Object
- Stratagem::Crawler::Authentication::Base
show all
- Includes:
- TraceUtils
- Defined in:
- lib/stratagem/crawler/authentication/base.rb,
lib/stratagem/crawler/authentication/automated.rb
Instance Method Summary
collapse
Methods included from TraceUtils
#model_invocations_for_request
Instance Method Details
#authenticate(user, recursion_count = 0) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/stratagem/crawler/authentication/base.rb', line 29
def authenticate(user, recursion_count=0)
reset_authentication
login(user)
route = application_model.routes.recognize(request.path, :post)
redirected_to = nil
page = site_model.add(route, controller, request, response) {|redirect_url| redirected_to = redirect_url }
authentication.response_page = page
begin
if (request.url == (redirected_to || '')) || (![200,302].include?(response.code.to_i))
authentication.success = false
else
authentication.success = authentication.response_page.login_form.nil?
end
rescue
Stratagem.logger.error($!)
end
puts "authenticated? #{authentication.success}"
if (response && authentication.success)
authentication.ssl = request.ssl?
authentication.authenticated_with = user
yield
logout
else
puts response.body
false
end
end
|
#authentication ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/stratagem/crawler/authentication/base.rb', line 21
def authentication
unless @authentication_data
@authentication_data = AuthenticationData.new()
site_model.authentication = @authentication_data
end
@authentication_data
end
|
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/stratagem/crawler/authentication/base.rb', line 61
def find_login_form
puts "finding login form"
if authentication.login_page.nil?
puts "locating login page"
puts "testing #{site_models.first.pages.size} pages"
possibilities = site_models.first.pages.select {|page| page.login_form != nil }
possibilities.sort! {|a,b| b.inbound_edges(:redirect).size <=> a.inbound_edges(:redirect).size }
if (possibilities.first.inbound_edges(:redirect).size > 0)
authentication.login_page = possibilities.first
return authentication.login_page
else
page = possibilities.sort {|a,b| a.login_form.inputs.size <=> b.login_form.inputs.size }.first
if (page)
authentication.login_page = page
return authentication.login_page
end
end
else
return authentication.login_page
end
nil
end
|
#guess_login_model(attr_names) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/stratagem/crawler/authentication/automated.rb', line 21
def guess_login_model(attr_names)
selections = application_model.models.select {|model|
puts "#{model.klass.name} - #{model.model_attributes.keys.inspect}"
intersect = (model.model_attributes.keys & attr_names)
intersect.size > 0
}.sort {|a,b|
a_intersect = (a.model_attributes.keys & attr_names)
b_intersect = (b.model_attributes.keys & attr_names)
b_intersect.size <=> a_intersect.size
}
explicit_model = application_model.models.find {|model| model.klass.name == 'User' }
selections.unshift explicit_model if explicit_model
puts "selecting model #{selections.first.klass.name} for authentication" if (selections.size > 0)
selections.first
end
|
#login(user) ⇒ Object
90
91
92
93
94
|
# File 'lib/stratagem/crawler/authentication/base.rb', line 90
def login(user)
populate_login_form(user).submit {|action,params|
post(action, params)
}
end
|
86
87
88
|
# File 'lib/stratagem/crawler/authentication/base.rb', line 86
def logout
reset!
end
|
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/stratagem/crawler/authentication/base.rb', line 96
def populate_login_form(user)
page = find_login_form
page.reload {|url| get url; [request,response] }
form = page.login_form
form.inputs.each do |input|
attribute_name = input.guess_attribute.to_sym
attribute_value = user.stratagem.read_mock_attribute(attribute_name) || input.value
if (attribute_value.nil? || attribute_value == '')
attribute_name = input.guess_alternate_attribute.to_sym
attribute_value = user.stratagem.read_mock_attribute(attribute_name) || input.value
end
if (attribute_value.nil? || attribute_value == '')
if (attribute_name.to_s =~ /confirm/)
possible_match = attribute_name.to_s.split('_').select {|a| a !~ /confirm/ }.join('_')
if user.stratagem.mock_attributes.keys.include?(possible_match)
attribute_value = user.stratagem.read_mock_attribute(possible_match) || input.value
end
end
end
if (input.kind_of? Stratagem::Crawler::Toggle)
input.check
elsif (user.stratagem.mock_attributes.keys.include?(attribute_name))
input.value = user.stratagem.read_mock_attribute(attribute_name) unless input.hidden?
elsif (attribute_name.to_s == 'authenticity_token')
puts input.value
else
puts user.stratagem.mock_attributes.inspect
puts "ERROR: Cannot find attribute #{attribute_name} in model #{user.class.name}"
end
end
form
end
|
#reset_authentication ⇒ Object
17
18
19
|
# File 'lib/stratagem/crawler/authentication/base.rb', line 17
def reset_authentication
@authentication_data = nil
end
|
#user_models ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/stratagem/crawler/authentication/automated.rb', line 9
def user_models
model = nil
page = find_login_form
if (page)
form = page.login_form
attr_names = form.inputs.map {|input| input.guess_attribute.to_sym }
model = guess_login_model(attr_names)
end
log "Authenticating with model #{model.klass.name}"
[model]
end
|
9
10
11
12
13
14
15
|
# File 'lib/stratagem/crawler/authentication/base.rb', line 9
def users
users = []
user_models.each do |user_model|
users += aquifer.instances_of(user_model.klass).select {|user| user.stratagem.mock_attributes.size > 0 }
end
users
end
|