ae_easy-login 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +1 -1
- data/Gemfile +1 -1
- data/LICENSE +1 -1
- data/README.md +2 -369
- data/Rakefile +0 -10
- data/ae_easy-login.gemspec +6 -12
- data/lib/ae_easy/login.rb +3 -8
- metadata +26 -75
- data/doc/AeEasy.html +0 -117
- data/doc/AeEasy/Login.html +0 -146
- data/doc/AeEasy/Login/Flow.html +0 -2589
- data/doc/AeEasy/Login/Plugin.html +0 -117
- data/doc/AeEasy/Login/Plugin/EnabledBehavior.html +0 -590
- data/doc/_index.html +0 -167
- data/doc/class_list.html +0 -51
- data/doc/css/common.css +0 -1
- data/doc/css/full_list.css +0 -58
- data/doc/css/style.css +0 -496
- data/doc/file.README.html +0 -514
- data/doc/file_list.html +0 -56
- data/doc/frames.html +0 -17
- data/doc/index.html +0 -514
- data/doc/js/app.js +0 -303
- data/doc/js/full_list.js +0 -216
- data/doc/js/jquery.js +0 -4
- data/doc/method_list.html +0 -291
- data/doc/top-level-namespace.html +0 -110
- data/lib/ae_easy/login/flow.rb +0 -297
- data/lib/ae_easy/login/plugin.rb +0 -8
- data/lib/ae_easy/login/plugin/enabled_behavior.rb +0 -47
- data/lib/ae_easy/login/version.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c96cd3e94350eace2111491b4c766595cf481509fd200e3684d927aa627c266c
|
4
|
+
data.tar.gz: e426fa3295e0980e9d34917a427c1756af0168236eba97d42fd8210434ca3996
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a4448c5a08e8d232eb1888429e379ed83fea048432d93632d8889b2e2f42238738ff6edd3ea9bd8c39959c7231269aa2568bbf6a846eb69eb408573882652f9
|
7
|
+
data.tar.gz: a78de6a6bf7eb52a55c6de7a0debcda6e909a650f1ea49bf2edb9b694582a2a82d1ab7e48c405ec4af856da7d88c6c970aa460813d3ac978da6e63a34ed2b021
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
|
|
55
55
|
## Enforcement
|
56
56
|
|
57
57
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
-
reported by contacting the project team at
|
58
|
+
reported by contacting the project team at perry@datahen.com. All
|
59
59
|
complaints will be reviewed and investigated and will result in a response that
|
60
60
|
is deemed necessary and appropriate to the circumstances. The project team is
|
61
61
|
obligated to maintain confidentiality with regard to the reporter of an incident.
|
data/Gemfile
CHANGED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
# AeEasy login module
|
6
6
|
## Description
|
7
7
|
|
8
|
-
AeEasy login is part of AeEasy gem collection. It provides an easy way to handle login and session recovery, quite useful when scraping websites with login features and expiring sessions.
|
8
|
+
AeEasy login is part of AeEasy gem collection and an alias to [dh_easy-login](https://github.com/DataHenOfficial/dh_easy-login/) gem. It provides an easy way to handle login and session recovery, quite useful when scraping websites with login features and expiring sessions.
|
9
9
|
|
10
10
|
Install gem:
|
11
11
|
```ruby
|
@@ -17,371 +17,4 @@ Require gem:
|
|
17
17
|
require 'ae_easy/login'
|
18
18
|
```
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
## How to implement
|
23
|
-
|
24
|
-
### Before you start
|
25
|
-
|
26
|
-
It is true that most user cases for `ae_easy-login` gem applies to websites with login pages and create sessions, so we will cover this scenario on our example.
|
27
|
-
|
28
|
-
Therefore, `ae_easy-login` gem is designed to handle **ANY** kind of session recovery, even those that doesn't requires a login form `POST` by just changing the flow from:
|
29
|
-
|
30
|
-
```
|
31
|
-
login -> login_post -> restore
|
32
|
-
```
|
33
|
-
|
34
|
-
To whatever you need like for example:
|
35
|
-
|
36
|
-
```
|
37
|
-
home -> search_page -> restore
|
38
|
-
```
|
39
|
-
|
40
|
-
Here are some user case examples that can be fixed by `ae_easy-login` gem:
|
41
|
-
|
42
|
-
* Websites that invalidate requests with fast expiring cookies created on first request.
|
43
|
-
* Websites that generates tokens on every search (either on cookies or query_params) that are required to fetch a detail page.
|
44
|
-
* Websites that expires session due inactivity.
|
45
|
-
* Websites that uses complex login flows.
|
46
|
-
* etc.
|
47
|
-
|
48
|
-
Feel confident to expirement with it until it fit all your needs.
|
49
|
-
|
50
|
-
### Adding ae_easy-login to your project
|
51
|
-
|
52
|
-
Let's assume a simple project implementing `ae_easy` like the one described on [ae_easy README.md](https://github.com/answersengine/ae_easy/blob/master/README.md) that scrapers your website.
|
53
|
-
|
54
|
-
Now lets assume your website has a login page `https://example.com/login` with a session that expires before our sample project scrape job finish, causing all remaining webpages to respond `403` HTTP response code and fail... quite the problem isn't it? Well, not anymore, `ae_easy-login` gem to the rescue!
|
55
|
-
|
56
|
-
First, let's create our base module that will contain our session validation and recovery logic, for this example, we will call it `LoginEnable` :
|
57
|
-
|
58
|
-
```ruby
|
59
|
-
# ./lib/login_enable.rb
|
60
|
-
|
61
|
-
module LoginEnable
|
62
|
-
include AeEasy::Login::Plugin::EnabledBehavior
|
63
|
-
|
64
|
-
# Hook to initialize login_flow configuration.
|
65
|
-
def initialize_hook_login_plugin_enabled_behavior opts = {}
|
66
|
-
opts = {app_config: AeEasy::Core::Config.new(opts)}.merge opts
|
67
|
-
@login_flow = AeEasy::Login::Flow.new opts
|
68
|
-
@cookie = nil
|
69
|
-
end
|
70
|
-
|
71
|
-
# Get cookie after applying response cookie.
|
72
|
-
# @return [String] Cookie string.
|
73
|
-
def cookie
|
74
|
-
return @cookie if @cookie.nil?
|
75
|
-
|
76
|
-
raw_cookie = page['response_cookie'] || page['response_headers']['Set-Cookie']
|
77
|
-
@cookie = AeEasy::Core::Helper::Cookie.update(page['headers']['Cookie'], raw_cookie)
|
78
|
-
@cookie
|
79
|
-
end
|
80
|
-
|
81
|
-
# Validates session.
|
82
|
-
# @return [Boolean] `true` when session is valid, else `false`.
|
83
|
-
def valid_session?
|
84
|
-
['200', '404'].include? page['response_status_code'].to_s.strip
|
85
|
-
end
|
86
|
-
|
87
|
-
# Fix page session when session is invalid.
|
88
|
-
# @return [Boolean] `true` when session is valid, else `false`.
|
89
|
-
def fix_session
|
90
|
-
return true if valid_session?
|
91
|
-
|
92
|
-
login_flow.fix_session do
|
93
|
-
save_pages [{
|
94
|
-
'url' => 'https://example.com/login',
|
95
|
-
'page_type' => 'login',
|
96
|
-
'priority' => 9,
|
97
|
-
'freshness' => Time.now.iso8601,
|
98
|
-
'cookie' => "stl=#{salt}",
|
99
|
-
'headers' => {
|
100
|
-
# Add any extra header you need here
|
101
|
-
'Cookie' => "stl=#{salt}"
|
102
|
-
}
|
103
|
-
}]
|
104
|
-
end
|
105
|
-
|
106
|
-
false
|
107
|
-
end
|
108
|
-
end
|
109
|
-
```
|
110
|
-
|
111
|
-
Notice that our example `valid_session` method uses `200` and `404` HTTP response codes to validate that our session hasn't expired yet, therefore, **_this might not be the case for your website_**, so make sure to modify this method to fit your needs.
|
112
|
-
|
113
|
-
Our `fix_session` method will store any page with a failed session by creating an output so it can be restored later once we have the new active session cookie.
|
114
|
-
|
115
|
-
`fix_session` method will also mark the current session cookie as expired and **_enqueue a new `login` page with HIGH priority as long as another parser hasn't already did it to avoid duplicates_**.
|
116
|
-
|
117
|
-
`cookie` method will merge the request cookies with the response cookies, so we can be sure that the cookies are always updated when needed.
|
118
|
-
|
119
|
-
Next step is to create a simple parser that enqueue the `POST` of our login page:
|
120
|
-
|
121
|
-
```ruby
|
122
|
-
# ./parsers/login.rb
|
123
|
-
|
124
|
-
module Parsers
|
125
|
-
class Login
|
126
|
-
include AeEasy::Core::Plugin::Parser
|
127
|
-
include LoginEnable
|
128
|
-
|
129
|
-
def parse
|
130
|
-
pages << {
|
131
|
-
'url' => 'http://example.com/login',
|
132
|
-
'page_type' => 'login_post',
|
133
|
-
'priority' => 10,
|
134
|
-
'method' => 'POST',
|
135
|
-
'cookie' => cookie,
|
136
|
-
'headers' => {
|
137
|
-
# Add any extra header you need here
|
138
|
-
'Cookie' => cookie
|
139
|
-
}
|
140
|
-
}
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
```
|
145
|
-
|
146
|
-
Now let's handle the login response, seed and restore any page with an expired session:
|
147
|
-
|
148
|
-
```ruby
|
149
|
-
# ./parsers/login_post.rb
|
150
|
-
|
151
|
-
module Parsers
|
152
|
-
class LoginPost
|
153
|
-
include AeEasy::Core::Plugin::Parser
|
154
|
-
include LoginEnable
|
155
|
-
|
156
|
-
def seed!
|
157
|
-
return if login_flow.seeded?
|
158
|
-
|
159
|
-
Seeders::Seeder.new(context: context).seed do |new_page|
|
160
|
-
login_flow.fix_page! new_page
|
161
|
-
end
|
162
|
-
|
163
|
-
login_flow.seeded!
|
164
|
-
end
|
165
|
-
|
166
|
-
def parse
|
167
|
-
login_flow.update_config(
|
168
|
-
'cookie' => get_cookie,
|
169
|
-
'expired' => false
|
170
|
-
)
|
171
|
-
|
172
|
-
# Wait for any pending fetch to be hold
|
173
|
-
sleep 10
|
174
|
-
|
175
|
-
login_flow.restore_held_pages
|
176
|
-
seed!
|
177
|
-
end
|
178
|
-
end
|
179
|
-
end
|
180
|
-
```
|
181
|
-
|
182
|
-
Notice something interesting? that's right, the seeding happens **AFTER** we got our new active session cookie, so the pages we seed includes the session cookie. We use `login_flow.fix_page!` method to add our latest active session cookie along some internal `page['vars']` (used to handle page recovery) to our seeded pages.
|
183
|
-
|
184
|
-
**IMPORTANT:** This example assumes that `login_post` pages will never fails, but you might need to add some extra validations to make sure the login attempt was successful before restoring your pages.
|
185
|
-
|
186
|
-
**_Note:_** This example assumes that all pages to be seeded requires an active session, so we will add it to all pages we seed, but this will likely not apply to all pages to be seeded in a real life scenario, so make sure to add it only to those pages that requires an active session.
|
187
|
-
|
188
|
-
So next step is to modify our seeder so it allow the cookie inclusion by adding a `block` param that will be used by our `Parsers::LoginPost#seed!` method:
|
189
|
-
|
190
|
-
```ruby
|
191
|
-
# ./seeder/seeder.rb
|
192
|
-
|
193
|
-
module Seeder
|
194
|
-
class Seeder
|
195
|
-
include AeEasy::Core::Plugin::Seeder
|
196
|
-
|
197
|
-
def seed &block
|
198
|
-
new_page = {
|
199
|
-
'url' => 'https://example.com/login.rb?query=food',
|
200
|
-
'page_type' => 'search'
|
201
|
-
}
|
202
|
-
block.call(page) unless block.nil?
|
203
|
-
pages << new_page
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
```
|
208
|
-
|
209
|
-
Now we will need to create a new seeder to seed login page:
|
210
|
-
|
211
|
-
```ruby
|
212
|
-
# ./seeder/login.rb
|
213
|
-
|
214
|
-
module Seeder
|
215
|
-
class Login
|
216
|
-
include AeEasy::Core::Plugin::Seeder
|
217
|
-
|
218
|
-
def seed
|
219
|
-
pages << {
|
220
|
-
'url' => 'https://example.com/login',
|
221
|
-
'page_type' => 'login',
|
222
|
-
'priority' => 9
|
223
|
-
}
|
224
|
-
end
|
225
|
-
end
|
226
|
-
end
|
227
|
-
```
|
228
|
-
|
229
|
-
Now let's modify our `./config.yaml` to add our new page types on it, as well as let us parse failed fetched pages since our example assumes that website will return `403` HTTP response code when session has expired:
|
230
|
-
|
231
|
-
```yaml
|
232
|
-
# ./config.yaml
|
233
|
-
|
234
|
-
parse_failed_pages: true
|
235
|
-
|
236
|
-
seeder:
|
237
|
-
file: ./router/seeder.rb
|
238
|
-
disabled: false
|
239
|
-
|
240
|
-
parsers:
|
241
|
-
- page_type: search
|
242
|
-
file: ./router/parser.rb
|
243
|
-
disabled: false
|
244
|
-
- page_type: product
|
245
|
-
file: ./router/parser.rb
|
246
|
-
disabled: false
|
247
|
-
- page_type: login
|
248
|
-
file: ./router/parser.rb
|
249
|
-
disabled: false
|
250
|
-
- page_type: login_post
|
251
|
-
file: ./router/parser.rb
|
252
|
-
disabled: false
|
253
|
-
```
|
254
|
-
|
255
|
-
And don't forget to modify `./ae_easy.yaml` to add our new routes and change our seeder so login page can be seed first instead of our old seeder:
|
256
|
-
|
257
|
-
```yaml
|
258
|
-
# ./ae_easy.yaml
|
259
|
-
|
260
|
-
router:
|
261
|
-
parser:
|
262
|
-
routes:
|
263
|
-
- page_type: search
|
264
|
-
class: Parsers::Search
|
265
|
-
- page_type: product
|
266
|
-
class: Parsers::Product
|
267
|
-
- page_type: login
|
268
|
-
class: Parsers::Login
|
269
|
-
- page_type: login_post
|
270
|
-
class: Parsers::LoginPost
|
271
|
-
|
272
|
-
seeder:
|
273
|
-
routes:
|
274
|
-
- class: Seeder::Login
|
275
|
-
```
|
276
|
-
|
277
|
-
Now, let's will need to modify our routers as well since we modified our `ae_easy.yaml` routes and added new classes:
|
278
|
-
|
279
|
-
```ruby
|
280
|
-
# ./router/seeder.rb
|
281
|
-
|
282
|
-
require 'ae_easy/router'
|
283
|
-
require './seeder/login'
|
284
|
-
|
285
|
-
AeEasy::Router::Seeder.new.route context: self
|
286
|
-
```
|
287
|
-
|
288
|
-
```ruby
|
289
|
-
# ./router/parser.rb
|
290
|
-
|
291
|
-
require 'cgi'
|
292
|
-
require 'ae_easy/router'
|
293
|
-
require 'ae_easy/login'
|
294
|
-
require './lib/login_enable'
|
295
|
-
require './seeder/seeder'
|
296
|
-
require './parsers/search'
|
297
|
-
require './parsers/product'
|
298
|
-
require './parsers/login'
|
299
|
-
require './parsers/login_post'
|
300
|
-
|
301
|
-
AeEasy::Router::Parser.new.route context: self
|
302
|
-
```
|
303
|
-
|
304
|
-
Next, we need to include our `LoginEnable` module on every parser that requires session validation to fix any expired session request. To do this, we will be using our `LoginEnable#fix_session` function as the first thing to do on each parser's `parse` method:
|
305
|
-
|
306
|
-
```ruby
|
307
|
-
# ./parsers/search.rb
|
308
|
-
|
309
|
-
module Parsers
|
310
|
-
class Search
|
311
|
-
include AeEasy::Core::Plugin::Parser
|
312
|
-
include LoginEnable
|
313
|
-
|
314
|
-
def parse
|
315
|
-
return unless fix_session
|
316
|
-
|
317
|
-
html = Nokogiri.HTML content
|
318
|
-
html.css('.name').each do |element|
|
319
|
-
name = element.text.strip
|
320
|
-
pages << {
|
321
|
-
'url' => "https://example.com/product/#{CGI::escape name}",
|
322
|
-
'page_type' => 'product',
|
323
|
-
'vars' => {'name' => name}
|
324
|
-
}
|
325
|
-
end
|
326
|
-
end
|
327
|
-
end
|
328
|
-
end
|
329
|
-
```
|
330
|
-
|
331
|
-
```ruby
|
332
|
-
# ./parsers/product.rb
|
333
|
-
|
334
|
-
module Parsers
|
335
|
-
class Product
|
336
|
-
include AeEasy::Core::Plugin::Parser
|
337
|
-
include LoginEnable
|
338
|
-
|
339
|
-
def parse
|
340
|
-
return unless fix_session
|
341
|
-
|
342
|
-
html = Nokogiri.HTML content
|
343
|
-
description = html.css('.description').first.text.strip
|
344
|
-
outputs << {
|
345
|
-
'_collection' => 'product',
|
346
|
-
'name' => page['vars']['name'],
|
347
|
-
'description' => description
|
348
|
-
}
|
349
|
-
end
|
350
|
-
end
|
351
|
-
end
|
352
|
-
```
|
353
|
-
|
354
|
-
**_Note:_** This example asumes that all pages requires an active session, so we will add it to all parsers, but this will likely not apply to all parsers in a real life scenario since not all web pages will require session, so make sure to add it to only the parsers that needs it.
|
355
|
-
|
356
|
-
Finally, we need to make sure that every page that requires an active session is enqueued within our latest active session cookie, so we need to use `login_flow.fix_page!` method on all pages to be enqueued that applies.
|
357
|
-
|
358
|
-
As for this example, we already add it to our search pages enqueued by our seeder, so the only place left to modify is `./parsers/search.rb` parser since it enqueues `product` pages:
|
359
|
-
|
360
|
-
```ruby
|
361
|
-
# ./parsers/search.rb
|
362
|
-
|
363
|
-
module Parsers
|
364
|
-
class Search
|
365
|
-
include AeEasy::Core::Plugin::Parser
|
366
|
-
include LoginEnable
|
367
|
-
|
368
|
-
def parse
|
369
|
-
return unless fix_session
|
370
|
-
|
371
|
-
html = Nokogiri.HTML content
|
372
|
-
html.css('.name').each do |element|
|
373
|
-
name = element.text.strip
|
374
|
-
new_page = {
|
375
|
-
'url' => "https://example.com/product/#{CGI::escape name}",
|
376
|
-
'page_type' => 'product',
|
377
|
-
'vars' => {'name' => name}
|
378
|
-
}
|
379
|
-
login_flow.fix_page! new_page
|
380
|
-
pages << new_page
|
381
|
-
end
|
382
|
-
end
|
383
|
-
end
|
384
|
-
end
|
385
|
-
```
|
386
|
-
|
387
|
-
Hurray! Now you have implemented a fully functional login flow with auto recovery capabilities on your project.
|
20
|
+
See [dh_easy-login](https://github.com/DataHenOfficial/dh_easy-login/) gem for documentation.
|
data/Rakefile
CHANGED
@@ -9,14 +9,4 @@ Rake::TestTask.new do |t|
|
|
9
9
|
t.test_files = FileList['./test/**/*_test.rb']
|
10
10
|
end
|
11
11
|
|
12
|
-
desc 'Benchmark another task execution | usage example: benchmark[my_task, param1, param2]'
|
13
|
-
task :benchmark, [:task] do |task, args|
|
14
|
-
task_name = args[:task]
|
15
|
-
if task_name.nil?
|
16
|
-
puts "Should select a task."
|
17
|
-
exit 1
|
18
|
-
end
|
19
|
-
puts Benchmark.measure{ Rake::Task[task_name].invoke *args.extras }
|
20
|
-
end
|
21
|
-
|
22
12
|
task default: :test
|
data/ae_easy-login.gemspec
CHANGED
@@ -1,22 +1,18 @@
|
|
1
1
|
|
2
2
|
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "ae_easy/login/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
6
|
spec.name = "ae_easy-login"
|
8
|
-
spec.version =
|
7
|
+
spec.version = "0.0.4"
|
9
8
|
spec.authors = ["Eduardo Rosales"]
|
10
9
|
spec.email = ["eduardo@datahen.com"]
|
11
10
|
|
12
|
-
spec.summary = %q{
|
13
|
-
spec.description = %q{
|
14
|
-
spec.homepage = "https://
|
11
|
+
spec.summary = %q{(Deprecated: Use dh_easy-login gem instead.) Compatibility alias for DataHen Easy toolkit login flow module}
|
12
|
+
spec.description = %q{(Deprecated: Use dh_easy-login gem instead.) Compatibility alias for DataHen Easy toolkit login flow module allows to handle login and session expire recovery with ease.}
|
13
|
+
spec.homepage = "https://datahen.com"
|
15
14
|
spec.license = "MIT"
|
16
15
|
|
17
|
-
# spec.cert_chain = ['certs/ae_easy.pem']
|
18
|
-
# spec.signing_key = File.expand_path("~/.ssh/gems/gem-private_ae_easy.pem") if $0 =~ /gem\z/
|
19
|
-
|
20
16
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
21
17
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
22
18
|
if spec.respond_to?(:metadata)
|
@@ -38,12 +34,10 @@ Gem::Specification.new do |spec|
|
|
38
34
|
spec.require_paths = ["lib"]
|
39
35
|
spec.required_ruby_version = '>= 2.2.2'
|
40
36
|
|
41
|
-
spec.add_dependency '
|
37
|
+
spec.add_dependency 'dh_easy-login', '>= 0'
|
38
|
+
spec.add_dependency 'ae_easy-core', '>= 0.2.1'
|
42
39
|
spec.add_development_dependency 'bundler', '>= 1'
|
43
40
|
spec.add_development_dependency 'rake', '~> 10'
|
44
41
|
spec.add_development_dependency 'minitest', '~> 5'
|
45
|
-
spec.add_development_dependency 'simplecov', '~> 0'
|
46
|
-
spec.add_development_dependency 'simplecov-console', '~> 0'
|
47
|
-
spec.add_development_dependency 'timecop', '~> 0'
|
48
42
|
spec.add_development_dependency 'byebug', '>= 0'
|
49
43
|
end
|