Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Added feature to create third party access tokens #968

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/PayPal/Auth/OAuthTokenCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,26 @@ class OAuthTokenCredential extends PayPalResourceModel
*/
private $cipher;

/**
* The encryted account number of the merchant on whose behalf the transaction is being done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encryted -> encrypted

*
* @var Subject
*/
private $subject;

/**
* Construct
*
* @param string $clientId client id obtained from the developer portal
* @param string $clientSecret client secret obtained from the developer portal
* @param null|string $subject subject used to create Third Party Token
*/
public function __construct($clientId, $clientSecret)
public function __construct($clientId, $clientSecret, $subject = null)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->cipher = new Cipher($this->clientSecret);
$this->subject = $subject;
}

/**
Expand All @@ -108,6 +117,16 @@ public function getClientSecret()
return $this->clientSecret;
}

/**
* Get the subject used to create Third Party Access Token
*
* @return string
*/
public function getSubject()
{
return $this->subject;
}

/**
* Get AccessToken
*
Expand Down Expand Up @@ -267,6 +286,11 @@ private function generateAccessToken($config, $refreshToken = null)
$params['grant_type'] = 'refresh_token';
$params['refresh_token'] = $refreshToken;
}

if ($this->subject != null && $refreshToken != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If subject is associated with a refreshToken, should we just pass the subject as an argument here, instead of adding it to a constructor ?

$params['target_subject'] = $this->subject;
}

$payload = http_build_query($params);
$response = $this->getToken($config, $this->clientId, $this->clientSecret, $payload);

Expand Down
21 changes: 21 additions & 0 deletions tests/PayPal/Test/Auth/OAuthTokenCredentialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ public function testGetAccessTokenUnit()
$this->assertNotNull($result);
}

public function testGetAccessTokenWithSubjectUnit()
{
$config = array(
'mode' => 'sandbox',
'cache.enabled' => true,
'cache.FileName' => AuthorizationCacheTest::CACHE_FILE
);
$cred = new OAuthTokenCredential('clientId', 'clientSecret', 'subject');

//{"clientId":{"clientId":"clientId","accessToken":"accessToken","tokenCreateTime":1421204091,"tokenExpiresIn":288000000}}
AuthorizationCache::push($config, 'clientId', $cred->encrypt('accessTokenWithSubject'), 1421204091, 288000000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If subject is relevant only for third party tokens (refresh tokens), we should not cache the response. This could lead to all consequent calls with just a clientId to use this subject based access token. This will be because, we only fetch this cache by clientId and nothing else.


$apiContext = new ApiContext($cred);
$apiContext->setConfig($config);
$this->assertEquals('clientId', $cred->getClientId());
$this->assertEquals('clientSecret', $cred->getClientSecret());
$this->assertEquals('subject', $cred->getSubject());
$result = $cred->getAccessToken($config);
$this->assertNotNull($result);
}

public function testGetAccessTokenUnitMock()
{
$config = array(
Expand Down