Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- SIE CONFIDENTIAL
- Network - NP S2S Website Sample
- Copyright (C) 2016 Sony Interactive Entertainment Inc.
- All Rights Reserved.
- */
- /**
- * This file contains the code which allows users signed in to PSN to retrieve their Profile and access information.
- * This information is saved as a session variable to be used by various PSN Web APIs that require it.
- */
- // @codeCoverageIgnoreStart
- require_once __DIR__ . '/defines.php';
- require_once __DIR__ . '/exceptions.php';
- require_once __DIR__ . '/PS4Auth.php';
- require_once __DIR__ . '/curlHelper.php';
- //Retieve the authorisation code that is returned via the PSN sign in gateway
- $authorisationCode = $_GET["code"];
- if(empty($authorisationCode)){
- //'code' was not present. Logout and re-direct
- header('Location: ./logout.php');
- } else {
- try {
- //Authenticate using the authorisation code, and obtain an access token
- $accessToken = getAccessToken($authorisationCode);
- //Verify the validity of the token by retrieving further information about it
- $tokenInfo = verifyAccessToken($accessToken);
- //Obtain basic account information of the currently logged in user
- $userInfo = obtainBasicAccountInfo($accessToken);
- } catch (AuthenticationException $exc) {
- //Authentication with PSN failed, re-direct the logout procedure to clear session
- header('Location: ./logout.php');
- die();
- }
- //Combine the information for later use
- $commonInfo = new PS4Auth($accessToken, $tokenInfo, $userInfo);
- //Store the information we created
- $_SESSION['COMMON_INFO'] = serialize($commonInfo);
- $_SESSION['COMMON_TICKET_EXPIRY'] = time() + $commonInfo->getExpiry();
- //Rewrite our session cookie to expire when our PSN access token expires
- setcookie(session_name(), session_id(), $_SESSION['COMMON_TICKET_EXPIRY'], '/');
- header('Location: ../../');
- }
- /**
- * Obtain a PSN WebAPI access token
- *
- * @param type $code The authorisation code returned from sign in
- */
- function getAccessToken($code){
- $curlhelper = new CurlHelper();
- $curlhelper->setMethod('POST');
- $curlhelper->setAuthorization(CLIENT_ID, CLIENT_SECRET);
- $curlhelper->setRequestMessage('grant_type=authorization_code&redirect_uri=' . S2S_LOGIN_URL . '&code=' . $code);
- $curlhelper->setUrl("https://auth.api.sp-int.ac.playstation.net/2.0/oauth/token");
- $success = $curlhelper->execute();
- if ($success === false || $curlhelper->getReturnCode() != '200') {
- $curlhelper->end();
- throw new AuthenticationException($curlhelper->getReturnCode(), 'Obtaining the authentication code failed');
- }
- $authenticationCode = $curlhelper->getBody();
- $curlhelper->end();
- return $authenticationCode;
- }
- /**
- * Verifies the validity of the access token
- *
- * @param $token The authorisation code returned from sign in
- */
- function verifyAccessToken($token)
- {
- $auth = json_decode($token);
- $curlhelper = new CurlHelper();
- $curlhelper->setMethod('GET');
- $curlhelper->setAuthorization(CLIENT_ID, CLIENT_SECRET);
- $curlhelper->setUrl("https://auth.api.sp-int.ac.playstation.net/2.0/oauth/token/" . $auth->access_token);
- $success = $curlhelper->execute();
- if ($success === false || $curlhelper->getReturnCode() != '200') {
- $curlhelper->end();
- throw new AuthenticationException($curlhelper->getReturnCode(), 'Access token verification failed.');
- }
- $validatedInfo = $curlhelper->getBody();
- $curlhelper->end();
- return $validatedInfo;
- }
- /**
- * Returns account information for the logged in user
- *
- * @param $token Access token object
- */
- function obtainBasicAccountInfo($token)
- {
- $auth = json_decode($token);
- $curlhelper = new CurlHelper();
- $curlhelper->setMethod('GET');
- $curlhelper->setHeader(array('Authorization: Bearer ' . $auth->access_token));
- $curlhelper->setUrl("https://vl.api.sp-int.ac.playstation.net/vl/api/v1/s2s/users/me/info");
- $success = $curlhelper->execute();
- if ($success === false || $curlhelper->getReturnCode() < 200 || $curlhelper->getReturnCode() >= 300) {
- $curlhelper->end();
- throw new AuthenticationException($curlhelper->getReturnCode(), 'Basic account info obtainment failed.');
- }
- $accountInfo = $curlhelper->getBody();
- $curlhelper->end();
- return $accountInfo;
- }
- // @codeCoverageIgnoreEnd
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement