OSDN Git Service

NP_OpenId v1.0.0
[nucleus-jp/nucleus-plugins.git] / trunk / NP_OpenId / sharedlibs / Auth / OpenID / TrustRoot.php
1 <?php
2 /**
3  * Functions for dealing with OpenID trust roots
4  *
5  * PHP versions 4 and 5
6  *
7  * LICENSE: See the COPYING file included in this distribution.
8  *
9  * @package OpenID
10  * @author JanRain, Inc. <openid@janrain.com>
11  * @copyright 2005 Janrain, Inc.
12  * @license http://www.gnu.org/copyleft/lesser.html LGPL
13  */
14
15 require_once 'Auth/OpenID/Discover.php';
16
17 /**
18  * A regular expression that matches a domain ending in a top-level domains.
19  * Used in checking trust roots for sanity.
20  *
21  * @access private
22  */
23 define('Auth_OpenID___TLDs',
24        '/\.(com|edu|gov|int|mil|net|org|biz|info|name|museum|coop|aero|ac|' .
25        'ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|' .
26        'bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|' .
27        'cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|' .
28        'fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|' .
29        'gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|' .
30        'ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|' .
31        'ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|' .
32        'nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|' .
33        'ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|' .
34        'so|sr|st|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|' .
35        'ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$/');
36
37 /**
38  * A wrapper for trust-root related functions
39  */
40 class Auth_OpenID_TrustRoot {
41     /*
42      * Return a discovery URL for this realm.
43      *
44      * Return null if the realm could not be parsed or was not valid.
45      *
46      * @param return_to The relying party return URL of the OpenID
47      * authentication request
48      *
49      * @return The URL upon which relying party discovery should be
50      * run in order to verify the return_to URL
51      */
52     function buildDiscoveryURL($realm)
53     {
54         $parsed = Auth_OpenID_TrustRoot::_parse($realm);
55
56         if ($parsed === false) {
57             return false;
58         }
59
60         if ($parsed['wildcard']) {
61             // Use "www." in place of the star
62             if ($parsed['host'][0] != '.') {
63                 return false;
64             }
65
66             $www_domain = 'www' . $parsed['host'];
67
68             return sprintf('%s://%s%s', $parsed['scheme'],
69                            $www_domain, $parsed['path']);
70         } else {
71             return $parsed['unparsed'];
72         }
73     }
74
75     /**
76      * Parse a URL into its trust_root parts.
77      *
78      * @static
79      *
80      * @access private
81      *
82      * @param string $trust_root The url to parse
83      *
84      * @return mixed $parsed Either an associative array of trust root
85      * parts or false if parsing failed.
86      */
87     function _parse($trust_root)
88     {
89         $parts = @parse_url($trust_root);
90         if ($parts === false) {
91             return false;
92         }
93         $required_parts = array('scheme', 'host');
94         $forbidden_parts = array('user', 'pass', 'fragment');
95         $keys = array_keys($parts);
96         if (array_intersect($keys, $required_parts) != $required_parts) {
97             return false;
98         }
99
100         if (array_intersect($keys, $forbidden_parts) != array()) {
101             return false;
102         }
103
104         // Return false if the original trust root value has more than
105         // one port specification.
106         if (preg_match("/:\/\/[^:]+(:\d+){2,}(\/|$)/", $trust_root)) {
107             return false;
108         }
109
110         $scheme = strtolower($parts['scheme']);
111         $allowed_schemes = array('http', 'https');
112         if (!in_array($scheme, $allowed_schemes)) {
113             return false;
114         }
115         $parts['scheme'] = $scheme;
116
117         $host = strtolower($parts['host']);
118         $hostparts = explode('*', $host);
119         switch (count($hostparts)) {
120         case 1:
121             $parts['wildcard'] = false;
122             break;
123         case 2:
124             if ($hostparts[0] ||
125                 ($hostparts[1] && substr($hostparts[1], 0, 1) != '.')) {
126                 return false;
127             }
128             $host = $hostparts[1];
129             $parts['wildcard'] = true;
130             break;
131         default:
132             return false;
133         }
134         if (strpos($host, ':') !== false) {
135             return false;
136         }
137
138         $parts['host'] = $host;
139
140         if (isset($parts['path'])) {
141             $path = strtolower($parts['path']);
142         } else {
143             $path = '/';
144         }
145
146         $parts['path'] = $path;
147         if (!isset($parts['port'])) {
148             $parts['port'] = false;
149         }
150
151         $parts['unparsed'] = $trust_root;
152
153         return $parts;
154     }
155
156     /**
157      * Is this trust root sane?
158      *
159      * A trust root is sane if it is syntactically valid and it has a
160      * reasonable domain name. Specifically, the domain name must be
161      * more than one level below a standard TLD or more than two
162      * levels below a two-letter tld.
163      *
164      * For example, '*.com' is not a sane trust root, but '*.foo.com'
165      * is.  '*.co.uk' is not sane, but '*.bbc.co.uk' is.
166      *
167      * This check is not always correct, but it attempts to err on the
168      * side of marking sane trust roots insane instead of marking
169      * insane trust roots sane. For example, 'kink.fm' is marked as
170      * insane even though it "should" (for some meaning of should) be
171      * marked sane.
172      *
173      * This function should be used when creating OpenID servers to
174      * alert the users of the server when a consumer attempts to get
175      * the user to accept a suspicious trust root.
176      *
177      * @static
178      * @param string $trust_root The trust root to check
179      * @return bool $sanity Whether the trust root looks OK
180      */
181     function isSane($trust_root)
182     {
183         $parts = Auth_OpenID_TrustRoot::_parse($trust_root);
184         if ($parts === false) {
185             return false;
186         }
187
188         // Localhost is a special case
189         if ($parts['host'] == 'localhost') {
190             return true;
191         }
192
193         // Get the top-level domain of the host. If it is not a valid TLD,
194         // it's not sane.
195         preg_match(Auth_OpenID___TLDs, $parts['host'], $matches);
196         if (!$matches) {
197             return false;
198         }
199         $tld = $matches[1];
200
201         // Require at least two levels of specificity for non-country
202         // tlds and three levels for country tlds.
203         $elements = explode('.', $parts['host']);
204         $n = count($elements);
205         if ($parts['wildcard']) {
206             $n -= 1;
207         }
208         if (strlen($tld) == 2) {
209             $n -= 1;
210         }
211         if ($n <= 1) {
212             return false;
213         }
214         return true;
215     }
216
217     /**
218      * Does this URL match the given trust root?
219      *
220      * Return whether the URL falls under the given trust root. This
221      * does not check whether the trust root is sane. If the URL or
222      * trust root do not parse, this function will return false.
223      *
224      * @param string $trust_root The trust root to match against
225      *
226      * @param string $url The URL to check
227      *
228      * @return bool $matches Whether the URL matches against the
229      * trust root
230      */
231     function match($trust_root, $url)
232     {
233         $trust_root_parsed = Auth_OpenID_TrustRoot::_parse($trust_root);
234         $url_parsed = Auth_OpenID_TrustRoot::_parse($url);
235         if (!$trust_root_parsed || !$url_parsed) {
236             return false;
237         }
238
239         // Check hosts matching
240         if ($url_parsed['wildcard']) {
241             return false;
242         }
243         if ($trust_root_parsed['wildcard']) {
244             $host_tail = $trust_root_parsed['host'];
245             $host = $url_parsed['host'];
246             if ($host_tail &&
247                 substr($host, -(strlen($host_tail))) != $host_tail &&
248                 substr($host_tail, 1) != $host) {
249                 return false;
250             }
251         } else {
252             if ($trust_root_parsed['host'] != $url_parsed['host']) {
253                 return false;
254             }
255         }
256
257         // Check path and query matching
258         $base_path = $trust_root_parsed['path'];
259         $path = $url_parsed['path'];
260         if (!isset($trust_root_parsed['query'])) {
261             if (substr($path, 0, strlen($base_path)) != $base_path) {
262                 return false;
263             }
264         } else {
265             $base_query = $trust_root_parsed['query'];
266             $query = @$url_parsed['query'];
267             $qplus = substr($query, 0, strlen($base_query) + 1);
268             $bqplus = $base_query . '&';
269             if ($base_path != $path ||
270                 ($base_query != $query && $qplus != $bqplus)) {
271                 return false;
272             }
273         }
274
275         // The port and scheme need to match exactly
276         return ($trust_root_parsed['scheme'] == $url_parsed['scheme'] &&
277                 $url_parsed['port'] === $trust_root_parsed['port']);
278     }
279 }
280
281 /*
282  * If the endpoint is a relying party OpenID return_to endpoint,
283  * return the endpoint URL. Otherwise, return None.
284  *
285  * This function is intended to be used as a filter for the Yadis
286  * filtering interface.
287  *
288  * @see: C{L{openid.yadis.services}}
289  * @see: C{L{openid.yadis.filters}}
290  *
291  * @param endpoint: An XRDS BasicServiceEndpoint, as returned by
292  * performing Yadis dicovery.
293  *
294  * @returns: The endpoint URL or None if the endpoint is not a
295  * relying party endpoint.
296  */
297 function filter_extractReturnURL(&$endpoint)
298 {
299     if ($endpoint->matchTypes(array(Auth_OpenID_RP_RETURN_TO_URL_TYPE))) {
300         return $endpoint;
301     } else {
302         return null;
303     }
304 }
305
306 function &Auth_OpenID_extractReturnURL(&$endpoint_list)
307 {
308     $result = array();
309
310     foreach ($endpoint_list as $endpoint) {
311         if (filter_extractReturnURL($endpoint)) {
312             $result[] = $endpoint;
313         }
314     }
315
316     return $result;
317 }
318
319 /*
320  * Is the return_to URL under one of the supplied allowed return_to
321  * URLs?
322  */
323 function Auth_OpenID_returnToMatches($allowed_return_to_urls, $return_to)
324 {
325     foreach ($allowed_return_to_urls as $allowed_return_to) {
326         // A return_to pattern works the same as a realm, except that
327         // it's not allowed to use a wildcard. We'll model this by
328         // parsing it as a realm, and not trying to match it if it has
329         // a wildcard.
330
331         $return_realm = Auth_OpenID_TrustRoot::_parse($allowed_return_to);
332         if (// Parses as a trust root
333             ($return_realm !== false) &&
334             // Does not have a wildcard
335             (!$return_realm['wildcard']) &&
336             // Matches the return_to that we passed in with it
337             (Auth_OpenID_TrustRoot::match($allowed_return_to, $return_to))) {
338             return true;
339         }
340     }
341
342     // No URL in the list matched
343     return false;
344 }
345
346 /*
347  * Given a relying party discovery URL return a list of return_to
348  * URLs.
349  */
350 function Auth_OpenID_getAllowedReturnURLs($relying_party_url, &$fetcher,
351               $discover_function=null)
352 {
353     if ($discover_function === null) {
354         $discover_function = array('Auth_Yadis_Yadis', 'discover');
355     }
356
357     list($rp_url_after_redirects, $endpoints) =
358       Auth_OpenID_discoverWithYadis($relying_party_url,
359                                     $fetcher,
360                                     'Auth_OpenID_extractReturnURL',
361                                     $discover_function);
362
363     if ($rp_url_after_redirects != $relying_party_url) {
364         // Verification caused a redirect
365         return false;
366     }
367
368     $return_to_urls = array();
369     foreach ($endpoints as $e) {
370         $return_to_urls[] = $e->server_url;
371     }
372
373     return $return_to_urls;
374 }
375
376 /*
377  * Verify that a return_to URL is valid for the given realm.
378  *
379  * This function builds a discovery URL, performs Yadis discovery on
380  * it, makes sure that the URL does not redirect, parses out the
381  * return_to URLs, and finally checks to see if the current return_to
382  * URL matches the return_to.
383  *
384  * @return true if the return_to URL is valid for the realm
385  */
386 function Auth_OpenID_verifyReturnTo($realm_str, $return_to, &$fetcher,
387               $_vrfy='Auth_OpenID_getAllowedReturnURLs')
388 {
389     $disco_url = Auth_OpenID_TrustRoot::buildDiscoveryURL($realm_str);
390
391     if ($disco_url === false) {
392         return false;
393     }
394
395     $allowable_urls = call_user_func_array($_vrfy,
396                            array($disco_url, &$fetcher));
397
398     // The realm_str could not be parsed.
399     if ($allowable_urls === false) {
400         return false;
401     }
402
403     if (Auth_OpenID_returnToMatches($allowable_urls, $return_to)) {
404         return true;
405     } else {
406         return false;
407     }
408 }
409
410 ?>