Skip to content

Instantly share code, notes, and snippets.

@spnkr
Created December 4, 2018 03:05
Show Gist options
  • Save spnkr/7ceca380cfbcd5a6e8efcc31a51cd2c2 to your computer and use it in GitHub Desktop.
Save spnkr/7ceca380cfbcd5a6e8efcc31a51cd2c2 to your computer and use it in GitHub Desktop.
Ruby PSN API
class _PSN
include HTTParty
attr_accessor :oauth, :error, :npsso, :grant_code, :refresh_token,
:email, :password, :ticket_uuid, :code
ACTIVITY_URL = "https://activity.api.np.km.playstation.net/activity/api/v1/users/"
OAUTH_URL = "https://auth.api.sonyentertainmentnetwork.com/2.0/oauth/token"
SSO_URL = "https://auth.api.sonyentertainmentnetwork.com/2.0/ssocookie"
CODE_URL = "https://auth.api.sonyentertainmentnetwork.com/2.0/oauth/authorize"
USERS_URL = "https://us-prof.np.community.playstation.net/userProfile/v1/users/"
MESSAGE_URL = "https://us-gmsg.np.community.playstation.net/groupMessaging/v1/messageGroups"
MESSAGE_THREADS_URL = "https://us-gmsg.np.community.playstation.net/groupMessaging/v1/threads"
MESSAGE_USERS_URL = "https://us-gmsg.np.community.playstation.net/groupMessaging/v1/users/"
TROPHY_URL = "https://us-tpy.np.community.playstation.net/trophy/v1/"
COMMUNITIES_URL = "https://communities.api.playstation.com/v1/communities/"
DOCUMENTATION_URL = "https://auth.api.sonyentertainmentnetwork.com/docs/"
def initialize(email, password, ticket_uuid = nil, code = nil)
@email = email
@password = password
# Required if the User has 2FA enabled
#
@ticket_uuid = ticket_uuid
@code = code
end
def perform
getNPSSOID()
getCode()
getOAuth()
end
def auth
end
def getNPSSOID
options = {
body: { }
}
if @ticket_uuid && @code
options[:body].merge!(two_factor_auth_request)
else
options[:body].merge!(login_request)
end
request = HTTParty.post(PSN::SSO_URL, options)
@npsso = request.parsed_response['npsso']
end
def getCode
options = {
body: code_request.to_query,
headers: {
'Cookie' => @npsso
}
}
puts options
request = HTTParty.get(PSN::CODE_URL, options)
puts request
end
def getOAuth
end
private
# The initial request which will get the NPSSO ID
# => POST
#
def login_request
return {
authentication_type: "password",
username: @email,
password: @password,
client_id: "71a7beb8-f21a-47d9-a604-2e71bee24fe0"
}
end
# Get the oAuth token
# => POST
#
def oauth_request
return {
app_context: "inapp_ios",
client_id: "b7cbf451-6bb6-4a5a-8913-71e61f462787",
client_secret: "zsISsjmCx85zgCJg",
code: nil,
duid: "0000000d000400808F4B3AA3301B4945B2E3636E38C0DDFC",
grant_type: "authorization_code",
scope: "capone:report_submission,psn:sceapp,user:account.get,user:account.settings.privacy.get,user:account.settings.privacy.update,user:account.realName.get,user:account.realName.update,kamaji:get_account_hash,kamaji:ugc:distributor,oauth:manage_device_usercodes"
}
end
def code_request
return {
state: "06d7AuZpOmJAwYYOWmVU63OMY",
duid: "0000000d000400808F4B3AA3301B4945B2E3636E38C0DDFC",
app_context: "inapp_ios",
client_id: "b7cbf451-6bb6-4a5a-8913-71e61f462787",
scope: "capone:report_submission,psn:sceapp,user:account.get,user:account.settings.privacy.get,user:account.settings.privacy.update,user:account.realName.get,user:account.realName.update,kamaji:get_account_hash,kamaji:ugc:distributor,oauth:manage_device_usercodes",
response_type: "code"
}
end
def refresh_oauth_request
return {
app_context: "inapp_ios",
client_id: "b7cbf451-6bb6-4a5a-8913-71e61f462787",
client_secret: "zsISsjmCx85zgCJg",
refresh_token: nil,
duid: "0000000d000400808F4B3AA3301B4945B2E3636E38C0DDFC",
grant_type: "refresh_token",
scope: "capone:report_submission,psn:sceapp,user:account.get,user:account.settings.privacy.get,user:account.settings.privacy.update,user:account.realName.get,user:account.realName.update,kamaji:get_account_hash,kamaji:ugc:distributor,oauth:manage_device_usercodes"
}
end
def two_factor_auth_request
return {
authentication_type: "two_step",
ticket_uuid: @ticket_uuid,
code: @code,
client_id: "b7cbf451-6bb6-4a5a-8913-71e61f462787",
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment