4 * Simple registration request and response parsing and object
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.
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:
15 * $sreg_req = Auth_OpenID_SRegRequest::build(array('email'));
16 * $auth_request->addExtension($sreg_req);
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:
24 * $sreg_req = Auth_OpenID_SRegRequest::fromOpenIDRequest(
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);
32 * 3. The relying party uses {@link
33 * Auth_OpenID_SRegResponse::fromSuccessResponse} to extract the data
34 * from the OpenID response:
36 * $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse(
43 * Import message and extension internals.
45 require_once 'Auth/OpenID/Message.php';
46 require_once 'Auth/OpenID/Extension.php';
48 // The data fields that are listed in the sreg spec
49 global $Auth_OpenID_sreg_data_fields;
50 $Auth_OpenID_sreg_data_fields = array(
51 'fullname' => 'Full Name',
52 'nickname' => 'Nickname',
53 'dob' => 'Date of Birth',
54 'email' => 'E-mail Address',
56 'postcode' => 'Postal Code',
57 'country' => 'Country',
58 'language' => 'Language',
59 'timezone' => 'Time Zone');
62 * Check to see that the given value is a valid simple registration
63 * data field name. Return true if so, false if not.
65 function Auth_OpenID_checkFieldName($field_name)
67 global $Auth_OpenID_sreg_data_fields;
69 if (!in_array($field_name, array_keys($Auth_OpenID_sreg_data_fields))) {
75 // URI used in the wild for Yadis documents advertising simple
76 // registration support
77 define('Auth_OpenID_SREG_NS_URI_1_0', 'http://openid.net/sreg/1.0');
79 // URI in the draft specification for simple registration 1.1
80 // <http://openid.net/specs/openid-simple-registration-extension-1_1-01.html>
81 define('Auth_OpenID_SREG_NS_URI_1_1', 'http://openid.net/extensions/sreg/1.1');
83 // This attribute will always hold the preferred URI to use when
84 // adding sreg support to an XRDS file or in an OpenID namespace
86 define('Auth_OpenID_SREG_NS_URI', Auth_OpenID_SREG_NS_URI_1_1);
88 Auth_OpenID_registerNamespaceAlias(Auth_OpenID_SREG_NS_URI_1_1, 'sreg');
91 * Does the given endpoint advertise support for simple
94 * $endpoint: The endpoint object as returned by OpenID discovery.
95 * returns whether an sreg type was advertised by the endpoint
97 function Auth_OpenID_supportsSReg($endpoint)
99 return ($endpoint->usesExtension(Auth_OpenID_SREG_NS_URI_1_1) ||
100 $endpoint->usesExtension(Auth_OpenID_SREG_NS_URI_1_0));
104 * A base class for classes dealing with Simple Registration protocol
109 class Auth_OpenID_SRegBase extends Auth_OpenID_Extension {
111 * Extract the simple registration namespace URI from the given
112 * OpenID message. Handles OpenID 1 and 2, as well as both sreg
113 * namespace URIs found in the wild, as well as missing namespace
114 * definitions (for OpenID 1)
116 * $message: The OpenID message from which to parse simple
117 * registration fields. This may be a request or response message.
119 * Returns the sreg namespace URI for the supplied message. The
120 * message may be modified to define a simple registration
125 static function _getSRegNS($message)
128 $found_ns_uri = null;
130 // See if there exists an alias for one of the two defined
131 // simple registration types.
132 foreach (array(Auth_OpenID_SREG_NS_URI_1_1,
133 Auth_OpenID_SREG_NS_URI_1_0) as $sreg_ns_uri) {
134 $alias = $message->namespaces->getAlias($sreg_ns_uri);
135 if ($alias !== null) {
136 $found_ns_uri = $sreg_ns_uri;
141 if ($alias === null) {
142 // There is no alias for either of the types, so try to
143 // add one. We default to using the modern value (1.1)
144 $found_ns_uri = Auth_OpenID_SREG_NS_URI_1_1;
145 if ($message->namespaces->addAlias(Auth_OpenID_SREG_NS_URI_1_1,
147 // An alias for the string 'sreg' already exists, but
148 // it's defined for something other than simple
154 return $found_ns_uri;
159 * An object to hold the state of a simple registration request.
161 * required: A list of the required fields in this simple registration
164 * optional: A list of the optional fields in this simple registration
169 class Auth_OpenID_SRegRequest extends Auth_OpenID_SRegBase {
171 var $ns_alias = 'sreg';
174 * Initialize an empty simple registration request.
176 static function build($required=null, $optional=null,
178 $sreg_ns_uri=Auth_OpenID_SREG_NS_URI,
179 $cls='Auth_OpenID_SRegRequest')
183 $obj->required = array();
184 $obj->optional = array();
185 $obj->policy_url = $policy_url;
186 $obj->ns_uri = $sreg_ns_uri;
189 if (!$obj->requestFields($required, true, true)) {
195 if (!$obj->requestFields($optional, false, true)) {
204 * Create a simple registration request that contains the fields
205 * that were requested in the OpenID request with the given
208 * $request: The OpenID authentication request from which to
209 * extract an sreg request.
211 * $cls: name of class to use when creating sreg request object.
214 * Returns the newly created simple registration request
216 static function fromOpenIDRequest($request, $cls='Auth_OpenID_SRegRequest')
219 $obj = call_user_func_array(array($cls, 'build'),
220 array(null, null, null, Auth_OpenID_SREG_NS_URI, $cls));
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;
226 $obj->ns_uri = $obj->_getSRegNS($m);
227 $args = $m->getArgs($obj->ns_uri);
229 if ($args === null || Auth_OpenID::isFailure($args)) {
233 $obj->parseExtensionArgs($args);
239 * Parse the unqualified simple registration request parameters
240 * and add them to this object.
242 * This method is essentially the inverse of
243 * getExtensionArgs. This method restores the serialized simple
244 * registration request fields.
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.
253 * $args == $message->getArgs($ns_uri);
254 * $request->parseExtensionArgs($args);
256 * $args: The unqualified simple registration arguments
258 * strict: Whether requests with fields that are not defined in
259 * the simple registration specification should be tolerated (and
262 function parseExtensionArgs($args, $strict=false)
264 foreach (array('required', 'optional') as $list_name) {
265 $required = ($list_name == 'required');
266 $items = Auth_OpenID::arrayGet($args, $list_name);
268 foreach (explode(',', $items) as $field_name) {
269 if (!$this->requestField($field_name, $required, $strict)) {
278 $this->policy_url = Auth_OpenID::arrayGet($args, 'policy_url');
284 * A list of all of the simple registration fields that were
285 * requested, whether they were required or optional.
287 function allRequestedFields()
289 return array_merge($this->required, $this->optional);
293 * Have any simple registration fields been requested?
295 function wereFieldsRequested()
297 return count($this->allRequestedFields());
301 * Was this field in the request?
303 function contains($field_name)
305 return (in_array($field_name, $this->required) ||
306 in_array($field_name, $this->optional));
310 * Request the specified field from the OpenID user
312 * $field_name: the unqualified simple registration field name
314 * required: whether the given field should be presented to the
315 * user as being a required to successfully complete the request
317 * strict: whether to raise an exception when a field is added to
318 * a request more than once
320 function requestField($field_name,
321 $required=false, $strict=false)
323 if (!Auth_OpenID_checkFieldName($field_name)) {
328 if ($this->contains($field_name)) {
332 if (in_array($field_name, $this->required)) {
336 if (in_array($field_name, $this->optional)) {
338 unset($this->optional[array_search($field_name,
347 $this->required[] = $field_name;
349 $this->optional[] = $field_name;
356 * Add the given list of fields to the request
358 * field_names: The simple registration data fields to request
360 * required: Whether these values should be presented to the user
363 * strict: whether to raise an exception when a field is added to
364 * a request more than once
366 function requestFields($field_names, $required=false, $strict=false)
368 if (!is_array($field_names)) {
372 foreach ($field_names as $field_name) {
373 if (!$this->requestField($field_name, $required, $strict=$strict)) {
382 * Get a dictionary of unqualified simple registration arguments
383 * representing this request.
385 * This method is essentially the inverse of
386 * C{L{parseExtensionArgs}}. This method serializes the simple
387 * registration request fields.
389 function getExtensionArgs()
393 if ($this->required) {
394 $args['required'] = implode(',', $this->required);
397 if ($this->optional) {
398 $args['optional'] = implode(',', $this->optional);
401 if ($this->policy_url) {
402 $args['policy_url'] = $this->policy_url;
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.
417 class Auth_OpenID_SRegResponse extends Auth_OpenID_SRegBase {
419 var $ns_alias = 'sreg';
421 function Auth_OpenID_SRegResponse($data=null,
422 $sreg_ns_uri=Auth_OpenID_SREG_NS_URI)
424 if ($data === null) {
425 $this->data = array();
430 $this->ns_uri = $sreg_ns_uri;
434 * Take a C{L{SRegRequest}} and a dictionary of simple
435 * registration values and create a C{L{SRegResponse}} object
436 * containing that data.
438 * request: The simple registration request object
440 * data: The simple registration data for this response, as a
441 * dictionary from unqualified simple registration field name to
442 * string (unicode) value. For instance, the nickname should be
443 * stored under the key 'nickname'.
445 static function extractResponse($request, $data)
447 $obj = new Auth_OpenID_SRegResponse();
448 $obj->ns_uri = $request->ns_uri;
450 foreach ($request->allRequestedFields() as $field) {
451 $value = Auth_OpenID::arrayGet($data, $field);
452 if ($value !== null) {
453 $obj->data[$field] = $value;
461 * Create a C{L{SRegResponse}} object from a successful OpenID
463 * (C{L{openid.consumer.consumer.SuccessResponse}}) response
466 * success_response: A SuccessResponse from consumer.complete()
468 * signed_only: Whether to process only data that was
469 * signed in the id_res message from the server.
471 * Returns a simple registration response containing the data that
472 * was supplied with the C{id_res} response.
474 static function fromSuccessResponse($success_response, $signed_only=true)
476 global $Auth_OpenID_sreg_data_fields;
478 $obj = new Auth_OpenID_SRegResponse();
479 $obj->ns_uri = $obj->_getSRegNS($success_response->message);
482 $args = $success_response->getSignedNS($obj->ns_uri);
484 $args = $success_response->message->getArgs($obj->ns_uri);
487 if ($args === null || Auth_OpenID::isFailure($args)) {
491 foreach ($Auth_OpenID_sreg_data_fields as $field_name => $desc) {
492 if (in_array($field_name, array_keys($args))) {
493 $obj->data[$field_name] = $args[$field_name];
500 function getExtensionArgs()
505 // Read-only dictionary interface
506 function get($field_name, $default=null)
508 if (!Auth_OpenID_checkFieldName($field_name)) {
512 return Auth_OpenID::arrayGet($this->data, $field_name, $default);