class PlayStationNetworkAPI::Session

Attributes

token[RW]

Public Class Methods

new(token) click to toggle source
# File lib/play_station_network_api/session.rb, line 13
def initialize(token)
  @token = token
end

Public Instance Methods

access_token() click to toggle source
# File lib/play_station_network_api/session.rb, line 24
def access_token
  oauth_token['access_token']
end
authenticate() click to toggle source
# File lib/play_station_network_api/session.rb, line 17
def authenticate
  code, cid = oauth_authorize
  oauth_token(code)
end
Also aliased as: refresh
expiration_date() click to toggle source
# 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?() click to toggle source
# File lib/play_station_network_api/session.rb, line 32
def expired?
  expiration_date < DateTime.now.to_s
end
oauth_authorize() click to toggle source

private

# 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) click to toggle source
# 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
refresh()
Alias for: authenticate