4 * The OpenID and Yadis discovery implementation for OpenID 1.2.
7 require_once "Auth/OpenID.php";
8 require_once "Auth/OpenID/Parse.php";
9 require_once "Auth/OpenID/Message.php";
10 require_once "Auth/Yadis/XRIRes.php";
11 require_once "Auth/Yadis/Yadis.php";
13 // XML namespace value
14 define('Auth_OpenID_XMLNS_1_0', 'http://openid.net/xmlns/1.0');
16 // Yadis service types
17 define('Auth_OpenID_TYPE_1_2', 'http://openid.net/signon/1.2');
18 define('Auth_OpenID_TYPE_1_1', 'http://openid.net/signon/1.1');
19 define('Auth_OpenID_TYPE_1_0', 'http://openid.net/signon/1.0');
20 define('Auth_OpenID_TYPE_2_0_IDP', 'http://specs.openid.net/auth/2.0/server');
21 define('Auth_OpenID_TYPE_2_0', 'http://specs.openid.net/auth/2.0/signon');
22 define('Auth_OpenID_RP_RETURN_TO_URL_TYPE',
23 'http://specs.openid.net/auth/2.0/return_to');
25 function Auth_OpenID_getOpenIDTypeURIs()
27 return array(Auth_OpenID_TYPE_2_0_IDP,
32 Auth_OpenID_RP_RETURN_TO_URL_TYPE);
36 * Object representing an OpenID service endpoint.
38 class Auth_OpenID_ServiceEndpoint {
39 function Auth_OpenID_ServiceEndpoint()
41 $this->claimed_id = null;
42 $this->server_url = null;
43 $this->type_uris = array();
44 $this->local_id = null;
45 $this->canonicalID = null;
46 $this->used_yadis = false; // whether this came from an XRDS
47 $this->display_identifier = null;
50 function getDisplayIdentifier()
52 if ($this->display_identifier) {
53 return $this->display_identifier;
55 if (! $this->claimed_id) {
56 return $this->claimed_id;
58 $parsed = parse_url($this->claimed_id);
59 $scheme = $parsed['scheme'];
60 $host = $parsed['host'];
61 $path = $parsed['path'];
62 if (array_key_exists('query', $parsed)) {
63 $query = $parsed['query'];
64 $no_frag = "$scheme://$host$path?$query";
66 $no_frag = "$scheme://$host$path";
71 function usesExtension($extension_uri)
73 return in_array($extension_uri, $this->type_uris);
76 function preferredNamespace()
78 if (in_array(Auth_OpenID_TYPE_2_0_IDP, $this->type_uris) ||
79 in_array(Auth_OpenID_TYPE_2_0, $this->type_uris)) {
80 return Auth_OpenID_OPENID2_NS;
82 return Auth_OpenID_OPENID1_NS;
87 * Query this endpoint to see if it has any of the given type
88 * URIs. This is useful for implementing other endpoint classes
89 * that e.g. need to check for the presence of multiple versions
90 * of a single protocol.
92 * @param $type_uris The URIs that you wish to check
94 * @return all types that are in both in type_uris and
97 function matchTypes($type_uris)
100 foreach ($type_uris as $test_uri) {
101 if ($this->supportsType($test_uri)) {
102 $result[] = $test_uri;
109 function supportsType($type_uri)
111 // Does this endpoint support this type?
112 return ((in_array($type_uri, $this->type_uris)) ||
113 (($type_uri == Auth_OpenID_TYPE_2_0) &&
114 $this->isOPIdentifier()));
117 function compatibilityMode()
119 return $this->preferredNamespace() != Auth_OpenID_OPENID2_NS;
122 function isOPIdentifier()
124 return in_array(Auth_OpenID_TYPE_2_0_IDP, $this->type_uris);
127 function fromOPEndpointURL($op_endpoint_url)
129 // Construct an OP-Identifier OpenIDServiceEndpoint object for
130 // a given OP Endpoint URL
131 $obj = new Auth_OpenID_ServiceEndpoint();
132 $obj->server_url = $op_endpoint_url;
133 $obj->type_uris = array(Auth_OpenID_TYPE_2_0_IDP);
137 function parseService($yadis_url, $uri, $type_uris, $service_element)
139 // Set the state of this object based on the contents of the
140 // service element. Return true if successful, false if not
141 // (if findOPLocalIdentifier returns false).
142 $this->type_uris = $type_uris;
143 $this->server_url = $uri;
144 $this->used_yadis = true;
146 if (!$this->isOPIdentifier()) {
147 $this->claimed_id = $yadis_url;
148 $this->local_id = Auth_OpenID_findOPLocalIdentifier(
151 if ($this->local_id === false) {
159 function getLocalID()
161 // Return the identifier that should be sent as the
162 // openid.identity_url parameter to the server.
163 if ($this->local_id === null && $this->canonicalID === null) {
164 return $this->claimed_id;
166 if ($this->local_id) {
167 return $this->local_id;
169 return $this->canonicalID;
175 * Parse the given document as XRDS looking for OpenID services.
177 * @return array of Auth_OpenID_ServiceEndpoint or null if the
178 * document cannot be parsed.
180 function fromXRDS($uri, $xrds_text)
182 $xrds =& Auth_Yadis_XRDS::parseXRDS($xrds_text);
186 $xrds->services(array('filter_MatchesAnyOpenIDType'));
187 return Auth_OpenID_makeOpenIDEndpoints($uri, $yadis_services);
194 * Create endpoints from a DiscoveryResult.
196 * @param discoveryResult Auth_Yadis_DiscoveryResult
197 * @return array of Auth_OpenID_ServiceEndpoint or null if
198 * endpoints cannot be created.
200 function fromDiscoveryResult($discoveryResult)
202 if ($discoveryResult->isXRDS()) {
203 return Auth_OpenID_ServiceEndpoint::fromXRDS(
204 $discoveryResult->normalized_uri,
205 $discoveryResult->response_text);
207 return Auth_OpenID_ServiceEndpoint::fromHTML(
208 $discoveryResult->normalized_uri,
209 $discoveryResult->response_text);
213 function fromHTML($uri, $html)
215 $discovery_types = array(
216 array(Auth_OpenID_TYPE_2_0,
217 'openid2.provider', 'openid2.local_id'),
218 array(Auth_OpenID_TYPE_1_1,
219 'openid.server', 'openid.delegate')
224 foreach ($discovery_types as $triple) {
225 list($type_uri, $server_rel, $delegate_rel) = $triple;
227 $urls = Auth_OpenID_legacy_discover($html, $server_rel,
230 if ($urls === false) {
234 list($delegate_url, $server_url) = $urls;
236 $service = new Auth_OpenID_ServiceEndpoint();
237 $service->claimed_id = $uri;
238 $service->local_id = $delegate_url;
239 $service->server_url = $server_url;
240 $service->type_uris = array($type_uri);
242 $services[] = $service;
250 $x = new Auth_OpenID_ServiceEndpoint();
252 $x->claimed_id = $this->claimed_id;
253 $x->server_url = $this->server_url;
254 $x->type_uris = $this->type_uris;
255 $x->local_id = $this->local_id;
256 $x->canonicalID = $this->canonicalID;
257 $x->used_yadis = $this->used_yadis;
263 function Auth_OpenID_findOPLocalIdentifier($service, $type_uris)
265 // Extract a openid:Delegate value from a Yadis Service element.
266 // If no delegate is found, returns null. Returns false on
267 // discovery failure (when multiple delegate/localID tags have
268 // different values).
270 $service->parser->registerNamespace('openid',
271 Auth_OpenID_XMLNS_1_0);
273 $service->parser->registerNamespace('xrd',
274 Auth_Yadis_XMLNS_XRD_2_0);
276 $parser =& $service->parser;
278 $permitted_tags = array();
280 if (in_array(Auth_OpenID_TYPE_1_1, $type_uris) ||
281 in_array(Auth_OpenID_TYPE_1_0, $type_uris)) {
282 $permitted_tags[] = 'openid:Delegate';
285 if (in_array(Auth_OpenID_TYPE_2_0, $type_uris)) {
286 $permitted_tags[] = 'xrd:LocalID';
291 foreach ($permitted_tags as $tag_name) {
292 $tags = $service->getElements($tag_name);
294 foreach ($tags as $tag) {
295 $content = $parser->content($tag);
297 if ($local_id === null) {
298 $local_id = $content;
299 } else if ($local_id != $content) {
308 function filter_MatchesAnyOpenIDType(&$service)
310 $uris = $service->getTypes();
312 foreach ($uris as $uri) {
313 if (in_array($uri, Auth_OpenID_getOpenIDTypeURIs())) {
321 function Auth_OpenID_bestMatchingService($service, $preferred_types)
323 // Return the index of the first matching type, or something
324 // higher if no type matches.
326 // This provides an ordering in which service elements that
327 // contain a type that comes earlier in the preferred types list
328 // come before service elements that come later. If a service
329 // element has more than one type, the most preferred one wins.
331 foreach ($preferred_types as $index => $typ) {
332 if (in_array($typ, $service->type_uris)) {
337 return count($preferred_types);
340 function Auth_OpenID_arrangeByType($service_list, $preferred_types)
342 // Rearrange service_list in a new list so services are ordered by
343 // types listed in preferred_types. Return the new list.
345 // Build a list with the service elements in tuples whose
346 // comparison will prefer the one with the best matching service
347 $prio_services = array();
348 foreach ($service_list as $index => $service) {
349 $prio_services[] = array(Auth_OpenID_bestMatchingService($service,
354 sort($prio_services);
356 // Now that the services are sorted by priority, remove the sort
357 // keys from the list.
358 foreach ($prio_services as $index => $s) {
359 $prio_services[$index] = $prio_services[$index][2];
362 return $prio_services;
365 // Extract OP Identifier services. If none found, return the rest,
366 // sorted with most preferred first according to
367 // OpenIDServiceEndpoint.openid_type_uris.
369 // openid_services is a list of OpenIDServiceEndpoint objects.
371 // Returns a list of OpenIDServiceEndpoint objects."""
372 function Auth_OpenID_getOPOrUserServices($openid_services)
374 $op_services = Auth_OpenID_arrangeByType($openid_services,
375 array(Auth_OpenID_TYPE_2_0_IDP));
377 $openid_services = Auth_OpenID_arrangeByType($openid_services,
378 Auth_OpenID_getOpenIDTypeURIs());
383 return $openid_services;
387 function Auth_OpenID_makeOpenIDEndpoints($uri, $yadis_services)
391 if (!$yadis_services) {
395 foreach ($yadis_services as $service) {
396 $type_uris = $service->getTypes();
397 $uris = $service->getURIs();
399 // If any Type URIs match and there is an endpoint URI
400 // specified, then this is an OpenID endpoint
403 foreach ($uris as $service_uri) {
404 $openid_endpoint = new Auth_OpenID_ServiceEndpoint();
405 if ($openid_endpoint->parseService($uri,
409 $s[] = $openid_endpoint;
418 function Auth_OpenID_discoverWithYadis($uri, &$fetcher,
419 $endpoint_filter='Auth_OpenID_getOPOrUserServices',
420 $discover_function=null)
422 // Discover OpenID services for a URI. Tries Yadis and falls back
423 // on old-style <link rel='...'> discovery if Yadis fails.
425 // Might raise a yadis.discover.DiscoveryFailure if no document
426 // came back for that URI at all. I don't think falling back to
427 // OpenID 1.0 discovery on the same URL will help, so don't bother
429 if ($discover_function === null) {
430 $discover_function = array('Auth_Yadis_Yadis', 'discover');
433 $openid_services = array();
435 $response = call_user_func_array($discover_function,
436 array($uri, &$fetcher));
438 $yadis_url = $response->normalized_uri;
439 $yadis_services = array();
441 if ($response->isFailure()) {
442 return array($uri, array());
445 $openid_services = Auth_OpenID_ServiceEndpoint::fromXRDS(
447 $response->response_text);
449 if (!$openid_services) {
450 if ($response->isXRDS()) {
451 return Auth_OpenID_discoverWithoutYadis($uri,
455 // Try to parse the response as HTML to get OpenID 1.0/1.1
457 $openid_services = Auth_OpenID_ServiceEndpoint::fromHTML(
459 $response->response_text);
462 $openid_services = call_user_func_array($endpoint_filter,
463 array(&$openid_services));
465 return array($yadis_url, $openid_services);
468 function Auth_OpenID_discoverURI($uri, &$fetcher)
470 $uri = Auth_OpenID::normalizeUrl($uri);
471 return Auth_OpenID_discoverWithYadis($uri, $fetcher);
474 function Auth_OpenID_discoverWithoutYadis($uri, &$fetcher)
476 $http_resp = @$fetcher->get($uri);
478 if ($http_resp->status != 200 and $http_resp->status != 206) {
479 return array($uri, array());
482 $identity_url = $http_resp->final_url;
484 // Try to parse the response as HTML to get OpenID 1.0/1.1 <link
486 $openid_services = Auth_OpenID_ServiceEndpoint::fromHTML(
490 return array($identity_url, $openid_services);
493 function Auth_OpenID_discoverXRI($iname, &$fetcher)
495 $resolver = new Auth_Yadis_ProxyResolver($fetcher);
496 list($canonicalID, $yadis_services) =
497 $resolver->query($iname,
498 Auth_OpenID_getOpenIDTypeURIs(),
499 array('filter_MatchesAnyOpenIDType'));
501 $openid_services = Auth_OpenID_makeOpenIDEndpoints($iname,
504 $openid_services = Auth_OpenID_getOPOrUserServices($openid_services);
506 for ($i = 0; $i < count($openid_services); $i++) {
507 $openid_services[$i]->canonicalID = $canonicalID;
508 $openid_services[$i]->claimed_id = $canonicalID;
509 $openid_services[$i]->display_identifier = $iname;
512 // FIXME: returned xri should probably be in some normal form
513 return array($iname, $openid_services);
516 function Auth_OpenID_discover($uri, &$fetcher)
518 // If the fetcher (i.e., PHP) doesn't support SSL, we can't do
519 // discovery on an HTTPS URL.
520 if ($fetcher->isHTTPS($uri) && !$fetcher->supportsSSL()) {
521 return array($uri, array());
524 if (Auth_Yadis_identifierScheme($uri) == 'XRI') {
525 $result = Auth_OpenID_discoverXRI($uri, $fetcher);
527 $result = Auth_OpenID_discoverURI($uri, $fetcher);
530 // If the fetcher doesn't support SSL, we can't interact with
531 // HTTPS server URLs; remove those endpoints from the list.
532 if (!$fetcher->supportsSSL()) {
533 $http_endpoints = array();
534 list($new_uri, $endpoints) = $result;
536 foreach ($endpoints as $e) {
537 if (!$fetcher->isHTTPS($e->server_url)) {
538 $http_endpoints[] = $e;
542 $result = array($new_uri, $http_endpoints);