Class: CompanionApi::Resources::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/companion_api/resources/account.rb

Constant Summary collapse

PLATFORM_IOS =
1
PLATFORM_ANDROID =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile) ⇒ Account

Returns a new instance of Account.



9
10
11
# File 'lib/companion_api/resources/account.rb', line 9

def initialize(profile)
  @profile = profile
end

Instance Attribute Details

#profileObject

Returns the value of attribute profile.



7
8
9
# File 'lib/companion_api/resources/account.rb', line 7

def profile
  @profile
end

Instance Method Details

#auto_login!(username, password) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/companion_api/resources/account.rb', line 13

def auto_login!(username, password)
  req = CompanionApi::Request.new(
    uri:       ,
    requestId: CompanionApi.uuid,
    absolute:  true,
  )

  res = req.get!
  body = res.body

  if !body.include?('cis_sessid') && body.include?('loginForm')
    html = Nokogiri::HTML(body)
    form = html.at_css("form[id='loginForm']")
    stored = form.at_css("input[name='_STORED_']")[:value]

    action = CGI.unescape(form[:action])

    data = {
      "_STORED_": stored,
      "sqexid":   username,
      "password": password,
    }

    req = CompanionApi::Request.new(
      uri:       CompanionApi::Request::URI_SE,
      endpoint:  "/oauth/oa/#{action}",
      requestId: CompanionApi.uuid,
      # token: profile.get("token"),
      form:      data,
    )

    res = req.post!
    body = res.body
  end

  html = Nokogiri::HTML(body)
  form = html.at_css("form[name='mainForm']")
  raise CompanionApi::LoginError, 'unexpected response received' if form.nil?

  cis_sessid = form.at_css("input[name='cis_sessid']")[:value]

  data = {
    "cis_sessid": cis_sessid,
    "provision":  '',
    "_c":         1,
  }

  req = CompanionApi::Request.new(
    uri:       form[:action],
    absolute:  true,
    # requestId: CompanionApi.uuid,
    query:     {
      token: @profile.get('token'),
      uid:   @profile.get('uid'),
    },
    return202: true,
    form:      data,
  )

  res = req.post!

  raise CompanionApi::LoginError, 'Login status could not be validated.' if res.status != 202

  @profile.set("lastLogin", Time.now.to_i)

  CompanionApi.refresh_uuid
  true
end

#get_tokenObject



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/companion_api/resources/account.rb', line 108

def get_token
  crypted_uuid = CompanionApi.rsa.public_encrypt(@profile.get('userId'))
  uid = Base64.encode64(crypted_uuid)

  req = CompanionApi::Request.new(
    uri:      CompanionApi::Request::URI,
    endpoint: '/login/token',
    json:     { 'platform' => PLATFORM_ANDROID, 'uid' => uid },
  )

  res = req.post!
  JSON.parse(res.body)
end

#login_urlObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/companion_api/resources/account.rb', line 82

def 
  @profile.set('userId', CompanionApi.uuid)

  json = get_token
  @profile.set('token', json['token'])
  @profile.set('salt', json['salt'])

  CompanionApi::Request::SQEX_AUTH_URI + '?' + {
    'client_id'     => 'ffxiv_comapp',
    'lang'          => 'en-us',
    'response_type' => 'code',
    'redirect_uri'  => redirect_uri,
  }.to_query
end

#redirect_uriObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/companion_api/resources/account.rb', line 97

def redirect_uri
  uid = CompanionApi.pbkdf2(@profile.get('userId'), @profile.get('salt'))
  @profile.set('uid', uid)

  CompanionApi::Request::OAUTH_CALLBACK + '?' + {
    'token'      => @profile.get('token'),
    'uid'        => uid,
    'request_id' => CompanionApi.uuid,
  }.to_query
end