Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
1,717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. SIE CONFIDENTIAL
  5. Network - NP S2S Website Sample
  6. Copyright (C) 2016 Sony Interactive Entertainment Inc.
  7. All Rights Reserved.
  8. */
  9.  
  10. /**
  11. * This file contains the code which allows users signed in to PSN to retrieve their Profile and access information.
  12. * This information is saved as a session variable to be used by various PSN Web APIs that require it.
  13. */
  14. // @codeCoverageIgnoreStart
  15.  
  16. require_once __DIR__ . '/defines.php';
  17. require_once __DIR__ . '/exceptions.php';
  18. require_once __DIR__ . '/PS4Auth.php';
  19. require_once __DIR__ . '/curlHelper.php';
  20.  
  21. //Retieve the authorisation code that is returned via the PSN sign in gateway
  22. $authorisationCode = $_GET["code"];
  23. if(empty($authorisationCode)){
  24. //'code' was not present. Logout and re-direct
  25. header('Location: ./logout.php');
  26. } else {
  27. try {
  28. //Authenticate using the authorisation code, and obtain an access token
  29. $accessToken = getAccessToken($authorisationCode);
  30.  
  31. //Verify the validity of the token by retrieving further information about it
  32. $tokenInfo = verifyAccessToken($accessToken);
  33.  
  34. //Obtain basic account information of the currently logged in user
  35. $userInfo = obtainBasicAccountInfo($accessToken);
  36.  
  37. } catch (AuthenticationException $exc) {
  38. //Authentication with PSN failed, re-direct the logout procedure to clear session
  39. header('Location: ./logout.php');
  40. die();
  41. }
  42.  
  43. //Combine the information for later use
  44. $commonInfo = new PS4Auth($accessToken, $tokenInfo, $userInfo);
  45.  
  46. //Store the information we created
  47. $_SESSION['COMMON_INFO'] = serialize($commonInfo);
  48. $_SESSION['COMMON_TICKET_EXPIRY'] = time() + $commonInfo->getExpiry();
  49.  
  50. //Rewrite our session cookie to expire when our PSN access token expires
  51. setcookie(session_name(), session_id(), $_SESSION['COMMON_TICKET_EXPIRY'], '/');
  52.  
  53. header('Location: ../../');
  54. }
  55.  
  56.  
  57. /**
  58. * Obtain a PSN WebAPI access token
  59. *
  60. * @param type $code The authorisation code returned from sign in
  61. */
  62. function getAccessToken($code){
  63. $curlhelper = new CurlHelper();
  64. $curlhelper->setMethod('POST');
  65. $curlhelper->setAuthorization(CLIENT_ID, CLIENT_SECRET);
  66. $curlhelper->setRequestMessage('grant_type=authorization_code&redirect_uri=' . S2S_LOGIN_URL . '&code=' . $code);
  67. $curlhelper->setUrl("https://auth.api.sp-int.ac.playstation.net/2.0/oauth/token");
  68.  
  69. $success = $curlhelper->execute();
  70. if ($success === false || $curlhelper->getReturnCode() != '200') {
  71. $curlhelper->end();
  72. throw new AuthenticationException($curlhelper->getReturnCode(), 'Obtaining the authentication code failed');
  73. }
  74.  
  75. $authenticationCode = $curlhelper->getBody();
  76.  
  77. $curlhelper->end();
  78.  
  79. return $authenticationCode;
  80. }
  81.  
  82.  
  83. /**
  84. * Verifies the validity of the access token
  85. *
  86. * @param $token The authorisation code returned from sign in
  87. */
  88. function verifyAccessToken($token)
  89. {
  90. $auth = json_decode($token);
  91.  
  92. $curlhelper = new CurlHelper();
  93. $curlhelper->setMethod('GET');
  94. $curlhelper->setAuthorization(CLIENT_ID, CLIENT_SECRET);
  95. $curlhelper->setUrl("https://auth.api.sp-int.ac.playstation.net/2.0/oauth/token/" . $auth->access_token);
  96. $success = $curlhelper->execute();
  97.  
  98. if ($success === false || $curlhelper->getReturnCode() != '200') {
  99. $curlhelper->end();
  100. throw new AuthenticationException($curlhelper->getReturnCode(), 'Access token verification failed.');
  101. }
  102.  
  103. $validatedInfo = $curlhelper->getBody();
  104.  
  105. $curlhelper->end();
  106.  
  107. return $validatedInfo;
  108. }
  109.  
  110. /**
  111. * Returns account information for the logged in user
  112. *
  113. * @param $token Access token object
  114. */
  115. function obtainBasicAccountInfo($token)
  116. {
  117. $auth = json_decode($token);
  118.  
  119. $curlhelper = new CurlHelper();
  120. $curlhelper->setMethod('GET');
  121. $curlhelper->setHeader(array('Authorization: Bearer ' . $auth->access_token));
  122. $curlhelper->setUrl("https://vl.api.sp-int.ac.playstation.net/vl/api/v1/s2s/users/me/info");
  123. $success = $curlhelper->execute();
  124.  
  125. if ($success === false || $curlhelper->getReturnCode() < 200 || $curlhelper->getReturnCode() >= 300) {
  126. $curlhelper->end();
  127. throw new AuthenticationException($curlhelper->getReturnCode(), 'Basic account info obtainment failed.');
  128. }
  129.  
  130. $accountInfo = $curlhelper->getBody();
  131.  
  132. $curlhelper->end();
  133.  
  134. return $accountInfo;
  135. }
  136.  
  137. // @codeCoverageIgnoreEnd
  138. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement