Module: Stratagem::Crawler::Authentication

Includes:
TraceUtils
Included in:
Session
Defined in:
lib/stratagem/crawler/authentication.rb

Instance Method Summary collapse

Methods included from TraceUtils

#model_invocations_for_request

Instance Method Details

#authenticate(user, recursion_count = 0) ⇒ Object



40
41
42
43
44
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
# File 'lib/stratagem/crawler/authentication.rb', line 40

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
    puts $!.message
    puts $!.backtrace
  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



32
33
34
35
36
37
38
# File 'lib/stratagem/crawler/authentication.rb', line 32

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

#find_login_formObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stratagem/crawler/authentication.rb', line 73

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)
      return possibilities.first
    else
      page = possibilities.sort {|a,b| a..inputs.size <=> b..inputs.size }.first
      if (page)
        authentication. = page
        return page
      end
    end
  else
    return authentication.
  end
  nil
end

#guess_login_model(attr_names) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/stratagem/crawler/authentication.rb', line 108

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



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

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

#logoutObject



97
98
99
# File 'lib/stratagem/crawler/authentication.rb', line 97

def logout
  reset!
end

#populate_login_form(user) ⇒ Object



127
128
129
130
131
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
# File 'lib/stratagem/crawler/authentication.rb', line 127

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



28
29
30
# File 'lib/stratagem/crawler/authentication.rb', line 28

def reset_authentication
  @authentication_data = nil
end

#usersObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/stratagem/crawler/authentication.rb', line 10

def users
  page = 
  users = []
  if (page)
    form = page.
    attr_names = form.inputs.map {|input| input.guess_attribute.to_sym }
    model = (attr_names)
    if (model)
      users = aquifer.instances_of(model.klass)
    else
      log "ERROR: Unable to determine user model"
    end
  else
    log "ERROR: Could not find login form"
  end
  users
end