amazon_auth 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5372326bf8eb4d5ad7208f1012fb3637f0d75bfb
4
- data.tar.gz: 1318f2610357e659f04a3054c62b6ff1da7aa9e7
3
+ metadata.gz: 54054238c0843dbd6acc152aa7cd42186645b564
4
+ data.tar.gz: 80d121226920bde30c3493d90e33bf9819bf2adb
5
5
  SHA512:
6
- metadata.gz: 2faa1765823348febcb15dca571b5849a8ac5fd9454f8e944cccbd4290354acb11d0f90b1dd782fffaefc553cd5f39a118cde44b4ceab6e972ade7b5ac0d06bf
7
- data.tar.gz: dd44a0edf4af32714802cd63d2f6c222993661114025b4bd3972e108855dc87f1c26cfa55dcece5e46dc58252215c1be3d84d5c5b53786ddb3aaf3502c20d357
6
+ metadata.gz: 2761572472a93eb91acb879cb19f7614ba0b393671c7de020552386319ef606e1dc8abac78c40c5ba33d8fe120e96bf4327a2f670c8b577af06645f9744e8f7b
7
+ data.tar.gz: 68d334892df7fe5a338df6772fec708fff3f634581e561412322c39a529b9be4e842e4370cf28e2b67e5556c14b781b532a1c63308cc2f57f35935550ab3e58a
data/README.md CHANGED
@@ -6,6 +6,7 @@
6
6
  Sign In Amazon using Capybara and Selenium
7
7
 
8
8
  ![amazon_auth_login](https://cloud.githubusercontent.com/assets/275284/25064724/7f5faae4-223b-11e7-9fc6-4a82d1d727ab.gif)
9
+
9
10
  Recorded with [Recordit](http://recordit.co/)
10
11
 
11
12
  ## Installation
@@ -48,6 +49,22 @@ Run `amazon_auth` and paste the output to _.env_.
48
49
 
49
50
  ![amazon_auth_setup](https://cloud.githubusercontent.com/assets/275284/25064607/9b9b80be-2238-11e7-95fc-c1547a83f178.gif)
50
51
 
52
+ ### envchain for security
53
+
54
+ You can store environment variables in macOS Keychain instead of dotenv.
55
+ Check out [envchain](https://github.com/sorah/envchain)
56
+
57
+ ```
58
+ brew install envchain
59
+
60
+ envchain --set amazon AMAZON_DOMAIN AMAZON_USERNAME_CODE AMAZON_PASSWORD_CODE AMAZON_CODE_SALT
61
+
62
+ # Run console with envchain
63
+ envchain amazon bin/console
64
+ envchain amazon bundle exec irb
65
+ envchain amazon rails console
66
+ ```
67
+
51
68
  ### Run
52
69
 
53
70
  In console, you can move around pages using Capybara DSL
@@ -73,6 +90,20 @@ Set `AMAZON_DOMAIN` in _.env_.
73
90
 
74
91
  e.g. `AMAZON_DOMAIN=amazon.co.jp` for Japanese site
75
92
 
93
+ ### Keep cookies
94
+
95
+ Using [capybara-sessionkeeper gem](https://github.com/kyamaguchi/capybara-sessionkeeper)
96
+
97
+ ```
98
+ client = AmazonAuth::Client.new(keep_cookie: true, debug: true)
99
+ ```
100
+
101
+ You can change Capyabra.save_path when it isn't set
102
+
103
+ ```
104
+ client = AmazonAuth::Client.new(keep_cookie: true, save_path: 'tmp/cookies', debug: true)
105
+ ```
106
+
76
107
  ### Logging
77
108
 
78
109
  Normal logging
@@ -110,6 +141,10 @@ In console,
110
141
  client = AmazonAuth::Client.new(driver: :firefox)
111
142
  ```
112
143
 
144
+ ## TODO
145
+
146
+ - [Idea] Hook `submit_signin_form` after every `visit` and `click` action
147
+
113
148
  ## Development
114
149
 
115
150
  Use _.env.development_ instead of _.env_ in development.
data/amazon_auth.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_runtime_dependency 'capybara'
23
+ spec.add_runtime_dependency 'capybara-sessionkeeper', "~> 0.1.1"
23
24
  spec.add_runtime_dependency 'selenium-webdriver'
24
25
  spec.add_runtime_dependency 'highline'
25
26
  spec.add_runtime_dependency 'dotenv'
data/lib/amazon_auth.rb CHANGED
@@ -2,6 +2,7 @@ require 'dotenv'
2
2
  Dotenv.load
3
3
  require "active_support/all"
4
4
  require "capybara"
5
+ require "capybara/sessionkeeper"
5
6
  require "highline/import"
6
7
  begin
7
8
  require 'byebug'
@@ -3,86 +3,23 @@ module AmazonAuth
3
3
  include AmazonAuth::CommonExtension
4
4
  include AmazonAuth::SessionExtension
5
5
 
6
- attr_accessor :initial_url
6
+ attr_accessor :options
7
7
 
8
8
  def initialize(options = {})
9
9
  @options = options
10
- @initial_url = options.fetch(:url) { "https://www.#{AmazonInfo.domain}/" }
11
- @login = options.fetch(:login) do
12
- if (amazon_username_code = ENV['AMAZON_USERNAME_CODE']).present?
13
- Converter.decode(amazon_username_code)
14
- else
15
- raise('AMAZON_USERNAME_CODE is required.')
16
- end
17
- end
18
- @password = options.fetch(:password) do
19
- if (amazon_password_code = ENV['AMAZON_PASSWORD_CODE']).present?
20
- Converter.decode(amazon_password_code)
21
- else
22
- raise('AMAZON_PASSWORD_CODE is required.')
23
- end
24
- end
25
10
  @driver = options.fetch(:driver, :selenium)
11
+ # Check credentials
12
+ raise('AMAZON_USERNAME_CODE is required.') unless (options[:login] || ENV['AMAZON_USERNAME_CODE']).present?
13
+ raise('AMAZON_PASSWORD_CODE is required.') unless (options[:password] || ENV['AMAZON_PASSWORD_CODE']).present?
14
+ Converter.salt if options[:login].blank? || options[:password].blank?
26
15
 
27
- Capybara.app_host = @initial_url
16
+ Capybara.save_path = options.fetch(:save_path, 'tmp') if Capybara.save_path.nil?
17
+ Capybara.app_host = initial_url if Capybara.app_host.nil?
28
18
  rescue => e
29
19
  puts "Please setup credentials of amazon_auth gem with folloing its instruction."
30
20
  raise e
31
21
  end
32
22
 
33
- def sign_in
34
- session.visit initial_url
35
- debug "Visiting #{initial_url}"
36
- link = links_for('#nav-signin-tooltip a').find{|link| link =~ %r{\A/gp/navigation/redirector.html} }
37
- debug "link: [#{link}]"
38
- session.visit(link) if link
39
- submit_signin_form
40
- end
41
-
42
- def submit_signin_form
43
- debug "Begin submit_signin_form"
44
- unless session.has_selector?('#signInSubmit')
45
- log "signInSubmit button not found"
46
- return false
47
- end
48
- session.fill_in 'ap_email', with: @login if session.first('#ap_email').value.blank?
49
- session.fill_in 'ap_password', with: @password
50
- session.first('#signInSubmit').click
51
- log "Clicked signInSubmit"
52
-
53
- raise('Failed on signin') if alert_displayed?
54
- while image_recognition_displayed? do
55
- retry_signin_form_with_image_recognition
56
- end
57
- debug "End submit_signin_form"
58
- true
59
- end
60
-
61
- def retry_signin_form_with_image_recognition
62
- return true unless session.has_selector?('#signInSubmit')
63
- session.fill_in 'ap_password', with: @password
64
- if image_recognition_displayed?
65
- input = ask "Got the prompt. Read characters from the image [blank to cancel]: "
66
- if input.blank?
67
- debug "Going back to #{initial_url}"
68
- session.visit initial_url
69
- return true
70
- end
71
- session.fill_in 'auth-captcha-guess', with: input
72
- end
73
- sleep 1
74
- session.click_on('signInSubmit')
75
- sleep 2
76
- end
77
-
78
- def alert_displayed?
79
- session.has_selector?('.a-alert-error')
80
- end
81
-
82
- def image_recognition_displayed?
83
- session.has_selector?('#auth-captcha-image-container')
84
- end
85
-
86
23
  def session
87
24
  @session ||= Capybara::Session.new(@driver)
88
25
  end
@@ -4,7 +4,7 @@ module AmazonAuth
4
4
  def log(message)
5
5
  return unless (@options[:debug] || @options[:verbose])
6
6
  puts "[#{Time.current.strftime('%Y-%m-%d %H:%M:%S')}] #{message}" +
7
- (@options[:debug] && session ? " -- #{session.current_url}" : '')
7
+ (@options[:debug] && @session ? " -- #{session.current_url}" : '')
8
8
  end
9
9
 
10
10
  def debug(message)
@@ -19,5 +19,88 @@ module AmazonAuth
19
19
  end
20
20
  end
21
21
  end
22
+
23
+ # Helpers for sign in
24
+ def initial_url
25
+ options.fetch(:url) { "https://www.#{AmazonInfo.domain}/" }
26
+ end
27
+
28
+ def sign_in(url = nil)
29
+ url ||= initial_url
30
+ session.visit url
31
+ debug "Visiting #{url}"
32
+ restore_cookies if keep_cookie?
33
+ if (link = links_for('a').find{|link| link =~ %r{\A/gp/navigation/redirector.html} })
34
+ debug "link: [#{link}]"
35
+ session.visit(link)
36
+ end
37
+ submit_signin_form
38
+ end
39
+
40
+ def restore_cookies
41
+ log "Restoring cookies"
42
+ wait_for_selector('body')
43
+ session.restore_cookies
44
+ session.visit session.current_url
45
+ session.save_cookies
46
+ end
47
+
48
+ def keep_cookie?
49
+ options[:keep_cookie]
50
+ end
51
+
52
+ def submit_signin_form
53
+ debug "Begin submit_signin_form"
54
+ unless session.has_selector?('#signInSubmit')
55
+ log "signInSubmit button not found in this page"
56
+ return false
57
+ end
58
+ session.fill_in 'ap_email', with: login if session.first('#ap_email').value.blank?
59
+ session.fill_in 'ap_password', with: password
60
+ session.first('#signInSubmit').click
61
+ log "Clicked signInSubmit"
62
+
63
+ raise('Failed on signin') if alert_displayed?
64
+ while image_recognition_displayed? do
65
+ retry_signin_form_with_image_recognition
66
+ end
67
+ debug "End submit_signin_form"
68
+ session.save_cookies if keep_cookie?
69
+ true
70
+ end
71
+
72
+ def retry_signin_form_with_image_recognition
73
+ return true unless session.has_selector?('#signInSubmit')
74
+ session.fill_in 'ap_password', with: password
75
+ if image_recognition_displayed?
76
+ input = ask "Got the prompt. Read characters from the image [blank to cancel]: "
77
+ if input.blank?
78
+ debug "Going back to #{initial_url}"
79
+ session.visit initial_url
80
+ return true
81
+ end
82
+ session.fill_in 'auth-captcha-guess', with: input
83
+ end
84
+ sleep 1
85
+ session.click_on('signInSubmit')
86
+ sleep 2
87
+ end
88
+
89
+ def alert_displayed?
90
+ session.has_selector?('.a-alert-error')
91
+ end
92
+
93
+ def image_recognition_displayed?
94
+ session.has_selector?('#auth-captcha-image-container')
95
+ end
96
+
97
+ private
98
+
99
+ def login
100
+ options.fetch(:login, Converter.decode(ENV['AMAZON_USERNAME_CODE']))
101
+ end
102
+ def password
103
+ options.fetch(:password, Converter.decode(ENV['AMAZON_PASSWORD_CODE']))
104
+ end
22
105
  end
23
106
  end
@@ -1,3 +1,3 @@
1
1
  module AmazonAuth
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazuho Yamaguchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-04 00:00:00.000000000 Z
11
+ date: 2017-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: capybara-sessionkeeper
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.1
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: selenium-webdriver
29
43
  requirement: !ruby/object:Gem::Requirement