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

Class Method Details

.auth(username, password, type = 'psn') ⇒ Mechanize::CookieJar|nil

Full authentication of user

Parameters:

  • username (String)

    user email for auth

  • password (String)

    user password for auth

  • type (String) (defaults to: 'psn')

    can be psn or live

Returns:

  • (Mechanize::CookieJar|nil)


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
       = agent.click link

      # sending form for sony or ms
      if type == 'psn'
        form = .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.click_button
          end
        end
      else
        # ms wanted enabled js, but we can send form without it
        ppft = .body.match(/name\="PPFT"(.*)value\="(.*?)"/)
        url = .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.cookies.each do |cookie|
        output.add cookie if needed.include? cookie.name
      end

      return output
    end
  end

  nil
end

.auth_possible?(cookies) ⇒ Boolean

Try authenticate with cookies

Parameters:

  • cookies (Array|CookieJar)

    array with [HTTP::Cookie] or [CookieJar]

Returns:

  • (Boolean)


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?(cookies)
  agent = Mechanize.new

  valid_cookies? cookies, true

  if cookies.is_a? Array
    cookies.each do |cookie|
      agent.cookie_jar.add cookie
    end
  else
    agent.cookie_jar = cookies
  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

Parameters:

  • cookies (Array)

    with [HTTP::Cookie]

  • raise (Boolean) (defaults to: false)

    cookie errors

Returns:

  • (Boolean)


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 valid_cookies?(cookies, raise = false)
  if cookies.is_a? Mechanize::CookieJar
    cookies = cookies.cookies
  elsif !(cookies.is_a?(Array) && cookies.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)
  cookies.each do |cookie|
    if needed.include?(cookie.name) && cookie.expired? == false
      needed.delete cookie.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