|
| 1 | +<?php |
| 2 | +namespace Fortifi\Api\Core\OAuth\Grants; |
| 3 | + |
| 4 | +use Fortifi\Api\Core\ApiDefinition\SecurityDefinition; |
| 5 | +use Fortifi\Api\Core\ApiRequest; |
| 6 | +use Fortifi\Api\Core\ApiRequestDetail; |
| 7 | +use Fortifi\Api\Core\IApiConnection; |
| 8 | +use Fortifi\Api\Core\OAuth\Tokens\AccessToken; |
| 9 | +use Fortifi\Api\Core\OAuth\Tokens\IToken; |
| 10 | +use Packaged\Helpers\Objects; |
| 11 | + |
| 12 | +class ServiceAccountGrant implements IGrant |
| 13 | +{ |
| 14 | + protected $_apiSecret; |
| 15 | + protected $_apiUser; |
| 16 | + |
| 17 | + /** |
| 18 | + * @param mixed $apiSecret |
| 19 | + * |
| 20 | + * @return ServiceAccountGrant |
| 21 | + */ |
| 22 | + public function setApiSecret($apiSecret) |
| 23 | + { |
| 24 | + $this->_apiSecret = $apiSecret; |
| 25 | + return $this; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @param mixed $apiUser |
| 30 | + * |
| 31 | + * @return ServiceAccountGrant |
| 32 | + */ |
| 33 | + public function setApiUser($apiUser) |
| 34 | + { |
| 35 | + $this->_apiUser = $apiUser; |
| 36 | + return $this; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @return mixed |
| 41 | + */ |
| 42 | + public function getApiSecret() |
| 43 | + { |
| 44 | + return $this->_apiSecret; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return mixed |
| 49 | + */ |
| 50 | + public function getApiUser() |
| 51 | + { |
| 52 | + return $this->_apiUser; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param IApiConnection $connection |
| 57 | + * @param SecurityDefinition $definition |
| 58 | + * |
| 59 | + * @return IToken |
| 60 | + */ |
| 61 | + public function getToken( |
| 62 | + IApiConnection $connection, SecurityDefinition $definition |
| 63 | + ) |
| 64 | + { |
| 65 | + if($definition->getType() !== 'oauth2') |
| 66 | + { |
| 67 | + throw new \InvalidArgumentException( |
| 68 | + 'The security definition provided is not a valid oAuth2 definition' |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + $params = []; |
| 73 | + $params['grant_type'] = $this->getGrantType(); |
| 74 | + $params['api_user'] = $this->getApiUser(); |
| 75 | + $params['api_key'] = $this->getApiSecret(); |
| 76 | + |
| 77 | + $request = new ApiRequest(); |
| 78 | + $request->setConnection($connection); |
| 79 | + $detail = new ApiRequestDetail(); |
| 80 | + $detail->setUrl($definition->getTokenUrl()); |
| 81 | + $detail->setPostFields($params); |
| 82 | + $request->setRequestDetail($detail); |
| 83 | + $connection->load($request); |
| 84 | + $tokenResponse = $request->getDecodedResponse(); |
| 85 | + |
| 86 | + $token = new AccessToken(); |
| 87 | + $token->setToken(Objects::property($tokenResponse, 'access_token')); |
| 88 | + $token->setType(Objects::property($tokenResponse, 'token_type', 'Bearer')); |
| 89 | + $token->setExpirySeconds(Objects::property($tokenResponse, 'expires_in')); |
| 90 | + $token->setExpiryTime(Objects::property($tokenResponse, 'expiry_time')); |
| 91 | + $token->setUserId(Objects::property($tokenResponse, 'uid')); |
| 92 | + $token->setSessionSecret( |
| 93 | + Objects::property($tokenResponse, 'session_secret') |
| 94 | + ); |
| 95 | + |
| 96 | + return $token; |
| 97 | + } |
| 98 | + |
| 99 | + public function getGrantType() |
| 100 | + { |
| 101 | + return 'service_account'; |
| 102 | + } |
| 103 | +} |
0 commit comments