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,
31 Auth_OpenID_TYPE_1_0);
34 function Auth_OpenID_getOpenIDConsumerTypeURIs()
36 return array(Auth_OpenID_RP_RETURN_TO_URL_TYPE);
41 * Provides a user-readable interpretation of a type uri.
42 * Useful for error messages.
44 function Auth_OpenID_getOpenIDTypeName($type_uri) {
46 case Auth_OpenID_TYPE_2_0_IDP:
47 return 'OpenID 2.0 IDP';
48 case Auth_OpenID_TYPE_2_0:
50 case Auth_OpenID_TYPE_1_2:
52 case Auth_OpenID_TYPE_1_1:
54 case Auth_OpenID_TYPE_1_0:
56 case Auth_OpenID_RP_RETURN_TO_URL_TYPE:
57 return 'OpenID relying party';
62 * Object representing an OpenID service endpoint.
64 class Auth_OpenID_ServiceEndpoint {
65 function Auth_OpenID_ServiceEndpoint()
67 $this->claimed_id = null;
68 $this->server_url = null;
69 $this->type_uris = array();
70 $this->local_id = null;
71 $this->canonicalID = null;
72 $this->used_yadis = false; // whether this came from an XRDS
73 $this->display_identifier = null;
76 function getDisplayIdentifier()
78 if ($this->display_identifier) {
79 return $this->display_identifier;
81 if (! $this->claimed_id) {
82 return $this->claimed_id;
84 $parsed = parse_url($this->claimed_id);
85 $scheme = $parsed['scheme'];
86 $host = $parsed['host'];
87 $path = $parsed['path'];
88 if (array_key_exists('query', $parsed)) {
89 $query = $parsed['query'];
90 $no_frag = "$scheme://$host$path?$query";
92 $no_frag = "$scheme://$host$path";
97 function usesExtension($extension_uri)
99 return in_array($extension_uri, $this->type_uris);
102 function preferredNamespace()
104 if (in_array(Auth_OpenID_TYPE_2_0_IDP, $this->type_uris) ||
105 in_array(Auth_OpenID_TYPE_2_0, $this->type_uris)) {
106 return Auth_OpenID_OPENID2_NS;
108 return Auth_OpenID_OPENID1_NS;
113 * Query this endpoint to see if it has any of the given type
114 * URIs. This is useful for implementing other endpoint classes
115 * that e.g. need to check for the presence of multiple versions
116 * of a single protocol.
118 * @param $type_uris The URIs that you wish to check
120 * @return all types that are in both in type_uris and
123 function matchTypes($type_uris)
126 foreach ($type_uris as $test_uri) {
127 if ($this->supportsType($test_uri)) {
128 $result[] = $test_uri;
135 function supportsType($type_uri)
137 // Does this endpoint support this type?
138 return ((in_array($type_uri, $this->type_uris)) ||
139 (($type_uri == Auth_OpenID_TYPE_2_0) &&
140 $this->isOPIdentifier()));
143 function compatibilityMode()
145 return $this->preferredNamespace() != Auth_OpenID_OPENID2_NS;
148 function isOPIdentifier()
150 return in_array(Auth_OpenID_TYPE_2_0_IDP, $this->type_uris);
153 static function fromOPEndpointURL($op_endpoint_url)
155 // Construct an OP-Identifier OpenIDServiceEndpoint object for
156 // a given OP Endpoint URL
157 $obj = new Auth_OpenID_ServiceEndpoint();
158 $obj->server_url = $op_endpoint_url;
159 $obj->type_uris = array(Auth_OpenID_TYPE_2_0_IDP);
163 function parseService($yadis_url, $uri, $type_uris, $service_element)
165 // Set the state of this object based on the contents of the
166 // service element. Return true if successful, false if not
167 // (if findOPLocalIdentifier returns false).
168 $this->type_uris = $type_uris;
169 $this->server_url = $uri;
170 $this->used_yadis = true;
172 if (!$this->isOPIdentifier()) {
173 $this->claimed_id = $yadis_url;
174 $this->local_id = Auth_OpenID_findOPLocalIdentifier(
177 if ($this->local_id === false) {
185 function getLocalID()
187 // Return the identifier that should be sent as the
188 // openid.identity_url parameter to the server.
189 if ($this->local_id === null && $this->canonicalID === null) {
190 return $this->claimed_id;
192 if ($this->local_id) {
193 return $this->local_id;
195 return $this->canonicalID;
201 * Parse the given document as XRDS looking for OpenID consumer services.
203 * @return array of Auth_OpenID_ServiceEndpoint or null if the
204 * document cannot be parsed.
206 function consumerFromXRDS($uri, $xrds_text)
208 $xrds =& Auth_Yadis_XRDS::parseXRDS($xrds_text);
212 $xrds->services(array('filter_MatchesAnyOpenIDConsumerType'));
213 return Auth_OpenID_makeOpenIDEndpoints($uri, $yadis_services);
220 * Parse the given document as XRDS looking for OpenID services.
222 * @return array of Auth_OpenID_ServiceEndpoint or null if the
223 * document cannot be parsed.
225 static function fromXRDS($uri, $xrds_text)
227 $xrds = Auth_Yadis_XRDS::parseXRDS($xrds_text);
231 $xrds->services(array('filter_MatchesAnyOpenIDType'));
232 return Auth_OpenID_makeOpenIDEndpoints($uri, $yadis_services);
239 * Create endpoints from a DiscoveryResult.
241 * @param discoveryResult Auth_Yadis_DiscoveryResult
242 * @return array of Auth_OpenID_ServiceEndpoint or null if
243 * endpoints cannot be created.
245 static function fromDiscoveryResult($discoveryResult)
247 if ($discoveryResult->isXRDS()) {
248 return Auth_OpenID_ServiceEndpoint::fromXRDS(
249 $discoveryResult->normalized_uri,
250 $discoveryResult->response_text);
252 return Auth_OpenID_ServiceEndpoint::fromHTML(
253 $discoveryResult->normalized_uri,
254 $discoveryResult->response_text);
258 static function fromHTML($uri, $html)
260 $discovery_types = array(
261 array(Auth_OpenID_TYPE_2_0,
262 'openid2.provider', 'openid2.local_id'),
263 array(Auth_OpenID_TYPE_1_1,
264 'openid.server', 'openid.delegate')
269 foreach ($discovery_types as $triple) {
270 list($type_uri, $server_rel, $delegate_rel) = $triple;
272 $urls = Auth_OpenID_legacy_discover($html, $server_rel,
275 if ($urls === false) {
279 list($delegate_url, $server_url) = $urls;
281 $service = new Auth_OpenID_ServiceEndpoint();
282 $service->claimed_id = $uri;
283 $service->local_id = $delegate_url;
284 $service->server_url = $server_url;
285 $service->type_uris = array($type_uri);
287 $services[] = $service;
295 $x = new Auth_OpenID_ServiceEndpoint();
297 $x->claimed_id = $this->claimed_id;
298 $x->server_url = $this->server_url;
299 $x->type_uris = $this->type_uris;
300 $x->local_id = $this->local_id;
301 $x->canonicalID = $this->canonicalID;
302 $x->used_yadis = $this->used_yadis;
308 function Auth_OpenID_findOPLocalIdentifier($service, $type_uris)
310 // Extract a openid:Delegate value from a Yadis Service element.
311 // If no delegate is found, returns null. Returns false on
312 // discovery failure (when multiple delegate/localID tags have
313 // different values).
315 $service->parser->registerNamespace('openid',
316 Auth_OpenID_XMLNS_1_0);
318 $service->parser->registerNamespace('xrd',
319 Auth_Yadis_XMLNS_XRD_2_0);
321 $parser = $service->parser;
323 $permitted_tags = array();
325 if (in_array(Auth_OpenID_TYPE_1_1, $type_uris) ||
326 in_array(Auth_OpenID_TYPE_1_0, $type_uris)) {
327 $permitted_tags[] = 'openid:Delegate';
330 if (in_array(Auth_OpenID_TYPE_2_0, $type_uris)) {
331 $permitted_tags[] = 'xrd:LocalID';
336 foreach ($permitted_tags as $tag_name) {
337 $tags = $service->getElements($tag_name);
339 foreach ($tags as $tag) {
340 $content = $parser->content($tag);
342 if ($local_id === null) {
343 $local_id = $content;
344 } else if ($local_id != $content) {
353 function filter_MatchesAnyOpenIDType($service)
355 $uris = $service->getTypes();
357 foreach ($uris as $uri) {
358 if (in_array($uri, Auth_OpenID_getOpenIDTypeURIs())) {
366 function filter_MatchesAnyOpenIDConsumerType(&$service)
368 $uris = $service->getTypes();
370 foreach ($uris as $uri) {
371 if (in_array($uri, Auth_OpenID_getOpenIDConsumerTypeURIs())) {
379 function Auth_OpenID_bestMatchingService($service, $preferred_types)
381 // Return the index of the first matching type, or something
382 // higher if no type matches.
384 // This provides an ordering in which service elements that
385 // contain a type that comes earlier in the preferred types list
386 // come before service elements that come later. If a service
387 // element has more than one type, the most preferred one wins.
389 foreach ($preferred_types as $index => $typ) {
390 if (in_array($typ, $service->type_uris)) {
395 return count($preferred_types);
398 function Auth_OpenID_arrangeByType($service_list, $preferred_types)
400 // Rearrange service_list in a new list so services are ordered by
401 // types listed in preferred_types. Return the new list.
403 // Build a list with the service elements in tuples whose
404 // comparison will prefer the one with the best matching service
405 $prio_services = array();
406 foreach ($service_list as $index => $service) {
407 $prio_services[] = array(Auth_OpenID_bestMatchingService($service,
412 sort($prio_services);
414 // Now that the services are sorted by priority, remove the sort
415 // keys from the list.
416 foreach ($prio_services as $index => $s) {
417 $prio_services[$index] = $prio_services[$index][2];
420 return $prio_services;
423 // Extract OP Identifier services. If none found, return the rest,
424 // sorted with most preferred first according to
425 // OpenIDServiceEndpoint.openid_type_uris.
427 // openid_services is a list of OpenIDServiceEndpoint objects.
429 // Returns a list of OpenIDServiceEndpoint objects."""
430 function Auth_OpenID_getOPOrUserServices($openid_services)
432 $op_services = Auth_OpenID_arrangeByType($openid_services,
433 array(Auth_OpenID_TYPE_2_0_IDP));
435 $openid_services = Auth_OpenID_arrangeByType($openid_services,
436 Auth_OpenID_getOpenIDTypeURIs());
441 return $openid_services;
445 function Auth_OpenID_makeOpenIDEndpoints($uri, $yadis_services)
449 if (!$yadis_services) {
453 foreach ($yadis_services as $service) {
454 $type_uris = $service->getTypes();
455 $uris = $service->getURIs();
457 // If any Type URIs match and there is an endpoint URI
458 // specified, then this is an OpenID endpoint
461 foreach ($uris as $service_uri) {
462 $openid_endpoint = new Auth_OpenID_ServiceEndpoint();
463 if ($openid_endpoint->parseService($uri,
467 $s[] = $openid_endpoint;
476 function Auth_OpenID_discoverWithYadis($uri, $fetcher,
477 $endpoint_filter='Auth_OpenID_getOPOrUserServices',
478 $discover_function=null)
480 // Discover OpenID services for a URI. Tries Yadis and falls back
481 // on old-style <link rel='...'> discovery if Yadis fails.
483 // Might raise a yadis.discover.DiscoveryFailure if no document
484 // came back for that URI at all. I don't think falling back to
485 // OpenID 1.0 discovery on the same URL will help, so don't bother
487 if ($discover_function === null) {
488 $discover_function = array('Auth_Yadis_Yadis', 'discover');
491 $openid_services = array();
493 $response = call_user_func_array($discover_function,
494 array($uri, $fetcher));
496 $yadis_url = $response->normalized_uri;
497 $yadis_services = array();
499 if ($response->isFailure() && !$response->isXRDS()) {
500 return array($uri, array());
503 $openid_services = Auth_OpenID_ServiceEndpoint::fromXRDS(
505 $response->response_text);
507 if (!$openid_services) {
508 if ($response->isXRDS()) {
509 return Auth_OpenID_discoverWithoutYadis($uri,
513 // Try to parse the response as HTML to get OpenID 1.0/1.1
515 $openid_services = Auth_OpenID_ServiceEndpoint::fromHTML(
517 $response->response_text);
520 $openid_services = call_user_func_array($endpoint_filter,
521 array($openid_services));
523 return array($yadis_url, $openid_services);
526 function Auth_OpenID_discoverURI($uri, $fetcher)
528 $uri = Auth_OpenID::normalizeUrl($uri);
529 return Auth_OpenID_discoverWithYadis($uri, $fetcher);
532 function Auth_OpenID_discoverWithoutYadis($uri, $fetcher)
534 $http_resp = @$fetcher->get($uri);
536 if ($http_resp->status != 200 and $http_resp->status != 206) {
537 return array($uri, array());
540 $identity_url = $http_resp->final_url;
542 // Try to parse the response as HTML to get OpenID 1.0/1.1 <link
544 $openid_services = Auth_OpenID_ServiceEndpoint::fromHTML(
548 return array($identity_url, $openid_services);
551 function Auth_OpenID_discoverXRI($iname, $fetcher)
553 $resolver = new Auth_Yadis_ProxyResolver($fetcher);
554 list($canonicalID, $yadis_services) =
555 $resolver->query($iname,
556 Auth_OpenID_getOpenIDTypeURIs(),
557 array('filter_MatchesAnyOpenIDType'));
559 $openid_services = Auth_OpenID_makeOpenIDEndpoints($iname,
562 $openid_services = Auth_OpenID_getOPOrUserServices($openid_services);
564 for ($i = 0; $i < count($openid_services); $i++) {
565 $openid_services[$i]->canonicalID = $canonicalID;
566 $openid_services[$i]->claimed_id = $canonicalID;
567 $openid_services[$i]->display_identifier = $iname;
570 // FIXME: returned xri should probably be in some normal form
571 return array($iname, $openid_services);
574 function Auth_OpenID_discover($uri, $fetcher)
576 // If the fetcher (i.e., PHP) doesn't support SSL, we can't do
577 // discovery on an HTTPS URL.
578 if ($fetcher->isHTTPS($uri) && !$fetcher->supportsSSL()) {
579 return array($uri, array());
582 if (Auth_Yadis_identifierScheme($uri) == 'XRI') {
583 $result = Auth_OpenID_discoverXRI($uri, $fetcher);
585 $result = Auth_OpenID_discoverURI($uri, $fetcher);
588 // If the fetcher doesn't support SSL, we can't interact with
589 // HTTPS server URLs; remove those endpoints from the list.
590 if (!$fetcher->supportsSSL()) {
591 $http_endpoints = array();
592 list($new_uri, $endpoints) = $result;
594 foreach ($endpoints as $e) {
595 if (!$fetcher->isHTTPS($e->server_url)) {
596 $http_endpoints[] = $e;
600 $result = array($new_uri, $http_endpoints);