OauthInterface.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Oauth;
  7. /**
  8. * OauthInterface provides methods consistent with implementing a 2-legged OAuth authentication mechanism. Methods
  9. * include creating a request token, getting an access token, and performing certain validations on tokens and
  10. * token requests. A method is also included for generating an OAuth header that can be used in an HTTP request.
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. interface OauthInterface
  16. {
  17. /**#@+
  18. * OAuth result statuses
  19. */
  20. const ERR_OK = 0;
  21. const ERR_VERSION_REJECTED = 1;
  22. const ERR_PARAMETER_ABSENT = 2;
  23. const ERR_PARAMETER_REJECTED = 3;
  24. const ERR_TIMESTAMP_REFUSED = 4;
  25. const ERR_NONCE_USED = 5;
  26. const ERR_SIGNATURE_METHOD_REJECTED = 6;
  27. const ERR_SIGNATURE_INVALID = 7;
  28. const ERR_CONSUMER_KEY_REJECTED = 8;
  29. const ERR_TOKEN_USED = 9;
  30. const ERR_TOKEN_EXPIRED = 10;
  31. const ERR_TOKEN_REVOKED = 11;
  32. const ERR_TOKEN_REJECTED = 12;
  33. const ERR_VERIFIER_INVALID = 13;
  34. const ERR_PERMISSION_UNKNOWN = 14;
  35. const ERR_PERMISSION_DENIED = 15;
  36. const ERR_METHOD_NOT_ALLOWED = 16;
  37. const ERR_CONSUMER_KEY_INVALID = 17;
  38. /**#@-*/
  39. /**#@+
  40. * Signature Methods
  41. */
  42. const SIGNATURE_SHA1 = 'HMAC-SHA1';
  43. const SIGNATURE_SHA256 = 'HMAC-SHA256';
  44. /**#@-*/
  45. /**
  46. * Issue a pre-authorization request token to the caller.
  47. *
  48. * @param array $params - Array containing parameters necessary for requesting Request Token.
  49. * <pre>
  50. * array (
  51. * 'oauth_version' => '1.0',
  52. * 'oauth_signature_method' => 'HMAC-SHA1',
  53. * 'oauth_nonce' => 'rI7PSWxTZRHWU3R',
  54. * 'oauth_timestamp' => '1377183099',
  55. * 'oauth_consumer_key' => 'a6aa81cc3e65e2960a4879392445e718',
  56. * 'oauth_signature' => 'VNg4mhFlXk7%2FvsxMqqUd5DWIj9s%3D'
  57. * )
  58. * </pre>
  59. * @param string $requestUrl - The request Url.
  60. * @param string $httpMethod - (default: 'POST')
  61. * @return array - The request token/secret pair.
  62. * <pre>
  63. * array (
  64. * 'oauth_token' => 'gshsjkndtyhwjhdbutfgbsnhtrequikf',
  65. * 'oauth_token_secret' => 'gshsjkndtyhwjhdbutfgbsnhtrequikf'
  66. * )
  67. * </pre>
  68. * @throws \Magento\Framework\Oauth\Exception - Validation errors.
  69. */
  70. public function getRequestToken($params, $requestUrl, $httpMethod = 'POST');
  71. /**
  72. * Get access token for a pre-authorized request token.
  73. *
  74. * @param array $params - Array containing parameters necessary for requesting Access Token.
  75. * <pre>
  76. * array (
  77. * 'oauth_version' => '1.0',
  78. * 'oauth_signature_method' => 'HMAC-SHA1',
  79. * 'oauth_token' => 'a6aa81cc3e65e2960a487939244sssss',
  80. * 'oauth_nonce' => 'rI7PSWxTZRHWU3R',
  81. * 'oauth_timestamp' => '1377183099',
  82. * 'oauth_consumer_key' => 'a6aa81cc3e65e2960a4879392445e718',
  83. * 'oauth_signature' => 'VNg4mhFlXk7%2FvsxMqqUd5DWIj9s%3D',
  84. * 'oauth_verifier' => 'a6aa81cc3e65e2960a487939244vvvvv'
  85. * )
  86. * </pre>
  87. * @param string $requestUrl - The request Url.
  88. * @param string $httpMethod - (default: 'POST')
  89. * @return array - The access token/secret pair.
  90. * <pre>
  91. * array (
  92. * 'oauth_token' => 'gshsjkndtyhwjhdbutfgbsnhtrequikf',
  93. * 'oauth_token_secret' => 'gshsjkndtyhwjhdbutfgbsnhtrequikf'
  94. * )
  95. * </pre>
  96. * @throws \Magento\Framework\Oauth\Exception
  97. */
  98. public function getAccessToken($params, $requestUrl, $httpMethod = 'POST');
  99. /**
  100. * Validate an access token request.
  101. *
  102. * @param array $params - Array containing parameters necessary for validating Access Token.
  103. * <pre>
  104. * array (
  105. * 'oauth_version' => '1.0',
  106. * 'oauth_signature_method' => 'HMAC-SHA1',
  107. * 'oauth_token' => 'a6aa81cc3e65e2960a487939244sssss',
  108. * 'oauth_nonce' => 'rI7PSWxTZRHWU3R',
  109. * 'oauth_timestamp' => '1377183099',
  110. * 'oauth_consumer_key' => 'a6aa81cc3e65e2960a4879392445e718',
  111. * 'oauth_signature' => 'VNg4mhFlXk7%2FvsxMqqUd5DWIj9s%3D'
  112. * )
  113. * </pre>
  114. * @param string $requestUrl - The request Url.
  115. * @param string $httpMethod - (default: 'POST')
  116. * @return int Consumer ID.
  117. * @throws \Magento\Framework\Oauth\Exception - Validation errors.
  118. */
  119. public function validateAccessTokenRequest($params, $requestUrl, $httpMethod = 'POST');
  120. /**
  121. * Validate an access token string.
  122. *
  123. * @param string $accessToken - The access token.
  124. * @return int - Consumer ID if the access token is valid.
  125. * @throws \Magento\Framework\Oauth\Exception - Validation errors.
  126. */
  127. public function validateAccessToken($accessToken);
  128. /**
  129. * Build the Oauth authorization header for an authenticated API request
  130. *
  131. * @param array $params - Array containing parameters to build the Oauth HTTP Authorization header
  132. * <pre>
  133. * array (
  134. * 'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85d',
  135. * 'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
  136. * 'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
  137. * 'oauth_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
  138. * 'custom_param1' => 'foo',
  139. * 'custom_param2' => 'bar'
  140. * );
  141. * </pre>
  142. * @param string $requestUrl e.g 'http://www.example.com/endpoint'
  143. * @param string $signatureMethod (default: 'HMAC-SHA1')
  144. * @param string $httpMethod (default: 'POST')
  145. * @return string
  146. * <pre>
  147. * OAuth oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_nonce="5X1aWR2qzf2uFm1",
  148. * oauth_timestamp="1381930661", oauth_consumer_key="34edf957ef88492f0a32eb7e1731e85d",
  149. * oauth_token="7c0709f789e1f38a17aa4b9a28e1b06c", oauth_signature="agVxK0epXOOeQK4%2Bc7UAqUXoAok%3D"
  150. * <pre>
  151. * @throws \Magento\Framework\Oauth\Exception
  152. */
  153. public function buildAuthorizationHeader(
  154. $params,
  155. $requestUrl,
  156. $signatureMethod = self::SIGNATURE_SHA1,
  157. $httpMethod = 'POST'
  158. );
  159. }