Class: PlayStationNetworkAPI::Session

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/play_station_network_api/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Session

Returns a new instance of Session.



13
14
15
# File 'lib/play_station_network_api/session.rb', line 13

def initialize(token)
  @token = token
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



11
12
13
# File 'lib/play_station_network_api/session.rb', line 11

def token
  @token
end

Instance Method Details

#access_tokenObject



24
25
26
# File 'lib/play_station_network_api/session.rb', line 24

def access_token
  oauth_token['access_token']
end

#authenticateObject Also known as: refresh



17
18
19
20
# File 'lib/play_station_network_api/session.rb', line 17

def authenticate
  code, cid = oauth_authorize
  oauth_token(code)
end

#expiration_dateObject



28
29
30
# File 'lib/play_station_network_api/session.rb', line 28

def expiration_date
  (Time.now + oauth_token['refresh_token_expires_in']).to_datetime.to_s
end

#expired?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/play_station_network_api/session.rb', line 32

def expired?
  expiration_date < DateTime.now.to_s
end

#oauth_authorizeObject

private



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/play_station_network_api/session.rb', line 38

def oauth_authorize
  request = self.class.get('/authz/v3/oauth/authorize',
    headers: {
      'Cookie' => HTTParty::CookieHash.new().add_cookies({ npsso: token }).to_cookie_string
    },

    query: {
      'access_type' => 'offline',
      'client_id' => CLIENT_ID,
      'redirect_uri' => 'com.playstation.PlayStationApp://redirect',
      'response_type' => 'code',
      'scope' => DEFAULT_SCOPES
    },

    follow_redirects: false
  )

  if (location = request.headers['location'])
    code = location.match(/code=([A-Za-z0-9:\?_\-\.\/=]+)/)[1]
    cid = location.match(/cid=([A-Za-z0-9:\?_\-\.\/=]+)/)[1]
    
    return code, cid
  end
end

#oauth_token(code = nil) ⇒ Object



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
# File 'lib/play_station_network_api/session.rb', line 63

def oauth_token(code = nil)
  body = {}

  if code
    body = {
      'code' => code,
      'grant_type' => 'authorization_code',
      'redirect_uri' => 'com.playstation.PlayStationApp://redirect'
    }
  else
    body = {
      'refresh_token' => token,
      'grant_type' => 'refresh_token'
    }
  end
  
  self.class.post('/authz/v3/oauth/token',
    headers: {
      'Host' => 'ca.account.sony.com',
      'Referer' =>  'https://my.playstation.com/',
      'Authorization' => 'Basic YWM4ZDE2MWEtZDk2Ni00NzI4LWIwZWEtZmZlYzIyZjY5ZWRjOkRFaXhFcVhYQ2RYZHdqMHY='
    },

    body: {
      'scope' => DEFAULT_SCOPES,
      **body,
      'token_format' => 'jwt'
    }
  ).parsed_response
end