Skip to content

Commit

Permalink
Merge pull request #47 from MichaelJ2324/2.0
Browse files Browse the repository at this point in the history
SUPP- 1723 - Fix Platform Handling for OAuth
  • Loading branch information
MichaelJ2324 committed Jul 7, 2020
2 parents 8ca8bde + c00eba1 commit 2e39879
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
"michaelj2324/php-rest-client": ">=1.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0|~5.0",
"phpdocumentor/phpdocumentor": "2.*"
"phpunit/phpunit": "~5.0",
"phpdocumentor/phpdocumentor": "2.*",
"squizlabs/php_codesniffer": "^3"
},
"autoload": {
"psr-4": {
Expand Down
62 changes: 59 additions & 3 deletions src/Auth/SugarOAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ class SugarOAuthController extends AbstractOAuth2Controller
{
const ACTION_SUGAR_SUDO = 'sudo';

const OAUTH_PROP_PLATFORM = 'platform';

protected static $_AUTH_HEADER = 'OAuth-Token';

protected static $_DEFAULT_GRANT_TYPE = self::OAUTH_RESOURCE_OWNER_GRANT;

protected static $_DEFAULT_PLATFORM = 'base';

protected static $_DEFAULT_SUGAR_AUTH_ACTIONS = array(
self::ACTION_SUGAR_SUDO
);
Expand All @@ -36,20 +40,46 @@ class SugarOAuthController extends AbstractOAuth2Controller
'password' => '',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
);

protected $platform = 'base';

/**
* @inheritdoc
*/
public function __construct()
{
parent::__construct();
$this->setPlatform(static::$_DEFAULT_PLATFORM);
foreach (static::$_DEFAULT_SUGAR_AUTH_ACTIONS as $action) {
$this->actions[] = $action;
}
}

/**
* @param $platform
* @return $this
*/
public function setPlatform($platform)
{
$this->platform = $platform;
if (!isset($this->credentials[self::OAUTH_PROP_PLATFORM]) ||
$this->credentials[self::OAUTH_PROP_PLATFORM] !== $platform){
$this->credentials[self::OAUTH_PROP_PLATFORM] = $this->platform;
$this->setCredentials($this->credentials);
}

return $this;
}

/**
* @return string
*/
public function getPlatform()
{
return $this->platform;
}

/**
* @inheritdoc
*/
Expand All @@ -64,6 +94,10 @@ protected function getAuthHeaderValue()
*/
public function setCredentials(array $credentials)
{
if (isset($credentials[self::OAUTH_PROP_PLATFORM]) &&
$credentials[self::OAUTH_PROP_PLATFORM] !== $this->platform){
$this->setPlatform($credentials[self::OAUTH_PROP_PLATFORM]);
}
parent::setCredentials($credentials);
if (!empty($this->credentials)){
$token = $this->getStoredToken($this->credentials);
Expand All @@ -79,7 +113,8 @@ public function setCredentials(array $credentials)
/**
* @inheritdoc
*/
public function updateCredentials(array $credentials){
public function updateCredentials(array $credentials)
{
$current = array_replace($this->getCredentials(),$credentials);
return $this->setCredentials($current);
}
Expand Down Expand Up @@ -150,6 +185,27 @@ public function sudo($user)
return FALSE;
}

/**
* @inheritDoc
*/
public function reset()
{
$this->setPlatform(static::$_DEFAULT_PLATFORM);
return parent::reset();
}

/**
* @inheritDoc
*/
protected function configureEndpoint(EndpointInterface $Endpoint, $action)
{
$Endpoint = parent::configureEndpoint($Endpoint, $action);

$platform = $this->getPlatform();
$Endpoint->getRequest()->addHeader('X-Sugar-Platform', $platform);
return $Endpoint;
}

/**
* Configure the Sudo Endpoint
* @param EndpointInterface $Endpoint
Expand All @@ -162,7 +218,7 @@ protected function configureSudoEndpoint(EndpointInterface $Endpoint,$user)
$Endpoint->setOptions(array($user));
$data = array();
$creds = $this->getCredentials();
$data['platform'] = $creds['platform'];
$data['platform'] = $this->platform;
$data['client_id'] = $creds['client_id'];
$Endpoint->setData($data);
return $Endpoint;
Expand Down
20 changes: 17 additions & 3 deletions tests/Auth/SugarOAuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function testConstructor()

/**
* @covers ::setCredentials
* @covers ::setPlatform
* @covers ::getPlatform
*/
public function testSetCredentials(){
$Auth = new SugarOAuthController();
Expand All @@ -61,6 +63,7 @@ public function testSetCredentials(){
'client_secret' => '',
'platform' => 'api'
)));
$this->assertEquals('api',$Auth->getPlatform());
$this->assertEmpty($Auth->getToken());
$Storage->store($Auth->getCredentials(),array(
'access_token' => '1234',
Expand All @@ -77,6 +80,17 @@ public function testSetCredentials(){
'access_token' => '1234',
'refresh_token' => '5678',
),$Auth->getToken());
$this->assertEquals($Auth,$Auth->setPlatform('mobile'));
$creds = $Auth->getCredentials();
$this->assertEquals('mobile',$creds[$Auth::OAUTH_PROP_PLATFORM]);
$this->assertEquals(array(
'username' => 'admin',
'password' => '',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'mobile'
),$creds);
$this->assertEquals('mobile',$Auth->getPlatform());
}

/**
Expand All @@ -89,7 +103,7 @@ public function testUpdateCredentials(){
'password' => '',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
'platform' => 'base'
),$Auth->getCredentials());
$this->assertEquals($Auth,$Auth->updateCredentials(array(
'username' => 'admin'
Expand All @@ -99,7 +113,7 @@ public function testUpdateCredentials(){
'password' => '',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
'platform' => 'base'
),$Auth->getCredentials());
$this->assertEquals($Auth,$Auth->updateCredentials(array(
'username' => 'system',
Expand All @@ -110,7 +124,7 @@ public function testUpdateCredentials(){
'password' => 'asdf',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
'platform' => 'base'
),$Auth->getCredentials());
$this->assertEquals($Auth,$Auth->updateCredentials(array(
'platform' => array()
Expand Down
10 changes: 5 additions & 5 deletions tests/Client/Sugar7APITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testConstructor(){
'password' => 'asdf',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
'platform' => 'base'
),$Client->getAuth()->getCredentials());
$this->assertNotEmpty($Client->getEndpointProvider());
$this->assertEquals(10,$Client->getVersion());
Expand All @@ -93,31 +93,31 @@ public function testLogin(){
'password' => 'asdf',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
'platform' => 'base'
),$Client->getAuth()->getCredentials());
$this->assertEquals(true,$Client->login('user1','asdf'));
$this->assertEquals(array(
'username' => 'user1',
'password' => 'asdf',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
'platform' => 'base'
),$Client->getAuth()->getCredentials());
$this->assertEquals(true,$Client->login(NULL,'abc123'));
$this->assertEquals(array(
'username' => 'user1',
'password' => 'abc123',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
'platform' => 'base'
),$Client->getAuth()->getCredentials());
$this->assertEquals(true,$Client->login());
$this->assertEquals(array(
'username' => 'user1',
'password' => 'abc123',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
'platform' => 'base'
),$Client->getAuth()->getCredentials());
}

Expand Down

0 comments on commit 2e39879

Please sign in to comment.