12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- <?php
- 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),
- 'file' => 'openid.pages.inc',
- );
- return $items;
- }
- function openid_menu_site_status_alter(&$menu_site_status, $path) {
-
- if ($menu_site_status == MENU_SITE_OFFLINE && user_is_anonymous() && $path == 'openid/authenticate') {
- $menu_site_status = MENU_SITE_ONLINE;
- }
- }
- function openid_admin_paths() {
- $paths = array(
- 'user/*/openid' => TRUE,
- 'user/*/openid/delete' => TRUE,
- );
- return $paths;
- }
- 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 in to 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 log in, 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 = '';
- $output .= '<h3>' . t('About') . '</h3>';
- $output .= '<p>' . t('The OpenID module allows users to log in using the OpenID single sign on service. <a href="@openid-net">OpenID</a> 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 the site being logged into. The main benefit to users is that they 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. For more information, see the online handbook entry for <a href="@handbook">OpenID module</a>.', array('@openid-net' => 'http://openid.net', '@handbook' => 'http://drupal.org/documentation/modules/openid')) . '</p>';
- $output .= '<h3>' . t('Uses') . '</h3>';
- $output .= '<dl>';
- $output .= '<dt>' . t('Logging in with OpenID') . '</dt>';
- $output .= '<dd>' . t("To log in using OpenID, a user must already have an OpenID account. Users can then create site 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 helps increase the user base, and offers convenience and security to the users. Because OpenID cannot guarantee a user is legitimate, email verification is still necessary. When logging in, users are presented with the option of entering their OpenID URL, which will look like <em>myusername.openidprovider.com</em>. The 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 the site being logged into record the user's OpenID password.") . '</dd>';
- $output .= '</dl>';
- return $output;
- }
- }
- function openid_user_insert(&$edit, $account, $category) {
- if (!empty($edit['openid_claimed_id'])) {
-
- if (variable_get('user_email_verification', TRUE)) {
- drupal_set_message(t('Once you have verified your e-mail address, you may log in via OpenID.'));
- }
- user_set_authmaps($account, array('authname_openid' => $edit['openid_claimed_id']));
- unset($_SESSION['openid']);
- unset($edit['openid_claimed_id']);
- }
- }
- function openid_user_login(&$edit, $account) {
- if (isset($_SESSION['openid'])) {
-
- user_cookie_save(array_intersect_key($_SESSION['openid']['user_login_values'], array_flip(array('openid_identifier'))));
- unset($_SESSION['openid']);
- }
- }
- function openid_user_logout($account) {
- if (isset($_COOKIE['Drupal_visitor_openid_identifier'])) {
- user_cookie_delete('openid_identifier');
- }
- }
- function openid_form_user_login_block_alter(&$form, &$form_state) {
- _openid_user_login_form_alter($form, $form_state);
- }
- function openid_form_user_login_alter(&$form, &$form_state) {
- _openid_user_login_form_alter($form, $form_state);
- }
- function _openid_user_login_form_alter(&$form, &$form_state) {
- $form['#attached']['css'][] = drupal_get_path('module', 'openid') . '/openid.css';
- $form['#attached']['js'][] = drupal_get_path('module', 'openid') . '/openid.js';
- $form['#attached']['library'][] = array('system', 'jquery.cookie');
- if (!empty($form_state['input']['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'), '#openid-login', array('external' => TRUE)),
- 'class' => array('openid-link'),
- );
- $items[] = array(
- 'data' => l(t('Cancel OpenID login'), '#', array('external' => TRUE)),
- 'class' => array('user-link'),
- );
- $form['openid_links'] = array(
- '#theme' => 'item_list',
- '#items' => $items,
- '#attributes' => array('class' => array('openid-links')),
- '#weight' => 1,
- );
- $form['links']['#weight'] = 2;
- $form['openid_identifier'] = array(
- '#type' => 'textfield',
- '#title' => t('Log in using OpenID'),
- '#size' => $form['name']['#size'],
- '#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())));
- }
- function openid_form_user_register_form_alter(&$form, &$form_state) {
- if (isset($_SESSION['openid']['response'])) {
- module_load_include('inc', 'openid');
- $response = $_SESSION['openid']['response'];
-
-
-
- $sreg_values = openid_extract_namespace($response, OPENID_NS_SREG, 'sreg', TRUE);
-
-
-
-
- $ax_values = openid_extract_namespace($response, OPENID_NS_AX, 'ax', TRUE);
- if (!empty($sreg_values['nickname'])) {
-
- $form['account']['name']['#default_value'] = $sreg_values['nickname'];
- }
- elseif ($ax_name_values = openid_extract_ax_values($ax_values, array('http://axschema.org/namePerson/friendly', 'http://schema.openid.net/namePerson/friendly'))) {
-
- $form['account']['name']['#default_value'] = current($ax_name_values);
- }
- else {
- $form['account']['name']['#default_value'] = '';
- }
- if (!empty($sreg_values['email'])) {
-
- $form['account']['mail']['#default_value'] = $sreg_values['email'];
- }
- elseif ($ax_mail_values = openid_extract_ax_values($ax_values, array('http://axschema.org/contact/email', 'http://schema.openid.net/contact/email'))) {
-
- $form['account']['mail']['#default_value'] = current($ax_mail_values);
- }
-
-
- if (!variable_get('user_email_verification', TRUE)) {
- $form['account']['pass']['#type'] = 'hidden';
- $form['account']['pass']['#value'] = user_password();
- }
- $form['openid_claimed_id'] = array(
- '#type' => 'value',
- '#default_value' => $response['openid.claimed_id'],
- );
- $form['openid_display'] = array(
- '#type' => 'item',
- '#title' => t('Your OpenID'),
- '#description' => t('This OpenID will be attached to your account after registration.'),
- '#markup' => check_plain($response['openid.claimed_id']),
- );
- }
- }
- 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']);
- }
- function openid_begin($claimed_id, $return_to = '', $form_values = array()) {
- module_load_include('inc', 'openid');
- $service = NULL;
- $claimed_id = openid_normalize($claimed_id);
- $discovery = openid_discovery($claimed_id);
- if (!empty($discovery['services'])) {
- $service = _openid_select_service($discovery['services']);
- }
-
- if (!$discovery || !$service) {
- form_set_error('openid_identifier', t('Sorry, that is not a valid OpenID. Ensure you have spelled your ID correctly.'));
- return;
- }
-
- if (!empty($discovery['claimed_id'])) {
- $claimed_id = $discovery['claimed_id'];
- }
-
- $_SESSION['openid']['service'] = $service;
-
- $_SESSION['openid']['claimed_id'] = $claimed_id;
-
-
- $_SESSION['openid']['user_login_values'] = $form_values;
-
- $assoc_handle = '';
- if (_openid_get_math_library()) {
- $assoc_handle = openid_association($service['uri']);
- }
- if (in_array('http://specs.openid.net/auth/2.0/server', $service['types'])) {
-
- $claimed_id = $identity = 'http://specs.openid.net/auth/2.0/identifier_select';
- }
- else {
-
-
- if (!empty($service['claimed_id'])) {
- $claimed_id = $service['claimed_id'];
- }
- $identity = !empty($service['identity']) ? $service['identity'] : $claimed_id;
- }
- $request = openid_authentication_request($claimed_id, $identity, $return_to, $assoc_handle, $service);
- if ($service['version'] == 2) {
- openid_redirect($service['uri'], $request);
- }
- else {
- openid_redirect_http($service['uri'], $request);
- }
- }
- function openid_complete($response = array()) {
- module_load_include('inc', 'openid');
- if (count($response) == 0) {
- $response = _openid_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 (!empty($service['claimed_id'])) {
- $response['openid.claimed_id'] = $service['claimed_id'];
- }
- elseif ($service['version'] == 2) {
-
-
-
- $response_claimed_id = openid_normalize($response['openid.claimed_id']);
-
-
-
-
-
- if ($response_claimed_id != $claimed_id || $response_claimed_id != $response['openid.identity']) {
- $discovery = openid_discovery($response['openid.claimed_id']);
- $uris = array();
- if ($discovery && !empty($discovery['services'])) {
- foreach ($discovery['services'] as $discovered_service) {
- if (!in_array('http://specs.openid.net/auth/2.0/server', $discovered_service['types']) && !in_array('http://specs.openid.net/auth/2.0/signon', $discovered_service['types'])) {
- continue;
- }
-
-
- if ($response_claimed_id != $response['openid.identity'] && (!isset($discovered_service['identity']) || $discovered_service['identity'] != $response['openid.identity'])) {
- continue;
- }
- $uris[] = $discovered_service['uri'];
- }
- }
- if (!in_array($service['uri'], $uris)) {
- return $response;
- }
- }
- }
- else {
- $response['openid.claimed_id'] = $claimed_id;
- }
- $response['status'] = 'success';
- }
- }
- }
- }
- return $response;
- }
- function openid_discovery($claimed_id) {
- module_load_include('inc', 'openid');
- $methods = module_invoke_all('openid_discovery_method_info');
- drupal_alter('openid_discovery_method_info', $methods);
-
- foreach ($methods as $method) {
- $discovery = $method($claimed_id);
- if (!empty($discovery)) {
- return $discovery;
- }
- }
- return FALSE;
- }
- function openid_openid_discovery_method_info() {
-
-
-
-
- return array(
- 'xri' => '_openid_xri_discovery',
- 'xrds' => '_openid_xrds_discovery',
- );
- }
- function _openid_xri_discovery($claimed_id) {
- if (_openid_is_xri($claimed_id)) {
-
-
- $xrds_url = variable_get('xri_proxy_resolver', 'http://xri.net/') . rawurlencode($claimed_id) . '?_xrd_r=application/xrds+xml';
- $discovery = _openid_xrds_discovery($xrds_url);
- if (!empty($discovery['services']) && is_array($discovery['services'])) {
- foreach ($discovery['services'] as $i => &$service) {
- $status = $service['xrd']->children(OPENID_NS_XRD)->Status;
- if ($status && $status->attributes()->cid == 'verified') {
- $service['claimed_id'] = openid_normalize((string)$service['xrd']->children(OPENID_NS_XRD)->CanonicalID);
- }
- else {
-
- unset($discovery['services'][$i]);
- }
- }
- if (!empty($discovery['services'])) {
- return $discovery;
- }
- }
- }
- }
- function _openid_xrds_discovery($claimed_id) {
- $services = array();
- $xrds_url = $claimed_id;
- $scheme = @parse_url($xrds_url, PHP_URL_SCHEME);
- if ($scheme == 'http' || $scheme == 'https') {
-
- $headers = array('Accept' => 'application/xrds+xml');
- $result = drupal_http_request($xrds_url, array('headers' => $headers));
-
-
-
-
-
- if (!isset($result->error) && $result->code == 200) {
-
- if (!empty($result->redirect_url)) {
- $claimed_id = openid_normalize($result->redirect_url);
- }
- if (isset($result->headers['content-type']) && preg_match("/application\/xrds\+xml/", $result->headers['content-type'])) {
-
- $services = _openid_xrds_parse($result->data);
- }
- else {
- $xrds_url = NULL;
- if (isset($result->headers['x-xrds-location'])) {
- $xrds_url = $result->headers['x-xrds-location'];
- }
- else {
-
- $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, array('headers' => $headers));
- if (!isset($xrds_result->error)) {
- $services = _openid_xrds_parse($xrds_result->data);
- }
- }
- }
-
- if (count($services) == 0) {
-
- $uri = _openid_link_href('openid2.provider', $result->data);
- $identity = _openid_link_href('openid2.local_id', $result->data);
- $type = 'http://specs.openid.net/auth/2.0/signon';
-
- if (empty($uri)) {
- $uri = _openid_link_href('openid.server', $result->data);
- $identity = _openid_link_href('openid.delegate', $result->data);
- $type = 'http://openid.net/signon/1.1';
- }
- if (!empty($uri)) {
- $services[] = array(
- 'uri' => $uri,
- 'identity' => $identity,
- 'types' => array($type),
- );
- }
- }
- }
- }
- if (!empty($services)) {
- return array(
- 'services' => $services,
- 'claimed_id' => $claimed_id,
- );
- }
- }
- function openid_openid_normalization_method_info() {
-
-
-
- return array(
- 'xri' => '_openid_xri_normalize',
- 'url' => '_openid_url_normalize',
- );
- }
- function openid_association($op_endpoint) {
- module_load_include('inc', 'openid');
-
- db_delete('openid_association')
- ->where('created + expires_in < :request_time', array(':request_time' => REQUEST_TIME))
- ->execute();
-
- $assoc_handle = db_query("SELECT assoc_handle FROM {openid_association} WHERE idp_endpoint_uri = :endpoint", array(':endpoint' => $op_endpoint))->fetchField();
- if (empty($assoc_handle)) {
- $mod = OPENID_DH_DEFAULT_MOD;
- $gen = OPENID_DH_DEFAULT_GEN;
- $r = _openid_dh_rand($mod);
- $private = _openid_math_add($r, 1);
- $public = _openid_math_powmod($gen, $private, $mod);
-
- $assoc_request = openid_association_request($public);
- $assoc_message = _openid_encode_message(_openid_create_message($assoc_request));
- $assoc_options = array(
- 'headers' => array('Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'),
- 'method' => 'POST',
- 'data' => $assoc_message,
- );
- $assoc_result = drupal_http_request($op_endpoint, $assoc_options);
- 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 = _openid_math_powmod($spub, $private, $mod);
- $assoc_response['mac_key'] = base64_encode(_openid_dh_xorsecret($shared, $enc_mac_key));
- }
- db_insert('openid_association')
- ->fields(array(
- 'idp_endpoint_uri' => $op_endpoint,
- 'session_type' => $assoc_response['session_type'],
- 'assoc_handle' => $assoc_response['assoc_handle'],
- 'assoc_type' => $assoc_response['assoc_type'],
- 'expires_in' => $assoc_response['expires_in'],
- 'mac_key' => $assoc_response['mac_key'],
- 'created' => REQUEST_TIME,
- ))
- ->execute();
- $assoc_handle = $assoc_response['assoc_handle'];
- }
- return $assoc_handle;
- }
- function openid_authentication($response) {
- $identity = $response['openid.claimed_id'];
- $account = user_external_load($identity);
-
-
- if (!isset($account->uid) && variable_get('openid_less_obtrusive_transition', FALSE)) {
- module_load_include('inc', 'openid');
- $account = _openid_invalid_openid_transition($identity, $response);
- }
- if (isset($account->uid)) {
- if (!variable_get('user_email_verification', TRUE) || $account->login) {
-
- $state['values']['name'] = $account->name;
- user_login_name_validate(array(), $state);
- if (!form_get_errors()) {
-
- $form_state['uid'] = $account->uid;
- user_login_submit(array(), $form_state);
-
- module_invoke_all('openid_response', $response, $account);
- }
- }
- else {
- drupal_set_message(t('You must validate your email address for this account before logging in via OpenID.'));
- }
- }
- elseif (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
-
-
- $_SESSION['openid']['response'] = $response;
- $form_state['values'] = array();
- $form_state['values']['op'] = t('Create new account');
- drupal_form_submit('user_register_form', $form_state);
- if (!empty($form_state['user'])) {
- module_invoke_all('openid_response', $response, $form_state['user']);
- drupal_goto();
- }
- $messages = drupal_get_messages('error');
- if (empty($form_state['values']['name']) || empty($form_state['values']['mail'])) {
-
-
-
- drupal_set_message(t('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');
- }
- else {
- drupal_set_message(t('Account registration using the information provided by your OpenID provider failed due to the reasons listed below. 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');
-
- foreach ($messages['error'] as $message) {
- drupal_set_message($message, 'error');
- }
- }
-
-
- $destination = drupal_get_destination();
- unset($_GET['destination']);
- drupal_goto('user/register', array('query' => $destination));
- }
- 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 = '', $service) {
- global $base_url;
- module_load_include('inc', 'openid');
- $request = array(
- 'openid.mode' => 'checkid_setup',
- 'openid.identity' => $identity,
- 'openid.assoc_handle' => $assoc_handle,
- 'openid.return_to' => $return_to,
- );
- if ($service['version'] == 2) {
- $request['openid.ns'] = OPENID_NS_2_0;
- $request['openid.claimed_id'] = $claimed_id;
- $request['openid.realm'] = $base_url .'/';
- }
- else {
- $request['openid.trust_root'] = $base_url .'/';
- }
-
-
- $request['openid.ns.sreg'] = OPENID_NS_SREG;
- $request['openid.sreg.required'] = 'nickname,email';
-
-
-
- if (in_array(OPENID_NS_AX, $service['types'])) {
- $request['openid.ns.ax'] = OPENID_NS_AX;
- $request['openid.ax.mode'] = 'fetch_request';
- $request['openid.ax.required'] = 'mail_ao,name_ao,mail_son,name_son';
-
-
-
-
-
- $request['openid.ax.type.mail_ao'] = 'http://axschema.org/contact/email';
- $request['openid.ax.type.name_ao'] = 'http://axschema.org/namePerson/friendly';
-
- $request['openid.ax.type.mail_son'] = 'http://schema.openid.net/contact/email';
- $request['openid.ax.type.name_son'] = 'http://schema.openid.net/namePerson/friendly';
- }
- $request = array_merge($request, module_invoke_all('openid', 'request', $request));
-
-
-
-
- $flattened_request = array();
- foreach ($request as $key => $value) {
- if (is_array($value)) {
- $flattened_request[$key] = end($value);
- }
- else {
- $flattened_request[$key] = $value;
- }
- }
- return $flattened_request;
- }
- function openid_verify_assertion($service, $response) {
- module_load_include('inc', 'openid');
-
-
- if (!openid_verify_assertion_nonce($service, $response)) {
- return FALSE;
- }
-
-
- if (!openid_verify_assertion_return_url($service, $response)) {
- return FALSE;
- }
-
-
- $valid = FALSE;
- $association = FALSE;
-
-
-
- if (!empty($response['openid.assoc_handle']) && empty($response['openid.invalidate_handle'])) {
- $association = db_query("SELECT * FROM {openid_association} WHERE idp_endpoint_uri = :endpoint AND assoc_handle = :assoc_handle", array(':endpoint' => $service['uri'], ':assoc_handle' => $response['openid.assoc_handle']))->fetchObject();
- }
- if ($association && isset($association->session_type)) {
-
-
- $valid = openid_verify_assertion_signature($service, $association, $response);
- }
- else {
-
-
-
-
- $request = $response;
- $request['openid.mode'] = 'check_authentication';
- $message = _openid_create_message($request);
- $options = array(
- 'headers' => array('Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'),
- 'method' => 'POST',
- 'data' => _openid_encode_message($message),
- );
- $result = drupal_http_request($service['uri'], $options);
- if (!isset($result->error)) {
- $response = _openid_parse_message($result->data);
- if (strtolower(trim($response['is_valid'])) == 'true') {
- $valid = TRUE;
- if (!empty($response['invalidate_handle'])) {
-
-
-
- db_delete('openid_association')
- ->condition('idp_endpoint_uri', $service['uri'])
- ->condition('assoc_handle', $response['invalidate_handle'])
- ->execute();
- }
- }
- else {
- $valid = FALSE;
- }
- }
- }
- return $valid;
- }
- function openid_verify_assertion_signature($service, $association, $response) {
- if ($service['version'] == 2) {
-
-
- $mandatory_keys = array('op_endpoint', 'return_to', 'response_nonce', 'assoc_handle');
- if (isset($response['openid.claimed_id'])) {
-
-
- $mandatory_keys[] = 'claimed_id';
- $mandatory_keys[] = 'identity';
- }
- }
- else {
-
- $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'];
- }
- 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;
- }
-
-
- $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;
- }
-
- db_insert('openid_nonce')
- ->fields(array(
- 'idp_endpoint_uri' => $service['uri'],
- 'nonce' => $response['openid.response_nonce'],
- 'expires' => $nonce_timestamp + $expiry,
- ))
- ->execute();
-
- $count_used = db_query("SELECT COUNT(*) FROM {openid_nonce} WHERE nonce = :nonce AND idp_endpoint_uri = :idp_endpoint_uri", array(
- ':nonce' => $response['openid.response_nonce'],
- ':idp_endpoint_uri' => $service['uri'],
- ))->fetchField();
- 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;
- }
- }
- function openid_verify_assertion_return_url($service, $response) {
- global $base_url;
- $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 FALSE;
- }
-
-
-
- parse_str(isset($return_to_parts['query']) ? $return_to_parts['query'] : '', $return_to_query_parameters);
- foreach ($return_to_query_parameters as $name => $value) {
- if (!isset($_GET[$name]) || $_GET[$name] != $value) {
- return FALSE;
- }
- }
- return TRUE;
- }
- function openid_cron() {
- db_delete('openid_nonce')
- ->condition('expires', REQUEST_TIME, '<')
- ->execute();
- }
|