AuthSubTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. require_once 'Zend/Gdata/AuthSub.php';
  23. require_once 'Zend/Gdata/HttpClient.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Gdata
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Gdata
  31. */
  32. class Zend_Gdata_AuthSubTest extends PHPUnit_Framework_TestCase
  33. {
  34. public function setUp()
  35. {
  36. }
  37. public function testNormalGetAuthSubTokenUri()
  38. {
  39. $uri = Zend_Gdata_AuthSub::getAuthSubTokenUri(
  40. 'http://www.example.com/foo.php', //next
  41. 'http://www.google.com/calendar/feeds', //scope
  42. 0, //secure
  43. 1); //session
  44. // Note: the scope here is not encoded. It should be encoded,
  45. // but the method getAuthSubTokenUri calls urldecode($scope).
  46. // This currently works (no reported bugs) as web browsers will
  47. // handle the encoding in most cases.
  48. $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);
  49. }
  50. public function testGetAuthSubTokenUriModifiedBase()
  51. {
  52. $uri = Zend_Gdata_AuthSub::getAuthSubTokenUri(
  53. 'http://www.example.com/foo.php', //next
  54. 'http://www.google.com/calendar/feeds', //scope
  55. 0, //secure
  56. 1, //session
  57. 'http://www.otherauthservice.com/accounts/AuthSubRequest');
  58. // Note: the scope here is not encoded. It should be encoded,
  59. // but the method getAuthSubTokenUri calls urldecode($scope).
  60. // This currently works (no reported bugs) as web browsers will
  61. // handle the encoding in most cases.
  62. $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);
  63. }
  64. public function testSecureAuthSubSigning()
  65. {
  66. if (!extension_loaded('openssl')) {
  67. $this->markTestSkipped('The openssl extension is not available');
  68. } else {
  69. $c = new Zend_Gdata_HttpClient();
  70. $c->setAuthSubPrivateKeyFile("Zend/Gdata/_files/RsaKey.pem",
  71. null, true);
  72. $c->setAuthSubToken('abcdefg');
  73. $requestData = $c->filterHttpRequest('POST',
  74. 'http://www.example.com/feed',
  75. array(),
  76. 'foo bar',
  77. 'text/plain');
  78. $authHeaderCheckPassed = false;
  79. $headers = $requestData['headers'];
  80. foreach ($headers as $headerName => $headerValue) {
  81. if (strtolower($headerName) == 'authorization') {
  82. preg_match('/data="([^"]*)"/', $headerValue, $matches);
  83. $dataToSign = $matches[1];
  84. preg_match('/sig="([^"]*)"/', $headerValue, $matches);
  85. $sig = $matches[1];
  86. if (function_exists('openssl_verify')) {
  87. $fp = fopen('Zend/Gdata/_files/RsaCert.pem', 'r', true);
  88. $cert = '';
  89. while (!feof($fp)) {
  90. $cert .= fread($fp, 8192);
  91. }
  92. fclose($fp);
  93. $pubkeyid = openssl_get_publickey($cert);
  94. $verified = openssl_verify($dataToSign,
  95. base64_decode($sig), $pubkeyid);
  96. $this->assertEquals(
  97. 1, $verified,
  98. 'The generated signature was unable ' .
  99. 'to be verified.');
  100. $authHeaderCheckPassed = true;
  101. }
  102. }
  103. }
  104. $this->assertEquals(true, $authHeaderCheckPassed,
  105. 'Auth header not found for sig verification.');
  106. }
  107. }
  108. }