13 require_once
"Auth/OpenID/Extension.php";
14 require_once
"Auth/OpenID/Message.php";
15 require_once
"Auth/OpenID/TrustRoot.php";
17 define(
'Auth_OpenID_AX_NS_URI',
18 'http://openid.net/srv/ax/1.0');
22 define(
'Auth_OpenID_AX_UNLIMITED_VALUES',
'unlimited');
26 define(
'Auth_OpenID_AX_MINIMUM_SUPPORTED_ALIAS_LENGTH', 32);
43 return is_a($thing,
'Auth_OpenID_AX_Error');
53 if (strpos($alias,
',') !==
false) {
55 "Alias %s must not contain comma", $alias));
57 if (strpos($alias,
'.') !==
false) {
59 "Alias %s must not contain period", $alias));
74 $this->message = $message;
109 if (
$mode != $this->mode) {
112 "Expected mode '%s'; got '%s'",
113 $this->mode,
$mode));
128 return array(
'mode' => $this->mode);
162 $this->required = $required;
168 $this->count = $count;
177 $this->type_uri = $type_uri;
187 $this->alias = $alias;
194 static function make($type_uri, $count=1, $required=
false,
197 if ($alias !== null) {
243 foreach (explode(
',', $alias_list_s) as $alias) {
244 $type_uri = $namespace_map->getNamespaceURI($alias);
245 if ($type_uri === null) {
249 sprintf(
'No type is defined for attribute name %s',
270 var
$mode =
'fetch_request';
278 $this->requested_attributes = array();
285 $this->update_url = $update_url;
295 function add($attribute)
297 if ($this->
contains($attribute->type_uri)) {
299 sprintf(
"The attribute %s has already been requested",
300 $attribute->type_uri));
303 $this->requested_attributes[$attribute->type_uri] = $attribute;
318 $if_available = array();
322 foreach ($this->requested_attributes as $type_uri => $attribute) {
323 if ($attribute->alias === null) {
326 $alias =
$aliases->addAlias($type_uri, $attribute->alias);
328 if ($alias === null) {
330 sprintf(
"Could not add alias %s for URI %s",
331 $attribute->alias, $type_uri
336 if ($attribute->required) {
337 $required[] = $alias;
339 $if_available[] = $alias;
342 if ($attribute->count != 1) {
343 $ax_args[
'count.' . $alias] = strval($attribute->count);
346 $ax_args[
'type.' . $alias] = $type_uri;
350 $ax_args[
'required'] = implode(
',', $required);
354 $ax_args[
'if_available'] = implode(
',', $if_available);
370 foreach ($this->requested_attributes as $type_uri => $attribute) {
371 if ($attribute->required) {
372 $required[] = $type_uri;
391 $m = $request->message;
393 $ax_args = $m->getArgs($obj->ns_uri);
395 $result = $obj->parseExtensionArgs($ax_args);
401 if ($obj->update_url) {
411 sprintf(
"Cannot validate update_url %s " .
412 "against absent realm", $obj->update_url));
416 sprintf(
"Update URL %s failed validation against realm %s",
417 $obj->update_url, $realm));
441 foreach ($ax_args as $key => $value) {
442 if (strpos($key,
'type.') === 0) {
443 $alias = substr($key, 5);
446 $alias =
$aliases->addAlias($type_uri, $alias);
448 if ($alias === null) {
450 sprintf(
"Could not add alias %s for URI %s",
458 if (($count ===
false) &&
466 if ($count ===
false) {
468 sprintf(
"Integer value expected for %s, got %s",
469 'count.' . $alias, $count_s));
479 $this->
add($attrinfo);
486 foreach ($required as $type_uri) {
487 $attrib = $this->requested_attributes[$type_uri];
494 $all_type_uris = array_merge($required, $if_available);
496 foreach (
$aliases->iterNamespaceURIs() as $type_uri) {
497 if (!in_array($type_uri, $all_type_uris)) {
499 sprintf(
'Type URI %s was in the request but not ' .
500 'present in "required" or "if_available"',
517 return array_values($this->requested_attributes);
522 return array_keys($this->requested_attributes);
530 return in_array($type_uri, $this->
iterTypes());
545 $this->data = array();
559 function addValue($type_uri, $value)
561 if (!array_key_exists($type_uri, $this->data)) {
562 $this->data[$type_uri] = array();
565 $values =& $this->data[$type_uri];
578 $this->data[$type_uri] =& $values;
598 foreach ($this->data as $type_uri => $values) {
601 $ax_args[
'type.' . $alias] = $type_uri;
602 $ax_args[
'count.' . $alias] = strval(
count($values));
604 foreach ($values as $i => $value) {
605 $key = sprintf(
'value.%s.%d', $alias, $i + 1);
606 $ax_args[$key] = $value;
630 foreach ($ax_args as $key => $value) {
631 if (strpos($key,
'type.') === 0) {
633 $alias = substr($key, 5);
641 $alias =
$aliases->addAlias($type_uri, $alias);
643 if ($alias === null) {
645 sprintf(
"Could not add alias %s for URI %s",
652 foreach (
$aliases->iteritems() as $pair) {
653 list($type_uri, $alias) = $pair;
657 $count_key =
'count.' . $alias;
658 $count_s = $ax_args[$count_key];
662 if ($count ===
false) {
664 sprintf(
"Integer value expected for %s, got %s",
665 'count. %s' . $alias, $count_s,
671 for ($i = 1; $i < $count + 1; $i++) {
672 $value_key = sprintf(
'value.%s.%d', $alias, $i);
674 if (!array_key_exists($value_key, $ax_args)) {
677 "No value found for key %s",
681 $value = $ax_args[$value_key];
685 $key =
'value.' . $alias;
687 if (!array_key_exists($key, $ax_args)) {
690 "No value found for key %s",
694 $value = $ax_args[
'value.' . $alias];
699 $values = array($value);
703 $this->data[$type_uri] = $values;
722 function getSingle($type_uri, $default=null)
727 }
else if (
count($values) == 1) {
731 sprintf(
'More than one value present for %s',
753 function get($type_uri)
755 if (array_key_exists($type_uri, $this->data)) {
756 return $this->data[$type_uri];
759 sprintf(
"Type URI %s not found in response",
775 function count($type_uri)
777 if (array_key_exists($type_uri, $this->data)) {
778 return count($this->
get($type_uri));
781 sprintf(
"Type URI %s not found in response",
794 var
$mode =
'fetch_response';
799 $this->update_url = $update_url;
814 $zero_value_types = array();
816 if ($request !== null) {
822 foreach ($this->data as $type_uri => $unused) {
823 if (!$request->contains($type_uri)) {
825 sprintf(
"Response attribute not present in request: %s",
831 foreach ($request->iterAttrs() as $attr_info) {
834 if ($attr_info->alias === null) {
835 $aliases->add($attr_info->type_uri);
837 $alias =
$aliases->addAlias($attr_info->type_uri,
840 if ($alias === null) {
842 sprintf(
"Could not add alias %s for URI %s",
843 $attr_info->alias, $attr_info->type_uri)
848 if (array_key_exists($attr_info->type_uri, $this->data)) {
849 $values = $this->data[$attr_info->type_uri];
852 $zero_value_types[] = $attr_info;
856 ($attr_info->count <
count($values))) {
858 sprintf(
"More than the number of requested values " .
859 "were specified for %s",
860 $attr_info->type_uri)
874 foreach ($zero_value_types as $attr_info) {
875 $alias =
$aliases->getAlias($attr_info->type_uri);
876 $kv_args[
'type.' . $alias] = $attr_info->type_uri;
877 $kv_args[
'count.' . $alias] =
'0';
882 $update_url = $request->update_url;
884 $update_url = $this->update_url;
888 $ax_args[
'update_url'] = $update_url;
929 $ax_args = $success_response->getSignedNS($obj->ns_uri);
931 $ax_args = $success_response->message->getArgs($obj->ns_uri);
934 sizeof($ax_args) == 0) {
938 $result = $obj->parseExtensionArgs($ax_args);
953 var
$mode =
'store_request';
983 function make($succeeded=
true, $error_message=null)
985 if (($succeeded) && ($error_message !== null)) {
987 'included in a failing fetch response');
1001 $this->error_message = $error_message;
1015 if ((!$this->
succeeded()) && $this->error_message) {
1016 $ax_args[
'error'] = $this->error_message;