Initial code using Drupal 6.38
This commit is contained in:
commit
4824608a33
467 changed files with 90887 additions and 0 deletions
BIN
modules/openid/login-bg.png
Normal file
BIN
modules/openid/login-bg.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 223 B |
39
modules/openid/openid.css
Normal file
39
modules/openid/openid.css
Normal file
|
@ -0,0 +1,39 @@
|
|||
|
||||
#edit-openid-identifier {
|
||||
background-image: url("login-bg.png");
|
||||
background-position: 0% 50%;
|
||||
background-repeat: no-repeat;
|
||||
padding-left: 20px;
|
||||
}
|
||||
div#edit-openid-identifier-wrapper {
|
||||
display: block;
|
||||
}
|
||||
html.js #user-login-form div#edit-openid-identifier-wrapper,
|
||||
html.js #user-login div#edit-openid-identifier-wrapper {
|
||||
display: none;
|
||||
}
|
||||
html.js #user-login-form li.openid-link,
|
||||
html.js #user-login li.openid-link {
|
||||
display : block;
|
||||
list-style: none;
|
||||
}
|
||||
#user-login-form ul {
|
||||
margin-top: 0;
|
||||
}
|
||||
#user-login ul {
|
||||
margin: 0 0 5px;
|
||||
}
|
||||
#user-login ul li {
|
||||
margin: 0;
|
||||
}
|
||||
#user-login-form li.openid-link,
|
||||
#user-login-form li.user-link,
|
||||
#user-login li.openid-link,
|
||||
#user-login li.user-link {
|
||||
display: none;
|
||||
}
|
||||
#user-login-form li.openid-link a,
|
||||
#user-login li.openid-link a {
|
||||
background: transparent url("login-bg.png") no-repeat 0 2px;
|
||||
padding: 0 20px;
|
||||
}
|
428
modules/openid/openid.inc
Normal file
428
modules/openid/openid.inc
Normal file
|
@ -0,0 +1,428 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* OpenID utility functions.
|
||||
*/
|
||||
|
||||
// Diffie-Hellman Key Exchange Default Value.
|
||||
define('OPENID_DH_DEFAULT_MOD', '155172898181473697471232257763715539915724801'.
|
||||
'966915404479707795314057629378541917580651227423698188993727816152646631'.
|
||||
'438561595825688188889951272158842675419950341258706556549803580104870537'.
|
||||
'681476726513255747040765857479291291572334510643245094715007229621094194'.
|
||||
'349783925984760375594985848253359305585439638443');
|
||||
|
||||
// Constants for Diffie-Hellman key exchange computations.
|
||||
define('OPENID_DH_DEFAULT_GEN', '2');
|
||||
define('OPENID_SHA1_BLOCKSIZE', 64);
|
||||
define('OPENID_RAND_SOURCE', '/dev/urandom');
|
||||
|
||||
// OpenID namespace URLs
|
||||
define('OPENID_NS_2_0', 'http://specs.openid.net/auth/2.0');
|
||||
define('OPENID_NS_1_1', 'http://openid.net/signon/1.1');
|
||||
define('OPENID_NS_1_0', 'http://openid.net/signon/1.0');
|
||||
|
||||
/**
|
||||
* Performs an HTTP 302 redirect (for the 1.x protocol).
|
||||
*/
|
||||
function openid_redirect_http($url, $message) {
|
||||
$query = array();
|
||||
foreach ($message as $key => $val) {
|
||||
$query[] = $key .'='. urlencode($val);
|
||||
}
|
||||
|
||||
$sep = (strpos($url, '?') === FALSE) ? '?' : '&';
|
||||
header('Location: '. $url . $sep . implode('&', $query), TRUE, 302);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a js auto-submit redirect for (for the 2.x protocol)
|
||||
*/
|
||||
function openid_redirect($url, $message) {
|
||||
$output = '<html><head><title>'. t('OpenID redirect') ."</title></head>\n<body>";
|
||||
$output .= drupal_get_form('openid_redirect_form', $url, $message);
|
||||
$output .= '<script type="text/javascript">document.getElementById("openid-redirect-form").submit();</script>';
|
||||
$output .= "</body></html>\n";
|
||||
print $output;
|
||||
exit;
|
||||
}
|
||||
|
||||
function openid_redirect_form(&$form_state, $url, $message) {
|
||||
$form = array();
|
||||
$form['#action'] = $url;
|
||||
$form['#method'] = "post";
|
||||
foreach ($message as $key => $value) {
|
||||
$form[$key] = array(
|
||||
'#type' => 'hidden',
|
||||
'#name' => $key,
|
||||
'#value' => $value,
|
||||
);
|
||||
}
|
||||
$form['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#prefix' => '<noscript>',
|
||||
'#suffix' => '</noscript>',
|
||||
'#value' => t('Send'),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given identifier is an XRI ID.
|
||||
*/
|
||||
function _openid_is_xri($identifier) {
|
||||
// Strip the xri:// scheme from the identifier if present.
|
||||
if (strpos(strtolower($identifier), 'xri://') !== FALSE) {
|
||||
$identifier = substr($identifier, 6);
|
||||
}
|
||||
|
||||
// Test whether the identifier starts with an XRI global context symbol or (.
|
||||
$firstchar = substr($identifier, 0, 1);
|
||||
if (strpos("=@+$!(", $firstchar) !== FALSE) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize the given identifier as per spec.
|
||||
*/
|
||||
function _openid_normalize($identifier) {
|
||||
if (_openid_is_xri($identifier)) {
|
||||
return _openid_normalize_xri($identifier);
|
||||
}
|
||||
else {
|
||||
return _openid_normalize_url($identifier);
|
||||
}
|
||||
}
|
||||
|
||||
function _openid_normalize_xri($xri) {
|
||||
$normalized_xri = $xri;
|
||||
if (stristr($xri, 'xri://') !== FALSE) {
|
||||
$normalized_xri = substr($xri, 6);
|
||||
}
|
||||
return $normalized_xri;
|
||||
}
|
||||
|
||||
function _openid_normalize_url($url) {
|
||||
$normalized_url = $url;
|
||||
|
||||
if (stristr($url, '://') === FALSE) {
|
||||
$normalized_url = 'http://'. $url;
|
||||
}
|
||||
|
||||
// Strip the fragment and fragment delimiter if present.
|
||||
$normalized_url = strtok($normalized_url, '#');
|
||||
|
||||
if (substr_count($normalized_url, '/') < 3) {
|
||||
$normalized_url .= '/';
|
||||
}
|
||||
|
||||
return $normalized_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a serialized message packet as per spec: $key:$value\n .
|
||||
*/
|
||||
function _openid_create_message($data) {
|
||||
$serialized = '';
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if ((strpos($key, ':') !== FALSE) || (strpos($key, "\n") !== FALSE) || (strpos($value, "\n") !== FALSE)) {
|
||||
return null;
|
||||
}
|
||||
$serialized .= "$key:$value\n";
|
||||
}
|
||||
return $serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a message from _openid_create_message for HTTP Post
|
||||
*/
|
||||
function _openid_encode_message($message) {
|
||||
$encoded_message = '';
|
||||
|
||||
$items = explode("\n", $message);
|
||||
foreach ($items as $item) {
|
||||
$parts = explode(':', $item, 2);
|
||||
|
||||
if (count($parts) == 2) {
|
||||
if ($encoded_message != '') {
|
||||
$encoded_message .= '&';
|
||||
}
|
||||
$encoded_message .= rawurlencode(trim($parts[0])) .'='. rawurlencode(trim($parts[1]));
|
||||
}
|
||||
}
|
||||
|
||||
return $encoded_message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a direct communication message
|
||||
* into an associative array.
|
||||
*/
|
||||
function _openid_parse_message($message) {
|
||||
$parsed_message = array();
|
||||
|
||||
$items = explode("\n", $message);
|
||||
foreach ($items as $item) {
|
||||
$parts = explode(':', $item, 2);
|
||||
|
||||
if (count($parts) == 2) {
|
||||
$parsed_message[$parts[0]] = $parts[1];
|
||||
}
|
||||
}
|
||||
|
||||
return $parsed_message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a nonce value - formatted per OpenID spec.
|
||||
*/
|
||||
function _openid_nonce() {
|
||||
// YYYY-MM-DDThh:mm:ssTZD UTC, plus some optional extra unique chars
|
||||
return gmstrftime('%Y-%m-%dT%H:%M:%S%Z') .
|
||||
chr(mt_rand(0, 25) + 65) .
|
||||
chr(mt_rand(0, 25) + 65) .
|
||||
chr(mt_rand(0, 25) + 65) .
|
||||
chr(mt_rand(0, 25) + 65);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pull the href attribute out of an html link element.
|
||||
*/
|
||||
function _openid_link_href($rel, $html) {
|
||||
$rel = preg_quote($rel);
|
||||
preg_match('|<link\s+rel=["\'](.*)'. $rel .'(.*)["\'](.*)/?>|iUs', $html, $matches);
|
||||
if (isset($matches[3])) {
|
||||
preg_match('|href=["\']([^"]+)["\']|iU', $matches[3], $href);
|
||||
return trim($href[1]);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pull the http-equiv attribute out of an html meta element
|
||||
*/
|
||||
function _openid_meta_httpequiv($equiv, $html) {
|
||||
preg_match('|<meta\s+http-equiv=["\']'. $equiv .'["\'](.*)/?>|iUs', $html, $matches);
|
||||
if (isset($matches[1])) {
|
||||
preg_match('|content=["\']([^"]+)["\']|iUs', $matches[1], $content);
|
||||
if (isset($content[1])) {
|
||||
return $content[1];
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign certain keys in a message
|
||||
* @param $association - object loaded from openid_association or openid_server_association table
|
||||
* - important fields are ->assoc_type and ->mac_key
|
||||
* @param $message_array - array of entire message about to be sent
|
||||
* @param $keys_to_sign - keys in the message to include in signature (without
|
||||
* 'openid.' appended)
|
||||
*/
|
||||
function _openid_signature($association, $message_array, $keys_to_sign) {
|
||||
$signature = '';
|
||||
$sign_data = array();
|
||||
|
||||
foreach ($keys_to_sign as $key) {
|
||||
if (isset($message_array['openid.'. $key])) {
|
||||
$sign_data[$key] = $message_array['openid.'. $key];
|
||||
}
|
||||
}
|
||||
|
||||
$message = _openid_create_message($sign_data);
|
||||
$secret = base64_decode($association->mac_key);
|
||||
$signature = _openid_hmac($secret, $message);
|
||||
|
||||
return base64_encode($signature);
|
||||
}
|
||||
|
||||
function _openid_hmac($key, $text) {
|
||||
if (strlen($key) > OPENID_SHA1_BLOCKSIZE) {
|
||||
$key = _openid_sha1($key, true);
|
||||
}
|
||||
|
||||
$key = str_pad($key, OPENID_SHA1_BLOCKSIZE, chr(0x00));
|
||||
$ipad = str_repeat(chr(0x36), OPENID_SHA1_BLOCKSIZE);
|
||||
$opad = str_repeat(chr(0x5c), OPENID_SHA1_BLOCKSIZE);
|
||||
$hash1 = _openid_sha1(($key ^ $ipad) . $text, true);
|
||||
$hmac = _openid_sha1(($key ^ $opad) . $hash1, true);
|
||||
|
||||
return $hmac;
|
||||
}
|
||||
|
||||
function _openid_sha1($text) {
|
||||
$hex = sha1($text);
|
||||
$raw = '';
|
||||
for ($i = 0; $i < 40; $i += 2) {
|
||||
$hexcode = substr($hex, $i, 2);
|
||||
$charcode = (int)base_convert($hexcode, 16, 10);
|
||||
$raw .= chr($charcode);
|
||||
}
|
||||
return $raw;
|
||||
}
|
||||
|
||||
function _openid_dh_base64_to_long($str) {
|
||||
$b64 = base64_decode($str);
|
||||
|
||||
return _openid_dh_binary_to_long($b64);
|
||||
}
|
||||
|
||||
function _openid_dh_long_to_base64($str) {
|
||||
return base64_encode(_openid_dh_long_to_binary($str));
|
||||
}
|
||||
|
||||
function _openid_dh_binary_to_long($str) {
|
||||
$bytes = array_merge(unpack('C*', $str));
|
||||
|
||||
$n = 0;
|
||||
foreach ($bytes as $byte) {
|
||||
$n = bcmul($n, pow(2, 8));
|
||||
$n = bcadd($n, $byte);
|
||||
}
|
||||
|
||||
return $n;
|
||||
}
|
||||
|
||||
function _openid_dh_long_to_binary($long) {
|
||||
$cmp = bccomp($long, 0);
|
||||
if ($cmp < 0) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if ($cmp == 0) {
|
||||
return "\x00";
|
||||
}
|
||||
|
||||
$bytes = array();
|
||||
|
||||
while (bccomp($long, 0) > 0) {
|
||||
array_unshift($bytes, bcmod($long, 256));
|
||||
$long = bcdiv($long, pow(2, 8));
|
||||
}
|
||||
|
||||
if ($bytes && ($bytes[0] > 127)) {
|
||||
array_unshift($bytes, 0);
|
||||
}
|
||||
|
||||
$string = '';
|
||||
foreach ($bytes as $byte) {
|
||||
$string .= pack('C', $byte);
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
function _openid_dh_xorsecret($shared, $secret) {
|
||||
$dh_shared_str = _openid_dh_long_to_binary($shared);
|
||||
$sha1_dh_shared = _openid_sha1($dh_shared_str);
|
||||
$xsecret = "";
|
||||
for ($i = 0; $i < strlen($secret); $i++) {
|
||||
$xsecret .= chr(ord($secret[$i]) ^ ord($sha1_dh_shared[$i]));
|
||||
}
|
||||
|
||||
return $xsecret;
|
||||
}
|
||||
|
||||
function _openid_dh_rand($stop) {
|
||||
static $duplicate_cache = array();
|
||||
|
||||
// Used as the key for the duplicate cache
|
||||
$rbytes = _openid_dh_long_to_binary($stop);
|
||||
|
||||
if (array_key_exists($rbytes, $duplicate_cache)) {
|
||||
list($duplicate, $nbytes) = $duplicate_cache[$rbytes];
|
||||
}
|
||||
else {
|
||||
if ($rbytes[0] == "\x00") {
|
||||
$nbytes = strlen($rbytes) - 1;
|
||||
}
|
||||
else {
|
||||
$nbytes = strlen($rbytes);
|
||||
}
|
||||
|
||||
$mxrand = bcpow(256, $nbytes);
|
||||
|
||||
// If we get a number less than this, then it is in the
|
||||
// duplicated range.
|
||||
$duplicate = bcmod($mxrand, $stop);
|
||||
|
||||
if (count($duplicate_cache) > 10) {
|
||||
$duplicate_cache = array();
|
||||
}
|
||||
|
||||
$duplicate_cache[$rbytes] = array($duplicate, $nbytes);
|
||||
}
|
||||
|
||||
do {
|
||||
$bytes = "\x00". drupal_random_bytes($nbytes);
|
||||
$n = _openid_dh_binary_to_long($bytes);
|
||||
// Keep looping if this value is in the low duplicated range.
|
||||
} while (bccomp($n, $duplicate) < 0);
|
||||
|
||||
return bcmod($n, $stop);
|
||||
}
|
||||
|
||||
function _openid_get_bytes($num_bytes) {
|
||||
return drupal_random_bytes($num_bytes);
|
||||
}
|
||||
|
||||
function _openid_response($str = NULL) {
|
||||
$data = array();
|
||||
|
||||
if (isset($_SERVER['REQUEST_METHOD'])) {
|
||||
$data = _openid_get_params($_SERVER['QUERY_STRING']);
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
||||
$str = file_get_contents('php://input');
|
||||
|
||||
$post = array();
|
||||
if ($str !== false) {
|
||||
$post = _openid_get_params($str);
|
||||
}
|
||||
|
||||
$data = array_merge($data, $post);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
function _openid_get_params($str) {
|
||||
$chunks = explode("&", $str);
|
||||
|
||||
$data = array();
|
||||
foreach ($chunks as $chunk) {
|
||||
$parts = explode("=", $chunk, 2);
|
||||
|
||||
if (count($parts) == 2) {
|
||||
list($k, $v) = $parts;
|
||||
$data[$k] = urldecode($v);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide bcpowmod support for PHP4.
|
||||
*/
|
||||
if (!function_exists('bcpowmod')) {
|
||||
function bcpowmod($base, $exp, $mod) {
|
||||
$square = bcmod($base, $mod);
|
||||
$result = 1;
|
||||
while (bccomp($exp, 0) > 0) {
|
||||
if (bcmod($exp, 2)) {
|
||||
$result = bcmod(bcmul($result, $square), $mod);
|
||||
}
|
||||
$square = bcmod(bcmul($square, $square), $mod);
|
||||
$exp = bcdiv($exp, 2);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
11
modules/openid/openid.info
Normal file
11
modules/openid/openid.info
Normal file
|
@ -0,0 +1,11 @@
|
|||
name = OpenID
|
||||
description = "Allows users to log into your site using OpenID."
|
||||
version = VERSION
|
||||
package = Core - optional
|
||||
core = 6.x
|
||||
|
||||
; Information added by Drupal.org packaging script on 2016-02-24
|
||||
version = "6.38"
|
||||
project = "drupal"
|
||||
datestamp = "1456343372"
|
||||
|
210
modules/openid/openid.install
Normal file
210
modules/openid/openid.install
Normal file
|
@ -0,0 +1,210 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Implementation of hook_install().
|
||||
*/
|
||||
function openid_install() {
|
||||
// Create table.
|
||||
drupal_install_schema('openid');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_uninstall().
|
||||
*/
|
||||
function openid_uninstall() {
|
||||
// Remove table.
|
||||
drupal_uninstall_schema('openid');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_schema().
|
||||
*/
|
||||
function openid_schema() {
|
||||
$schema['openid_association'] = array(
|
||||
'description' => 'Stores temporary shared key association information for OpenID authentication.',
|
||||
'fields' => array(
|
||||
'idp_endpoint_uri' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'description' => 'Primary Key: URI of the OpenID Provider endpoint.',
|
||||
),
|
||||
'assoc_handle' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'description' => 'Used to refer to this association in subsequent messages.',
|
||||
),
|
||||
'assoc_type' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'description' => 'The signature algorithm used: one of HMAC-SHA1 or HMAC-SHA256.',
|
||||
),
|
||||
'session_type' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'description' => 'Valid association session types: "no-encryption", "DH-SHA1", and "DH-SHA256".',
|
||||
),
|
||||
'mac_key' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'description' => 'The MAC key (shared secret) for this association.',
|
||||
),
|
||||
'created' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'description' => 'UNIX timestamp for when the association was created.',
|
||||
),
|
||||
'expires_in' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'description' => 'The lifetime, in seconds, of this association.',
|
||||
),
|
||||
),
|
||||
'primary key' => array('idp_endpoint_uri'),
|
||||
'unique keys' => array(
|
||||
'assoc_handle' => array('assoc_handle'),
|
||||
),
|
||||
);
|
||||
|
||||
$schema['openid_nonce'] = array(
|
||||
'description' => 'Stores received openid.response_nonce per OpenID endpoint URL to prevent replay attacks.',
|
||||
'fields' => array(
|
||||
'idp_endpoint_uri' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'description' => 'URI of the OpenID Provider endpoint.',
|
||||
),
|
||||
'nonce' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'description' => 'The value of openid.response_nonce'
|
||||
),
|
||||
'expires' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'description' => 'A Unix timestamp indicating when the entry should expire.',
|
||||
),
|
||||
),
|
||||
'indexes' => array(
|
||||
'nonce' => array('nonce'),
|
||||
'expires' => array('expires'),
|
||||
),
|
||||
);
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* @addtogroup updates-6.x-extra
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add the openid_nonce table.
|
||||
*
|
||||
* Implementation of hook_update_N().
|
||||
*/
|
||||
function openid_update_6000() {
|
||||
$ret = array();
|
||||
|
||||
$schema['openid_nonce'] = array(
|
||||
'description' => 'Stores received openid.response_nonce per OpenID endpoint URL to prevent replay attacks.',
|
||||
'fields' => array(
|
||||
'idp_endpoint_uri' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'description' => 'URI of the OpenID Provider endpoint.',
|
||||
),
|
||||
'nonce' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'description' => 'The value of openid.response_nonce'
|
||||
),
|
||||
'expires' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'description' => 'A Unix timestamp indicating when the entry should expire.',
|
||||
),
|
||||
),
|
||||
'indexes' => array(
|
||||
'nonce' => array('nonce'),
|
||||
'expires' => array('expires'),
|
||||
),
|
||||
);
|
||||
|
||||
db_create_table($ret, 'openid_nonce', $schema['openid_nonce']);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind associations to their providers.
|
||||
*/
|
||||
function openid_update_6001() {
|
||||
$ret = array();
|
||||
|
||||
db_drop_table($ret, 'openid_association');
|
||||
|
||||
$schema['openid_association'] = array(
|
||||
'description' => 'Stores temporary shared key association information for OpenID authentication.',
|
||||
'fields' => array(
|
||||
'idp_endpoint_uri' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'description' => 'Primary Key: URI of the OpenID Provider endpoint.',
|
||||
),
|
||||
'assoc_handle' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'description' => 'Used to refer to this association in subsequent messages.',
|
||||
),
|
||||
'assoc_type' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'description' => 'The signature algorithm used: one of HMAC-SHA1 or HMAC-SHA256.',
|
||||
),
|
||||
'session_type' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'description' => 'Valid association session types: "no-encryption", "DH-SHA1", and "DH-SHA256".',
|
||||
),
|
||||
'mac_key' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'description' => 'The MAC key (shared secret) for this association.',
|
||||
),
|
||||
'created' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'description' => 'UNIX timestamp for when the association was created.',
|
||||
),
|
||||
'expires_in' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'description' => 'The lifetime, in seconds, of this association.',
|
||||
),
|
||||
),
|
||||
'primary key' => array('idp_endpoint_uri'),
|
||||
'unique keys' => array(
|
||||
'assoc_handle' => array('assoc_handle'),
|
||||
),
|
||||
);
|
||||
|
||||
db_create_table($ret, 'openid_association', $schema['openid_association']);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "addtogroup updates-6.x-extra".
|
||||
* The next series of updates should start at 7000.
|
||||
*/
|
37
modules/openid/openid.js
Normal file
37
modules/openid/openid.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
|
||||
Drupal.behaviors.openid = function (context) {
|
||||
var $loginElements = $("#edit-name-wrapper, #edit-pass-wrapper, li.openid-link");
|
||||
var $openidElements = $("#edit-openid-identifier-wrapper, li.user-link");
|
||||
|
||||
// This behavior attaches by ID, so is only valid once on a page.
|
||||
if (!$("#edit-openid-identifier.openid-processed").size() && $("#edit-openid-identifier").val()) {
|
||||
$("#edit-openid-identifier").addClass('openid-processed');
|
||||
$loginElements.hide();
|
||||
// Use .css("display", "block") instead of .show() to be Konqueror friendly.
|
||||
$openidElements.css("display", "block");
|
||||
}
|
||||
$("li.openid-link:not(.openid-processed)", context)
|
||||
.addClass('openid-processed')
|
||||
.click( function() {
|
||||
$loginElements.hide();
|
||||
$openidElements.css("display", "block");
|
||||
// Remove possible error message.
|
||||
$("#edit-name, #edit-pass").removeClass("error");
|
||||
$("div.messages.error").hide();
|
||||
// Set focus on OpenID Identifier field.
|
||||
$("#edit-openid-identifier")[0].focus();
|
||||
return false;
|
||||
});
|
||||
$("li.user-link:not(.openid-processed)", context)
|
||||
.addClass('openid-processed')
|
||||
.click(function() {
|
||||
$openidElements.hide();
|
||||
$loginElements.css("display", "block");
|
||||
// Clear OpenID Identifier field and remove possible error message.
|
||||
$("#edit-openid-identifier").val('').removeClass("error");
|
||||
$("div.messages.error").css("display", "block");
|
||||
// Set focus on username field.
|
||||
$("#edit-name")[0].focus();
|
||||
return false;
|
||||
});
|
||||
};
|
701
modules/openid/openid.module
Normal file
701
modules/openid/openid.module
Normal file
|
@ -0,0 +1,701 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Implement OpenID Relying Party support for Drupal
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implementation of hook_menu.
|
||||
*/
|
||||
function openid_menu() {
|
||||
$items['openid/authenticate'] = array(
|
||||
'title' => 'OpenID Login',
|
||||
'page callback' => 'openid_authentication_page',
|
||||
'access callback' => 'user_is_anonymous',
|
||||
'type' => MENU_CALLBACK,
|
||||
'file' => 'openid.pages.inc',
|
||||
);
|
||||
$items['user/%user/openid'] = array(
|
||||
'title' => 'OpenID identities',
|
||||
'page callback' => 'openid_user_identities',
|
||||
'page arguments' => array(1),
|
||||
'access callback' => 'user_edit_access',
|
||||
'access arguments' => array(1),
|
||||
'type' => MENU_LOCAL_TASK,
|
||||
'file' => 'openid.pages.inc',
|
||||
);
|
||||
$items['user/%user/openid/delete'] = array(
|
||||
'title' => 'Delete OpenID',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('openid_user_delete_form', 1),
|
||||
'access callback' => 'user_edit_access',
|
||||
'access arguments' => array(1),
|
||||
'type' => MENU_CALLBACK,
|
||||
'file' => 'openid.pages.inc',
|
||||
);
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_help().
|
||||
*/
|
||||
function openid_help($path, $arg) {
|
||||
switch ($path) {
|
||||
case 'user/%/openid':
|
||||
$output = '<p>'. t('This site supports <a href="@openid-net">OpenID</a>, a secure way to log into many websites using a single username and password. OpenID can reduce the necessity of managing many usernames and passwords for many websites.', array('@openid-net' => 'http://openid.net')) .'</p>';
|
||||
$output .= '<p>'. t('To use OpenID you must first establish an identity on a public or private OpenID server. If you do not have an OpenID and would like one, look into one of the <a href="@openid-providers">free public providers</a>. You can find out more about OpenID at <a href="@openid-net">this website</a>.', array('@openid-providers' => 'http://openid.net/get/', '@openid-net' => 'http://openid.net')) .'</p>';
|
||||
$output .= '<p>'. t('If you already have an OpenID, enter the URL to your OpenID server below (e.g. myusername.openidprovider.com). Next time you login, you will be able to use this URL instead of a regular username and password. You can have multiple OpenID servers if you like; just keep adding them here.') .'</p>';
|
||||
return $output;
|
||||
|
||||
case 'admin/help#openid':
|
||||
$output = '<p>'. t('OpenID is a secure method for logging into many websites with a single username and password. It does not require special software, and it does not share passwords with any site to which it is associated; including your site.') .'</p>';
|
||||
$output .= '<p>'. t('Users can create accounts using their OpenID, assign one or more OpenIDs to an existing account, and log in using an OpenID. This lowers the barrier to registration, which is good for the site, and offers convenience and security to the users. OpenID is not a trust system, so email verification is still necessary. The benefit stems from the fact that users can have a single password that they can use on many websites. This means they can easily update their single password from a centralized location, rather than having to change dozens of passwords individually.') .'</p>';
|
||||
$output .= '<p>'. t('The basic concept is as follows: A user has an account on an OpenID server. This account provides them with a unique URL (such as myusername.openidprovider.com). When the user comes to your site, they are presented with the option of entering this URL. Your site then communicates with the OpenID server, asking it to verify the identity of the user. If the user is logged into their OpenID server, the server communicates back to your site, verifying the user. If they are not logged in, the OpenID server will ask the user for their password. At no point does your site record, or need to record the user\'s password.') .'</p>';
|
||||
$output .= '<p>'. t('More information on OpenID is available at <a href="@openid-net">OpenID.net</a>.', array('@openid-net' => url('http://openid.net'))) .'</p>';
|
||||
$output .= '<p>'. t('For more information, see the online handbook entry for <a href="@handbook">OpenID module</a>.', array('@handbook' => 'http://drupal.org/handbook/modules/openid')) .'</p>';
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_user().
|
||||
*/
|
||||
function openid_user($op, &$edit, &$account, $category = NULL) {
|
||||
if ($op == 'insert' && isset($_SESSION['openid']['values'])) {
|
||||
// The user has registered after trying to login via OpenID.
|
||||
if (variable_get('user_email_verification', TRUE)) {
|
||||
drupal_set_message(t('Once you have verified your email address, you may log in via OpenID.'));
|
||||
}
|
||||
unset($_SESSION['openid']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_form_alter : adds OpenID login to the login forms.
|
||||
*/
|
||||
function openid_form_alter(&$form, $form_state, $form_id) {
|
||||
if ($form_id == 'user_login_block' || $form_id == 'user_login') {
|
||||
drupal_add_css(drupal_get_path('module', 'openid') .'/openid.css', 'module');
|
||||
drupal_add_js(drupal_get_path('module', 'openid') .'/openid.js');
|
||||
if (!empty($form_state['post']['openid_identifier'])) {
|
||||
$form['name']['#required'] = FALSE;
|
||||
$form['pass']['#required'] = FALSE;
|
||||
unset($form['#submit']);
|
||||
$form['#validate'] = array('openid_login_validate');
|
||||
}
|
||||
|
||||
$items = array();
|
||||
$items[] = array(
|
||||
'data' => l(t('Log in using OpenID'), '#'),
|
||||
'class' => 'openid-link',
|
||||
);
|
||||
$items[] = array(
|
||||
'data' => l(t('Cancel OpenID login'), '#'),
|
||||
'class' => 'user-link',
|
||||
);
|
||||
|
||||
$form['openid_links'] = array(
|
||||
'#value' => theme('item_list', $items),
|
||||
'#weight' => 1,
|
||||
);
|
||||
|
||||
$form['links']['#weight'] = 2;
|
||||
|
||||
$form['openid_identifier'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Log in using OpenID'),
|
||||
'#size' => ($form_id == 'user_login') ? 58 : 13,
|
||||
'#maxlength' => 255,
|
||||
'#weight' => -1,
|
||||
'#description' => l(t('What is OpenID?'), 'http://openid.net/', array('external' => TRUE)),
|
||||
);
|
||||
$form['openid.return_to'] = array('#type' => 'hidden', '#value' => url('openid/authenticate', array('absolute' => TRUE, 'query' => user_login_destination())));
|
||||
}
|
||||
elseif ($form_id == 'user_register' && isset($_SESSION['openid']['values'])) {
|
||||
// We were unable to auto-register a new user. Prefill the registration
|
||||
// form with the values we have.
|
||||
$form['name']['#default_value'] = $_SESSION['openid']['values']['name'];
|
||||
$form['mail']['#default_value'] = $_SESSION['openid']['values']['mail'];
|
||||
// If user_email_verification is off, hide the password field and just fill
|
||||
// with random password to avoid confusion.
|
||||
if (!variable_get('user_email_verification', TRUE)) {
|
||||
$form['pass']['#type'] = 'hidden';
|
||||
$form['pass']['#value'] = user_password();
|
||||
}
|
||||
$form['auth_openid'] = array('#type' => 'hidden', '#value' => $_SESSION['openid']['values']['auth_openid']);
|
||||
$form['openid_display'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Your OpenID'),
|
||||
'#description' => t('This OpenID will be attached to your account after registration.'),
|
||||
'#value' => check_plain($_SESSION['openid']['values']['auth_openid']),
|
||||
);
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Login form _validate hook
|
||||
*/
|
||||
function openid_login_validate($form, &$form_state) {
|
||||
$return_to = $form_state['values']['openid.return_to'];
|
||||
if (empty($return_to)) {
|
||||
$return_to = url('', array('absolute' => TRUE));
|
||||
}
|
||||
|
||||
openid_begin($form_state['values']['openid_identifier'], $return_to, $form_state['values']);
|
||||
}
|
||||
|
||||
/**
|
||||
* The initial step of OpenID authentication responsible for the following:
|
||||
* - Perform discovery on the claimed OpenID.
|
||||
* - If possible, create an association with the Provider's endpoint.
|
||||
* - Create the authentication request.
|
||||
* - Perform the appropriate redirect.
|
||||
*
|
||||
* @param $claimed_id The OpenID to authenticate
|
||||
* @param $return_to The endpoint to return to from the OpenID Provider
|
||||
*/
|
||||
function openid_begin($claimed_id, $return_to = '', $form_values = array()) {
|
||||
module_load_include('inc', 'openid');
|
||||
|
||||
$claimed_id = _openid_normalize($claimed_id);
|
||||
|
||||
$services = openid_discovery($claimed_id);
|
||||
if (count($services) == 0) {
|
||||
form_set_error('openid_identifier', t('Sorry, that is not a valid OpenID. Please ensure you have spelled your ID correctly.'));
|
||||
return;
|
||||
}
|
||||
|
||||
// Store discovered information in the users' session so we don't have to rediscover.
|
||||
$_SESSION['openid']['service'] = $services[0];
|
||||
// Store the claimed id
|
||||
$_SESSION['openid']['claimed_id'] = $claimed_id;
|
||||
// Store the login form values so we can pass them to
|
||||
// user_exteral_login later.
|
||||
$_SESSION['openid']['user_login_values'] = $form_values;
|
||||
|
||||
$op_endpoint = $services[0]['uri'];
|
||||
// If bcmath is present, then create an association
|
||||
$assoc_handle = '';
|
||||
if (function_exists('bcadd')) {
|
||||
$assoc_handle = openid_association($op_endpoint);
|
||||
}
|
||||
|
||||
// Now that there is an association created, move on
|
||||
// to request authentication from the IdP
|
||||
// First check for LocalID. If not found, check for Delegate. Fall
|
||||
// back to $claimed_id if neither is found.
|
||||
if (!empty($services[0]['localid'])) {
|
||||
$identity = $services[0]['localid'];
|
||||
}
|
||||
else if (!empty($services[0]['delegate'])) {
|
||||
$identity = $services[0]['delegate'];
|
||||
}
|
||||
else {
|
||||
$identity = $claimed_id;
|
||||
}
|
||||
|
||||
if (isset($services[0]['types']) && is_array($services[0]['types']) && in_array(OPENID_NS_2_0 .'/server', $services[0]['types'])) {
|
||||
$claimed_id = $identity = 'http://specs.openid.net/auth/2.0/identifier_select';
|
||||
}
|
||||
$authn_request = openid_authentication_request($claimed_id, $identity, $return_to, $assoc_handle, $services[0]['version']);
|
||||
|
||||
if ($services[0]['version'] == 2) {
|
||||
openid_redirect($op_endpoint, $authn_request);
|
||||
}
|
||||
else {
|
||||
openid_redirect_http($op_endpoint, $authn_request);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Completes OpenID authentication by validating returned data from the OpenID
|
||||
* Provider.
|
||||
*
|
||||
* @param $response Array of returned values from the OpenID Provider.
|
||||
*
|
||||
* @return $response Response values for further processing with
|
||||
* $response['status'] set to one of 'success', 'failed' or 'cancel'.
|
||||
*/
|
||||
function openid_complete($response = array()) {
|
||||
global $base_url;
|
||||
module_load_include('inc', 'openid');
|
||||
|
||||
if (count($response) == 0) {
|
||||
$response = _openid_response();
|
||||
}
|
||||
|
||||
// Default to failed response
|
||||
$response['status'] = 'failed';
|
||||
if (isset($_SESSION['openid']['service']['uri']) && isset($_SESSION['openid']['claimed_id'])) {
|
||||
$service = $_SESSION['openid']['service'];
|
||||
$claimed_id = $_SESSION['openid']['claimed_id'];
|
||||
unset($_SESSION['openid']['service']);
|
||||
unset($_SESSION['openid']['claimed_id']);
|
||||
if (isset($response['openid.mode'])) {
|
||||
if ($response['openid.mode'] == 'cancel') {
|
||||
$response['status'] = 'cancel';
|
||||
}
|
||||
else {
|
||||
if (openid_verify_assertion($service, $response)) {
|
||||
// If the returned claimed_id is different from the session claimed_id,
|
||||
// then we need to do discovery and make sure the op_endpoint matches.
|
||||
if ($service['version'] == 2) {
|
||||
// Returned Claimed Identifier could contain unique fragment
|
||||
// identifier to allow identifier recycling so we need to preserve
|
||||
// it in the response.
|
||||
$response_claimed_id = _openid_normalize($response['openid.claimed_id']);
|
||||
|
||||
if ($response_claimed_id != $claimed_id || $response_claimed_id != $response['openid.identity']) {
|
||||
$disco = openid_discovery($response['openid.claimed_id']);
|
||||
|
||||
if ($disco[0]['uri'] != $service['uri']) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
if (!empty($disco[0]['localid'])) {
|
||||
$identity = $disco[0]['localid'];
|
||||
}
|
||||
else if (!empty($disco[0]['delegate'])) {
|
||||
$identity = $disco[0]['delegate'];
|
||||
}
|
||||
else {
|
||||
$identity = FALSE;
|
||||
}
|
||||
|
||||
// The OP-Local Identifier (if different than the Claimed
|
||||
// Identifier) must be present in the XRDS document.
|
||||
if ($response_claimed_id != $response['openid.identity'] && (!$identity || $identity != $response['openid.identity'])) {
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
$response['openid.claimed_id'] = $claimed_id;
|
||||
}
|
||||
// Verify that openid.return_to matches the current URL (see OpenID
|
||||
// Authentication 2.0, section 11.1).
|
||||
// While OpenID Authentication 1.1, section 4.3 does not mandate
|
||||
// return_to verification, the received return_to should still
|
||||
// match these constraints.
|
||||
$return_to_parts = parse_url($response['openid.return_to']);
|
||||
|
||||
$base_url_parts = parse_url($base_url);
|
||||
$current_parts = parse_url($base_url_parts['scheme'] .'://'. $base_url_parts['host'] . request_uri());
|
||||
|
||||
if ($return_to_parts['scheme'] != $current_parts['scheme'] ||
|
||||
$return_to_parts['host'] != $current_parts['host'] ||
|
||||
$return_to_parts['path'] != $current_parts['path']) {
|
||||
|
||||
return $response;
|
||||
}
|
||||
// Verify that all query parameters in the openid.return_to URL have
|
||||
// the same value in the current URL. In addition, the current URL
|
||||
// contains a number of other parameters added by the OpenID Provider.
|
||||
parse_str(isset($return_to_parts['query']) ? $return_to_parts['query'] : '', $return_to_query_parameters);
|
||||
foreach ($return_to_query_parameters as $name => $value) {
|
||||
if (!array_key_exists($name, $_GET) || $_GET[$name] != $value) {
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
$response['status'] = 'success';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform discovery on a claimed ID to determine the OpenID provider endpoint.
|
||||
*
|
||||
* @param $claimed_id The OpenID URL to perform discovery on.
|
||||
*
|
||||
* @return Array of services discovered (including OpenID version, endpoint
|
||||
* URI, etc).
|
||||
*/
|
||||
function openid_discovery($claimed_id) {
|
||||
module_load_include('inc', 'openid');
|
||||
module_load_include('inc', 'openid', 'xrds');
|
||||
|
||||
$services = array();
|
||||
|
||||
$xrds_url = $claimed_id;
|
||||
if (_openid_is_xri($claimed_id)) {
|
||||
$xrds_url = 'http://xri.net/'. $claimed_id;
|
||||
}
|
||||
$url = @parse_url($xrds_url);
|
||||
if ($url['scheme'] == 'http' || $url['scheme'] == 'https') {
|
||||
// For regular URLs, try Yadis resolution first, then HTML-based discovery
|
||||
$headers = array('Accept' => 'application/xrds+xml');
|
||||
$result = drupal_http_request($xrds_url, $headers);
|
||||
|
||||
if (!isset($result->error)) {
|
||||
if (isset($result->headers['Content-Type']) && preg_match("/application\/xrds\+xml/", $result->headers['Content-Type'])) {
|
||||
// Parse XML document to find URL
|
||||
$services = xrds_parse($result->data);
|
||||
}
|
||||
else {
|
||||
$xrds_url = NULL;
|
||||
if (isset($result->headers['X-XRDS-Location'])) {
|
||||
$xrds_url = $result->headers['X-XRDS-Location'];
|
||||
}
|
||||
else {
|
||||
// Look for meta http-equiv link in HTML head
|
||||
$xrds_url = _openid_meta_httpequiv('X-XRDS-Location', $result->data);
|
||||
}
|
||||
if (!empty($xrds_url)) {
|
||||
$headers = array('Accept' => 'application/xrds+xml');
|
||||
$xrds_result = drupal_http_request($xrds_url, $headers);
|
||||
if (!isset($xrds_result->error)) {
|
||||
$services = xrds_parse($xrds_result->data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for HTML delegation
|
||||
if (count($services) == 0) {
|
||||
// Look for 2.0 links
|
||||
$uri = _openid_link_href('openid2.provider', $result->data);
|
||||
$delegate = _openid_link_href('openid2.local_id', $result->data);
|
||||
$version = 2;
|
||||
|
||||
// 1.0 links
|
||||
if (empty($uri)) {
|
||||
$uri = _openid_link_href('openid.server', $result->data);
|
||||
$delegate = _openid_link_href('openid.delegate', $result->data);
|
||||
$version = 1;
|
||||
}
|
||||
if (!empty($uri)) {
|
||||
$services[] = array('uri' => $uri, 'delegate' => $delegate, 'version' => $version);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $services;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to create a shared secret with the OpenID Provider.
|
||||
*
|
||||
* @param $op_endpoint URL of the OpenID Provider endpoint.
|
||||
*
|
||||
* @return $assoc_handle The association handle.
|
||||
*/
|
||||
function openid_association($op_endpoint) {
|
||||
module_load_include('inc', 'openid');
|
||||
|
||||
// Remove Old Associations:
|
||||
db_query("DELETE FROM {openid_association} WHERE created + expires_in < %d", time());
|
||||
|
||||
// Check to see if we have an association for this IdP already
|
||||
$assoc_handle = db_result(db_query("SELECT assoc_handle FROM {openid_association} WHERE idp_endpoint_uri = '%s'", $op_endpoint));
|
||||
if (empty($assoc_handle)) {
|
||||
$mod = OPENID_DH_DEFAULT_MOD;
|
||||
$gen = OPENID_DH_DEFAULT_GEN;
|
||||
$r = _openid_dh_rand($mod);
|
||||
$private = bcadd($r, 1);
|
||||
$public = bcpowmod($gen, $private, $mod);
|
||||
|
||||
// If there is no existing association, then request one
|
||||
$assoc_request = openid_association_request($public);
|
||||
$assoc_message = _openid_encode_message(_openid_create_message($assoc_request));
|
||||
$assoc_headers = array('Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8');
|
||||
$assoc_result = drupal_http_request($op_endpoint, $assoc_headers, 'POST', $assoc_message);
|
||||
if (isset($assoc_result->error)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$assoc_response = _openid_parse_message($assoc_result->data);
|
||||
if (isset($assoc_response['mode']) && $assoc_response['mode'] == 'error') {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if ($assoc_response['session_type'] == 'DH-SHA1') {
|
||||
$spub = _openid_dh_base64_to_long($assoc_response['dh_server_public']);
|
||||
$enc_mac_key = base64_decode($assoc_response['enc_mac_key']);
|
||||
$shared = bcpowmod($spub, $private, $mod);
|
||||
$assoc_response['mac_key'] = base64_encode(_openid_dh_xorsecret($shared, $enc_mac_key));
|
||||
}
|
||||
db_query("INSERT INTO {openid_association} (idp_endpoint_uri, session_type, assoc_handle, assoc_type, expires_in, mac_key, created) VALUES('%s', '%s', '%s', '%s', %d, '%s', %d)",
|
||||
$op_endpoint, $assoc_response['session_type'], $assoc_response['assoc_handle'], $assoc_response['assoc_type'], $assoc_response['expires_in'], $assoc_response['mac_key'], time());
|
||||
|
||||
$assoc_handle = $assoc_response['assoc_handle'];
|
||||
}
|
||||
|
||||
return $assoc_handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate a user or attempt registration.
|
||||
*
|
||||
* @param $response Response values from the OpenID Provider.
|
||||
*/
|
||||
function openid_authentication($response) {
|
||||
module_load_include('inc', 'openid');
|
||||
|
||||
$identity = $response['openid.claimed_id'];
|
||||
|
||||
$account = user_external_load($identity);
|
||||
if (isset($account->uid)) {
|
||||
if (!variable_get('user_email_verification', TRUE) || $account->login) {
|
||||
user_external_login($account, $_SESSION['openid']['user_login_values']);
|
||||
}
|
||||
else {
|
||||
drupal_set_message(t('You must validate your email address for this account before logging in via OpenID'));
|
||||
}
|
||||
}
|
||||
elseif (variable_get('user_register', 1)) {
|
||||
// Register new user
|
||||
$form_state['redirect'] = NULL;
|
||||
// Only signed SREG keys are included as required by OpenID Simple
|
||||
// Registration Extension 1.0, section 4.
|
||||
$signed_keys = explode(',', $response['openid.signed']);
|
||||
$form_state['values']['name'] = in_array('sreg.nickname', $signed_keys) ? $response['openid.sreg.nickname'] : '';
|
||||
$form_state['values']['mail'] = in_array('sreg.email', $signed_keys) ? $response['openid.sreg.email'] : '';
|
||||
$form_state['values']['pass'] = user_password();
|
||||
$form_state['values']['status'] = variable_get('user_register', 1) == 1;
|
||||
$form_state['values']['response'] = $response;
|
||||
$form_state['values']['auth_openid'] = $identity;
|
||||
|
||||
if (empty($form_state['values']['name']) && empty($form_state['values']['mail'])) {
|
||||
drupal_set_message(t('Please complete the registration by filling out the form below. If you already have an account, you can <a href="@login">log in</a> now and add your OpenID under "My account".', array('@login' => url('user/login'))), 'warning');
|
||||
$success = FALSE;
|
||||
}
|
||||
else {
|
||||
$form = drupal_retrieve_form('user_register', $form_state);
|
||||
drupal_prepare_form('user_register', $form, $form_state);
|
||||
drupal_validate_form('user_register', $form, $form_state);
|
||||
$success = !form_get_errors();
|
||||
if (!$success) {
|
||||
drupal_set_message(t('Account registration using the information provided by your OpenID provider failed due to the reasons listed below. Please complete the registration by filling out the form below. If you already have an account, you can <a href="@login">log in</a> now and add your OpenID under "My account".', array('@login' => url('user/login'))), 'warning');
|
||||
// Append form validation errors below the above warning.
|
||||
$messages = drupal_get_messages('error');
|
||||
foreach ($messages['error'] as $message) {
|
||||
drupal_set_message( $message, 'error');
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$success) {
|
||||
// We were unable to register a valid new user, redirect to standard
|
||||
// user/register and prefill with the values we received.
|
||||
$_SESSION['openid']['values'] = $form_state['values'];
|
||||
// We'll want to redirect back to the same place.
|
||||
$destination = drupal_get_destination();
|
||||
unset($_REQUEST['destination']);
|
||||
drupal_goto('user/register', $destination);
|
||||
}
|
||||
else {
|
||||
unset($form_state['values']['response']);
|
||||
$account = user_save('', $form_state['values']);
|
||||
// Terminate if an error occured during user_save().
|
||||
if (!$account) {
|
||||
drupal_set_message(t("Error saving user account."), 'error');
|
||||
drupal_goto();
|
||||
}
|
||||
user_external_login($account);
|
||||
}
|
||||
drupal_redirect_form($form, $form_state['redirect']);
|
||||
}
|
||||
else {
|
||||
drupal_set_message(t('Only site administrators can create new user accounts.'), 'error');
|
||||
}
|
||||
drupal_goto();
|
||||
}
|
||||
|
||||
function openid_association_request($public) {
|
||||
module_load_include('inc', 'openid');
|
||||
|
||||
$request = array(
|
||||
'openid.ns' => OPENID_NS_2_0,
|
||||
'openid.mode' => 'associate',
|
||||
'openid.session_type' => 'DH-SHA1',
|
||||
'openid.assoc_type' => 'HMAC-SHA1'
|
||||
);
|
||||
|
||||
if ($request['openid.session_type'] == 'DH-SHA1' || $request['openid.session_type'] == 'DH-SHA256') {
|
||||
$cpub = _openid_dh_long_to_base64($public);
|
||||
$request['openid.dh_consumer_public'] = $cpub;
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
function openid_authentication_request($claimed_id, $identity, $return_to = '', $assoc_handle = '', $version = 2) {
|
||||
global $base_url;
|
||||
|
||||
module_load_include('inc', 'openid');
|
||||
|
||||
$ns = ($version == 2) ? OPENID_NS_2_0 : OPENID_NS_1_0;
|
||||
$request = array(
|
||||
'openid.ns' => $ns,
|
||||
'openid.mode' => 'checkid_setup',
|
||||
'openid.identity' => $identity,
|
||||
'openid.claimed_id' => $claimed_id,
|
||||
'openid.assoc_handle' => $assoc_handle,
|
||||
'openid.return_to' => $return_to,
|
||||
);
|
||||
|
||||
if ($version == 2) {
|
||||
$request['openid.realm'] = $base_url . '/';
|
||||
}
|
||||
else {
|
||||
$request['openid.trust_root'] = $base_url . '/';
|
||||
}
|
||||
|
||||
// Simple Registration
|
||||
$request['openid.sreg.required'] = 'nickname,email';
|
||||
$request['openid.ns.sreg'] = "http://openid.net/extensions/sreg/1.1";
|
||||
|
||||
$request = array_merge($request, module_invoke_all('openid', 'request', $request));
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to verify the response received from the OpenID Provider.
|
||||
*
|
||||
* @param $service
|
||||
* Array describing the OpenID provider.
|
||||
* @param $response
|
||||
* Array of response values from the provider.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function openid_verify_assertion($service, $response) {
|
||||
module_load_include('inc', 'openid');
|
||||
|
||||
// http://openid.net/specs/openid-authentication-2_0.html#rfc.section.11.3
|
||||
// Check the Nonce to protect against replay attacks.
|
||||
if (!openid_verify_assertion_nonce($service, $response)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// http://openid.net/specs/openid-authentication-2_0.html#rfc.section.11.4
|
||||
// Verify the signatures.
|
||||
$valid = FALSE;
|
||||
$association = db_fetch_object(db_query("SELECT * FROM {openid_association} WHERE idp_endpoint_uri = '%s' AND assoc_handle = '%s'", $service['uri'], $response['openid.assoc_handle']));
|
||||
if ($association && isset($association->session_type)) {
|
||||
// http://openid.net/specs/openid-authentication-2_0.html#rfc.section.11.4.2
|
||||
// Verification using an association.
|
||||
$valid = openid_verify_assertion_signature($service, $association, $response);
|
||||
}
|
||||
else {
|
||||
// http://openid.net/specs/openid-authentication-2_0.html#rfc.section.11.4.3
|
||||
// Direct verification.
|
||||
$request = $response;
|
||||
$request['openid.mode'] = 'check_authentication';
|
||||
$message = _openid_create_message($request);
|
||||
$headers = array('Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8');
|
||||
$result = drupal_http_request($service['uri'], $headers, 'POST', _openid_encode_message($message));
|
||||
if (!isset($result->error)) {
|
||||
$response = _openid_parse_message($result->data);
|
||||
if (strtolower(trim($response['is_valid'])) == 'true') {
|
||||
$valid = TRUE;
|
||||
}
|
||||
else {
|
||||
$valid = FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the signature of the response received from the OpenID provider.
|
||||
*
|
||||
* @param $service
|
||||
* Array describing the OpenID provider.
|
||||
* @param $association
|
||||
* Information on the association with the OpenID provider.
|
||||
* @param $response
|
||||
* Array of response values from the provider.
|
||||
*
|
||||
* @return
|
||||
* TRUE if the signature is valid and covers all fields required to be signed.
|
||||
* @see http://openid.net/specs/openid-authentication-2_0.html#rfc.section.11.4
|
||||
*/
|
||||
function openid_verify_assertion_signature($service, $association, $response) {
|
||||
if ($service['version'] == 2) {
|
||||
// OpenID Authentication 2.0, section 10.1:
|
||||
// These keys must always be signed.
|
||||
$mandatory_keys = array('op_endpoint', 'return_to', 'response_nonce', 'assoc_handle');
|
||||
if (isset($response['openid.claimed_id'])) {
|
||||
// If present, these two keys must also be signed. According to the spec,
|
||||
// they are either both present or both absent.
|
||||
$mandatory_keys[] = 'claimed_id';
|
||||
$mandatory_keys[] = 'identity';
|
||||
}
|
||||
}
|
||||
else {
|
||||
// OpenID Authentication 1.1. section 4.3.3.
|
||||
$mandatory_keys = array('identity', 'return_to');
|
||||
}
|
||||
|
||||
$keys_to_sign = explode(',', $response['openid.signed']);
|
||||
|
||||
if (count(array_diff($mandatory_keys, $keys_to_sign)) > 0) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return _openid_signature($association, $response, $keys_to_sign) == $response['openid.sig'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the nonce has not been used in earlier assertions from the same OpenID provider.
|
||||
*
|
||||
* @param $service
|
||||
* Array describing the OpenID provider.
|
||||
* @param $response
|
||||
* Array of response values from the provider.
|
||||
*
|
||||
* @return
|
||||
* TRUE if the nonce has not expired and has not been used earlier.
|
||||
*/
|
||||
function openid_verify_assertion_nonce($service, $response) {
|
||||
if ($service['version'] != 2) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/', $response['openid.response_nonce'], $matches)) {
|
||||
list(, $year, $month, $day, $hour, $minutes, $seconds) = $matches;
|
||||
$nonce_timestamp = gmmktime($hour, $minutes, $seconds, $month, $day, $year);
|
||||
}
|
||||
else {
|
||||
watchdog('openid', 'Nonce from @endpoint rejected because it is not correctly formatted, nonce: @nonce.', array('@endpoint' => $service['uri'], '@nonce' => $response['openid.response_nonce']), WATCHDOG_WARNING);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// A nonce with a timestamp to far in the past or future will already have
|
||||
// been removed and cannot be checked for single use anymore.
|
||||
$time = time();
|
||||
$expiry = 900;
|
||||
if ($nonce_timestamp <= $time - $expiry || $nonce_timestamp >= $time + $expiry) {
|
||||
watchdog('openid', 'Nonce received from @endpoint is out of range (time difference: @intervals). Check possible clock skew.', array('@endpoint' => $service['uri'], '@interval' => $time - $nonce_timestamp), WATCHDOG_WARNING);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Record that this nonce was used.
|
||||
db_query("INSERT INTO {openid_nonce} (idp_endpoint_uri, nonce, expires) VALUES ('%s', '%s', %d)", $service['uri'], $response['openid.response_nonce'], $nonce_timestamp + $expiry);
|
||||
|
||||
// Count the number of times this nonce was used.
|
||||
$count_used = db_result(db_query("SELECT COUNT(*) FROM {openid_nonce} WHERE nonce = '%s' AND idp_endpoint_uri = '%s'", $response['openid.response_nonce'], $service['uri']));
|
||||
|
||||
if ($count_used == 1) {
|
||||
return TRUE;
|
||||
}
|
||||
else {
|
||||
watchdog('openid', 'Nonce replay attempt blocked from @ip, nonce: @nonce.', array('@ip' => ip_address(), '@nonce' => $response['openid.response_nonce']), WATCHDOG_CRITICAL);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove expired nonces from the database.
|
||||
*
|
||||
* Implementation of hook_cron().
|
||||
*/
|
||||
function openid_cron() {
|
||||
db_query("DELETE FROM {openid_nonce} WHERE expires < %d", time());
|
||||
}
|
113
modules/openid/openid.pages.inc
Normal file
113
modules/openid/openid.pages.inc
Normal file
|
@ -0,0 +1,113 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* User page callbacks for the openid module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Menu callback; Process an OpenID authentication.
|
||||
*/
|
||||
function openid_authentication_page() {
|
||||
$result = openid_complete();
|
||||
switch ($result['status']) {
|
||||
case 'success':
|
||||
return openid_authentication($result);
|
||||
case 'failed':
|
||||
drupal_set_message(t('OpenID login failed.'), 'error');
|
||||
break;
|
||||
case 'cancel':
|
||||
drupal_set_message(t('OpenID login cancelled.'));
|
||||
break;
|
||||
}
|
||||
drupal_goto();
|
||||
}
|
||||
|
||||
/**
|
||||
* Menu callback; Manage OpenID identities for the specified user.
|
||||
*/
|
||||
function openid_user_identities($account) {
|
||||
drupal_set_title(check_plain($account->name));
|
||||
drupal_add_css(drupal_get_path('module', 'openid') .'/openid.css', 'module');
|
||||
|
||||
// Check to see if we got a response
|
||||
$result = openid_complete();
|
||||
if ($result['status'] == 'success') {
|
||||
$identity = $result['openid.claimed_id'];
|
||||
db_query("INSERT INTO {authmap} (uid, authname, module) VALUES (%d, '%s','openid')", $account->uid, $identity);
|
||||
drupal_set_message(t('Successfully added %identity', array('%identity' => $identity)));
|
||||
}
|
||||
|
||||
$header = array(t('OpenID'), t('Operations'));
|
||||
$rows = array();
|
||||
|
||||
$result = db_query("SELECT * FROM {authmap} WHERE module='openid' AND uid=%d", $account->uid);
|
||||
while ($identity = db_fetch_object($result)) {
|
||||
$rows[] = array(check_plain($identity->authname), l(t('Delete'), 'user/'. $account->uid .'/openid/delete/'. $identity->aid));
|
||||
}
|
||||
|
||||
$output = theme('table', $header, $rows);
|
||||
$output .= drupal_get_form('openid_user_add');
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Form builder; Add an OpenID identity.
|
||||
*
|
||||
* @ingroup forms
|
||||
* @see openid_user_add_validate()
|
||||
*/
|
||||
function openid_user_add() {
|
||||
$form['openid_identifier'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('OpenID'),
|
||||
);
|
||||
$form['submit'] = array('#type' => 'submit', '#value' => t('Add an OpenID'));
|
||||
return $form;
|
||||
}
|
||||
|
||||
function openid_user_add_validate($form, &$form_state) {
|
||||
// Check for existing entries.
|
||||
$claimed_id = _openid_normalize($form_state['values']['openid_identifier']);
|
||||
if (db_result(db_query("SELECT authname FROM {authmap} WHERE authname='%s'", $claimed_id))) {
|
||||
form_set_error('openid_identifier', t('That OpenID is already in use on this site.'));
|
||||
}
|
||||
}
|
||||
|
||||
function openid_user_add_submit($form, &$form_state) {
|
||||
$return_to = url('user/'. arg(1) .'/openid', array('absolute' => TRUE));
|
||||
openid_begin($form_state['values']['openid_identifier'], $return_to);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Present a confirmation form to delete the specified OpenID identity from the system.
|
||||
*
|
||||
* @ingroup forms
|
||||
* @see openid_user_delete_form_submit()
|
||||
*/
|
||||
function openid_user_delete_form($form_state, $account, $aid = 0) {
|
||||
$authname = db_result(db_query('SELECT authname FROM {authmap} WHERE uid = %d AND aid = %d', $account->uid, $aid));
|
||||
|
||||
$form = array();
|
||||
|
||||
$form['uid'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => $account->uid,
|
||||
);
|
||||
|
||||
$form['aid'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => $aid,
|
||||
);
|
||||
|
||||
return confirm_form($form, t('Are you sure you want to delete the OpenID %authname for %user?', array('%authname' => $authname, '%user' => $account->name)), 'user/'. $account->uid .'/openid');
|
||||
}
|
||||
|
||||
function openid_user_delete_form_submit($form, &$form_state) {
|
||||
db_query("DELETE FROM {authmap} WHERE uid = %d AND aid = %d AND module = 'openid'", $form_state['values']['uid'], $form_state['values']['aid']);
|
||||
if (db_affected_rows()) {
|
||||
drupal_set_message(t('OpenID deleted.'));
|
||||
}
|
||||
$form_state['redirect'] = 'user/'. $form_state['values']['uid'] .'/openid';
|
||||
}
|
97
modules/openid/xrds.inc
Normal file
97
modules/openid/xrds.inc
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
// Global variables to track parsing state
|
||||
$xrds_open_elements = array();
|
||||
$xrds_services = array();
|
||||
$xrds_current_service = array();
|
||||
|
||||
/**
|
||||
* Main entry point for parsing XRDS documents
|
||||
*/
|
||||
function xrds_parse($xml) {
|
||||
global $xrds_services;
|
||||
|
||||
$parser = xml_parser_create_ns();
|
||||
xml_set_element_handler($parser, '_xrds_element_start', '_xrds_element_end');
|
||||
xml_set_character_data_handler($parser, '_xrds_cdata');
|
||||
|
||||
// Since DOCTYPE declarations from an untrusted source could be malicious, we
|
||||
// stop parsing here and treat the XML as invalid. XRDS documents do not
|
||||
// require, and are not expected to have, a DOCTYPE.
|
||||
if (preg_match('/<!DOCTYPE/i', $xml)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
// Also stop parsing if there is an unreasonably large number of tags.
|
||||
// substr_count() has much better performance (compared to preg_match_all())
|
||||
// for large payloads but is less accurate, so we check for twice the desired
|
||||
// number of allowed tags (to take into account opening/closing tags as well
|
||||
// as false positives).
|
||||
if (substr_count($xml, '<') > 2 * variable_get('openid_xrds_maximum_tag_count', 30000)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
xml_parse($parser, $xml);
|
||||
xml_parser_free($parser);
|
||||
|
||||
return $xrds_services;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parser callback functions
|
||||
*/
|
||||
function _xrds_element_start(&$parser, $name, $attribs) {
|
||||
global $xrds_open_elements;
|
||||
|
||||
$xrds_open_elements[] = _xrds_strip_namespace($name);
|
||||
}
|
||||
|
||||
function _xrds_element_end(&$parser, $name) {
|
||||
global $xrds_open_elements, $xrds_services, $xrds_current_service;
|
||||
|
||||
$name = _xrds_strip_namespace($name);
|
||||
if ($name == 'SERVICE') {
|
||||
if (in_array(OPENID_NS_2_0 .'/signon', $xrds_current_service['types']) ||
|
||||
in_array(OPENID_NS_2_0 .'/server', $xrds_current_service['types'])) {
|
||||
$xrds_current_service['version'] = 2;
|
||||
}
|
||||
elseif (in_array(OPENID_NS_1_1, $xrds_current_service['types']) ||
|
||||
in_array(OPENID_NS_1_0, $xrds_current_service['types'])) {
|
||||
$xrds_current_service['version'] = 1;
|
||||
}
|
||||
if (!empty($xrds_current_service['version'])) {
|
||||
$xrds_services[] = $xrds_current_service;
|
||||
}
|
||||
$xrds_current_service = array();
|
||||
}
|
||||
array_pop($xrds_open_elements);
|
||||
}
|
||||
|
||||
function _xrds_cdata(&$parser, $data) {
|
||||
global $xrds_open_elements, $xrds_services, $xrds_current_service;
|
||||
$path = strtoupper(implode('/', $xrds_open_elements));
|
||||
switch ($path) {
|
||||
case 'XRDS/XRD/SERVICE/TYPE':
|
||||
$xrds_current_service['types'][] = $data;
|
||||
break;
|
||||
case 'XRDS/XRD/SERVICE/URI':
|
||||
$xrds_current_service['uri'] = $data;
|
||||
break;
|
||||
case 'XRDS/XRD/SERVICE/DELEGATE':
|
||||
$xrds_current_service['delegate'] = $data;
|
||||
break;
|
||||
case 'XRDS/XRD/SERVICE/LOCALID':
|
||||
$xrds_current_service['localid'] = $data;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function _xrds_strip_namespace($name) {
|
||||
// Strip namespacing.
|
||||
$pos = strrpos($name, ':');
|
||||
if ($pos !== FALSE) {
|
||||
$name = substr($name, $pos + 1, strlen($name));
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
Reference in a new issue