AuthSubTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Gdata/AuthSub.php';
  22. require_once 'Zend/Gdata/HttpClient.php';
  23. /**
  24. * @package Zend_Gdata
  25. * @subpackage UnitTests
  26. */
  27. class Zend_Gdata_AuthSubTest extends PHPUnit_Framework_TestCase
  28. {
  29. public function setUp()
  30. {
  31. }
  32. public function testNormalGetAuthSubTokenUri()
  33. {
  34. $uri = Zend_Gdata_AuthSub::getAuthSubTokenUri(
  35. 'http://www.example.com/foo.php', //next
  36. 'http://www.google.com/calendar/feeds', //scope
  37. 0, //secure
  38. 1); //session
  39. // Note: the scope here is not encoded. It should be encoded,
  40. // but the method getAuthSubTokenUri calls urldecode($scope).
  41. // This currently works (no reported bugs) as web browsers will
  42. // handle the encoding in most cases.
  43. $this->assertEquals('https://www.google.com/accounts/AuthSubRequest?next=http%3A%2F%2Fwww.example.com%2Ffoo.php&scope=http://www.google.com/calendar/feeds&secure=0&session=1', $uri);
  44. }
  45. public function testGetAuthSubTokenUriModifiedBase()
  46. {
  47. $uri = Zend_Gdata_AuthSub::getAuthSubTokenUri(
  48. 'http://www.example.com/foo.php', //next
  49. 'http://www.google.com/calendar/feeds', //scope
  50. 0, //secure
  51. 1, //session
  52. 'http://www.otherauthservice.com/accounts/AuthSubRequest');
  53. // Note: the scope here is not encoded. It should be encoded,
  54. // but the method getAuthSubTokenUri calls urldecode($scope).
  55. // This currently works (no reported bugs) as web browsers will
  56. // handle the encoding in most cases.
  57. $this->assertEquals('http://www.otherauthservice.com/accounts/AuthSubRequest?next=http%3A%2F%2Fwww.example.com%2Ffoo.php&scope=http://www.google.com/calendar/feeds&secure=0&session=1', $uri);
  58. }
  59. public function testSecureAuthSubSigning()
  60. {
  61. if (!extension_loaded('openssl')) {
  62. $this->markTestSkipped('The openssl extension is not available');
  63. } else {
  64. $c = new Zend_Gdata_HttpClient();
  65. $c->setAuthSubPrivateKeyFile("Zend/Gdata/_files/RsaKey.pem",
  66. null, true);
  67. $c->setAuthSubToken('abcdefg');
  68. $requestData = $c->filterHttpRequest('POST',
  69. 'http://www.example.com/feed',
  70. array(),
  71. 'foo bar',
  72. 'text/plain');
  73. $authHeaderCheckPassed = false;
  74. $headers = $requestData['headers'];
  75. foreach ($headers as $headerName => $headerValue) {
  76. if (strtolower($headerName) == 'authorization') {
  77. preg_match('/data="([^"]*)"/', $headerValue, $matches);
  78. $dataToSign = $matches[1];
  79. preg_match('/sig="([^"]*)"/', $headerValue, $matches);
  80. $sig = $matches[1];
  81. if (function_exists('openssl_verify')) {
  82. $fp = fopen('Zend/Gdata/_files/RsaCert.pem', 'r', true);
  83. $cert = '';
  84. while (!feof($fp)) {
  85. $cert .= fread($fp, 8192);
  86. }
  87. fclose($fp);
  88. $pubkeyid = openssl_get_publickey($cert);
  89. $verified = openssl_verify($dataToSign,
  90. base64_decode($sig), $pubkeyid);
  91. $this->assertEquals(
  92. 1, $verified,
  93. 'The generated signature was unable ' .
  94. 'to be verified.');
  95. $authHeaderCheckPassed = true;
  96. }
  97. }
  98. }
  99. $this->assertEquals(true, $authHeaderCheckPassed,
  100. 'Auth header not found for sig verification.');
  101. }
  102. }
  103. }