Module: Stratagem::Crawler::Authentication

Includes:
TraceUtils
Included in:
Session
Defined in:
lib/stratagem/crawler/authentication.rb,
lib/stratagem/crawler/authentication/base.rb,
lib/stratagem/crawler/authentication/automated.rb,
lib/stratagem/crawler/authentication/configured.rb

Defined Under Namespace

Classes: AuthenticationData, Base, Configured

Instance Method Summary collapse

Methods included from TraceUtils

#model_invocations_for_request

Instance Method Details

#authenticate(user, recursion_count = 0) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/stratagem/crawler/authentication.rb', line 45

def authenticate(user, recursion_count=0)
  reset_authentication

  (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..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

#authenticationObject



37
38
39
40
41
42
43
# File 'lib/stratagem/crawler/authentication.rb', line 37

def authentication
  unless @authentication_data
    @authentication_data = AuthenticationData.new()
    site_model.authentication = @authentication_data
  end
  @authentication_data
end

#find_login_formObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/stratagem/crawler/authentication.rb', line 77

def 
  puts "finding login form"
  if authentication..nil?
    puts "locating login page"
    puts "testing #{site_models.first.pages.size} pages"
    possibilities = site_models.first.pages.select {|page| page. != nil }
    possibilities.sort! {|a,b| b.inbound_edges(:redirect).size <=> a.inbound_edges(:redirect).size }

    # if the first page has one or more redirects to it then use it; otherwise select with page with the fewest inputs
    if (possibilities.first.inbound_edges(:redirect).size > 0)
      authentication. = possibilities.first
      return authentication.
    else
      page = possibilities.sort {|a,b| a..inputs.size <=> b..inputs.size }.first
      if (page)
        authentication. = page
        return authentication.
      end
    end
  else
    return authentication.
  end
  nil
end

#guess_login_model(attr_names) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/stratagem/crawler/authentication.rb', line 113

def (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



106
107
108
109
110
111
# File 'lib/stratagem/crawler/authentication.rb', line 106

def (user)
  (user).submit {|action,params| 
    p params
    post(action, params)
  }
end

#logoutObject



102
103
104
# File 'lib/stratagem/crawler/authentication.rb', line 102

def logout
  reset!
end

#populate_login_form(user) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/stratagem/crawler/authentication.rb', line 132

def (user)
  # set up the form
  page = 
  page.reload {|url| get url; [request,response] }
  form = page.

  # map the input values
  form.inputs.each do |input|
    # try the expected
    attribute_name = input.guess_attribute.to_sym
    attribute_value = user.stratagem.read_mock_attribute(attribute_name) || input.value

    # try again
    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 it still failed is it a confirmation value? if so, try the original
    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_authenticationObject



33
34
35
# File 'lib/stratagem/crawler/authentication.rb', line 33

def reset_authentication
  @authentication_data = nil
end

#user_modelObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/stratagem/crawler/authentication.rb', line 10

def user_model
  model = nil
  page = 
  if (page)
    form = page.
    attr_names = form.inputs.map {|input| input.guess_attribute.to_sym }
    model = (attr_names)
  end
  log "Authenticating with model #{model.klass.name}"
  model
end

#usersObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/stratagem/crawler/authentication.rb', line 22

def users
  users = []
  model = user_model()
  if (model)
    users = aquifer.instances_of(model.klass).select {|user| user.stratagem.mock_attributes.size > 0 }
  else
    log "ERROR: Unable to determine authentication model and / or form"
  end
  users
end