/src/OAuth/Server/Signature/Utility.php

https://github.com/boekkooi/Oauth_Server · PHP · 105 lines · 42 code · 9 blank · 54 comment · 8 complexity · 9b5237e63745a2095cf319336d8bb525 MD5 · raw file

  1. <?php
  2. namespace OAuth\Server\Signature;
  3. /**
  4. * @package OAuth_Server
  5. * @author Warnar Boekkooi
  6. *
  7. * The MIT License
  8. *
  9. * Copyright (c) 2010 Warnar Boekkooi
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the \"Software\"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. */
  29. class Utility {
  30. /**
  31. * Validate if the given signature method is a supported.
  32. *
  33. * @param string $method The signature method.
  34. * @return bool TRUE if the method is supported else FALSE.
  35. */
  36. public function isValidSignatureMethod($method)
  37. {
  38. // Validate given method
  39. if (empty($method) || !in_array(strtoupper($method), array(
  40. 'HMAC-SHA1', 'HMAC-SHA256', 'PLAINTEXT'
  41. ))) {
  42. return false;
  43. }
  44. return true;
  45. }
  46. /**
  47. * Get the class and hash algorithm belonging to the given signature method.
  48. *
  49. * @throws Zend_Oauth_Exception When a unsupported signature method is provided.
  50. * @param string $method The signature method.
  51. * @return array An array (0 => method class name, 1 => hash algorithm)
  52. */
  53. protected function getSignatureInfo($method) {
  54. if (empty($method) || !$this->isValidSignatureMethod($method)) {
  55. throw new \RuntimeException('Unsupported signature method: '
  56. . $method
  57. . '. Supported are HMAC-SHA1, PLAINTEXT and HMAC-SHA256');
  58. }
  59. // Signature class
  60. $hashAlgorithm = null;
  61. $signatureMethod = strtoupper($method);
  62. $parts = explode('-', $signatureMethod);
  63. if (count($parts) > 1) {
  64. $className = 'OAuth\Server\Signature\\' . ucfirst(strtolower($parts[0]));
  65. $hashAlgorithm = $parts[1];
  66. } else {
  67. $className = 'OAuth\Server\Signature\\' . ucfirst(strtolower($signatureMethod));
  68. }
  69. return array($className, $hashAlgorithm);
  70. }
  71. /**
  72. * Verify a requests signature.
  73. *
  74. * @param string $requestUrl
  75. * @param array $params
  76. * @param string $signatureMethod
  77. * @param string $consumerSecret
  78. * @return boolean
  79. */
  80. public function verifySignature($requestUrl, array $params, $consumerSecret, $tokenSecret = null)
  81. {
  82. // Get the response method
  83. $responseMethod = \Zend_Oauth::POST;
  84. // Get the signature class and algorithm based on the given oauth_signature_method
  85. list($className, $hashAlgorithm) = $this->getSignatureInfo($params['oauth_signature_method']);
  86. // Create the signature class and verify the send oauth_signature
  87. $signatureObject = new $className($consumerSecret, $tokenSecret, $hashAlgorithm);
  88. $rtn = $signatureObject->verify($params['oauth_signature'], $params, $responseMethod, $requestUrl);
  89. // Some consumers don't agree with the optional part of oauth_version so let's add this extra check in case the signature fails
  90. if (!isset($params['oauth_version']) && $rtn === false) {
  91. $params['oauth_version'] = '1.0';
  92. $rtn = $signatureObject->verify($params['oauth_signature'], $params, $responseMethod, $requestUrl);
  93. }
  94. return $rtn;
  95. }
  96. }