PAPE.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. require_once "Auth/OpenID/PAPE.php";
  3. require_once "Auth/OpenID/Message.php";
  4. require_once "Auth/OpenID/Server.php";
  5. class PapeRequestTestCase extends PHPUnit_Framework_TestCase {
  6. function setUp()
  7. {
  8. $this->req = new Auth_OpenID_PAPE_Request();
  9. }
  10. function test_construct()
  11. {
  12. $this->assertEquals(array(), $this->req->preferred_auth_policies);
  13. $this->assertEquals(null, $this->req->max_auth_age);
  14. $this->assertEquals('pape', $this->req->ns_alias);
  15. $req2 = new Auth_OpenID_PAPE_Request(array(PAPE_AUTH_MULTI_FACTOR), 1000);
  16. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $req2->preferred_auth_policies);
  17. $this->assertEquals(1000, $req2->max_auth_age);
  18. }
  19. function test_add_policy_uri()
  20. {
  21. $this->assertEquals(array(), $this->req->preferred_auth_policies);
  22. $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
  23. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $this->req->preferred_auth_policies);
  24. $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
  25. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $this->req->preferred_auth_policies);
  26. $this->req->addPolicyURI(PAPE_AUTH_PHISHING_RESISTANT);
  27. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT),
  28. $this->req->preferred_auth_policies);
  29. $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
  30. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT),
  31. $this->req->preferred_auth_policies);
  32. }
  33. function test_getExtensionArgs() {
  34. $this->assertEquals(array('preferred_auth_policies' => ''), $this->req->getExtensionArgs());
  35. $this->req->addPolicyURI('http://uri');
  36. $this->assertEquals(array('preferred_auth_policies' => 'http://uri'), $this->req->getExtensionArgs());
  37. $this->req->addPolicyURI('http://zig');
  38. $this->assertEquals(array('preferred_auth_policies' => 'http://uri http://zig'), $this->req->getExtensionArgs());
  39. $this->req->max_auth_age = 789;
  40. $this->assertEquals(array('preferred_auth_policies' => 'http://uri http://zig', 'max_auth_age' => '789'), $this->req->getExtensionArgs());
  41. }
  42. function test_parseExtensionArgs() {
  43. $args = array('preferred_auth_policies' => 'http://foo http://bar',
  44. 'max_auth_age' => '9');
  45. $this->req->parseExtensionArgs($args);
  46. $this->assertEquals(9, $this->req->max_auth_age);
  47. $this->assertEquals(array('http://foo','http://bar'), $this->req->preferred_auth_policies);
  48. }
  49. function test_parseExtensionArgs_empty() {
  50. $this->req->parseExtensionArgs(array());
  51. $this->assertEquals(null, $this->req->max_auth_age);
  52. $this->assertEquals(array(), $this->req->preferred_auth_policies);
  53. }
  54. function test_fromOpenIDRequest() {
  55. $openid_req_msg = Auth_OpenID_Message::fromOpenIDArgs(array(
  56. 'mode' => 'checkid_setup',
  57. 'ns' => Auth_OpenID_OPENID2_NS,
  58. 'ns.pape' => Auth_OpenID_PAPE_NS_URI,
  59. 'pape.preferred_auth_policies' => implode(' ', array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT)),
  60. 'pape.max_auth_age' => '5476'
  61. ));
  62. $oid_req = new Auth_OpenID_Request();
  63. $oid_req->message = $openid_req_msg;
  64. $req = Auth_OpenID_PAPE_Request::fromOpenIDRequest($oid_req);
  65. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT), $req->preferred_auth_policies);
  66. $this->assertEquals(5476, $req->max_auth_age);
  67. }
  68. function test_fromOpenIDRequest_no_pape() {
  69. $message = new Auth_OpenID_Message();
  70. $openid_req = new Auth_OpenID_Request();
  71. $openid_req->message = $message;
  72. $pape_req = Auth_OpenID_PAPE_Request::fromOpenIDRequest($openid_req);
  73. $this->assertTrue($pape_req === null);
  74. }
  75. function test_preferred_types() {
  76. $this->req->addPolicyURI(PAPE_AUTH_PHISHING_RESISTANT);
  77. $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
  78. $pt = $this->req->preferredTypes(array(PAPE_AUTH_MULTI_FACTOR,
  79. PAPE_AUTH_MULTI_FACTOR_PHYSICAL));
  80. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $pt);
  81. }
  82. }
  83. class PAPE_DummySuccessResponse {
  84. function PAPE_DummySuccessResponse($message, $signed_stuff)
  85. {
  86. $this->message = $message;
  87. $this->signed_stuff = $signed_stuff;
  88. }
  89. function getSignedNS($ns_uri)
  90. {
  91. return $this->signed_stuff;
  92. }
  93. }
  94. class PapeResponseTestCase extends PHPUnit_Framework_TestCase {
  95. function setUp() {
  96. $this->req = new Auth_OpenID_PAPE_Response();
  97. }
  98. function test_construct() {
  99. $this->assertEquals(array(), $this->req->auth_policies);
  100. $this->assertEquals(null, $this->req->auth_time);
  101. $this->assertEquals('pape', $this->req->ns_alias);
  102. $this->assertEquals(null, $this->req->nist_auth_level);
  103. $req2 = new Auth_OpenID_PAPE_Response(array(PAPE_AUTH_MULTI_FACTOR),
  104. '2001-01-01T04:05:23Z',
  105. 3);
  106. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $req2->auth_policies);
  107. $this->assertEquals('2001-01-01T04:05:23Z', $req2->auth_time);
  108. $this->assertEquals(3, $req2->nist_auth_level);
  109. }
  110. function test_add_policy_uri() {
  111. $this->assertEquals(array(), $this->req->auth_policies);
  112. $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
  113. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $this->req->auth_policies);
  114. $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
  115. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $this->req->auth_policies);
  116. $this->req->addPolicyURI(PAPE_AUTH_PHISHING_RESISTANT);
  117. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT), $this->req->auth_policies);
  118. $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
  119. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT), $this->req->auth_policies);
  120. }
  121. function test_getExtensionArgs() {
  122. $this->assertEquals(array('auth_policies' => 'none'), $this->req->getExtensionArgs());
  123. $this->req->addPolicyURI('http://uri');
  124. $this->assertEquals(array('auth_policies' => 'http://uri'), $this->req->getExtensionArgs());
  125. $this->req->addPolicyURI('http://zig');
  126. $this->assertEquals(array('auth_policies' => 'http://uri http://zig'), $this->req->getExtensionArgs());
  127. $this->req->auth_time = '2008-03-02T12:34:56Z';
  128. $this->assertEquals(array('auth_policies' => 'http://uri http://zig', 'auth_time' => '2008-03-02T12:34:56Z'), $this->req->getExtensionArgs());
  129. $this->req->nist_auth_level = 3;
  130. $this->assertEquals(array('auth_policies' => 'http://uri http://zig', 'auth_time' => '2008-03-02T12:34:56Z', 'nist_auth_level' => '3'), $this->req->getExtensionArgs());
  131. }
  132. function test_getExtensionArgs_error_auth_age() {
  133. $this->req->auth_time = "foo2008-03-02T12:34:56Z";
  134. $this->assertEquals(false, $this->req->getExtensionArgs());
  135. $this->req->auth_time = "2008-03-02T12:34:56Zbar";
  136. $this->assertEquals(false, $this->req->getExtensionArgs());
  137. }
  138. function test_getExtensionArgs_error_nist_auth_level() {
  139. $this->req->nist_auth_level = "high as a kite";
  140. $this->assertEquals(false, $this->req->getExtensionArgs());
  141. $this->req->nist_auth_level = 5;
  142. $this->assertEquals(false, $this->req->getExtensionArgs());
  143. $this->req->nist_auth_level = -1;
  144. $this->assertEquals(false, $this->req->getExtensionArgs());
  145. }
  146. function test_parseExtensionArgs() {
  147. $args = array('auth_policies' => 'http://foo http://bar',
  148. 'auth_time' => '2008-03-02T12:34:56Z');
  149. $this->req->parseExtensionArgs($args);
  150. $this->assertEquals('2008-03-02T12:34:56Z', $this->req->auth_time);
  151. $this->assertEquals(array('http://foo','http://bar'), $this->req->auth_policies);
  152. }
  153. function test_parseExtensionArgs_empty() {
  154. $this->req->parseExtensionArgs(array());
  155. $this->assertEquals(null, $this->req->auth_time);
  156. $this->assertEquals(array(), $this->req->auth_policies);
  157. }
  158. function test_parseExtensionArgs_strict_bogus1() {
  159. $args = array('auth_policies' => 'http://foo http://bar',
  160. 'auth_time' => 'yesterday');
  161. $this->assertEquals(false, $this->req->parseExtensionArgs($args, true));
  162. }
  163. function test_parseExtensionArgs_strict_bogus2() {
  164. $args = array('auth_policies' => 'http://foo http://bar',
  165. 'auth_time' => '63',
  166. 'nist_auth_level' => 'some');
  167. $this->assertEquals(false, $this->req->parseExtensionArgs($args, true));
  168. }
  169. function test_parseExtensionArgs_strict_good() {
  170. $args = array('auth_policies' => 'http://foo http://bar',
  171. 'auth_time' => '2008-03-02T12:34:56Z',
  172. 'nist_auth_level' => '0');
  173. $this->req->parseExtensionArgs($args, true);
  174. $this->assertEquals(array('http://foo','http://bar'), $this->req->auth_policies);
  175. $this->assertEquals('2008-03-02T12:34:56Z', $this->req->auth_time);
  176. $this->assertEquals(0, $this->req->nist_auth_level);
  177. }
  178. function test_parseExtensionArgs_nostrict_bogus() {
  179. $args = array('auth_policies' => 'http://foo http://bar',
  180. 'auth_time' => 'the other day',
  181. 'nist_auth_level' => 'some');
  182. $this->req->parseExtensionArgs($args);
  183. $this->assertEquals(array('http://foo','http://bar'), $this->req->auth_policies);
  184. $this->assertEquals(null, $this->req->auth_time);
  185. $this->assertEquals(null, $this->req->nist_auth_level);
  186. }
  187. function test_fromSuccessResponse() {
  188. $openid_req_msg = Auth_OpenID_Message::fromOpenIDArgs(array(
  189. 'mode' => 'id_res',
  190. 'ns' => Auth_OpenID_OPENID2_NS,
  191. 'ns.pape' => Auth_OpenID_PAPE_NS_URI,
  192. 'auth_policies' => implode(' ', array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT)),
  193. 'auth_time' => '2008-03-02T12:34:56Z'
  194. ));
  195. $signed_stuff = array(
  196. 'auth_policies' => implode(' ', array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT)),
  197. 'auth_time' => '2008-03-02T12:34:56Z'
  198. );
  199. $oid_req = new PAPE_DummySuccessResponse($openid_req_msg, $signed_stuff);
  200. $req = Auth_OpenID_PAPE_Response::fromSuccessResponse($oid_req);
  201. $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT), $req->auth_policies);
  202. $this->assertEquals('2008-03-02T12:34:56Z', $req->auth_time);
  203. }
  204. }
  205. class Tests_Auth_OpenID_PAPE extends PHPUnit_Framework_TestSuite {
  206. function getName() {
  207. return "Tests_Auth_OpenID_PAPE";
  208. }
  209. function Tests_Auth_OpenID_PAPE() {
  210. $this->addTestSuite('PapeRequestTestCase');
  211. $this->addTestSuite('PapeResponseTestCase');
  212. }
  213. }