3 * Functions for dealing with OpenID trust roots
7 * LICENSE: See the COPYING file included in this distribution.
10 * @author JanRain, Inc. <openid@janrain.com>
11 * @copyright 2005-2008 Janrain, Inc.
12 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
15 require_once 'Auth/OpenID/Discover.php';
18 * A regular expression that matches a domain ending in a top-level domains.
19 * Used in checking trust roots for sanity.
23 define('Auth_OpenID___TLDs',
24 '/\.(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia' .
25 '|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br' .
26 '|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co' .
27 '|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg' .
28 '|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl' .
29 '|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie' .
30 '|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh' .
31 '|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly' .
32 '|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt' .
33 '|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no' .
34 '|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt' .
35 '|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl' .
36 '|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm' .
37 '|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve' .
38 '|vg|vi|vn|vu|wf|ws|xn--0zwm56d|xn--11b5bs3a9aj6g' .
39 '|xn--80akhbyknj4f|xn--9t4b11yi5a|xn--deba0ad|xn--g6w251d' .
40 '|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--jxalpdlp' .
41 '|xn--kgbechtv|xn--zckzah|ye|yt|yu|za|zm|zw)\.?$/');
43 define('Auth_OpenID___HostSegmentRe',
44 "/^(?:[-a-zA-Z0-9!$&'\\(\\)\\*+,;=._~]|%[a-zA-Z0-9]{2})*$/");
47 * A wrapper for trust-root related functions
49 class Auth_OpenID_TrustRoot {
51 * Return a discovery URL for this realm.
53 * Return null if the realm could not be parsed or was not valid.
55 * @param return_to The relying party return URL of the OpenID
56 * authentication request
58 * @return The URL upon which relying party discovery should be
59 * run in order to verify the return_to URL
61 static function buildDiscoveryURL($realm)
63 $parsed = Auth_OpenID_TrustRoot::_parse($realm);
65 if ($parsed === false) {
69 if ($parsed['wildcard']) {
70 // Use "www." in place of the star
71 if ($parsed['host'][0] != '.') {
75 $www_domain = 'www' . $parsed['host'];
77 return sprintf('%s://%s%s', $parsed['scheme'],
78 $www_domain, $parsed['path']);
80 return $parsed['unparsed'];
85 * Parse a URL into its trust_root parts.
91 * @param string $trust_root The url to parse
93 * @return mixed $parsed Either an associative array of trust root
94 * parts or false if parsing failed.
96 static function _parse($trust_root)
98 $trust_root = Auth_OpenID_urinorm($trust_root);
99 if ($trust_root === null) {
103 if (preg_match("/:\/\/[^:]+(:\d+){2,}(\/|$)/", $trust_root)) {
107 $parts = @parse_url($trust_root);
108 if ($parts === false) {
112 $required_parts = array('scheme', 'host');
113 $forbidden_parts = array('user', 'pass', 'fragment');
114 $keys = array_keys($parts);
115 if (array_intersect($keys, $required_parts) != $required_parts) {
119 if (array_intersect($keys, $forbidden_parts) != array()) {
123 if (!preg_match(Auth_OpenID___HostSegmentRe, $parts['host'])) {
127 $scheme = strtolower($parts['scheme']);
128 $allowed_schemes = array('http', 'https');
129 if (!in_array($scheme, $allowed_schemes)) {
132 $parts['scheme'] = $scheme;
134 $host = strtolower($parts['host']);
135 $hostparts = explode('*', $host);
136 switch (count($hostparts)) {
138 $parts['wildcard'] = false;
142 ($hostparts[1] && substr($hostparts[1], 0, 1) != '.')) {
145 $host = $hostparts[1];
146 $parts['wildcard'] = true;
151 if (strpos($host, ':') !== false) {
155 $parts['host'] = $host;
157 if (isset($parts['path'])) {
158 $path = strtolower($parts['path']);
159 if (substr($path, 0, 1) != '/') {
166 $parts['path'] = $path;
167 if (!isset($parts['port'])) {
168 $parts['port'] = false;
172 $parts['unparsed'] = $trust_root;
178 * Is this trust root sane?
180 * A trust root is sane if it is syntactically valid and it has a
181 * reasonable domain name. Specifically, the domain name must be
182 * more than one level below a standard TLD or more than two
183 * levels below a two-letter tld.
185 * For example, '*.com' is not a sane trust root, but '*.foo.com'
186 * is. '*.co.uk' is not sane, but '*.bbc.co.uk' is.
188 * This check is not always correct, but it attempts to err on the
189 * side of marking sane trust roots insane instead of marking
190 * insane trust roots sane. For example, 'kink.fm' is marked as
191 * insane even though it "should" (for some meaning of should) be
194 * This function should be used when creating OpenID servers to
195 * alert the users of the server when a consumer attempts to get
196 * the user to accept a suspicious trust root.
199 * @param string $trust_root The trust root to check
200 * @return bool $sanity Whether the trust root looks OK
202 static function isSane($trust_root)
204 $parts = Auth_OpenID_TrustRoot::_parse($trust_root);
205 if ($parts === false) {
209 // Localhost is a special case
210 if ($parts['host'] == 'localhost') {
214 $host_parts = explode('.', $parts['host']);
215 if ($parts['wildcard']) {
216 // Remove the empty string from the beginning of the array
217 array_shift($host_parts);
220 if ($host_parts && !$host_parts[count($host_parts) - 1]) {
221 array_pop($host_parts);
228 // Don't allow adjacent dots
229 if (in_array('', $host_parts, true)) {
233 // Get the top-level domain of the host. If it is not a valid TLD,
235 preg_match(Auth_OpenID___TLDs, $parts['host'], $matches);
241 if (count($host_parts) == 1) {
245 if ($parts['wildcard']) {
246 // It's a 2-letter tld with a short second to last segment
247 // so there needs to be more than two segments specified
248 // (e.g. *.co.uk is insane)
249 $second_level = $host_parts[count($host_parts) - 2];
250 if (strlen($tld) == 2 && strlen($second_level) <= 3) {
251 return count($host_parts) > 2;
259 * Does this URL match the given trust root?
261 * Return whether the URL falls under the given trust root. This
262 * does not check whether the trust root is sane. If the URL or
263 * trust root do not parse, this function will return false.
265 * @param string $trust_root The trust root to match against
267 * @param string $url The URL to check
269 * @return bool $matches Whether the URL matches against the
272 static function match($trust_root, $url)
274 $trust_root_parsed = Auth_OpenID_TrustRoot::_parse($trust_root);
275 $url_parsed = Auth_OpenID_TrustRoot::_parse($url);
276 if (!$trust_root_parsed || !$url_parsed) {
280 // Check hosts matching
281 if ($url_parsed['wildcard']) {
284 if ($trust_root_parsed['wildcard']) {
285 $host_tail = $trust_root_parsed['host'];
286 $host = $url_parsed['host'];
288 substr($host, -(strlen($host_tail))) != $host_tail &&
289 substr($host_tail, 1) != $host) {
293 if ($trust_root_parsed['host'] != $url_parsed['host']) {
298 // Check path and query matching
299 $base_path = $trust_root_parsed['path'];
300 $path = $url_parsed['path'];
301 if (!isset($trust_root_parsed['query'])) {
302 if ($base_path != $path) {
303 if (substr($path, 0, strlen($base_path)) != $base_path) {
306 if (substr($base_path, strlen($base_path) - 1, 1) != '/' &&
307 substr($path, strlen($base_path), 1) != '/') {
312 $base_query = $trust_root_parsed['query'];
313 $query = @$url_parsed['query'];
314 $qplus = substr($query, 0, strlen($base_query) + 1);
315 $bqplus = $base_query . '&';
316 if ($base_path != $path ||
317 ($base_query != $query && $qplus != $bqplus)) {
322 // The port and scheme need to match exactly
323 return ($trust_root_parsed['scheme'] == $url_parsed['scheme'] &&
324 $url_parsed['port'] === $trust_root_parsed['port']);
329 * If the endpoint is a relying party OpenID return_to endpoint,
330 * return the endpoint URL. Otherwise, return None.
332 * This function is intended to be used as a filter for the Yadis
333 * filtering interface.
335 * @see: C{L{openid.yadis.services}}
336 * @see: C{L{openid.yadis.filters}}
338 * @param endpoint: An XRDS BasicServiceEndpoint, as returned by
339 * performing Yadis dicovery.
341 * @returns: The endpoint URL or None if the endpoint is not a
342 * relying party endpoint.
344 function filter_extractReturnURL($endpoint)
346 if ($endpoint->matchTypes(array(Auth_OpenID_RP_RETURN_TO_URL_TYPE))) {
353 function &Auth_OpenID_extractReturnURL(&$endpoint_list)
357 foreach ($endpoint_list as $endpoint) {
358 if (filter_extractReturnURL($endpoint)) {
359 $result[] = $endpoint;
367 * Is the return_to URL under one of the supplied allowed return_to
370 function Auth_OpenID_returnToMatches($allowed_return_to_urls, $return_to)
372 foreach ($allowed_return_to_urls as $allowed_return_to) {
373 // A return_to pattern works the same as a realm, except that
374 // it's not allowed to use a wildcard. We'll model this by
375 // parsing it as a realm, and not trying to match it if it has
378 $return_realm = Auth_OpenID_TrustRoot::_parse($allowed_return_to);
379 if (// Parses as a trust root
380 ($return_realm !== false) &&
381 // Does not have a wildcard
382 (!$return_realm['wildcard']) &&
383 // Matches the return_to that we passed in with it
384 (Auth_OpenID_TrustRoot::match($allowed_return_to, $return_to))) {
389 // No URL in the list matched
394 * Given a relying party discovery URL return a list of return_to
397 function Auth_OpenID_getAllowedReturnURLs($relying_party_url, $fetcher,
398 $discover_function=null)
400 if ($discover_function === null) {
401 $discover_function = array('Auth_Yadis_Yadis', 'discover');
404 $xrds_parse_cb = array('Auth_OpenID_ServiceEndpoint', 'consumerFromXRDS');
406 list($rp_url_after_redirects, $endpoints) =
407 Auth_Yadis_getServiceEndpoints($relying_party_url, $xrds_parse_cb,
408 $discover_function, $fetcher);
410 if ($rp_url_after_redirects != $relying_party_url) {
411 // Verification caused a redirect
415 call_user_func_array($discover_function,
416 array($relying_party_url, $fetcher));
418 $return_to_urls = array();
419 $matching_endpoints = Auth_OpenID_extractReturnURL($endpoints);
421 foreach ($matching_endpoints as $e) {
422 $return_to_urls[] = $e->server_url;
425 return $return_to_urls;
429 * Verify that a return_to URL is valid for the given realm.
431 * This function builds a discovery URL, performs Yadis discovery on
432 * it, makes sure that the URL does not redirect, parses out the
433 * return_to URLs, and finally checks to see if the current return_to
434 * URL matches the return_to.
436 * @return true if the return_to URL is valid for the realm
438 function Auth_OpenID_verifyReturnTo($realm_str, $return_to, $fetcher,
439 $_vrfy='Auth_OpenID_getAllowedReturnURLs')
441 $disco_url = Auth_OpenID_TrustRoot::buildDiscoveryURL($realm_str);
443 if ($disco_url === false) {
447 $allowable_urls = call_user_func_array($_vrfy,
448 array($disco_url, $fetcher));
450 // The realm_str could not be parsed.
451 if ($allowable_urls === false) {
455 if (Auth_OpenID_returnToMatches($allowable_urls, $return_to)) {