You are here

function openid_extract_namespace in Drupal 7

Extract all the parameters belonging to an extension in a response message.

OpenID 2.0 defines a simple extension mechanism, based on a namespace prefix.

Each request or response can define a prefix using:


  openid.ns.[prefix] = [extension_namespace]
  openid.[prefix].[key1] = [value1]
  openid.[prefix].[key2] = [value2]
  ...

This function extracts all the keys belonging to an extension namespace in a response, optionally using a fallback prefix if none is provided in the response.

Note that you cannot assume that a given extension namespace will use the same prefix on the response and the request: each party may use a different prefix to refer to the same namespace.

Parameters

$response: The response array.

$extension_namespace: The namespace of the extension.

$fallback_prefix: An optional prefix that will be used in case no prefix is found for the target extension namespace.

$only_signed: Return only keys that are included in the message signature in openid.sig. Unsigned fields may have been modified or added by other parties than the OpenID Provider.

Return value

An associative array containing all the parameters in the response message that belong to the extension. The keys are stripped from their namespace prefix.

See also

http://openid.net/specs/openid-authentication-2_0.html#extensions

3 calls to openid_extract_namespace()
OpenIDTestCase::testOpenidExtractNamespace in modules/openid/openid.test
Test openid_extract_namespace().
openid_form_user_register_form_alter in modules/openid/openid.module
Implements hook_form_FORM_ID_alter().
_openid_invalid_openid_transition in modules/openid/openid.inc
Provides transition for accounts with possibly invalid OpenID identifiers in authmap.

File

modules/openid/openid.inc, line 648
OpenID utility functions.

Code

function openid_extract_namespace($response, $extension_namespace, $fallback_prefix = NULL, $only_signed = FALSE) {
  $signed_keys = explode(',', $response['openid.signed']);

  // Find the namespace prefix.
  $prefix = $fallback_prefix;
  foreach ($response as $key => $value) {
    if ($value == $extension_namespace && preg_match('/^openid\\.ns\\.([^.]+)$/', $key, $matches)) {
      $prefix = $matches[1];
      if ($only_signed && !in_array('ns.' . $matches[1], $signed_keys)) {

        // The namespace was defined but was not signed as required. In this
        // case we do not fall back to $fallback_prefix.
        $prefix = NULL;
      }
      break;
    }
  }

  // Now extract the namespace keys from the response.
  $output = array();
  if (!isset($prefix)) {
    return $output;
  }
  foreach ($response as $key => $value) {
    if (preg_match('/^openid\\.' . $prefix . '\\.(.+)$/', $key, $matches)) {
      $local_key = $matches[1];
      if (!$only_signed || in_array($prefix . '.' . $local_key, $signed_keys)) {
        $output[$local_key] = $value;
      }
    }
  }
  return $output;
}