<?php |
<?php |
/** |
/** |
* This class provides a simple interface for OpenID (1.1 and 2.0) authentication. |
* This class provides a simple interface for OpenID (1.1 and 2.0) authentication. |
* Supports Yadis discovery. |
* Supports Yadis discovery. |
* The authentication process is stateless/dumb. |
* The authentication process is stateless/dumb. |
* |
* |
* Usage: |
* Usage: |
* Sign-on with OpenID is a two step process: |
* Sign-on with OpenID is a two step process: |
* Step one is authentication with the provider: |
* Step one is authentication with the provider: |
* <code> |
* <code> |
* $openid = new LightOpenID('my-host.example.org'); |
* $openid = new LightOpenID('my-host.example.org'); |
* $openid->identity = 'ID supplied by user'; |
* $openid->identity = 'ID supplied by user'; |
* header('Location: ' . $openid->authUrl()); |
* header('Location: ' . $openid->authUrl()); |
* </code> |
* </code> |
* The provider then sends various parameters via GET, one of them is openid_mode. |
* The provider then sends various parameters via GET, one of them is openid_mode. |
* Step two is verification: |
* Step two is verification: |
* <code> |
* <code> |
* if ($this->data['openid_mode']) { |
* if ($this->data['openid_mode']) { |
* $openid = new LightOpenID('my-host.example.org'); |
* $openid = new LightOpenID('my-host.example.org'); |
* echo $openid->validate() ? 'Logged in.' : 'Failed'; |
* echo $openid->validate() ? 'Logged in.' : 'Failed'; |
* } |
* } |
* </code> |
* </code> |
* |
* |
* Change the 'my-host.example.org' to your domain name. Do NOT use $_SERVER['HTTP_HOST'] |
* Change the 'my-host.example.org' to your domain name. Do NOT use $_SERVER['HTTP_HOST'] |
* for that, unless you know what you are doing. |
* for that, unless you know what you are doing. |
* |
* |
* Optionally, you can set $returnUrl and $realm (or $trustRoot, which is an alias). |
* Optionally, you can set $returnUrl and $realm (or $trustRoot, which is an alias). |
* The default values for those are: |
* The default values for those are: |
* $openid->realm = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; |
* $openid->realm = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; |
* $openid->returnUrl = $openid->realm . $_SERVER['REQUEST_URI']; |
* $openid->returnUrl = $openid->realm . $_SERVER['REQUEST_URI']; |
* If you don't know their meaning, refer to any openid tutorial, or specification. Or just guess. |
* If you don't know their meaning, refer to any openid tutorial, or specification. Or just guess. |
* |
* |
* AX and SREG extensions are supported. |
* AX and SREG extensions are supported. |
* To use them, specify $openid->required and/or $openid->optional before calling $openid->authUrl(). |
* To use them, specify $openid->required and/or $openid->optional before calling $openid->authUrl(). |
* These are arrays, with values being AX schema paths (the 'path' part of the URL). |
* These are arrays, with values being AX schema paths (the 'path' part of the URL). |
* For example: |
* For example: |
* $openid->required = array('namePerson/friendly', 'contact/email'); |
* $openid->required = array('namePerson/friendly', 'contact/email'); |
* $openid->optional = array('namePerson/first'); |
* $openid->optional = array('namePerson/first'); |
* If the server supports only SREG or OpenID 1.1, these are automaticaly |
* If the server supports only SREG or OpenID 1.1, these are automaticaly |
* mapped to SREG names, so that user doesn't have to know anything about the server. |
* mapped to SREG names, so that user doesn't have to know anything about the server. |
* |
* |
* To get the values, use $openid->getAttributes(). |
* To get the values, use $openid->getAttributes(). |
* |
* |
* |
* |
* The library requires PHP >= 5.1.2 with curl or http/https stream wrappers enabled. |
* The library requires PHP >= 5.1.2 with curl or http/https stream wrappers enabled. |
* @author Mewp |
* @author Mewp |
* @copyright Copyright (c) 2010, Mewp |
* @copyright Copyright (c) 2010, Mewp |
* @license http://www.opensource.org/licenses/mit-license.php MIT |
* @license http://www.opensource.org/licenses/mit-license.php MIT |
*/ |
*/ |
class LightOpenID |
class LightOpenID |
{ |
{ |
public $returnUrl |
public $returnUrl |
, $required = array() |
, $required = array() |
, $optional = array() |
, $optional = array() |
, $verify_peer = null |
, $verify_peer = null |
, $capath = null |
, $capath = null |
, $cainfo = null |
, $cainfo = null |
, $data; |
, $data; |
private $identity, $claimed_id; |
private $identity, $claimed_id; |
protected $server, $version, $trustRoot, $aliases, $identifier_select = false |
protected $server, $version, $trustRoot, $aliases, $identifier_select = false |
, $ax = false, $sreg = false, $setup_url = null; |
, $ax = false, $sreg = false, $setup_url = null; |
static protected $ax_to_sreg = array( |
static protected $ax_to_sreg = array( |
'namePerson/friendly' => 'nickname', |
'namePerson/friendly' => 'nickname', |
'contact/email' => 'email', |
'contact/email' => 'email', |
'namePerson' => 'fullname', |
'namePerson' => 'fullname', |
'birthDate' => 'dob', |
'birthDate' => 'dob', |
'person/gender' => 'gender', |
'person/gender' => 'gender', |
'contact/postalCode/home' => 'postcode', |
'contact/postalCode/home' => 'postcode', |
'contact/country/home' => 'country', |
'contact/country/home' => 'country', |
'pref/language' => 'language', |
'pref/language' => 'language', |
'pref/timezone' => 'timezone', |
'pref/timezone' => 'timezone', |
); |
); |
|
|
function __construct($host) |
function __construct($host) |
{ |
{ |
$this->trustRoot = (strpos($host, '://') ? $host : 'http://' . $host); |
$this->trustRoot = (strpos($host, '://') ? $host : 'http://' . $host); |
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') |
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') |
|| (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) |
|| (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) |
&& $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') |
&& $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') |
) { |
) { |
$this->trustRoot = (strpos($host, '://') ? $host : 'https://' . $host); |
$this->trustRoot = (strpos($host, '://') ? $host : 'https://' . $host); |
} |
} |
|
|
if(($host_end = strpos($this->trustRoot, '/', 8)) !== false) { |
if(($host_end = @strpos($this->trustRoot, '/', 8)) !== false) { |
$this->trustRoot = substr($this->trustRoot, 0, $host_end); |
$this->trustRoot = substr($this->trustRoot, 0, $host_end); |
} |
} |
|
|
$uri = rtrim(preg_replace('#((?<=\?)|&)openid\.[^&]+#', '', $_SERVER['REQUEST_URI']), '?'); |
$uri = rtrim(preg_replace('#((?<=\?)|&)openid\.[^&]+#', '', $_SERVER['REQUEST_URI']), '?'); |
$this->returnUrl = $this->trustRoot . $uri; |
$this->returnUrl = $this->trustRoot . $uri; |
|
|
$this->data = ($_SERVER['REQUEST_METHOD'] === 'POST') ? $_POST : $_GET; |
$this->data = ($_SERVER['REQUEST_METHOD'] === 'POST') ? $_POST : $_GET; |
|
|
if(!function_exists('curl_init') && !in_array('https', stream_get_wrappers())) { |
if(!function_exists('curl_init') && !in_array('https', stream_get_wrappers())) { |
throw new ErrorException('You must have either https wrappers or curl enabled.'); |
throw new ErrorException('You must have either https wrappers or curl enabled.'); |
} |
} |
} |
} |
|
|
function __set($name, $value) |
function __set($name, $value) |
{ |
{ |
switch ($name) { |
switch ($name) { |
case 'identity': |
case 'identity': |
if (strlen($value = trim((String) $value))) { |
if (strlen($value = trim((String) $value))) { |
if (preg_match('#^xri:/*#i', $value, $m)) { |
if (preg_match('#^xri:/*#i', $value, $m)) { |
$value = substr($value, strlen($m[0])); |
$value = substr($value, strlen($m[0])); |
} elseif (!preg_match('/^(?:[=@+\$!\(]|https?:)/i', $value)) { |
} elseif (!preg_match('/^(?:[=@+\$!\(]|https?:)/i', $value)) { |
$value = "http://$value"; |
$value = "http://$value"; |
} |
} |
if (preg_match('#^https?://[^/]+$#i', $value, $m)) { |
if (preg_match('#^https?://[^/]+$#i', $value, $m)) { |
$value .= '/'; |
$value .= '/'; |
} |
} |
} |
} |
$this->$name = $this->claimed_id = $value; |
$this->$name = $this->claimed_id = $value; |
break; |
break; |
case 'trustRoot': |
case 'trustRoot': |
case 'realm': |
case 'realm': |
$this->trustRoot = trim($value); |
$this->trustRoot = trim($value); |
} |
} |
} |
} |
|
|
function __get($name) |
function __get($name) |
{ |
{ |
switch ($name) { |
switch ($name) { |
case 'identity': |
case 'identity': |
# We return claimed_id instead of identity, |
# We return claimed_id instead of identity, |
# because the developer should see the claimed identifier, |
# because the developer should see the claimed identifier, |
# i.e. what he set as identity, not the op-local identifier (which is what we verify) |
# i.e. what he set as identity, not the op-local identifier (which is what we verify) |
return $this->claimed_id; |
return $this->claimed_id; |
case 'trustRoot': |
case 'trustRoot': |
case 'realm': |
case 'realm': |
return $this->trustRoot; |
return $this->trustRoot; |
case 'mode': |
case 'mode': |
return empty($this->data['openid_mode']) ? null : $this->data['openid_mode']; |
return empty($this->data['openid_mode']) ? null : $this->data['openid_mode']; |
} |
} |
} |
} |
|
|
/** |
/** |
* Checks if the server specified in the url exists. |
* Checks if the server specified in the url exists. |
* |
* |
* @param $url url to check |
* @param $url url to check |
* @return true, if the server exists; false otherwise |
* @return true, if the server exists; false otherwise |
*/ |
*/ |
function hostExists($url) |
function hostExists($url) |
{ |
{ |
if (strpos($url, '/') === false) { |
if (strpos($url, '/') === false) { |
$server = $url; |
$server = $url; |
} else { |
} else { |
$server = @parse_url($url, PHP_URL_HOST); |
$server = @parse_url($url, PHP_URL_HOST); |
} |
} |
|
|
if (!$server) { |
if (!$server) { |
return false; |
return false; |
} |
} |
|
|
return !!gethostbynamel($server); |
return !!gethostbynamel($server); |
} |
} |
|
|
protected function request_curl($url, $method='GET', $params=array()) |
protected function request_curl($url, $method='GET', $params=array()) |
{ |
{ |
$params = http_build_query($params, '', '&'); |
$params = http_build_query($params, '', '&'); |
$curl = curl_init($url . ($method == 'GET' && $params ? '?' . $params : '')); |
$curl = curl_init($url . ($method == 'GET' && $params ? '?' . $params : '')); |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
curl_setopt($curl, CURLOPT_HEADER, false); |
curl_setopt($curl, CURLOPT_HEADER, false); |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/xrds+xml, */*')); |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/xrds+xml, */*')); |
|
|
if($this->verify_peer !== null) { |
if($this->verify_peer !== null) { |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verify_peer); |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verify_peer); |
if($this->capath) { |
if($this->capath) { |
curl_setopt($curl, CURLOPT_CAPATH, $this->capath); |
curl_setopt($curl, CURLOPT_CAPATH, $this->capath); |
} |
} |
|
|
if($this->cainfo) { |
if($this->cainfo) { |
curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo); |
curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo); |
} |
} |
} |
} |
|
|
if ($method == 'POST') { |
if ($method == 'POST') { |
curl_setopt($curl, CURLOPT_POST, true); |
curl_setopt($curl, CURLOPT_POST, true); |
curl_setopt($curl, CURLOPT_POSTFIELDS, $params); |
curl_setopt($curl, CURLOPT_POSTFIELDS, $params); |
} elseif ($method == 'HEAD') { |
} elseif ($method == 'HEAD') { |
curl_setopt($curl, CURLOPT_HEADER, true); |
curl_setopt($curl, CURLOPT_HEADER, true); |
curl_setopt($curl, CURLOPT_NOBODY, true); |
curl_setopt($curl, CURLOPT_NOBODY, true); |
} else { |
} else { |
curl_setopt($curl, CURLOPT_HTTPGET, true); |
curl_setopt($curl, CURLOPT_HTTPGET, true); |
} |
} |
$response = curl_exec($curl); |
$response = curl_exec($curl); |
|
|
if($method == 'HEAD') { |
if($method == 'HEAD') { |
$headers = array(); |
$headers = array(); |
foreach(explode("\n", $response) as $header) { |
foreach(explode("\n", $response) as $header) { |
$pos = strpos($header,':'); |
$pos = strpos($header,':'); |
$name = strtolower(trim(substr($header, 0, $pos))); |
$name = strtolower(trim(substr($header, 0, $pos))); |
$headers[$name] = trim(substr($header, $pos+1)); |
$headers[$name] = trim(substr($header, $pos+1)); |
} |
} |
|
|
# Updating claimed_id in case of redirections. |
# Updating claimed_id in case of redirections. |
$effective_url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); |
$effective_url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); |
if($effective_url != $url) { |
if($effective_url != $url) { |
$this->identity = $this->claimed_id = $effective_url; |
$this->identity = $this->claimed_id = $effective_url; |
} |
} |
|
|
return $headers; |
return $headers; |
} |
} |
|
|
if (curl_errno($curl)) { |
if (curl_errno($curl)) { |
throw new ErrorException(curl_error($curl), curl_errno($curl)); |
throw new ErrorException(curl_error($curl), curl_errno($curl)); |
} |
} |
|
|
return $response; |
return $response; |
} |
} |
|
|
protected function request_streams($url, $method='GET', $params=array()) |
protected function request_streams($url, $method='GET', $params=array()) |
{ |
{ |
if(!$this->hostExists($url)) { |
if(!$this->hostExists($url)) { |
throw new ErrorException("Could not connect to $url.", 404); |
throw new ErrorException("Could not connect to $url.", 404); |
} |
} |
|
|
$params = http_build_query($params, '', '&'); |
$params = http_build_query($params, '', '&'); |
switch($method) { |
switch($method) { |
case 'GET': |
case 'GET': |
$opts = array( |
$opts = array( |
'http' => array( |
'http' => array( |
'method' => 'GET', |
'method' => 'GET', |
'header' => 'Accept: application/xrds+xml, */*', |
'header' => 'Accept: application/xrds+xml, */*', |
'ignore_errors' => true, |
'ignore_errors' => true, |
), 'ssl' => array( |
), 'ssl' => array( |
'CN_match' => parse_url($url, PHP_URL_HOST), |
'CN_match' => parse_url($url, PHP_URL_HOST), |
), |
), |
); |
); |
$url = $url . ($params ? '?' . $params : ''); |
$url = $url . ($params ? '?' . $params : ''); |
break; |
break; |
case 'POST': |
case 'POST': |
$opts = array( |
$opts = array( |
'http' => array( |
'http' => array( |
'method' => 'POST', |
'method' => 'POST', |
'header' => 'Content-type: application/x-www-form-urlencoded', |
'header' => 'Content-type: application/x-www-form-urlencoded', |
'content' => $params, |
'content' => $params, |
'ignore_errors' => true, |
'ignore_errors' => true, |
), 'ssl' => array( |
), 'ssl' => array( |
'CN_match' => parse_url($url, PHP_URL_HOST), |
'CN_match' => parse_url($url, PHP_URL_HOST), |
), |
), |
); |
); |
break; |
break; |
case 'HEAD': |
case 'HEAD': |
# We want to send a HEAD request, |
# We want to send a HEAD request, |
# but since get_headers doesn't accept $context parameter, |
# but since get_headers doesn't accept $context parameter, |
# we have to change the defaults. |
# we have to change the defaults. |
$default = stream_context_get_options(stream_context_get_default()); |
$default = stream_context_get_options(stream_context_get_default()); |
stream_context_get_default( |
stream_context_get_default( |
array( |
array( |
'http' => array( |
'http' => array( |
'method' => 'HEAD', |
'method' => 'HEAD', |
'header' => 'Accept: application/xrds+xml, */*', |
'header' => 'Accept: application/xrds+xml, */*', |
'ignore_errors' => true, |
'ignore_errors' => true, |
), 'ssl' => array( |
), 'ssl' => array( |
'CN_match' => parse_url($url, PHP_URL_HOST), |
'CN_match' => parse_url($url, PHP_URL_HOST), |
), |
), |
) |
) |
); |
); |
|
|
$url = $url . ($params ? '?' . $params : ''); |
$url = $url . ($params ? '?' . $params : ''); |
$headers_tmp = get_headers ($url); |
$headers_tmp = get_headers ($url); |
if(!$headers_tmp) { |
if(!$headers_tmp) { |
return array(); |
return array(); |
} |
} |
|
|
# Parsing headers. |
# Parsing headers. |
$headers = array(); |
$headers = array(); |
foreach($headers_tmp as $header) { |
foreach($headers_tmp as $header) { |
$pos = strpos($header,':'); |
$pos = strpos($header,':'); |
$name = strtolower(trim(substr($header, 0, $pos))); |
$name = strtolower(trim(substr($header, 0, $pos))); |
$headers[$name] = trim(substr($header, $pos+1)); |
$headers[$name] = trim(substr($header, $pos+1)); |
|
|
# Following possible redirections. The point is just to have |
# Following possible redirections. The point is just to have |
# claimed_id change with them, because get_headers() will |
# claimed_id change with them, because get_headers() will |
# follow redirections automatically. |
# follow redirections automatically. |
# We ignore redirections with relative paths. |
# We ignore redirections with relative paths. |
# If any known provider uses them, file a bug report. |
# If any known provider uses them, file a bug report. |
if($name == 'location') { |
if($name == 'location') { |
if(strpos($headers[$name], 'http') === 0) { |
if(strpos($headers[$name], 'http') === 0) { |
$this->identity = $this->claimed_id = $headers[$name]; |
$this->identity = $this->claimed_id = $headers[$name]; |
} elseif($headers[$name][0] == '/') { |
} elseif($headers[$name][0] == '/') { |
$parsed_url = parse_url($this->claimed_id); |
$parsed_url = parse_url($this->claimed_id); |
$this->identity = |
$this->identity = |
$this->claimed_id = $parsed_url['scheme'] . '://' |
$this->claimed_id = $parsed_url['scheme'] . '://' |
. $parsed_url['host'] |
. $parsed_url['host'] |
. $headers[$name]; |
. $headers[$name]; |
} |
} |
} |
} |
} |
} |
|
|
# And restore them. |
# And restore them. |
stream_context_get_default($default); |
stream_context_get_default($default); |
return $headers; |
return $headers; |
} |
} |
|
|
if($this->verify_peer) { |
if($this->verify_peer) { |
$opts['ssl'] += array( |
$opts['ssl'] += array( |
'verify_peer' => true, |
'verify_peer' => true, |
'capath' => $this->capath, |
'capath' => $this->capath, |
'cafile' => $this->cainfo, |
'cafile' => $this->cainfo, |
); |
); |
} |
} |
|
|
$context = stream_context_create ($opts); |
$context = stream_context_create ($opts); |
|
|
return file_get_contents($url, false, $context); |
return file_get_contents($url, false, $context); |
} |
} |
|
|
protected function request($url, $method='GET', $params=array()) |
protected function request($url, $method='GET', $params=array()) |
{ |
{ |
if (function_exists('curl_init') |
if (function_exists('curl_init') |
&& (!in_array('https', stream_get_wrappers()) || !ini_get('safe_mode') && !ini_get('open_basedir')) |
&& (!in_array('https', stream_get_wrappers()) || !ini_get('safe_mode') && !ini_get('open_basedir')) |
) { |
) { |
return $this->request_curl($url, $method, $params); |
return $this->request_curl($url, $method, $params); |
} |
} |
return $this->request_streams($url, $method, $params); |
return $this->request_streams($url, $method, $params); |
} |
} |
|
|
protected function build_url($url, $parts) |
protected function build_url($url, $parts) |
{ |
{ |
if (isset($url['query'], $parts['query'])) { |
if (isset($url['query'], $parts['query'])) { |
$parts['query'] = $url['query'] . '&' . $parts['query']; |
$parts['query'] = $url['query'] . '&' . $parts['query']; |
} |
} |
|
|
$url = $parts + $url; |
$url = $parts + $url; |
$url = $url['scheme'] . '://' |
$url = $url['scheme'] . '://' |
. (empty($url['username'])?'' |
. (empty($url['username'])?'' |
:(empty($url['password'])? "{$url['username']}@" |
:(empty($url['password'])? "{$url['username']}@" |
:"{$url['username']}:{$url['password']}@")) |
:"{$url['username']}:{$url['password']}@")) |
. $url['host'] |
. $url['host'] |
. (empty($url['port'])?'':":{$url['port']}") |
. (empty($url['port'])?'':":{$url['port']}") |
. (empty($url['path'])?'':$url['path']) |
. (empty($url['path'])?'':$url['path']) |
. (empty($url['query'])?'':"?{$url['query']}") |
. (empty($url['query'])?'':"?{$url['query']}") |
. (empty($url['fragment'])?'':"#{$url['fragment']}"); |
. (empty($url['fragment'])?'':"#{$url['fragment']}"); |
return $url; |
return $url; |
} |
} |
|
|
/** |
/** |
* Helper function used to scan for <meta>/<link> tags and extract information |
* Helper function used to scan for <meta>/<link> tags and extract information |
* from them |
* from them |
*/ |
*/ |
protected function htmlTag($content, $tag, $attrName, $attrValue, $valueName) |
protected function htmlTag($content, $tag, $attrName, $attrValue, $valueName) |
{ |
{ |
preg_match_all("#<{$tag}[^>]*$attrName=['\"].*?$attrValue.*?['\"][^>]*$valueName=['\"](.+?)['\"][^>]*/?>#i", $content, $matches1); |
preg_match_all("#<{$tag}[^>]*$attrName=['\"].*?$attrValue.*?['\"][^>]*$valueName=['\"](.+?)['\"][^>]*/?>#i", $content, $matches1); |
preg_match_all("#<{$tag}[^>]*$valueName=['\"](.+?)['\"][^>]*$attrName=['\"].*?$attrValue.*?['\"][^>]*/?>#i", $content, $matches2); |
preg_match_all("#<{$tag}[^>]*$valueName=['\"](.+?)['\"][^>]*$attrName=['\"].*?$attrValue.*?['\"][^>]*/?>#i", $content, $matches2); |
|
|
$result = array_merge($matches1[1], $matches2[1]); |
$result = array_merge($matches1[1], $matches2[1]); |
return empty($result)?false:$result[0]; |
return empty($result)?false:$result[0]; |
} |
} |
|
|
/** |
/** |
* Performs Yadis and HTML discovery. Normally not used. |
* Performs Yadis and HTML discovery. Normally not used. |
* @param $url Identity URL. |
* @param $url Identity URL. |
* @return String OP Endpoint (i.e. OpenID provider address). |
* @return String OP Endpoint (i.e. OpenID provider address). |
* @throws ErrorException |
* @throws ErrorException |
*/ |
*/ |
function discover($url) |
function discover($url) |
{ |
{ |
if (!$url) throw new ErrorException('No identity supplied.'); |
if (!$url) throw new ErrorException('No identity supplied.'); |
# Use xri.net proxy to resolve i-name identities |
# Use xri.net proxy to resolve i-name identities |
if (!preg_match('#^https?:#', $url)) { |
if (!preg_match('#^https?:#', $url)) { |
$url = "https://xri.net/$url"; |
$url = "https://xri.net/$url"; |
} |
} |
|
|
# We save the original url in case of Yadis discovery failure. |
# We save the original url in case of Yadis discovery failure. |
# It can happen when we'll be lead to an XRDS document |
# It can happen when we'll be lead to an XRDS document |
# which does not have any OpenID2 services. |
# which does not have any OpenID2 services. |
$originalUrl = $url; |
$originalUrl = $url; |
|
|
# A flag to disable yadis discovery in case of failure in headers. |
# A flag to disable yadis discovery in case of failure in headers. |
$yadis = true; |
$yadis = true; |
|
|
# We'll jump a maximum of 5 times, to avoid endless redirections. |
# We'll jump a maximum of 5 times, to avoid endless redirections. |
for ($i = 0; $i < 5; $i ++) { |
for ($i = 0; $i < 5; $i ++) { |
if ($yadis) { |
if ($yadis) { |
$headers = $this->request($url, 'HEAD'); |
$headers = $this->request($url, 'HEAD'); |
|
|
$next = false; |
$next = false; |
if (isset($headers['x-xrds-location'])) { |
if (isset($headers['x-xrds-location'])) { |
$url = $this->build_url(parse_url($url), parse_url(trim($headers['x-xrds-location']))); |
$url = $this->build_url(parse_url($url), parse_url(trim($headers['x-xrds-location']))); |
$next = true; |
$next = true; |
} |
} |
|
|
if (isset($headers['content-type']) |
if (isset($headers['content-type']) |
&& (strpos($headers['content-type'], 'application/xrds+xml') !== false |
&& (strpos($headers['content-type'], 'application/xrds+xml') !== false |
|| strpos($headers['content-type'], 'text/xml') !== false) |
|| strpos($headers['content-type'], 'text/xml') !== false) |
) { |
) { |
# Apparently, some providers return XRDS documents as text/html. |
# Apparently, some providers return XRDS documents as text/html. |
# While it is against the spec, allowing this here shouldn't break |
# While it is against the spec, allowing this here shouldn't break |
# compatibility with anything. |
# compatibility with anything. |
# --- |
# --- |
# Found an XRDS document, now let's find the server, and optionally delegate. |
# Found an XRDS document, now let's find the server, and optionally delegate. |
$content = $this->request($url, 'GET'); |
$content = $this->request($url, 'GET'); |
|
|
preg_match_all('#<Service.*?>(.*?)</Service>#s', $content, $m); |
preg_match_all('#<Service.*?>(.*?)</Service>#s', $content, $m); |
foreach($m[1] as $content) { |
foreach($m[1] as $content) { |
$content = ' ' . $content; # The space is added, so that strpos doesn't return 0. |
$content = ' ' . $content; # The space is added, so that strpos doesn't return 0. |
|
|
# OpenID 2 |
# OpenID 2 |
$ns = preg_quote('http://specs.openid.net/auth/2.0/'); |
$ns = preg_quote('http://specs.openid.net/auth/2.0/'); |
if(preg_match('#<Type>\s*'.$ns.'(server|signon)\s*</Type>#s', $content, $type)) { |
if(preg_match('#<Type>\s*'.$ns.'(server|signon)\s*</Type>#s', $content, $type)) { |
if ($type[1] == 'server') $this->identifier_select = true; |
if ($type[1] == 'server') $this->identifier_select = true; |
|
|
preg_match('#<URI.*?>(.*)</URI>#', $content, $server); |
preg_match('#<URI.*?>(.*)</URI>#', $content, $server); |
preg_match('#<(Local|Canonical)ID>(.*)</\1ID>#', $content, $delegate); |
preg_match('#<(Local|Canonical)ID>(.*)</\1ID>#', $content, $delegate); |
if (empty($server)) { |
if (empty($server)) { |
return false; |
return false; |
} |
} |
# Does the server advertise support for either AX or SREG? |
# Does the server advertise support for either AX or SREG? |
$this->ax = (bool) strpos($content, '<Type>http://openid.net/srv/ax/1.0</Type>'); |
$this->ax = (bool) strpos($content, '<Type>http://openid.net/srv/ax/1.0</Type>'); |
$this->sreg = strpos($content, '<Type>http://openid.net/sreg/1.0</Type>') |
$this->sreg = strpos($content, '<Type>http://openid.net/sreg/1.0</Type>') |
|| strpos($content, '<Type>http://openid.net/extensions/sreg/1.1</Type>'); |
|| strpos($content, '<Type>http://openid.net/extensions/sreg/1.1</Type>'); |
|
|
$server = $server[1]; |
$server = $server[1]; |
if (isset($delegate[2])) $this->identity = trim($delegate[2]); |
if (isset($delegate[2])) $this->identity = trim($delegate[2]); |
$this->version = 2; |
$this->version = 2; |
|
|
$this->server = $server; |
$this->server = $server; |
return $server; |
return $server; |
} |
} |
|
|
# OpenID 1.1 |
# OpenID 1.1 |
$ns = preg_quote('http://openid.net/signon/1.1'); |
$ns = preg_quote('http://openid.net/signon/1.1'); |
if (preg_match('#<Type>\s*'.$ns.'\s*</Type>#s', $content)) { |
if (preg_match('#<Type>\s*'.$ns.'\s*</Type>#s', $content)) { |
|
|
preg_match('#<URI.*?>(.*)</URI>#', $content, $server); |
preg_match('#<URI.*?>(.*)</URI>#', $content, $server); |
preg_match('#<.*?Delegate>(.*)</.*?Delegate>#', $content, $delegate); |
preg_match('#<.*?Delegate>(.*)</.*?Delegate>#', $content, $delegate); |
if (empty($server)) { |
if (empty($server)) { |
return false; |
return false; |
} |
} |
# AX can be used only with OpenID 2.0, so checking only SREG |
# AX can be used only with OpenID 2.0, so checking only SREG |
$this->sreg = strpos($content, '<Type>http://openid.net/sreg/1.0</Type>') |
$this->sreg = strpos($content, '<Type>http://openid.net/sreg/1.0</Type>') |
|| strpos($content, '<Type>http://openid.net/extensions/sreg/1.1</Type>'); |
|| strpos($content, '<Type>http://openid.net/extensions/sreg/1.1</Type>'); |
|
|
$server = $server[1]; |
$server = $server[1]; |
if (isset($delegate[1])) $this->identity = $delegate[1]; |
if (isset($delegate[1])) $this->identity = $delegate[1]; |
$this->version = 1; |
$this->version = 1; |
|
|
$this->server = $server; |
$this->server = $server; |
return $server; |
return $server; |
} |
} |
} |
} |
|
|
$next = true; |
$next = true; |
$yadis = false; |
$yadis = false; |
$url = $originalUrl; |
$url = $originalUrl; |
$content = null; |
$content = null; |
break; |
break; |
} |
} |
if ($next) continue; |
if ($next) continue; |
|
|
# There are no relevant information in headers, so we search the body. |
# There are no relevant information in headers, so we search the body. |
$content = $this->request($url, 'GET'); |
$content = $this->request($url, 'GET'); |
$location = $this->htmlTag($content, 'meta', 'http-equiv', 'X-XRDS-Location', 'content'); |
$location = $this->htmlTag($content, 'meta', 'http-equiv', 'X-XRDS-Location', 'content'); |
if ($location) { |
if ($location) { |
$url = $this->build_url(parse_url($url), parse_url($location)); |
$url = $this->build_url(parse_url($url), parse_url($location)); |
continue; |
continue; |
} |
} |
} |
} |
|
|
if (!$content) $content = $this->request($url, 'GET'); |
if (!$content) $content = $this->request($url, 'GET'); |
|
|
# At this point, the YADIS Discovery has failed, so we'll switch |
# At this point, the YADIS Discovery has failed, so we'll switch |
# to openid2 HTML discovery, then fallback to openid 1.1 discovery. |
# to openid2 HTML discovery, then fallback to openid 1.1 discovery. |
$server = $this->htmlTag($content, 'link', 'rel', 'openid2.provider', 'href'); |
$server = $this->htmlTag($content, 'link', 'rel', 'openid2.provider', 'href'); |
$delegate = $this->htmlTag($content, 'link', 'rel', 'openid2.local_id', 'href'); |
$delegate = $this->htmlTag($content, 'link', 'rel', 'openid2.local_id', 'href'); |
$this->version = 2; |
$this->version = 2; |
|
|
if (!$server) { |
if (!$server) { |
# The same with openid 1.1 |
# The same with openid 1.1 |
$server = $this->htmlTag($content, 'link', 'rel', 'openid.server', 'href'); |
$server = $this->htmlTag($content, 'link', 'rel', 'openid.server', 'href'); |
$delegate = $this->htmlTag($content, 'link', 'rel', 'openid.delegate', 'href'); |
$delegate = $this->htmlTag($content, 'link', 'rel', 'openid.delegate', 'href'); |
$this->version = 1; |
$this->version = 1; |
} |
} |
|
|
if ($server) { |
if ($server) { |
# We found an OpenID2 OP Endpoint |
# We found an OpenID2 OP Endpoint |
if ($delegate) { |
if ($delegate) { |
# We have also found an OP-Local ID. |
# We have also found an OP-Local ID. |
$this->identity = $delegate; |
$this->identity = $delegate; |
} |
} |
$this->server = $server; |
$this->server = $server; |
return $server; |
return $server; |
} |
} |
|
|
throw new ErrorException("No OpenID Server found at $url", 404); |
throw new ErrorException("No OpenID Server found at $url", 404); |
} |
} |
throw new ErrorException('Endless redirection!', 500); |
throw new ErrorException('Endless redirection!', 500); |
} |
} |
|
|
protected function sregParams() |
protected function sregParams() |
{ |
{ |
$params = array(); |
$params = array(); |
# We always use SREG 1.1, even if the server is advertising only support for 1.0. |
# We always use SREG 1.1, even if the server is advertising only support for 1.0. |
# That's because it's fully backwards compatibile with 1.0, and some providers |
# That's because it's fully backwards compatibile with 1.0, and some providers |
# advertise 1.0 even if they accept only 1.1. One such provider is myopenid.com |
# advertise 1.0 even if they accept only 1.1. One such provider is myopenid.com |
$params['openid.ns.sreg'] = 'http://openid.net/extensions/sreg/1.1'; |
$params['openid.ns.sreg'] = 'http://openid.net/extensions/sreg/1.1'; |
if ($this->required) { |
if ($this->required) { |
$params['openid.sreg.required'] = array(); |
$params['openid.sreg.required'] = array(); |
foreach ($this->required as $required) { |
foreach ($this->required as $required) { |
if (!isset(self::$ax_to_sreg[$required])) continue; |
if (!isset(self::$ax_to_sreg[$required])) continue; |
$params['openid.sreg.required'][] = self::$ax_to_sreg[$required]; |
$params['openid.sreg.required'][] = self::$ax_to_sreg[$required]; |
} |
} |
$params['openid.sreg.required'] = implode(',', $params['openid.sreg.required']); |
$params['openid.sreg.required'] = implode(',', $params['openid.sreg.required']); |
} |
} |
|
|
if ($this->optional) { |
if ($this->optional) { |
$params['openid.sreg.optional'] = array(); |
$params['openid.sreg.optional'] = array(); |
foreach ($this->optional as $optional) { |
foreach ($this->optional as $optional) { |
if (!isset(self::$ax_to_sreg[$optional])) continue; |
if (!isset(self::$ax_to_sreg[$optional])) continue; |
$params['openid.sreg.optional'][] = self::$ax_to_sreg[$optional]; |
$params['openid.sreg.optional'][] = self::$ax_to_sreg[$optional]; |
} |
} |
$params['openid.sreg.optional'] = implode(',', $params['openid.sreg.optional']); |
$params['openid.sreg.optional'] = implode(',', $params['openid.sreg.optional']); |
} |
} |
return $params; |
return $params; |
} |
} |
|
|
protected function axParams() |
protected function axParams() |
{ |
{ |
$params = array(); |
$params = array(); |
if ($this->required || $this->optional) { |
if ($this->required || $this->optional) { |
$params['openid.ns.ax'] = 'http://openid.net/srv/ax/1.0'; |
$params['openid.ns.ax'] = 'http://openid.net/srv/ax/1.0'; |
$params['openid.ax.mode'] = 'fetch_request'; |
$params['openid.ax.mode'] = 'fetch_request'; |
$this->aliases = array(); |
$this->aliases = array(); |
$counts = array(); |
$counts = array(); |
$required = array(); |
$required = array(); |
$optional = array(); |
$optional = array(); |
foreach (array('required','optional') as $type) { |
foreach (array('required','optional') as $type) { |
foreach ($this->$type as $alias => $field) { |
foreach ($this->$type as $alias => $field) { |
if (is_int($alias)) $alias = strtr($field, '/', '_'); |
if (is_int($alias)) $alias = strtr($field, '/', '_'); |
$this->aliases[$alias] = 'http://axschema.org/' . $field; |
$this->aliases[$alias] = 'http://axschema.org/' . $field; |
if (empty($counts[$alias])) $counts[$alias] = 0; |
if (empty($counts[$alias])) $counts[$alias] = 0; |
$counts[$alias] += 1; |
$counts[$alias] += 1; |
${$type}[] = $alias; |
${$type}[] = $alias; |
} |
} |
} |
} |
foreach ($this->aliases as $alias => $ns) { |
foreach ($this->aliases as $alias => $ns) { |
$params['openid.ax.type.' . $alias] = $ns; |
$params['openid.ax.type.' . $alias] = $ns; |
} |
} |
foreach ($counts as $alias => $count) { |
foreach ($counts as $alias => $count) { |
if ($count == 1) continue; |
if ($count == 1) continue; |
$params['openid.ax.count.' . $alias] = $count; |
$params['openid.ax.count.' . $alias] = $count; |
} |
} |
|
|
# Don't send empty ax.requied and ax.if_available. |
# Don't send empty ax.requied and ax.if_available. |
# Google and possibly other providers refuse to support ax when one of these is empty. |
# Google and possibly other providers refuse to support ax when one of these is empty. |
if($required) { |
if($required) { |
$params['openid.ax.required'] = implode(',', $required); |
$params['openid.ax.required'] = implode(',', $required); |
} |
} |
if($optional) { |
if($optional) { |
$params['openid.ax.if_available'] = implode(',', $optional); |
$params['openid.ax.if_available'] = implode(',', $optional); |
} |
} |
} |
} |
return $params; |
return $params; |
} |
} |
|
|
protected function authUrl_v1($immediate) |
protected function authUrl_v1($immediate) |
{ |
{ |
$returnUrl = $this->returnUrl; |
$returnUrl = $this->returnUrl; |
# If we have an openid.delegate that is different from our claimed id, |
# If we have an openid.delegate that is different from our claimed id, |
# we need to somehow preserve the claimed id between requests. |
# we need to somehow preserve the claimed id between requests. |
# The simplest way is to just send it along with the return_to url. |
# The simplest way is to just send it along with the return_to url. |
if($this->identity != $this->claimed_id) { |
if($this->identity != $this->claimed_id) { |
$returnUrl .= (strpos($returnUrl, '?') ? '&' : '?') . 'openid.claimed_id=' . $this->claimed_id; |
$returnUrl .= (strpos($returnUrl, '?') ? '&' : '?') . 'openid.claimed_id=' . $this->claimed_id; |
} |
} |
|
|
$params = array( |
$params = array( |
'openid.return_to' => $returnUrl, |
'openid.return_to' => $returnUrl, |
'openid.mode' => $immediate ? 'checkid_immediate' : 'checkid_setup', |
'openid.mode' => $immediate ? 'checkid_immediate' : 'checkid_setup', |
'openid.identity' => $this->identity, |
'openid.identity' => $this->identity, |
'openid.trust_root' => $this->trustRoot, |
'openid.trust_root' => $this->trustRoot, |
) + $this->sregParams(); |
) + $this->sregParams(); |
|
|
return $this->build_url(parse_url($this->server) |
return $this->build_url(parse_url($this->server) |
, array('query' => http_build_query($params, '', '&'))); |
, array('query' => http_build_query($params, '', '&'))); |
} |
} |
|
|
protected function authUrl_v2($immediate) |
protected function authUrl_v2($immediate) |
{ |
{ |
$params = array( |
$params = array( |
'openid.ns' => 'http://specs.openid.net/auth/2.0', |
'openid.ns' => 'http://specs.openid.net/auth/2.0', |
'openid.mode' => $immediate ? 'checkid_immediate' : 'checkid_setup', |
'openid.mode' => $immediate ? 'checkid_immediate' : 'checkid_setup', |
'openid.return_to' => $this->returnUrl, |
'openid.return_to' => $this->returnUrl, |
'openid.realm' => $this->trustRoot, |
'openid.realm' => $this->trustRoot, |
); |
); |
if ($this->ax) { |
if ($this->ax) { |
$params += $this->axParams(); |
$params += $this->axParams(); |
} |
} |
if ($this->sreg) { |
if ($this->sreg) { |
$params += $this->sregParams(); |
$params += $this->sregParams(); |
} |
} |
if (!$this->ax && !$this->sreg) { |
if (!$this->ax && !$this->sreg) { |
# If OP doesn't advertise either SREG, nor AX, let's send them both |
# If OP doesn't advertise either SREG, nor AX, let's send them both |
# in worst case we don't get anything in return. |
# in worst case we don't get anything in return. |
$params += $this->axParams() + $this->sregParams(); |
$params += $this->axParams() + $this->sregParams(); |
} |
} |
|
|
if ($this->identifier_select) { |
if ($this->identifier_select) { |
$params['openid.identity'] = $params['openid.claimed_id'] |
$params['openid.identity'] = $params['openid.claimed_id'] |
= 'http://specs.openid.net/auth/2.0/identifier_select'; |
= 'http://specs.openid.net/auth/2.0/identifier_select'; |
} else { |
} else { |
$params['openid.identity'] = $this->identity; |
$params['openid.identity'] = $this->identity; |
$params['openid.claimed_id'] = $this->claimed_id; |
$params['openid.claimed_id'] = $this->claimed_id; |
} |
} |
|
|
return $this->build_url(parse_url($this->server) |
return $this->build_url(parse_url($this->server) |
, array('query' => http_build_query($params, '', '&'))); |
, array('query' => http_build_query($params, '', '&'))); |
} |
} |
|
|
/** |
/** |
* Returns authentication url. Usually, you want to redirect your user to it. |
* Returns authentication url. Usually, you want to redirect your user to it. |
* @return String The authentication url. |
* @return String The authentication url. |
* @param String $select_identifier Whether to request OP to select identity for an user in OpenID 2. Does not affect OpenID 1. |
* @param String $select_identifier Whether to request OP to select identity for an user in OpenID 2. Does not affect OpenID 1. |
* @throws ErrorException |
* @throws ErrorException |
*/ |
*/ |
function authUrl($immediate = false) |
function authUrl($immediate = false) |
{ |
{ |
if ($this->setup_url && !$immediate) return $this->setup_url; |
if ($this->setup_url && !$immediate) return $this->setup_url; |
if (!$this->server) $this->discover($this->identity); |
if (!$this->server) $this->discover($this->identity); |
|
|
if ($this->version == 2) { |
if ($this->version == 2) { |
return $this->authUrl_v2($immediate); |
return $this->authUrl_v2($immediate); |
} |
} |
return $this->authUrl_v1($immediate); |
return $this->authUrl_v1($immediate); |
} |
} |
|
|
/** |
/** |
* Performs OpenID verification with the OP. |
* Performs OpenID verification with the OP. |
* @return Bool Whether the verification was successful. |
* @return Bool Whether the verification was successful. |
* @throws ErrorException |
* @throws ErrorException |
*/ |
*/ |
function validate() |
function validate() |
{ |
{ |
# If the request was using immediate mode, a failure may be reported |
# If the request was using immediate mode, a failure may be reported |
# by presenting user_setup_url (for 1.1) or reporting |
# by presenting user_setup_url (for 1.1) or reporting |
# mode 'setup_needed' (for 2.0). Also catching all modes other than |
# mode 'setup_needed' (for 2.0). Also catching all modes other than |
# id_res, in order to avoid throwing errors. |
# id_res, in order to avoid throwing errors. |
if(isset($this->data['openid_user_setup_url'])) { |
if(isset($this->data['openid_user_setup_url'])) { |
$this->setup_url = $this->data['openid_user_setup_url']; |
$this->setup_url = $this->data['openid_user_setup_url']; |
return false; |
return false; |
} |
} |
if($this->mode != 'id_res') { |
if($this->mode != 'id_res') { |
return false; |
return false; |
} |
} |
|
|
$this->claimed_id = isset($this->data['openid_claimed_id'])?$this->data['openid_claimed_id']:$this->data['openid_identity']; |
$this->claimed_id = isset($this->data['openid_claimed_id'])?$this->data['openid_claimed_id']:$this->data['openid_identity']; |
$params = array( |
$params = array( |
'openid.assoc_handle' => $this->data['openid_assoc_handle'], |
'openid.assoc_handle' => $this->data['openid_assoc_handle'], |
'openid.signed' => $this->data['openid_signed'], |
'openid.signed' => $this->data['openid_signed'], |
'openid.sig' => $this->data['openid_sig'], |
'openid.sig' => $this->data['openid_sig'], |
); |
); |
|
|
if (isset($this->data['openid_ns'])) { |
if (isset($this->data['openid_ns'])) { |
# We're dealing with an OpenID 2.0 server, so let's set an ns |
# We're dealing with an OpenID 2.0 server, so let's set an ns |
# Even though we should know location of the endpoint, |
# Even though we should know location of the endpoint, |
# we still need to verify it by discovery, so $server is not set here |
# we still need to verify it by discovery, so $server is not set here |
$params['openid.ns'] = 'http://specs.openid.net/auth/2.0'; |
$params['openid.ns'] = 'http://specs.openid.net/auth/2.0'; |
} elseif (isset($this->data['openid_claimed_id']) |
} elseif (isset($this->data['openid_claimed_id']) |
&& $this->data['openid_claimed_id'] != $this->data['openid_identity'] |
&& $this->data['openid_claimed_id'] != $this->data['openid_identity'] |
) { |
) { |
# If it's an OpenID 1 provider, and we've got claimed_id, |
# If it's an OpenID 1 provider, and we've got claimed_id, |
# we have to append it to the returnUrl, like authUrl_v1 does. |
# we have to append it to the returnUrl, like authUrl_v1 does. |
$this->returnUrl .= (strpos($this->returnUrl, '?') ? '&' : '?') |
$this->returnUrl .= (strpos($this->returnUrl, '?') ? '&' : '?') |
. 'openid.claimed_id=' . $this->claimed_id; |
. 'openid.claimed_id=' . $this->claimed_id; |
} |
} |
|
|
if ($this->data['openid_return_to'] != $this->returnUrl) { |
if ($this->data['openid_return_to'] != $this->returnUrl) { |
# The return_to url must match the url of current request. |
# The return_to url must match the url of current request. |
# I'm assuing that noone will set the returnUrl to something that doesn't make sense. |
# I'm assuing that noone will set the returnUrl to something that doesn't make sense. |
return false; |
return false; |
} |
} |
|
|
$server = $this->discover($this->claimed_id); |
$server = $this->discover($this->claimed_id); |
|
|
foreach (explode(',', $this->data['openid_signed']) as $item) { |
foreach (explode(',', $this->data['openid_signed']) as $item) { |
# Checking whether magic_quotes_gpc is turned on, because |
# Checking whether magic_quotes_gpc is turned on, because |
# the function may fail if it is. For example, when fetching |
# the function may fail if it is. For example, when fetching |
# AX namePerson, it might containg an apostrophe, which will be escaped. |
# AX namePerson, it might containg an apostrophe, which will be escaped. |
# In such case, validation would fail, since we'd send different data than OP |
# In such case, validation would fail, since we'd send different data than OP |
# wants to verify. stripslashes() should solve that problem, but we can't |
# wants to verify. stripslashes() should solve that problem, but we can't |
# use it when magic_quotes is off. |
# use it when magic_quotes is off. |
$value = $this->data['openid_' . str_replace('.','_',$item)]; |
$value = $this->data['openid_' . str_replace('.','_',$item)]; |
$params['openid.' . $item] = get_magic_quotes_gpc() ? stripslashes($value) : $value; |
$params['openid.' . $item] = get_magic_quotes_gpc() ? stripslashes($value) : $value; |
|
|
} |
} |
|
|
$params['openid.mode'] = 'check_authentication'; |
$params['openid.mode'] = 'check_authentication'; |
|
|
$response = $this->request($server, 'POST', $params); |
$response = $this->request($server, 'POST', $params); |
|
|
return preg_match('/is_valid\s*:\s*true/i', $response); |
return preg_match('/is_valid\s*:\s*true/i', $response); |
} |
} |
|
|
protected function getAxAttributes() |
protected function getAxAttributes() |
{ |
{ |
$alias = null; |
$alias = null; |
if (isset($this->data['openid_ns_ax']) |
if (isset($this->data['openid_ns_ax']) |
&& $this->data['openid_ns_ax'] != 'http://openid.net/srv/ax/1.0' |
&& $this->data['openid_ns_ax'] != 'http://openid.net/srv/ax/1.0' |
) { # It's the most likely case, so we'll check it before |
) { # It's the most likely case, so we'll check it before |
$alias = 'ax'; |
$alias = 'ax'; |
} else { |
} else { |
# 'ax' prefix is either undefined, or points to another extension, |
# 'ax' prefix is either undefined, or points to another extension, |
# so we search for another prefix |
# so we search for another prefix |
foreach ($this->data as $key => $val) { |
foreach ($this->data as $key => $val) { |
if (substr($key, 0, strlen('openid_ns_')) == 'openid_ns_' |
if (substr($key, 0, strlen('openid_ns_')) == 'openid_ns_' |
&& $val == 'http://openid.net/srv/ax/1.0' |
&& $val == 'http://openid.net/srv/ax/1.0' |
) { |
) { |
$alias = substr($key, strlen('openid_ns_')); |
$alias = substr($key, strlen('openid_ns_')); |
break; |
break; |
} |
} |
} |
} |
} |
} |
if (!$alias) { |
if (!$alias) { |
# An alias for AX schema has not been found, |
# An alias for AX schema has not been found, |
# so there is no AX data in the OP's response |
# so there is no AX data in the OP's response |
return array(); |
return array(); |
} |
} |
|
|
$attributes = array(); |
$attributes = array(); |
foreach (explode(',', $this->data['openid_signed']) as $key) { |
foreach (explode(',', $this->data['openid_signed']) as $key) { |
$keyMatch = $alias . '.value.'; |
$keyMatch = $alias . '.value.'; |
if (substr($key, 0, strlen($keyMatch)) != $keyMatch) { |
if (substr($key, 0, strlen($keyMatch)) != $keyMatch) { |
continue; |
continue; |
} |
} |
$key = substr($key, strlen($keyMatch)); |
$key = substr($key, strlen($keyMatch)); |
if (!isset($this->data['openid_' . $alias . '_type_' . $key])) { |
if (!isset($this->data['openid_' . $alias . '_type_' . $key])) { |
# OP is breaking the spec by returning a field without |
# OP is breaking the spec by returning a field without |
# associated ns. This shouldn't happen, but it's better |
# associated ns. This shouldn't happen, but it's better |
# to check, than cause an E_NOTICE. |
# to check, than cause an E_NOTICE. |
continue; |
continue; |
} |
} |
$value = $this->data['openid_' . $alias . '_value_' . $key]; |
$value = $this->data['openid_' . $alias . '_value_' . $key]; |
$key = substr($this->data['openid_' . $alias . '_type_' . $key], |
$key = substr($this->data['openid_' . $alias . '_type_' . $key], |
strlen('http://axschema.org/')); |
strlen('http://axschema.org/')); |
|
|
$attributes[$key] = $value; |
$attributes[$key] = $value; |
} |
} |
return $attributes; |
return $attributes; |
} |
} |
|
|
protected function getSregAttributes() |
protected function getSregAttributes() |
{ |
{ |
$attributes = array(); |
$attributes = array(); |
$sreg_to_ax = array_flip(self::$ax_to_sreg); |
$sreg_to_ax = array_flip(self::$ax_to_sreg); |
foreach (explode(',', $this->data['openid_signed']) as $key) { |
foreach (explode(',', $this->data['openid_signed']) as $key) { |
$keyMatch = 'sreg.'; |
$keyMatch = 'sreg.'; |
if (substr($key, 0, strlen($keyMatch)) != $keyMatch) { |
if (substr($key, 0, strlen($keyMatch)) != $keyMatch) { |
continue; |
continue; |
} |
} |
$key = substr($key, strlen($keyMatch)); |
$key = substr($key, strlen($keyMatch)); |
if (!isset($sreg_to_ax[$key])) { |
if (!isset($sreg_to_ax[$key])) { |
# The field name isn't part of the SREG spec, so we ignore it. |
# The field name isn't part of the SREG spec, so we ignore it. |
continue; |
continue; |
} |
} |
$attributes[$sreg_to_ax[$key]] = $this->data['openid_sreg_' . $key]; |
$attributes[$sreg_to_ax[$key]] = $this->data['openid_sreg_' . $key]; |
} |
} |
return $attributes; |
return $attributes; |
} |
} |
|
|
/** |
/** |
* Gets AX/SREG attributes provided by OP. should be used only after successful validaton. |
* Gets AX/SREG attributes provided by OP. should be used only after successful validaton. |
* Note that it does not guarantee that any of the required/optional parameters will be present, |
* Note that it does not guarantee that any of the required/optional parameters will be present, |
* or that there will be no other attributes besides those specified. |
* or that there will be no other attributes besides those specified. |
* In other words. OP may provide whatever information it wants to. |
* In other words. OP may provide whatever information it wants to. |
* * SREG names will be mapped to AX names. |
* * SREG names will be mapped to AX names. |
* * @return Array Array of attributes with keys being the AX schema names, e.g. 'contact/email' |
* * @return Array Array of attributes with keys being the AX schema names, e.g. 'contact/email' |
* @see http://www.axschema.org/types/ |
* @see http://www.axschema.org/types/ |
*/ |
*/ |
function getAttributes() |
function getAttributes() |
{ |
{ |
if (isset($this->data['openid_ns']) |
if (isset($this->data['openid_ns']) |
&& $this->data['openid_ns'] == 'http://specs.openid.net/auth/2.0' |
&& $this->data['openid_ns'] == 'http://specs.openid.net/auth/2.0' |
) { # OpenID 2.0 |
) { # OpenID 2.0 |
# We search for both AX and SREG attributes, with AX taking precedence. |
# We search for both AX and SREG attributes, with AX taking precedence. |
return $this->getAxAttributes() + $this->getSregAttributes(); |
return $this->getAxAttributes() + $this->getSregAttributes(); |
} |
} |
return $this->getSregAttributes(); |
return $this->getSregAttributes(); |
} |
} |
} |
} |
|
|