Support Joomla!

Packages

Package: OpenID

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /openid/Auth/OpenID/SReg.php

Documentation is available at SReg.php

  1. <?php
  2.  
  3. /**
  4.  * Simple registration request and response parsing and object
  5.  * representation.
  6.  *
  7.  * This module contains objects representing simple registration
  8.  * requests and responses that can be used with both OpenID relying
  9.  * parties and OpenID providers.
  10.  *
  11.  * 1. The relying party creates a request object and adds it to the
  12.  * {@link Auth_OpenID_AuthRequest} object before making the
  13.  * checkid request to the OpenID provider:
  14.  *
  15.  *   $sreg_req = Auth_OpenID_SRegRequest::build(array('email'));
  16.  *   $auth_request->addExtension($sreg_req);
  17.  *
  18.  * 2. The OpenID provider extracts the simple registration request
  19.  * from the OpenID request using {@link }
  20.  * Auth_OpenID_SRegRequest::fromOpenIDRequest}, gets the user's
  21.  * approval and data, creates an {@link Auth_OpenID_SRegResponse}
  22.  * object and adds it to the id_res response:
  23.  *
  24.  *   $sreg_req = Auth_OpenID_SRegRequest::fromOpenIDRequest(
  25.  *                                  $checkid_request);
  26.  *   // [ get the user's approval and data, informing the user that
  27.  *   //   the fields in sreg_response were requested ]
  28.  *   $sreg_resp = Auth_OpenID_SRegResponse::extractResponse(
  29.  *                                  $sreg_req, $user_data);
  30.  *   $sreg_resp->toMessage($openid_response->fields);
  31.  *
  32.  * 3. The relying party uses {@link }
  33.  * Auth_OpenID_SRegResponse::fromSuccessResponse} to extract the data
  34.  * from the OpenID response:
  35.  *
  36.  *   $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse(
  37.  *                                  $success_response);
  38.  *
  39.  * @package OpenID
  40.  */
  41.  
  42. // Do not allow direct access
  43. defined'_JEXEC' or die'Restricted access' );
  44.  
  45. /**
  46.  * Import message and extension internals.
  47.  */
  48. require_once 'Auth/OpenID/Message.php';
  49. require_once 'Auth/OpenID/Extension.php';
  50.  
  51. // The data fields that are listed in the sreg spec
  52. global $Auth_OpenID_sreg_data_fields;
  53. $Auth_OpenID_sreg_data_fields array(
  54.                                       'fullname' => 'Full Name',
  55.                                       'nickname' => 'Nickname',
  56.                                       'dob' => 'Date of Birth',
  57.                                       'email' => 'E-mail Address',
  58.                                       'gender' => 'Gender',
  59.                                       'postcode' => 'Postal Code',
  60.                                       'country' => 'Country',
  61.                                       'language' => 'Language',
  62.                                       'timezone' => 'Time Zone');
  63.  
  64. /**
  65.  * Check to see that the given value is a valid simple registration
  66.  * data field name.  Return true if so, false if not.
  67.  */
  68. function Auth_OpenID_checkFieldName($field_name)
  69. {
  70.     global $Auth_OpenID_sreg_data_fields;
  71.  
  72.     if (!in_array($field_namearray_keys($Auth_OpenID_sreg_data_fields))) {
  73.         return false;
  74.     }
  75.     return true;
  76. }
  77.  
  78. // URI used in the wild for Yadis documents advertising simple
  79. // registration support
  80. define('Auth_OpenID_SREG_NS_URI_1_0''http://openid.net/sreg/1.0');
  81.  
  82. // URI in the draft specification for simple registration 1.1
  83. // <http://openid.net/specs/openid-simple-registration-extension-1_1-01.html>
  84. define('Auth_OpenID_SREG_NS_URI_1_1''http://openid.net/extensions/sreg/1.1');
  85.  
  86. // This attribute will always hold the preferred URI to use when
  87. // adding sreg support to an XRDS file or in an OpenID namespace
  88. // declaration.
  89. define('Auth_OpenID_SREG_NS_URI'Auth_OpenID_SREG_NS_URI_1_1);
  90.  
  91.  
  92. /**
  93.  * Does the given endpoint advertise support for simple
  94.  * registration?
  95.  *
  96.  * $endpoint: The endpoint object as returned by OpenID discovery.
  97.  * returns whether an sreg type was advertised by the endpoint
  98.  */
  99. function Auth_OpenID_supportsSReg(&$endpoint)
  100. {
  101.     return ($endpoint->usesExtension(Auth_OpenID_SREG_NS_URI_1_1||
  102.             $endpoint->usesExtension(Auth_OpenID_SREG_NS_URI_1_0));
  103. }
  104.  
  105. /**
  106.  * A base class for classes dealing with Simple Registration protocol
  107.  * messages.
  108.  *
  109.  * @package OpenID
  110.  */
  111.     /**
  112.      * Extract the simple registration namespace URI from the given
  113.      * OpenID message. Handles OpenID 1 and 2, as well as both sreg
  114.      * namespace URIs found in the wild, as well as missing namespace
  115.      * definitions (for OpenID 1)
  116.      *
  117.      * $message: The OpenID message from which to parse simple
  118.      * registration fields. This may be a request or response message.
  119.      *
  120.      * Returns the sreg namespace URI for the supplied message. The
  121.      * message may be modified to define a simple registration
  122.      * namespace.
  123.      *
  124.      * @access private
  125.      */
  126.     function _getSRegNS(&$message)
  127.     {
  128.         $alias null;
  129.         $found_ns_uri null;
  130.  
  131.         // See if there exists an alias for one of the two defined
  132.         // simple registration types.
  133.         foreach (array(Auth_OpenID_SREG_NS_URI_1_1,
  134.                        Auth_OpenID_SREG_NS_URI_1_0as $sreg_ns_uri{
  135.             $alias $message->namespaces->getAlias($sreg_ns_uri);
  136.             if ($alias !== null{
  137.                 $found_ns_uri $sreg_ns_uri;
  138.                 break;
  139.             }
  140.         }
  141.  
  142.         if ($alias === null{
  143.             // There is no alias for either of the types, so try to
  144.             // add one. We default to using the modern value (1.1)
  145.             $found_ns_uri Auth_OpenID_SREG_NS_URI_1_1;
  146.             if ($message->namespaces->addAlias(Auth_OpenID_SREG_NS_URI_1_1,
  147.                                                'sreg'=== null{
  148.                 // An alias for the string 'sreg' already exists, but
  149.                 // it's defined for something other than simple
  150.                 // registration
  151.                 return null;
  152.             }
  153.         }
  154.  
  155.         return $found_ns_uri;
  156.     }
  157. }
  158.  
  159. /**
  160.  * An object to hold the state of a simple registration request.
  161.  *
  162.  * required: A list of the required fields in this simple registration
  163.  * request
  164.  *
  165.  * optional: A list of the optional fields in this simple registration
  166.  * request
  167.  *
  168.  * @package OpenID
  169.  */
  170.  
  171.     var $ns_alias = 'sreg';
  172.  
  173.     /**
  174.      * Initialize an empty simple registration request.
  175.      */
  176.     function build($required=null$optional=null,
  177.                    $policy_url=null,
  178.                    $sreg_ns_uri=Auth_OpenID_SREG_NS_URI,
  179.                    $cls='Auth_OpenID_SRegRequest')
  180.     {
  181.         $obj new $cls();
  182.  
  183.         $obj->required array();
  184.         $obj->optional array();
  185.         $obj->policy_url $policy_url;
  186.         $obj->ns_uri $sreg_ns_uri;
  187.  
  188.         if ($required{
  189.             if (!$obj->requestFields($requiredtruetrue)) {
  190.                 return null;
  191.             }
  192.         }
  193.  
  194.         if ($optional{
  195.             if (!$obj->requestFields($optionalfalsetrue)) {
  196.                 return null;
  197.             }
  198.         }
  199.  
  200.         return $obj;
  201.     }
  202.  
  203.     /**
  204.      * Create a simple registration request that contains the fields
  205.      * that were requested in the OpenID request with the given
  206.      * arguments
  207.      *
  208.      * $request: The OpenID authentication request from which to
  209.      * extract an sreg request.
  210.      *
  211.      * $cls: name of class to use when creating sreg request object.
  212.      * Used for testing.
  213.      *
  214.      * Returns the newly created simple registration request
  215.      */
  216.     function fromOpenIDRequest($request$cls='Auth_OpenID_SRegRequest')
  217.     {
  218.  
  219.         $obj call_user_func_array(array($cls'build'),
  220.                  array(nullnullnullAuth_OpenID_SREG_NS_URI$cls));
  221.  
  222.         // Since we're going to mess with namespace URI mapping, don't
  223.         // mutate the object that was passed in.
  224.         $m $request->message;
  225.  
  226.         $obj->ns_uri $obj->_getSRegNS($m);
  227.         $args $m->getArgs($obj->ns_uri);
  228.  
  229.         if ($args === null || Auth_OpenID::isFailure($args)) {
  230.             return null;
  231.         }
  232.  
  233.         $obj->parseExtensionArgs($args);
  234.  
  235.         return $obj;
  236.     }
  237.  
  238.     /**
  239.      * Parse the unqualified simple registration request parameters
  240.      * and add them to this object.
  241.      *
  242.      * This method is essentially the inverse of
  243.      * getExtensionArgs. This method restores the serialized simple
  244.      * registration request fields.
  245.      *
  246.      * If you are extracting arguments from a standard OpenID
  247.      * checkid_* request, you probably want to use fromOpenIDRequest,
  248.      * which will extract the sreg namespace and arguments from the
  249.      * OpenID request. This method is intended for cases where the
  250.      * OpenID server needs more control over how the arguments are
  251.      * parsed than that method provides.
  252.      *
  253.      * $args == $message->getArgs($ns_uri);
  254.      * $request->parseExtensionArgs($args);
  255.      *
  256.      * $args: The unqualified simple registration arguments
  257.      *
  258.      * strict: Whether requests with fields that are not defined in
  259.      * the simple registration specification should be tolerated (and
  260.      * ignored)
  261.      */
  262.     function parseExtensionArgs($args$strict=false)
  263.     {
  264.         foreach (array('required''optional'as $list_name{
  265.             $required ($list_name == 'required');
  266.             $items Auth_OpenID::arrayGet($args$list_name);
  267.             if ($items{
  268.                 foreach (explode(','$itemsas $field_name{
  269.                     if (!$this->requestField($field_name$required$strict)) {
  270.                         if ($strict{
  271.                             return false;
  272.                         }
  273.                     }
  274.                 }
  275.             }
  276.         }
  277.  
  278.         $this->policy_url Auth_OpenID::arrayGet($args'policy_url');
  279.  
  280.         return true;
  281.     }
  282.  
  283.     /**
  284.      * A list of all of the simple registration fields that were
  285.      * requested, whether they were required or optional.
  286.      */
  287.     function allRequestedFields()
  288.     {
  289.         return array_merge($this->required$this->optional);
  290.     }
  291.  
  292.     /**
  293.      * Have any simple registration fields been requested?
  294.      */
  295.     function wereFieldsRequested()
  296.     {
  297.         return count($this->allRequestedFields());
  298.     }
  299.  
  300.     /**
  301.      * Was this field in the request?
  302.      */
  303.     function contains($field_name)
  304.     {
  305.         return (in_array($field_name$this->required||
  306.                 in_array($field_name$this->optional));
  307.     }
  308.  
  309.     /**
  310.      * Request the specified field from the OpenID user
  311.      *
  312.      * $field_name: the unqualified simple registration field name
  313.      *
  314.      * required: whether the given field should be presented to the
  315.      * user as being a required to successfully complete the request
  316.      *
  317.      * strict: whether to raise an exception when a field is added to
  318.      * a request more than once
  319.      */
  320.     function requestField($field_name,
  321.                           $required=false$strict=false)
  322.     {
  323.         if (!Auth_OpenID_checkFieldName($field_name)) {
  324.             return false;
  325.         }
  326.  
  327.         if ($strict{
  328.             if ($this->contains($field_name)) {
  329.                 return false;
  330.             }
  331.         else {
  332.             if (in_array($field_name$this->required)) {
  333.                 return true;
  334.             }
  335.  
  336.             if (in_array($field_name$this->optional)) {
  337.                 if ($required{
  338.                     unset($this->optional[array_search($field_name,
  339.                                                        $this->optional)]);
  340.                 else {
  341.                     return true;
  342.                 }
  343.             }
  344.         }
  345.  
  346.         if ($required{
  347.             $this->required[$field_name;
  348.         else {
  349.             $this->optional[$field_name;
  350.         }
  351.  
  352.         return true;
  353.     }
  354.  
  355.     /**
  356.      * Add the given list of fields to the request
  357.      *
  358.      * field_names: The simple registration data fields to request
  359.      *
  360.      * required: Whether these values should be presented to the user
  361.      * as required
  362.      *
  363.      * strict: whether to raise an exception when a field is added to
  364.      * a request more than once
  365.      */
  366.     function requestFields($field_names$required=false$strict=false)
  367.     {
  368.         if (!is_array($field_names)) {
  369.             return false;
  370.         }
  371.  
  372.         foreach ($field_names as $field_name{
  373.             if (!$this->requestField($field_name$required$strict=$strict)) {
  374.                 return false;
  375.             }
  376.         }
  377.  
  378.         return true;
  379.     }
  380.  
  381.     /**
  382.      * Get a dictionary of unqualified simple registration arguments
  383.      * representing this request.
  384.      *
  385.      * This method is essentially the inverse of
  386.      * C{L{parseExtensionArgs}}. This method serializes the simple
  387.      * registration request fields.
  388.      */
  389.     function getExtensionArgs()
  390.     {
  391.         $args array();
  392.  
  393.         if ($this->required{
  394.             $args['required'implode(','$this->required);
  395.         }
  396.  
  397.         if ($this->optional{
  398.             $args['optional'implode(','$this->optional);
  399.         }
  400.  
  401.         if ($this->policy_url{
  402.             $args['policy_url'$this->policy_url;
  403.         }
  404.  
  405.         return $args;
  406.     }
  407. }
  408.  
  409. /**
  410.  * Represents the data returned in a simple registration response
  411.  * inside of an OpenID C{id_res} response. This object will be created
  412.  * by the OpenID server, added to the C{id_res} response object, and
  413.  * then extracted from the C{id_res} message by the Consumer.
  414.  *
  415.  * @package OpenID
  416.  */
  417.  
  418.     var $ns_alias = 'sreg';
  419.  
  420.     function Auth_OpenID_SRegResponse($data=null,
  421.                                       $sreg_ns_uri=Auth_OpenID_SREG_NS_URI)
  422.     {
  423.         if ($data === null{
  424.             $this->data array();
  425.         else {
  426.             $this->data $data;
  427.         }
  428.  
  429.         $this->ns_uri = $sreg_ns_uri;
  430.     }
  431.  
  432.     /**
  433.      * Take a C{L{SRegRequest}} and a dictionary of simple
  434.      * registration values and create a C{L{SRegResponse}} object
  435.      * containing that data.
  436.      *
  437.      * request: The simple registration request object
  438.      *
  439.      * data: The simple registration data for this response, as a
  440.      * dictionary from unqualified simple registration field name to
  441.      * string (unicode) value. For instance, the nickname should be
  442.      * stored under the key 'nickname'.
  443.      */
  444.     function extractResponse($request$data)
  445.     {
  446.         $obj new Auth_OpenID_SRegResponse();
  447.         $obj->ns_uri $request->ns_uri;
  448.  
  449.         foreach ($request->allRequestedFields(as $field{
  450.             $value Auth_OpenID::arrayGet($data$field);
  451.             if ($value !== null{
  452.                 $obj->data[$field$value;
  453.             }
  454.         }
  455.  
  456.         return $obj;
  457.     }
  458.  
  459.     /**
  460.      * Create a C{L{SRegResponse}} object from a successful OpenID
  461.      * library response
  462.      * (C{L{openid.consumer.consumer.SuccessResponse}}) response
  463.      * message
  464.      *
  465.      * success_response: A SuccessResponse from consumer.complete()
  466.      *
  467.      * signed_only: Whether to process only data that was
  468.      * signed in the id_res message from the server.
  469.      *
  470.      * Returns a simple registration response containing the data that
  471.      * was supplied with the C{id_res} response.
  472.      */
  473.     function fromSuccessResponse(&$success_response$signed_only=true)
  474.     {
  475.         global $Auth_OpenID_sreg_data_fields;
  476.  
  477.         $obj new Auth_OpenID_SRegResponse();
  478.         $obj->ns_uri $obj->_getSRegNS($success_response->message);
  479.  
  480.         if ($signed_only{
  481.             $args $success_response->getSignedNS($obj->ns_uri);
  482.         else {
  483.             $args $success_response->message->getArgs($obj->ns_uri);
  484.         }
  485.  
  486.         if ($args === null || Auth_OpenID::isFailure($args)) {
  487.             return null;
  488.         }
  489.  
  490.         foreach ($Auth_OpenID_sreg_data_fields as $field_name => $desc{
  491.             if (in_array($field_namearray_keys($args))) {
  492.                 $obj->data[$field_name$args[$field_name];
  493.             }
  494.         }
  495.  
  496.         return $obj;
  497.     }
  498.  
  499.     function getExtensionArgs()
  500.     {
  501.         return $this->data;
  502.     }
  503.  
  504.     // Read-only dictionary interface
  505.         function get($field_name$default=null)
  506.     {
  507.         if (!Auth_OpenID_checkFieldName($field_name)) {
  508.             return null;
  509.         }
  510.  
  511.         return Auth_OpenID::arrayGet($this->data$field_name$default);
  512.     }
  513.  
  514.     function contents()
  515.     {
  516.         return $this->data;
  517.     }
  518. }
  519.  
  520. ?>

Documentation generated on Sat, 14 Nov 2009 11:20:48 +0000 by phpDocumentor 1.3.1