Module: BungieClient::Auth
- Defined in:
- lib/bungie_client/auth.rb
Overview
Module Auth with two methods: authentication in bungie.net by PSN or Xbox Live and checking this authentication with cookies that was returned early.
Class Method Summary collapse
-
.auth(username, password, type = 'psn') ⇒ Mechanize::CookieJar|nil
Full authentication of user.
-
.auth_possible?(cookies) ⇒ Boolean
Try authenticate with cookies.
-
.valid_cookies?(cookies, raise = false) ⇒ Boolean
Check cookies for private requests.
Class Method Details
.auth(username, password, type = 'psn') ⇒ Mechanize::CookieJar|nil
Full authentication of user
38 39 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bungie_client/auth.rb', line 38 def auth(username, password, type = 'psn') # client init agent = Mechanize.new # getting index page agent.get 'https://www.bungie.net/' do |page| result = nil link = page.link_with :text => search_query(type) unless link.nil? # call auth page login = agent.click link # sending form for sony or ms if type == 'psn' form = login.forms.first unless form.nil? name = form.field_with(:type => 'email') pass = form.field_with(:type => 'password') if !name.nil? && !pass.nil? name.value = username pass.value = password result = form. end end else # ms wanted enabled js, but we can send form without it ppft = login.body.match(/name\="PPFT"(.*)value\="(.*?)"/) url = login.body.match(/urlPost\:'(.*?)'/) if !ppft.nil? && !url.nil? result = agent.post url.to_a.last, 'login' => username, 'passwd' => password, 'KMSI' => 1, 'PPFT' => ppft.to_a.last end end end # if result not nil and after authentication we returned to bungie, all ok if !result.nil? && result.uri.to_s.include?('?code') needed = %w(bungled bungleatk bungledid bungles) output = Mechanize::CookieJar.new agent..each do || output.add if needed.include? .name end return output end end nil end |
.auth_possible?(cookies) ⇒ Boolean
Try authenticate with cookies
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/bungie_client/auth.rb', line 101 def auth_possible?() agent = Mechanize.new , true if .is_a? Array .each do || agent..add end else agent. = end result = nil agent.get 'https://www.bungie.net/' do |page| link = page.link_with :text => search_query('psn') result = agent.click link unless link.nil? end !result.nil? && result.uri.host == "www.bungie.net" end |
.valid_cookies?(cookies, raise = false) ⇒ Boolean
Check cookies for private requests
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bungie_client/auth.rb', line 10 def (, raise = false) if .is_a? Mechanize::CookieJar = . elsif !(.is_a?(Array) && .all? { |c| c.is_a? HTTP::Cookie }) raise "Wrong cookie option: It must be Array with HTTP::Cookie elements or CookieJar." if raise return false end needed = %w(bungled bungleatk bungles) .each do || if needed.include?(.name) && .expired? == false needed.delete .name return true if needed.length == 0 end end (raise) ? raise("Your argument doesn't have all needed cookies for private requests #{needed.join ','}.") : false end |