-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
354 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
namespace Fortifi\Api\Core\OAuth\Grants; | ||
|
||
use Fortifi\Api\Core\ApiDefinition\SecurityDefinition; | ||
use Fortifi\Api\Core\IApiConnection; | ||
use Fortifi\Api\Core\OAuth\Tokens\IToken; | ||
|
||
interface IGrant | ||
{ | ||
/** | ||
* @param IApiConnection $connection | ||
* @param SecurityDefinition $definition | ||
* | ||
* @return IToken | ||
*/ | ||
public function getToken( | ||
IApiConnection $connection, SecurityDefinition $definition | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
namespace Fortifi\Api\Core\OAuth\Grants; | ||
|
||
use Fortifi\Api\Core\ApiDefinition\SecurityDefinition; | ||
use Fortifi\Api\Core\ApiRequest; | ||
use Fortifi\Api\Core\ApiRequestDetail; | ||
use Fortifi\Api\Core\IApiConnection; | ||
use Fortifi\Api\Core\OAuth\Tokens\AccessToken; | ||
use Fortifi\Api\Core\OAuth\Tokens\IToken; | ||
use Packaged\Helpers\Objects; | ||
|
||
class ServiceAccountGrant implements IGrant | ||
{ | ||
protected $_apiSecret; | ||
protected $_apiUser; | ||
|
||
/** | ||
* @param mixed $apiSecret | ||
* | ||
* @return ServiceAccountGrant | ||
*/ | ||
public function setApiSecret($apiSecret) | ||
{ | ||
$this->_apiSecret = $apiSecret; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param mixed $apiUser | ||
* | ||
* @return ServiceAccountGrant | ||
*/ | ||
public function setApiUser($apiUser) | ||
{ | ||
$this->_apiUser = $apiUser; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getApiSecret() | ||
{ | ||
return $this->_apiSecret; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getApiUser() | ||
{ | ||
return $this->_apiUser; | ||
} | ||
|
||
/** | ||
* @param IApiConnection $connection | ||
* @param SecurityDefinition $definition | ||
* | ||
* @return IToken | ||
*/ | ||
public function getToken( | ||
IApiConnection $connection, SecurityDefinition $definition | ||
) | ||
{ | ||
if($definition->getType() !== 'oauth2') | ||
{ | ||
throw new \InvalidArgumentException( | ||
'The security definition provided is not a valid oAuth2 definition' | ||
); | ||
} | ||
|
||
$params = []; | ||
$params['grant_type'] = $this->getGrantType(); | ||
$params['api_user'] = $this->getApiUser(); | ||
$params['api_key'] = $this->getApiSecret(); | ||
|
||
$request = new ApiRequest(); | ||
$request->setConnection($connection); | ||
$detail = new ApiRequestDetail(); | ||
$detail->setUrl($definition->getTokenUrl()); | ||
$detail->setPostFields($params); | ||
$request->setRequestDetail($detail); | ||
$connection->load($request); | ||
$tokenResponse = $request->getDecodedResponse(); | ||
|
||
$token = new AccessToken(); | ||
$token->setToken(Objects::property($tokenResponse, 'access_token')); | ||
$token->setType(Objects::property($tokenResponse, 'token_type', 'Bearer')); | ||
$token->setExpirySeconds(Objects::property($tokenResponse, 'expires_in')); | ||
$token->setExpiryTime(Objects::property($tokenResponse, 'expiry_time')); | ||
$token->setUserId(Objects::property($tokenResponse, 'uid')); | ||
$token->setSessionSecret( | ||
Objects::property($tokenResponse, 'session_secret') | ||
); | ||
|
||
return $token; | ||
} | ||
|
||
public function getGrantType() | ||
{ | ||
return 'service_account'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
namespace Fortifi\Api\Core\OAuth\TokenStorage; | ||
|
||
use Packaged\Helpers\Path; | ||
|
||
class TmpFileTokenStorage implements TokenStorageInterface | ||
{ | ||
/** | ||
* Store a token in storage | ||
* | ||
* @param string $key location key to store the token in | ||
* | ||
* @param string $token | ||
* | ||
* @return bool | ||
*/ | ||
public function storeToken($key, $token) | ||
{ | ||
return file_put_contents($this->_createFileName($key), $token) !== false; | ||
} | ||
|
||
/** | ||
* Retrieve a token from storage | ||
* | ||
* @param string $key location key for token | ||
* | ||
* @return string|null | ||
*/ | ||
public function retrieveToken($key) | ||
{ | ||
$location = $this->_createFileName($key); | ||
if(file_exists($location)) | ||
{ | ||
return file_get_contents($location); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Create a temporary filename | ||
* | ||
* @param $key | ||
* | ||
* @return string | ||
*/ | ||
private function _createFileName($key) | ||
{ | ||
return Path::build(sys_get_temp_dir(), 'Fortifi-Token-' . $key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace Fortifi\Api\Core\OAuth\TokenStorage; | ||
|
||
interface TokenStorageInterface | ||
{ | ||
/** | ||
* Store a token in storage | ||
* | ||
* @param string $key location key to store the token in | ||
* | ||
* @param string $token | ||
* | ||
* @return bool | ||
*/ | ||
public function storeToken($key, $token); | ||
|
||
/** | ||
* Retrieve a token from storage | ||
* | ||
* @param string $key location key for token | ||
* | ||
* @return string|null | ||
*/ | ||
public function retrieveToken($key); | ||
} |
Oops, something went wrong.