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 Janrain, Inc.
12 * @license http://www.gnu.org/copyleft/lesser.html LGPL
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 '/\.(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)$/');
38 * A wrapper for trust-root related functions
40 class Auth_OpenID_TrustRoot {
42 * Return a discovery URL for this realm.
44 * Return null if the realm could not be parsed or was not valid.
46 * @param return_to The relying party return URL of the OpenID
47 * authentication request
49 * @return The URL upon which relying party discovery should be
50 * run in order to verify the return_to URL
52 function buildDiscoveryURL($realm)
54 $parsed = Auth_OpenID_TrustRoot::_parse($realm);
56 if ($parsed === false) {
60 if ($parsed['wildcard']) {
61 // Use "www." in place of the star
62 if ($parsed['host'][0] != '.') {
66 $www_domain = 'www' . $parsed['host'];
68 return sprintf('%s://%s%s', $parsed['scheme'],
69 $www_domain, $parsed['path']);
71 return $parsed['unparsed'];
76 * Parse a URL into its trust_root parts.
82 * @param string $trust_root The url to parse
84 * @return mixed $parsed Either an associative array of trust root
85 * parts or false if parsing failed.
87 function _parse($trust_root)
89 $parts = @parse_url($trust_root);
90 if ($parts === false) {
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) {
100 if (array_intersect($keys, $forbidden_parts) != array()) {
104 // Return false if the original trust root value has more than
105 // one port specification.
106 if (preg_match("/:\/\/[^:]+(:\d+){2,}(\/|$)/", $trust_root)) {
110 $scheme = strtolower($parts['scheme']);
111 $allowed_schemes = array('http', 'https');
112 if (!in_array($scheme, $allowed_schemes)) {
115 $parts['scheme'] = $scheme;
117 $host = strtolower($parts['host']);
118 $hostparts = explode('*', $host);
119 switch (count($hostparts)) {
121 $parts['wildcard'] = false;
125 ($hostparts[1] && substr($hostparts[1], 0, 1) != '.')) {
128 $host = $hostparts[1];
129 $parts['wildcard'] = true;
134 if (strpos($host, ':') !== false) {
138 $parts['host'] = $host;
140 if (isset($parts['path'])) {
141 $path = strtolower($parts['path']);
146 $parts['path'] = $path;
147 if (!isset($parts['port'])) {
148 $parts['port'] = false;
151 $parts['unparsed'] = $trust_root;
157 * Is this trust root sane?
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.
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.
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
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.
178 * @param string $trust_root The trust root to check
179 * @return bool $sanity Whether the trust root looks OK
181 function isSane($trust_root)
183 $parts = Auth_OpenID_TrustRoot::_parse($trust_root);
184 if ($parts === false) {
188 // Localhost is a special case
189 if ($parts['host'] == 'localhost') {
193 // Get the top-level domain of the host. If it is not a valid TLD,
195 preg_match(Auth_OpenID___TLDs, $parts['host'], $matches);
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']) {
208 if (strlen($tld) == 2) {
218 * Does this URL match the given trust root?
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.
224 * @param string $trust_root The trust root to match against
226 * @param string $url The URL to check
228 * @return bool $matches Whether the URL matches against the
231 function match($trust_root, $url)
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) {
239 // Check hosts matching
240 if ($url_parsed['wildcard']) {
243 if ($trust_root_parsed['wildcard']) {
244 $host_tail = $trust_root_parsed['host'];
245 $host = $url_parsed['host'];
247 substr($host, -(strlen($host_tail))) != $host_tail &&
248 substr($host_tail, 1) != $host) {
252 if ($trust_root_parsed['host'] != $url_parsed['host']) {
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) {
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)) {
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']);
282 * If the endpoint is a relying party OpenID return_to endpoint,
283 * return the endpoint URL. Otherwise, return None.
285 * This function is intended to be used as a filter for the Yadis
286 * filtering interface.
288 * @see: C{L{openid.yadis.services}}
289 * @see: C{L{openid.yadis.filters}}
291 * @param endpoint: An XRDS BasicServiceEndpoint, as returned by
292 * performing Yadis dicovery.
294 * @returns: The endpoint URL or None if the endpoint is not a
295 * relying party endpoint.
297 function filter_extractReturnURL(&$endpoint)
299 if ($endpoint->matchTypes(array(Auth_OpenID_RP_RETURN_TO_URL_TYPE))) {
306 function &Auth_OpenID_extractReturnURL(&$endpoint_list)
310 foreach ($endpoint_list as $endpoint) {
311 if (filter_extractReturnURL($endpoint)) {
312 $result[] = $endpoint;
320 * Is the return_to URL under one of the supplied allowed return_to
323 function Auth_OpenID_returnToMatches($allowed_return_to_urls, $return_to)
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
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))) {
342 // No URL in the list matched
347 * Given a relying party discovery URL return a list of return_to
350 function Auth_OpenID_getAllowedReturnURLs($relying_party_url, &$fetcher,
351 $discover_function=null)
353 if ($discover_function === null) {
354 $discover_function = array('Auth_Yadis_Yadis', 'discover');
357 list($rp_url_after_redirects, $endpoints) =
358 Auth_OpenID_discoverWithYadis($relying_party_url,
360 'Auth_OpenID_extractReturnURL',
363 if ($rp_url_after_redirects != $relying_party_url) {
364 // Verification caused a redirect
368 $return_to_urls = array();
369 foreach ($endpoints as $e) {
370 $return_to_urls[] = $e->server_url;
373 return $return_to_urls;
377 * Verify that a return_to URL is valid for the given realm.
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.
384 * @return true if the return_to URL is valid for the realm
386 function Auth_OpenID_verifyReturnTo($realm_str, $return_to, &$fetcher,
387 $_vrfy='Auth_OpenID_getAllowedReturnURLs')
389 $disco_url = Auth_OpenID_TrustRoot::buildDiscoveryURL($realm_str);
391 if ($disco_url === false) {
395 $allowable_urls = call_user_func_array($_vrfy,
396 array($disco_url, &$fetcher));
398 // The realm_str could not be parsed.
399 if ($allowable_urls === false) {
403 if (Auth_OpenID_returnToMatches($allowable_urls, $return_to)) {