aboutsummaryrefslogtreecommitdiff
path: root/models/openid-php-openid-782224d/Tests/Auth/OpenID/PAPE.php
blob: d71036d19f660f965ad80d530b739f6842d742a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php

require_once "Auth/OpenID/PAPE.php";
require_once "Auth/OpenID/Message.php";
require_once "Auth/OpenID/Server.php";

class PapeRequestTestCase extends PHPUnit_Framework_TestCase {
    function setUp()
    {
        $this->req = new Auth_OpenID_PAPE_Request();
    }

    function test_construct()
    {
      $this->assertEquals(array(), $this->req->preferred_auth_policies);
      $this->assertEquals(null, $this->req->max_auth_age);
      $this->assertEquals('pape', $this->req->ns_alias);

      $req2 = new Auth_OpenID_PAPE_Request(array(PAPE_AUTH_MULTI_FACTOR), 1000);
      $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $req2->preferred_auth_policies);
      $this->assertEquals(1000, $req2->max_auth_age);
    }

    function test_add_policy_uri()
    {
      $this->assertEquals(array(), $this->req->preferred_auth_policies);
      $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
      $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $this->req->preferred_auth_policies);
      $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
      $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $this->req->preferred_auth_policies);
      $this->req->addPolicyURI(PAPE_AUTH_PHISHING_RESISTANT);
      $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT),
                          $this->req->preferred_auth_policies);
      $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
      $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT),
                          $this->req->preferred_auth_policies);
    }

    function test_getExtensionArgs() {
      $this->assertEquals(array('preferred_auth_policies' => ''), $this->req->getExtensionArgs());
      $this->req->addPolicyURI('http://uri');
      $this->assertEquals(array('preferred_auth_policies' => 'http://uri'), $this->req->getExtensionArgs());
      $this->req->addPolicyURI('http://zig');
      $this->assertEquals(array('preferred_auth_policies' => 'http://uri http://zig'), $this->req->getExtensionArgs());
      $this->req->max_auth_age = 789;
      $this->assertEquals(array('preferred_auth_policies' => 'http://uri http://zig', 'max_auth_age' => '789'), $this->req->getExtensionArgs());
    }

    function test_parseExtensionArgs() {
      $args = array('preferred_auth_policies' => 'http://foo http://bar',
                    'max_auth_age' => '9');
      $this->req->parseExtensionArgs($args);
      $this->assertEquals(9, $this->req->max_auth_age);
      $this->assertEquals(array('http://foo','http://bar'), $this->req->preferred_auth_policies);
    }

    function test_parseExtensionArgs_empty() {
      $this->req->parseExtensionArgs(array());
      $this->assertEquals(null, $this->req->max_auth_age);
      $this->assertEquals(array(), $this->req->preferred_auth_policies);
    }

    function test_fromOpenIDRequest() {
      $openid_req_msg = Auth_OpenID_Message::fromOpenIDArgs(array(
          'mode' => 'checkid_setup',
          'ns' => Auth_OpenID_OPENID2_NS,
          'ns.pape' => Auth_OpenID_PAPE_NS_URI,
          'pape.preferred_auth_policies' => implode(' ', array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT)),
          'pape.max_auth_age' => '5476'
          ));
      $oid_req = new Auth_OpenID_Request();
      $oid_req->message = $openid_req_msg;
      $req = Auth_OpenID_PAPE_Request::fromOpenIDRequest($oid_req);
      $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT), $req->preferred_auth_policies);
      $this->assertEquals(5476, $req->max_auth_age);
    }

    function test_fromOpenIDRequest_no_pape() {
      $message = new Auth_OpenID_Message();
      $openid_req = new Auth_OpenID_Request();
      $openid_req->message = $message;
      $pape_req = Auth_OpenID_PAPE_Request::fromOpenIDRequest($openid_req);
      $this->assertTrue($pape_req === null);
    }

    function test_preferred_types() {
        $this->req->addPolicyURI(PAPE_AUTH_PHISHING_RESISTANT);
        $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
        $pt = $this->req->preferredTypes(array(PAPE_AUTH_MULTI_FACTOR,
                                               PAPE_AUTH_MULTI_FACTOR_PHYSICAL));
        $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $pt);
    }
}

class PAPE_DummySuccessResponse {
  function PAPE_DummySuccessResponse($message, $signed_stuff)
  {
    $this->message = $message;
    $this->signed_stuff = $signed_stuff;
  }

  function getSignedNS($ns_uri)
  {
    return $this->signed_stuff;
  }
}

class PapeResponseTestCase extends PHPUnit_Framework_TestCase {
  function setUp() {
    $this->req = new Auth_OpenID_PAPE_Response();
  }

  function test_construct() {
    $this->assertEquals(array(), $this->req->auth_policies);
    $this->assertEquals(null, $this->req->auth_time);
    $this->assertEquals('pape', $this->req->ns_alias);
    $this->assertEquals(null, $this->req->nist_auth_level);

    $req2 = new Auth_OpenID_PAPE_Response(array(PAPE_AUTH_MULTI_FACTOR),
                                          '2001-01-01T04:05:23Z',
                                          3);
    $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $req2->auth_policies);
    $this->assertEquals('2001-01-01T04:05:23Z', $req2->auth_time);
    $this->assertEquals(3, $req2->nist_auth_level);
  }

  function test_add_policy_uri() {
    $this->assertEquals(array(), $this->req->auth_policies);
    $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
    $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $this->req->auth_policies);
    $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
    $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR), $this->req->auth_policies);
    $this->req->addPolicyURI(PAPE_AUTH_PHISHING_RESISTANT);
    $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT), $this->req->auth_policies);
    $this->req->addPolicyURI(PAPE_AUTH_MULTI_FACTOR);
    $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT), $this->req->auth_policies);
  }

  function test_getExtensionArgs() {
    $this->assertEquals(array('auth_policies' => 'none'), $this->req->getExtensionArgs());
    $this->req->addPolicyURI('http://uri');
    $this->assertEquals(array('auth_policies' => 'http://uri'), $this->req->getExtensionArgs());
    $this->req->addPolicyURI('http://zig');
    $this->assertEquals(array('auth_policies' => 'http://uri http://zig'), $this->req->getExtensionArgs());
    $this->req->auth_time = '2008-03-02T12:34:56Z';
    $this->assertEquals(array('auth_policies' => 'http://uri http://zig', 'auth_time' => '2008-03-02T12:34:56Z'), $this->req->getExtensionArgs());
    $this->req->nist_auth_level = 3;
    $this->assertEquals(array('auth_policies' => 'http://uri http://zig', 'auth_time' => '2008-03-02T12:34:56Z', 'nist_auth_level' => '3'), $this->req->getExtensionArgs());
  }

  function test_getExtensionArgs_error_auth_age() {
    $this->req->auth_time = "foo2008-03-02T12:34:56Z";
    $this->assertEquals(false, $this->req->getExtensionArgs());
    $this->req->auth_time = "2008-03-02T12:34:56Zbar";
    $this->assertEquals(false, $this->req->getExtensionArgs());
  }

  function test_getExtensionArgs_error_nist_auth_level() {
    $this->req->nist_auth_level = "high as a kite";
    $this->assertEquals(false, $this->req->getExtensionArgs());
    $this->req->nist_auth_level = 5;
    $this->assertEquals(false, $this->req->getExtensionArgs());
    $this->req->nist_auth_level = -1;
    $this->assertEquals(false, $this->req->getExtensionArgs());
  }

  function test_parseExtensionArgs() {
    $args = array('auth_policies' => 'http://foo http://bar',
                  'auth_time' => '2008-03-02T12:34:56Z');
    $this->req->parseExtensionArgs($args);
    $this->assertEquals('2008-03-02T12:34:56Z', $this->req->auth_time);
    $this->assertEquals(array('http://foo','http://bar'), $this->req->auth_policies);
  }

  function test_parseExtensionArgs_empty() {
    $this->req->parseExtensionArgs(array());
    $this->assertEquals(null, $this->req->auth_time);
    $this->assertEquals(array(), $this->req->auth_policies);
  }

  function test_parseExtensionArgs_strict_bogus1() {
    $args = array('auth_policies' => 'http://foo http://bar',
                  'auth_time' => 'yesterday');
    $this->assertEquals(false, $this->req->parseExtensionArgs($args, true));
  }

  function test_parseExtensionArgs_strict_bogus2() {
    $args = array('auth_policies' => 'http://foo http://bar',
                  'auth_time' => '63',
                  'nist_auth_level' => 'some');
    $this->assertEquals(false, $this->req->parseExtensionArgs($args, true));
  }

  function test_parseExtensionArgs_strict_good() {
    $args = array('auth_policies' => 'http://foo http://bar',
                  'auth_time' => '2008-03-02T12:34:56Z',
                  'nist_auth_level' => '0');
    $this->req->parseExtensionArgs($args, true);
    $this->assertEquals(array('http://foo','http://bar'), $this->req->auth_policies);
    $this->assertEquals('2008-03-02T12:34:56Z', $this->req->auth_time);
    $this->assertEquals(0, $this->req->nist_auth_level);
  }

  function test_parseExtensionArgs_nostrict_bogus() {
    $args = array('auth_policies' => 'http://foo http://bar',
                  'auth_time' => 'the other day',
                  'nist_auth_level' => 'some');
    $this->req->parseExtensionArgs($args);
    $this->assertEquals(array('http://foo','http://bar'), $this->req->auth_policies);
    $this->assertEquals(null, $this->req->auth_time);
    $this->assertEquals(null, $this->req->nist_auth_level);
  }

  function test_fromSuccessResponse() {
    $openid_req_msg = Auth_OpenID_Message::fromOpenIDArgs(array(
          'mode' => 'id_res',
          'ns' => Auth_OpenID_OPENID2_NS,
          'ns.pape' => Auth_OpenID_PAPE_NS_URI,
          'auth_policies' => implode(' ', array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT)),
          'auth_time' => '2008-03-02T12:34:56Z'
          ));
    $signed_stuff = array(
          'auth_policies' => implode(' ', array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT)),
          'auth_time' => '2008-03-02T12:34:56Z'
        );
    $oid_req = new PAPE_DummySuccessResponse($openid_req_msg, $signed_stuff);
    $req = Auth_OpenID_PAPE_Response::fromSuccessResponse($oid_req);
    $this->assertEquals(array(PAPE_AUTH_MULTI_FACTOR, PAPE_AUTH_PHISHING_RESISTANT), $req->auth_policies);
    $this->assertEquals('2008-03-02T12:34:56Z', $req->auth_time);
  }
}

class Tests_Auth_OpenID_PAPE extends PHPUnit_Framework_TestSuite {
  function getName() {
    return "Tests_Auth_OpenID_PAPE";
  }

  function Tests_Auth_OpenID_PAPE() {
    $this->addTestSuite('PapeRequestTestCase');
    $this->addTestSuite('PapeResponseTestCase');
  }
}