OAuthGateway.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Braintree;
  3. /**
  4. * Braintree OAuthGateway module
  5. * PHP Version 5
  6. * Creates and manages Braintree Addresses
  7. *
  8. * @package Braintree
  9. */
  10. class OAuthGateway
  11. {
  12. private $_gateway;
  13. private $_config;
  14. private $_http;
  15. public function __construct($gateway)
  16. {
  17. $this->_gateway = $gateway;
  18. $this->_config = $gateway->config;
  19. $this->_http = new Http($gateway->config);
  20. $this->_http->useClientCredentials();
  21. $this->_config->assertHasClientCredentials();
  22. }
  23. public function createTokenFromCode($params)
  24. {
  25. $params['grantType'] = "authorization_code";
  26. return $this->_createToken($params);
  27. }
  28. public function createTokenFromRefreshToken($params)
  29. {
  30. $params['grantType'] = "refresh_token";
  31. return $this->_createToken($params);
  32. }
  33. public function revokeAccessToken($accessToken)
  34. {
  35. $params = ['token' => $accessToken];
  36. $response = $this->_http->post('/oauth/revoke_access_token', $params);
  37. return $this->_verifyGatewayResponse($response);
  38. }
  39. private function _createToken($params)
  40. {
  41. $params = ['credentials' => $params];
  42. $response = $this->_http->post('/oauth/access_tokens', $params);
  43. return $this->_verifyGatewayResponse($response);
  44. }
  45. private function _verifyGatewayResponse($response)
  46. {
  47. if (isset($response['credentials'])) {
  48. $result = new Result\Successful(
  49. OAuthCredentials::factory($response['credentials'])
  50. );
  51. return $this->_mapSuccess($result);
  52. } else if (isset($response['result'])) {
  53. $result = new Result\Successful(
  54. OAuthResult::factory($response['result'])
  55. );
  56. return $this->_mapAccessTokenRevokeSuccess($result);
  57. } else if (isset($response['apiErrorResponse'])) {
  58. $result = new Result\Error($response['apiErrorResponse']);
  59. return $this->_mapError($result);
  60. } else {
  61. throw new Exception\Unexpected(
  62. "Expected credentials or apiErrorResponse"
  63. );
  64. }
  65. }
  66. public function _mapError($result)
  67. {
  68. $error = $result->errors->deepAll()[0];
  69. if ($error->code == Error\Codes::OAUTH_INVALID_GRANT) {
  70. $result->error = 'invalid_grant';
  71. } else if ($error->code == Error\Codes::OAUTH_INVALID_CREDENTIALS) {
  72. $result->error = 'invalid_credentials';
  73. } else if ($error->code == Error\Codes::OAUTH_INVALID_SCOPE) {
  74. $result->error = 'invalid_scope';
  75. }
  76. $result->errorDescription = explode(': ', $error->message)[1];
  77. return $result;
  78. }
  79. public function _mapAccessTokenRevokeSuccess($result)
  80. {
  81. $result->revocationResult = $result->success;
  82. return $result;
  83. }
  84. public function _mapSuccess($result)
  85. {
  86. $credentials = $result->credentials;
  87. $result->accessToken = $credentials->accessToken;
  88. $result->refreshToken = $credentials->refreshToken;
  89. $result->tokenType = $credentials->tokenType;
  90. $result->expiresAt = $credentials->expiresAt;
  91. return $result;
  92. }
  93. public function connectUrl($params = [])
  94. {
  95. $query = Util::camelCaseToDelimiterArray($params, '_');
  96. $query['client_id'] = $this->_config->getClientId();
  97. $queryString = preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($query));
  98. return $this->_config->baseUrl() . '/oauth/connect?' . $queryString;
  99. }
  100. /**
  101. * @deprecated since version 3.26.1
  102. */
  103. public function computeSignature($url)
  104. {
  105. $key = hash('sha256', $this->_config->getClientSecret(), true);
  106. return hash_hmac('sha256', $url, $key);
  107. }
  108. }
  109. class_alias('Braintree\OAuthGateway', 'Braintree_OAuthGateway');