TrustRoot.php

Go to the documentation of this file.
00001 <?php
00015 require_once 'Auth/OpenID/Discover.php';
00016 
00023 define('Auth_OpenID___TLDs',
00024        '/\.(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia' .
00025        '|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br' .
00026        '|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co' .
00027        '|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg' .
00028        '|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl' .
00029        '|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie' .
00030        '|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh' .
00031        '|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly' .
00032        '|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt' .
00033        '|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no' .
00034        '|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt' .
00035        '|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl' .
00036        '|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm' .
00037        '|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve' .
00038        '|vg|vi|vn|vu|wf|ws|xn--0zwm56d|xn--11b5bs3a9aj6g' .
00039        '|xn--80akhbyknj4f|xn--9t4b11yi5a|xn--deba0ad|xn--g6w251d' .
00040        '|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--jxalpdlp' .
00041        '|xn--kgbechtv|xn--zckzah|ye|yt|yu|za|zm|zw)\.?$/');
00042 
00043 define('Auth_OpenID___HostSegmentRe',
00044        "/^(?:[-a-zA-Z0-9!$&'\\(\\)\\*+,;=._~]|%[a-zA-Z0-9]{2})*$/");
00045 
00049 class Auth_OpenID_TrustRoot {
00050     /*
00051      * Return a discovery URL for this realm.
00052      *
00053      * Return null if the realm could not be parsed or was not valid.
00054      *
00055      * @param return_to The relying party return URL of the OpenID
00056      * authentication request
00057      *
00058      * @return The URL upon which relying party discovery should be
00059      * run in order to verify the return_to URL
00060      */
00061     function buildDiscoveryURL($realm)
00062     {
00063         $parsed = Auth_OpenID_TrustRoot::_parse($realm);
00064 
00065         if ($parsed === false) {
00066             return false;
00067         }
00068 
00069         if ($parsed['wildcard']) {
00070             // Use "www." in place of the star
00071             if ($parsed['host'][0] != '.') {
00072                 return false;
00073             }
00074 
00075             $www_domain = 'www' . $parsed['host'];
00076 
00077             return sprintf('%s://%s%s', $parsed['scheme'],
00078                            $www_domain, $parsed['path']);
00079         } else {
00080             return $parsed['unparsed'];
00081         }
00082     }
00083 
00096     function _parse($trust_root)
00097     {
00098         $trust_root = Auth_OpenID_urinorm($trust_root);
00099         if ($trust_root === null) {
00100             return false;
00101         }
00102 
00103         if (preg_match("/:\/\/[^:]+(:\d+){2,}(\/|$)/", $trust_root)) {
00104             return false;
00105         }
00106 
00107         $parts = @parse_url($trust_root);
00108         if ($parts === false) {
00109             return false;
00110         }
00111 
00112         $required_parts = array('scheme', 'host');
00113         $forbidden_parts = array('user', 'pass', 'fragment');
00114         $keys = array_keys($parts);
00115         if (array_intersect($keys, $required_parts) != $required_parts) {
00116             return false;
00117         }
00118 
00119         if (array_intersect($keys, $forbidden_parts) != array()) {
00120             return false;
00121         }
00122 
00123         if (!preg_match(Auth_OpenID___HostSegmentRe, $parts['host'])) {
00124             return false;
00125         }
00126 
00127         $scheme = strtolower($parts['scheme']);
00128         $allowed_schemes = array('http', 'https');
00129         if (!in_array($scheme, $allowed_schemes)) {
00130             return false;
00131         }
00132         $parts['scheme'] = $scheme;
00133 
00134         $host = strtolower($parts['host']);
00135         $hostparts = explode('*', $host);
00136         switch (count($hostparts)) {
00137         case 1:
00138             $parts['wildcard'] = false;
00139             break;
00140         case 2:
00141             if ($hostparts[0] ||
00142                 ($hostparts[1] && substr($hostparts[1], 0, 1) != '.')) {
00143                 return false;
00144             }
00145             $host = $hostparts[1];
00146             $parts['wildcard'] = true;
00147             break;
00148         default:
00149             return false;
00150         }
00151         if (strpos($host, ':') !== false) {
00152             return false;
00153         }
00154 
00155         $parts['host'] = $host;
00156 
00157         if (isset($parts['path'])) {
00158             $path = strtolower($parts['path']);
00159             if (substr($path, 0, 1) != '/') {
00160                 return false;
00161             }
00162         } else {
00163             $path = '/';
00164         }
00165 
00166         $parts['path'] = $path;
00167         if (!isset($parts['port'])) {
00168             $parts['port'] = false;
00169         }
00170 
00171 
00172         $parts['unparsed'] = $trust_root;
00173 
00174         return $parts;
00175     }
00176 
00202     function isSane($trust_root)
00203     {
00204         $parts = Auth_OpenID_TrustRoot::_parse($trust_root);
00205         if ($parts === false) {
00206             return false;
00207         }
00208 
00209         // Localhost is a special case
00210         if ($parts['host'] == 'localhost') {
00211             return true;
00212         }
00213         
00214         $host_parts = explode('.', $parts['host']);
00215         if ($parts['wildcard']) {
00216             // Remove the empty string from the beginning of the array
00217             array_shift($host_parts);
00218         }
00219 
00220         if ($host_parts && !$host_parts[count($host_parts) - 1]) {
00221             array_pop($host_parts);
00222         }
00223 
00224         if (!$host_parts) {
00225             return false;
00226         }
00227 
00228         // Don't allow adjacent dots
00229         if (in_array('', $host_parts, true)) {
00230             return false;
00231         }
00232 
00233         // Get the top-level domain of the host. If it is not a valid TLD,
00234         // it's not sane.
00235         preg_match(Auth_OpenID___TLDs, $parts['host'], $matches);
00236         if (!$matches) {
00237             return false;
00238         }
00239         $tld = $matches[1];
00240 
00241         if (count($host_parts) == 1) {
00242             return false;
00243         }
00244 
00245         if ($parts['wildcard']) {
00246             // It's a 2-letter tld with a short second to last segment
00247             // so there needs to be more than two segments specified
00248             // (e.g. *.co.uk is insane)
00249             $second_level = $host_parts[count($host_parts) - 2];
00250             if (strlen($tld) == 2 && strlen($second_level) <= 3) {
00251                 return count($host_parts) > 2;
00252             }
00253         }
00254 
00255         return true;
00256     }
00257 
00272     function match($trust_root, $url)
00273     {
00274         $trust_root_parsed = Auth_OpenID_TrustRoot::_parse($trust_root);
00275         $url_parsed = Auth_OpenID_TrustRoot::_parse($url);
00276         if (!$trust_root_parsed || !$url_parsed) {
00277             return false;
00278         }
00279 
00280         // Check hosts matching
00281         if ($url_parsed['wildcard']) {
00282             return false;
00283         }
00284         if ($trust_root_parsed['wildcard']) {
00285             $host_tail = $trust_root_parsed['host'];
00286             $host = $url_parsed['host'];
00287             if ($host_tail &&
00288                 substr($host, -(strlen($host_tail))) != $host_tail &&
00289                 substr($host_tail, 1) != $host) {
00290                 return false;
00291             }
00292         } else {
00293             if ($trust_root_parsed['host'] != $url_parsed['host']) {
00294                 return false;
00295             }
00296         }
00297 
00298         // Check path and query matching
00299         $base_path = $trust_root_parsed['path'];
00300         $path = $url_parsed['path'];
00301         if (!isset($trust_root_parsed['query'])) {
00302             if ($base_path != $path) {
00303                 if (substr($path, 0, strlen($base_path)) != $base_path) {
00304                     return false;
00305                 }
00306                 if (substr($base_path, strlen($base_path) - 1, 1) != '/' &&
00307                     substr($path, strlen($base_path), 1) != '/') {
00308                     return false;
00309                 }
00310             }
00311         } else {
00312             $base_query = $trust_root_parsed['query'];
00313             $query = @$url_parsed['query'];
00314             $qplus = substr($query, 0, strlen($base_query) + 1);
00315             $bqplus = $base_query . '&';
00316             if ($base_path != $path ||
00317                 ($base_query != $query && $qplus != $bqplus)) {
00318                 return false;
00319             }
00320         }
00321 
00322         // The port and scheme need to match exactly
00323         return ($trust_root_parsed['scheme'] == $url_parsed['scheme'] &&
00324                 $url_parsed['port'] === $trust_root_parsed['port']);
00325     }
00326 }
00327 
00328 /*
00329  * If the endpoint is a relying party OpenID return_to endpoint,
00330  * return the endpoint URL. Otherwise, return None.
00331  *
00332  * This function is intended to be used as a filter for the Yadis
00333  * filtering interface.
00334  *
00335  * @see: C{L{openid.yadis.services}}
00336  * @see: C{L{openid.yadis.filters}}
00337  *
00338  * @param endpoint: An XRDS BasicServiceEndpoint, as returned by
00339  * performing Yadis dicovery.
00340  *
00341  * @returns: The endpoint URL or None if the endpoint is not a
00342  * relying party endpoint.
00343  */
00344 function filter_extractReturnURL(&$endpoint)
00345 {
00346     if ($endpoint->matchTypes(array(Auth_OpenID_RP_RETURN_TO_URL_TYPE))) {
00347         return $endpoint;
00348     } else {
00349         return null;
00350     }
00351 }
00352 
00353 function &Auth_OpenID_extractReturnURL(&$endpoint_list)
00354 {
00355     $result = array();
00356 
00357     foreach ($endpoint_list as $endpoint) {
00358         if (filter_extractReturnURL($endpoint)) {
00359             $result[] = $endpoint;
00360         }
00361     }
00362 
00363     return $result;
00364 }
00365 
00366 /*
00367  * Is the return_to URL under one of the supplied allowed return_to
00368  * URLs?
00369  */
00370 function Auth_OpenID_returnToMatches($allowed_return_to_urls, $return_to)
00371 {
00372     foreach ($allowed_return_to_urls as $allowed_return_to) {
00373         // A return_to pattern works the same as a realm, except that
00374         // it's not allowed to use a wildcard. We'll model this by
00375         // parsing it as a realm, and not trying to match it if it has
00376         // a wildcard.
00377 
00378         $return_realm = Auth_OpenID_TrustRoot::_parse($allowed_return_to);
00379         if (// Parses as a trust root
00380             ($return_realm !== false) &&
00381             // Does not have a wildcard
00382             (!$return_realm['wildcard']) &&
00383             // Matches the return_to that we passed in with it
00384             (Auth_OpenID_TrustRoot::match($allowed_return_to, $return_to))) {
00385             return true;
00386         }
00387     }
00388 
00389     // No URL in the list matched
00390     return false;
00391 }
00392 
00393 /*
00394  * Given a relying party discovery URL return a list of return_to
00395  * URLs.
00396  */
00397 function Auth_OpenID_getAllowedReturnURLs($relying_party_url, &$fetcher,
00398               $discover_function=null)
00399 {
00400     if ($discover_function === null) {
00401         $discover_function = array('Auth_Yadis_Yadis', 'discover');
00402     }
00403 
00404     $xrds_parse_cb = array('Auth_OpenID_ServiceEndpoint', 'fromXRDS');
00405 
00406     list($rp_url_after_redirects, $endpoints) =
00407         Auth_Yadis_getServiceEndpoints($relying_party_url, $xrds_parse_cb,
00408                                        $discover_function, $fetcher);
00409 
00410     if ($rp_url_after_redirects != $relying_party_url) {
00411         // Verification caused a redirect
00412         return false;
00413     }
00414 
00415     call_user_func_array($discover_function,
00416                          array($relying_party_url, $fetcher));
00417 
00418     $return_to_urls = array();
00419     $matching_endpoints = Auth_OpenID_extractReturnURL($endpoints);
00420 
00421     foreach ($matching_endpoints as $e) {
00422         $return_to_urls[] = $e->server_url;
00423     }
00424 
00425     return $return_to_urls;
00426 }
00427 
00428 /*
00429  * Verify that a return_to URL is valid for the given realm.
00430  *
00431  * This function builds a discovery URL, performs Yadis discovery on
00432  * it, makes sure that the URL does not redirect, parses out the
00433  * return_to URLs, and finally checks to see if the current return_to
00434  * URL matches the return_to.
00435  *
00436  * @return true if the return_to URL is valid for the realm
00437  */
00438 function Auth_OpenID_verifyReturnTo($realm_str, $return_to, &$fetcher,
00439               $_vrfy='Auth_OpenID_getAllowedReturnURLs')
00440 {
00441     $disco_url = Auth_OpenID_TrustRoot::buildDiscoveryURL($realm_str);
00442 
00443     if ($disco_url === false) {
00444         return false;
00445     }
00446 
00447     $allowable_urls = call_user_func_array($_vrfy,
00448                            array($disco_url, &$fetcher));
00449 
00450     // The realm_str could not be parsed.
00451     if ($allowable_urls === false) {
00452         return false;
00453     }
00454 
00455     if (Auth_OpenID_returnToMatches($allowable_urls, $return_to)) {
00456         return true;
00457     } else {
00458         return false;
00459     }
00460 }
00461 
00462 ?>

Generated on Thu Feb 19 15:02:21 2009 for OXID eShop CE by  doxygen 1.5.5