3 namespace PayPal\Auth;
\r
6 * Oauth Token credential
\r
9 use PayPal\Rest\RestHandler;
\r
10 use PayPal\Common\PPUserAgent;
\r
11 use PayPal\Core\PPLoggingManager;
\r
12 use PayPal\Core\PPConstants;
\r
13 use PayPal\Core\PPHttpConfig;
\r
14 use PayPal\Core\PPConnectionManager;
\r
15 use PayPal\Exception\PPConfigurationException;
\r
17 class OAuthTokenCredential {
\r
19 private static $expiryBufferTime = 120;
\r
24 * Client ID as obtained from the developer portal
\r
29 * Client secret as obtained from the developer portal
\r
31 private $clientSecret;
\r
35 * Generated Access Token
\r
37 private $accessToken;
\r
40 * Seconds for with access token is valid
\r
42 private $tokenExpiresIn;
\r
45 * Last time (in milliseconds) when access token was generated
\r
47 private $tokenCreateTime;
\r
51 * @param string $clientId client id obtained from the developer portal
\r
52 * @param string $clientSecret client secret obtained from the developer portal
\r
54 public function __construct($clientId, $clientSecret) {
\r
55 $this->clientId = $clientId;
\r
56 $this->clientSecret = $clientSecret;
\r
60 * @return the accessToken
\r
62 public function getAccessToken($config) {
\r
64 $this->logger = new PPLoggingManager(__CLASS__, $config);
\r
65 // Check if Access Token is not null and has not expired.
\r
66 // The API returns expiry time as a relative time unit
\r
67 // We use a buffer time when checking for token expiry to account
\r
68 // for API call delays and any delay between the time the token is
\r
69 // retrieved and subsequently used
\r
70 if ($this->accessToken != null &&
\r
71 (time() - $this->tokenCreateTime) > ($this->tokenExpiresIn - self::$expiryBufferTime)) {
\r
72 $this->accessToken = null;
\r
74 // If accessToken is Null, obtain a new token
\r
75 if ($this->accessToken == null) {
\r
76 $this->_generateAccessToken($config);
\r
78 return $this->accessToken;
\r
82 * Generates a new access token
\r
84 private function _generateAccessToken($config) {
\r
86 $base64ClientID = base64_encode($this->clientId . ":" . $this->clientSecret);
\r
88 "User-Agent" => PPUserAgent::getValue(RestHandler::$sdkName, RestHandler::$sdkVersion),
\r
89 "Authorization" => "Basic " . $base64ClientID,
\r
92 $httpConfiguration = $this->getOAuthHttpConfiguration($config);
\r
93 $httpConfiguration->setHeaders($headers);
\r
95 $connection = PPConnectionManager::getInstance()->getConnection($httpConfiguration, $config);
\r
96 $res = $connection->execute("grant_type=client_credentials");
\r
97 $jsonResponse = json_decode($res, true);
\r
98 if($jsonResponse == NULL ||
\r
99 !isset($jsonResponse["access_token"]) || !isset($jsonResponse["expires_in"]) ) {
\r
100 $this->accessToken = NULL;
\r
101 $this->tokenExpiresIn = NULL;
\r
102 $this->logger->warning("Could not generate new Access token. Invalid response from server: " . $jsonResponse);
\r
104 $this->accessToken = $jsonResponse["access_token"];
\r
105 $this->tokenExpiresIn = $jsonResponse["expires_in"];
\r
107 $this->tokenCreateTime = time();
\r
108 return $this->accessToken;
\r
112 * Get HttpConfiguration object for OAuth API
\r
114 private function getOAuthHttpConfiguration($config) {
\r
115 if (isset($config['oauth.EndPoint'])) {
\r
116 $baseEndpoint = $config['oauth.EndPoint'];
\r
117 } else if (isset($config['service.EndPoint'])) {
\r
118 $baseEndpoint = $config['service.EndPoint'];
\r
119 } else if (isset($config['mode'])) {
\r
120 switch (strtoupper($config['mode'])) {
\r
122 $baseEndpoint = PPConstants::REST_SANDBOX_ENDPOINT;
\r
125 $baseEndpoint = PPConstants::REST_LIVE_ENDPOINT;
\r
128 throw new PPConfigurationException('The mode config parameter must be set to either sandbox/live');
\r
131 throw new PPConfigurationException('You must set one of service.endpoint or mode parameters in your configuration');
\r
134 $baseEndpoint = rtrim(trim($baseEndpoint), '/');
\r
135 return new PPHttpConfig($baseEndpoint . "/v1/oauth2/token", "POST");
\r