AuthSubTest.php 5.4 KB

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