Class: Stratagem::Crawler::Authentication::Base

Inherits:
Object
  • Object
show all
Includes:
TraceUtils
Defined in:
lib/stratagem/crawler/authentication/base.rb,
lib/stratagem/crawler/authentication/automated.rb

Direct Known Subclasses

Configured

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

  (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



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

#find_login_formObject



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



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 (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 (user)
  (user).submit {|action,params| 
    post(action, params)
  }
end

#logoutObject



86
87
88
# File 'lib/stratagem/crawler/authentication/base.rb', line 86

def logout
  reset!
end

#populate_login_form(user) ⇒ Object



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



17
18
19
# File 'lib/stratagem/crawler/authentication/base.rb', line 17

def reset_authentication
  @authentication_data = nil
end

#user_modelsObject



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 = 
  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



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