Signature.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace OAuth\OAuth1\Signature;
  3. use OAuth\Common\Consumer\CredentialsInterface;
  4. use OAuth\Common\Http\Uri\UriInterface;
  5. use OAuth\OAuth1\Signature\Exception\UnsupportedHashAlgorithmException;
  6. class Signature implements SignatureInterface
  7. {
  8. /**
  9. * @var Credentials
  10. */
  11. protected $credentials;
  12. /**
  13. * @var string
  14. */
  15. protected $algorithm;
  16. /**
  17. * @var string
  18. */
  19. protected $tokenSecret = null;
  20. /**
  21. * @param CredentialsInterface $credentials
  22. */
  23. public function __construct(CredentialsInterface $credentials)
  24. {
  25. $this->credentials = $credentials;
  26. }
  27. /**
  28. * @param string $algorithm
  29. */
  30. public function setHashingAlgorithm($algorithm)
  31. {
  32. $this->algorithm = $algorithm;
  33. }
  34. /**
  35. * @param string $token
  36. */
  37. public function setTokenSecret($token)
  38. {
  39. $this->tokenSecret = $token;
  40. }
  41. /**
  42. * @param UriInterface $uri
  43. * @param array $params
  44. * @param string $method
  45. *
  46. * @return string
  47. */
  48. public function getSignature(UriInterface $uri, array $params, $method = 'POST')
  49. {
  50. parse_str($uri->getQuery(), $queryStringData);
  51. foreach (array_merge($queryStringData, $params) as $key => $value) {
  52. $signatureData[rawurlencode($key)] = rawurlencode($value);
  53. }
  54. ksort($signatureData);
  55. // determine base uri
  56. $baseUri = $uri->getScheme() . '://' . $uri->getRawAuthority();
  57. if ('/' === $uri->getPath()) {
  58. $baseUri .= $uri->hasExplicitTrailingHostSlash() ? '/' : '';
  59. } else {
  60. $baseUri .= $uri->getPath();
  61. }
  62. $baseString = strtoupper($method) . '&';
  63. $baseString .= rawurlencode($baseUri) . '&';
  64. $baseString .= rawurlencode($this->buildSignatureDataString($signatureData));
  65. return base64_encode($this->hash($baseString));
  66. }
  67. /**
  68. * @param array $signatureData
  69. *
  70. * @return string
  71. */
  72. protected function buildSignatureDataString(array $signatureData)
  73. {
  74. $signatureString = '';
  75. $delimiter = '';
  76. foreach ($signatureData as $key => $value) {
  77. $signatureString .= $delimiter . $key . '=' . $value;
  78. $delimiter = '&';
  79. }
  80. return $signatureString;
  81. }
  82. /**
  83. * @return string
  84. */
  85. protected function getSigningKey()
  86. {
  87. $signingKey = rawurlencode($this->credentials->getConsumerSecret()) . '&';
  88. if ($this->tokenSecret !== null) {
  89. $signingKey .= rawurlencode($this->tokenSecret);
  90. }
  91. return $signingKey;
  92. }
  93. /**
  94. * @param string $data
  95. *
  96. * @return string
  97. *
  98. * @throws UnsupportedHashAlgorithmException
  99. */
  100. protected function hash($data)
  101. {
  102. switch (strtoupper($this->algorithm)) {
  103. case 'HMAC-SHA1':
  104. return hash_hmac('sha1', $data, $this->getSigningKey(), true);
  105. default:
  106. throw new UnsupportedHashAlgorithmException(
  107. 'Unsupported hashing algorithm (' . $this->algorithm . ') used.'
  108. );
  109. }
  110. }
  111. }