View source
<?php
use Auth0\SDK\Auth0;
use Auth0\SDK\API\Authentication;
use Auth0\SDK\API\Management;
use Auth0\SDK\JWTVerifier;
define('AUTH0_WIDGET_CDN', 'https://cdn.auth0.com/js/lock/10.3/lock.min.js');
define('AUTH_LOGIN_CSS', "#a0-widget .a0-panel {\n min-width: 90%;\n padding: 5%;\n box-shadow: none;\n -webkit-box-shadow: none;\n}\n#a0-widget .a0-panel {\n background-color: #f6f6f2;\n border-color: #f9f9f9;\n}");
function auth0_menu() {
$items = array();
$items['auth0/callback'] = array(
'description' => 'Callback handler from auth0',
'page callback' => 'auth0_callback',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['auth0/verify_email'] = array(
'description' => 'Verify email action',
'page callback' => 'auth0_verify_email_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['user/%user/auth0'] = array(
'title' => 'Auth0',
'description' => 'Verify email action',
'page callback' => 'auth0_user_info_page',
'page arguments' => array(
1,
),
'access arguments' => array(
'administer users',
),
'type' => MENU_LOCAL_TASK,
'weight' => 100,
);
$items['admin/config/people/auth0'] = array(
'title' => 'Auth0 Login Settings',
'description' => 'Configure your auth0 account and widget.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'auth0_basic_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
);
$items['admin/config/people/auth0/basic'] = array(
'title' => 'Basic',
'description' => 'Configure your auth0 account and widget.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'auth0_basic_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/people/auth0/advanced'] = array(
'title' => 'Advanced',
'description' => 'Configure your auth0 account and widget.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'auth0_advanced_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
return $items;
}
function auth0_user_info_page($user) {
drupal_page_is_cacheable(FALSE);
if (!auth0_check_dependencies()) {
return drupal_goto();
}
if ($object = auth0_get_auth0_object_from_drupal_uid($user->uid)) {
if (defined('JSON_PRETTY_PRINT')) {
return '<pre>' . json_encode($object, JSON_PRETTY_PRINT) . '</pre>';
}
else {
return '<pre>' . print_r($object, TRUE) . '</pre>';
}
}
else {
return t('This user has not authenticated with Auth0');
}
}
function auth0_verify_email_page() {
drupal_page_is_cacheable(FALSE);
if (!auth0_enabled('login')) {
return drupal_goto();
}
$token = $_REQUEST['idToken'];
$domain = variable_get('auth0_domain', '');
$client_id = variable_get('auth0_client_id', '');
$client_secret = variable_get('auth0_client_secret', '');
$secret_base64_encoded = variable_get('auth0_secret_base64_encoded', FALSE);
$jwt_signature_alg = variable_get('auth0_jwt_signature_alg', "HS256");
$auth0_domain = 'https://' . $domain . '/';
$auth0_settings = array();
$auth0_settings['authorized_iss'] = [
$auth0_domain,
];
$auth0_settings['supported_algs'] = [
$jwt_signature_alg,
];
$auth0_settings['valid_audiences'] = [
$client_id,
];
$auth0_settings['client_secret'] = $client_secret;
$auth0_settings['secret_base64_encoded'] = $secret_base64_encoded;
$jwt_verifier = new JWTVerifier($auth0_settings);
try {
$user = $jwt_verifier
->verifyAndDecode($token);
} catch (\Exception $e) {
drupal_set_message(t('There was a problem re-sending the email.'), 'error');
watchdog('Auth0', "Error validating the token while resending the email: " . $e
->getMessage(), WATCHDOG_ERROR);
return drupal_goto();
}
try {
$userId = $user->sub;
$url = "https://{$domain}/api/users/{$userId}/send_verification_email";
$headers = array(
'Authorization' => "Bearer {$token}",
);
$result = drupal_http_request($url, array(
'headers' => $headers,
'method' => 'POST',
));
if ($result->code == 200) {
drupal_set_message(t('A verification message with further instructions has been sent to your e-mail address.'));
}
else {
drupal_set_message(t('Sorry, we could not send a verification e-mail. Please try again later.'), 'error');
}
} catch (Exception $e) {
drupal_set_message(t('Sorry, we could not send a verification e-mail. Please try again later.'), 'error');
}
return drupal_goto();
}
function auth0_callback() {
drupal_page_is_cacheable(FALSE);
if (!auth0_enabled('login')) {
return drupal_goto();
}
$query = drupal_get_query_parameters();
if (isset($query['error']) && $query['error'] == 'login_required') {
$authorizeUrl = _auth0_generate_authorize_url(FALSE);
unset($_GET['destination']);
drupal_static_reset('drupal_get_destination');
drupal_get_destination();
return drupal_goto($authorizeUrl, array(
'external' => TRUE,
'absolute' => TRUE,
));
}
$domain = variable_get('auth0_domain', '');
$client_id = variable_get('auth0_client_id', '');
$client_secret = variable_get('auth0_client_secret', '');
$secret_base64_encoded = variable_get('auth0_secret_base64_encoded', FALSE);
$jwt_signature_alg = variable_get('auth0_jwt_signature_alg', "HS256");
$auth0 = new Auth0(array(
'domain' => $domain,
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => url('auth0/callback', array(
'absolute' => TRUE,
)),
'store' => NULL,
'persist_id_token' => FALSE,
'persist_user' => FALSE,
'persist_access_token' => FALSE,
'persist_refresh_token' => FALSE,
));
$user_info = NULL;
try {
$user_info = $auth0
->getUser();
$id_token = $auth0
->getIdToken();
} catch (Exception $e) {
drupal_set_message(t('There was a problem logging you in, sorry for the inconvenience.'), 'error');
watchdog('Auth0', 'Error occurred while getting the Auth0 user info or ID token: @exception', array(
'@exception' => print_r($e, TRUE),
), WATCHDOG_ERROR);
return drupal_goto();
}
$query = drupal_get_query_parameters();
if (!isset($query['state']) || !drupal_valid_token($query['state'], 'auth0_state')) {
drupal_set_message(t('There was a problem logging you in, sorry for the inconvenience.'), 'error');
watchdog('Auth0', "Could not validate the state", WATCHDOG_ERROR);
return drupal_goto();
}
$auth0_domain = 'https://' . $domain . '/';
$auth0_settings = array();
$auth0_settings['authorized_iss'] = [
$auth0_domain,
];
$auth0_settings['supported_algs'] = [
$jwt_signature_alg,
];
$auth0_settings['valid_audiences'] = [
$client_id,
];
$auth0_settings['client_secret'] = $client_secret;
$auth0_settings['secret_base64_encoded'] = $secret_base64_encoded;
$jwt_verifier = new JWTVerifier($auth0_settings);
try {
$user = $jwt_verifier
->verifyAndDecode($id_token);
} catch (\Exception $e) {
drupal_set_message(t('There was a problem logging you in, sorry for the inconvenience.'), 'error');
watchdog('Auth0', "Error validating the token: " . $e
->getMessage(), WATCHDOG_ERROR);
return drupal_goto();
}
$success = FALSE;
if (isset($user_info['sub']) && !isset($user_info['user_id'])) {
$user_info['user_id'] = $user_info['sub'];
}
if ($user_info) {
$success = auth0_login_auth0_user($user_info, $id_token);
}
if (!$success) {
drupal_set_message(t('There was a problem logging you in, sorry for the inconvenience.'), 'error');
watchdog('Auth0', "user_info missing", WATCHDOG_ERROR);
}
return drupal_goto();
}
function auth0_fail_with_verify_email($idToken) {
$url = url('auth0/verify_email', array());
$formText = "<form style='display:none' name='auth0VerifyEmail' action=@url method='post'><input type='hidden' value=@token name='idToken'/></form>";
$linkText = "<a href='javascript:null' onClick='document.forms[\"auth0VerifyEmail\"].submit();'>here</a>";
$message = t($formText . "Please verify your email and log in again. Click {$linkText} to resend verification email.", array(
'@url' => $url,
'@token' => $idToken,
));
drupal_set_message($message, 'warning');
return drupal_goto();
}
function auth0_login_auth0_user($user_info, $id_token) {
$requires_email = variable_get('auth0_requires_email', TRUE);
$requires_verified_email = $requires_email && variable_get('user_email_verification', TRUE);
drupal_alter('auth0_user_pre_login', $user_info, $id_token);
if ($requires_email && empty($user_info['email'])) {
return drupal_set_message(t('This account does not have an e-mail address associated with it. Please log in with a different provider.'), 'error');
}
if ($requires_verified_email && isset($user_info['email']) && empty($user_info['email_verified'])) {
return auth0_fail_with_verify_email($id_token);
}
function_exists('dd') && dd($user_info['user_id'], 'looking up drupal user by auth0 user_id');
$uid = auth0_find_auth0_user($user_info['user_id']);
if ($uid) {
function_exists('dd') && dd($uid, 'uid of existing drupal user found');
auth0_update_auth0_object($user_info);
auth0_update_fields_and_roles($user_info, $uid);
return auth0_authenticate_user($uid);
}
else {
function_exists('dd') && dd('existing drupal user NOT found');
$isDatabaseUser = FALSE;
$hasIdentities = is_object($user_info) && $user_info
->has('identities') || is_array($user_info) && array_key_exists('identities', $user_info);
if (!$hasIdentities) {
$mgmtClient = new Management($id_token, variable_get('auth0_domain', ''));
$user = $mgmtClient->users
->get($user_info['user_id']);
$user_info['identities'] = $user['identities'];
}
foreach ($user_info['identities'] as $identity) {
if ($identity['provider'] == "auth0") {
$isDatabaseUser = TRUE;
}
}
function_exists('dd') && dd($isDatabaseUser, 'isDatabaseUser');
$joinUser = FALSE;
if (variable_get('auth0_join_user_by_mail_enabled', FALSE)) {
function_exists('dd') && dd($user_info['email'], 'join user by mail is enabled, looking up user by email');
if (!empty($user_info['email_verified']) || $isDatabaseUser) {
$joinUser = user_load_by_mail($user_info['email']);
}
}
else {
function_exists('dd') && dd($user_info['email'], 'join user by mail is not enabled, skipping lookup user by email');
}
if ($joinUser) {
function_exists('dd') && dd($joinUser->uid, 'drupal user found by email with uid');
if (empty($user_info['email_verified'])) {
return auth0_fail_with_verify_email($id_token);
}
$uid = $joinUser->uid;
}
else {
if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_ADMINISTRATORS_ONLY) {
return drupal_set_message(t('Only site administrators can create new user accounts.'), 'error');
}
else {
function_exists('dd') && dd('creating new drupal user from auth0 user');
$uid = auth0_create_user_from_auth0($user_info);
}
}
function_exists('dd') && dd($uid, 'inserting auth0 user with uid');
auth0_insert_auth0_user($user_info, $uid);
auth0_update_fields_and_roles($user_info, $uid);
return auth0_authenticate_user($uid);
}
return FALSE;
}
function auth0_update_fields_and_roles($user_info, $uid) {
function_exists('dd') && dd($user_info, 'auth0_update_fields_and_roles called');
$the_user = user_load($uid);
function_exists('dd') && dd($the_user, 'the_user before updates');
$edit = array();
auth0_update_fields($user_info, $uid, $the_user, $edit);
auth0_update_roles($user_info, $uid, $the_user, $edit);
function_exists('dd') && dd($edit, 'values to edit');
user_save($the_user, $edit);
function_exists('dd') && dd(user_load($uid), 'the_user after updates');
}
function auth0_update_fields($user_info, $uid, $the_user, &$edit) {
$auth0_claim_mapping = variable_get('auth0_claim_mapping');
function_exists('dd') && dd($auth0_claim_mapping, 'auth0_claim_mapping');
if (isset($auth0_claim_mapping) && !empty($auth0_claim_mapping)) {
$mappings = auth0_pipeListToArray($auth0_claim_mapping);
function_exists('dd') && dd($mappings, 'auth0_claim_mapping as array');
$skip_mappings = array(
'uid',
'name',
'mail',
'init',
'is_new',
'status',
'pass',
);
foreach ($mappings as $mapping) {
function_exists('dd') && dd($mapping, 'mapping');
$key = $mapping[1];
if (in_array($key, $skip_mappings)) {
function_exists('dd') && dd($mapping, 'skipping mapping handled already by auth0 module');
}
else {
$value = isset($user_info[$mapping[0]]) ? $user_info[$mapping[0]] : '';
$edit[$key] = array(
LANGUAGE_NONE => array(
0 => array(
'value' => $value,
),
),
);
}
}
}
}
function auth0_update_roles($user_info, $uid, $the_user, &$edit) {
$auth0_claim_to_use_for_role = variable_get('auth0_claim_to_use_for_role');
if (isset($auth0_claim_to_use_for_role) && !empty($auth0_claim_to_use_for_role)) {
$claim_value = isset($user_info[$auth0_claim_to_use_for_role]) ? $user_info[$auth0_claim_to_use_for_role] : '';
function_exists('dd') && dd($claim_value, 'claim_value');
$claim_values = array();
if (is_array($claim_value)) {
$claim_values = $claim_value;
}
else {
$claim_values[] = $claim_value;
}
function_exists('dd') && dd($claim_values, 'claim_values');
$auth0_role_mapping = variable_get('auth0_role_mapping');
$mappings = auth0_pipeListToArray($auth0_role_mapping);
function_exists('dd') && dd($mappings, 'auth0_role_mapping as array');
$roles_granted = array();
$roles_managed_by_mapping = array();
foreach ($mappings as $mapping) {
function_exists('dd') && dd($mapping, 'mapping');
$roles_managed_by_mapping[] = $mapping[1];
if (in_array($mapping[0], $claim_values)) {
$roles_granted[] = $mapping[1];
}
}
$roles_granted = array_unique($roles_granted);
$roles_managed_by_mapping = array_unique($roles_managed_by_mapping);
function_exists('dd') && dd($roles_granted, 'roles_granted');
function_exists('dd') && dd($roles_managed_by_mapping, 'roles_managed_by_mapping');
$not_granted = array_diff($roles_managed_by_mapping, $roles_granted);
function_exists('dd') && dd($not_granted, 'not_granted');
$user_roles = $the_user->roles;
function_exists('dd') && dd($user_roles, 'user_roles');
$new_user_roles = array_merge(array_diff($user_roles, $not_granted), $roles_granted);
function_exists('dd') && dd($new_user_roles, 'new_user_roles');
$tmp = array_diff($new_user_roles, $user_roles);
if (!empty($tmp)) {
$new_user_roles_map = array();
foreach ($new_user_roles as $new_role) {
$role = user_role_load_by_name($new_role);
$new_user_roles_map[$role->rid] = $role->name;
}
function_exists('dd') && dd($new_user_roles_map, 'changes to roles detected');
$edit['roles'] = $new_user_roles_map;
$the_user->roles = $new_user_roles_map;
}
}
}
function auth0_mappingsToPipeList($mappings) {
$result_text = "";
foreach ($mappings as $map) {
$result_text .= $map['from'] . '|' . $map['user_entered'] . "\n";
}
return $result_text;
}
function auth0_pipeListToArray($mapping_list_txt, $make_item0_lowercase = FALSE) {
$result_array = array();
$mappings = preg_split('/[\\n\\r]+/', $mapping_list_txt);
foreach ($mappings as $line) {
if (count($mapping = explode('|', trim($line))) == 2) {
$item_0 = $make_item0_lowercase ? drupal_strtolower(trim($mapping[0])) : trim($mapping[0]);
$result_array[] = array(
$item_0,
trim($mapping[1]),
);
}
}
return $result_array;
}
function auth0_authenticate_user($uid) {
$form_state['uid'] = $uid;
user_login_submit(array(), $form_state);
return TRUE;
}
function auth0_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'delete') {
return auth0_user_delete($account);
}
}
function auth0_user_delete($account) {
db_delete('auth0_user')
->condition('drupal_id', $account->uid, '=')
->execute();
}
function auth0_find_auth0_user($id) {
$rs = db_select('auth0_user', 'a')
->fields('a', array(
'drupal_id',
))
->condition('auth0_id', $id, '=')
->execute()
->fetchAssoc();
return empty($rs) ? FALSE : $rs['drupal_id'];
}
function auth0_get_auth0_object_from_drupal_uid($uid) {
$rs = db_select('auth0_user', 'a')
->fields('a')
->condition('drupal_id', $uid, '=')
->execute()
->fetch();
if (!empty($rs)) {
$rs = drupal_unpack($rs, 'auth0_object');
unset($rs->auth0_object);
return $rs;
}
return FALSE;
}
function auth0_update_auth0_object($user_info) {
db_update('auth0_user')
->fields(array(
'auth0_object' => serialize($user_info),
))
->condition('auth0_id', $user_info['user_id'], '=')
->execute();
}
function auth0_insert_auth0_user($user_info, $uid) {
$auth0_user = array(
'auth0_id' => $user_info['user_id'],
'drupal_id' => $uid,
'auth0_object' => serialize($user_info),
);
drupal_write_record('auth0_user', $auth0_user);
}
function auth0_create_user_from_auth0($user_info) {
$user = new stdClass();
if (isset($user_info['email']) && !empty($user_info['email'])) {
$email = $user_info['email'];
}
else {
$email = "";
}
$user->mail = $email;
$user->init = $email;
$username = $user_info['nickname'];
function_exists('dd') && dd($username, 'checking if drupal user already exists with auth0 nickname');
if (user_load_by_name($username)) {
$username .= time();
function_exists('dd') && dd($username, 'existing drupal user found, using new random name');
}
$user->name = $username;
$user->is_new = TRUE;
$auth0_auto_register = variable_get('auth0_auto_register', FALSE);
if ($auth0_auto_register) {
$user->status = TRUE;
}
else {
$user->status = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS;
}
$user->pass = user_password();
function_exists('dd') && dd($user, 'saving new drupal user');
$new_user = user_save($user);
if ($user) {
watchdog('Auth0', 'Account created for %name', array(
'%name' => $user->name,
), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $user->uid . '/edit'));
}
if (!$user->status) {
drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.'));
}
return $new_user->uid;
}
function auth0_theme() {
return array(
'auth0_lock' => array(
'variables' => array(
'mode' => 'signin',
),
'template' => 'auth0-lock',
),
);
}
function auth0_basic_settings_form($form, &$form_state) {
if (!auth0_check_dependencies()) {
auth0_missing_dependencies_message();
}
$form['auth0_domain'] = array(
'#type' => 'textfield',
'#title' => t('Domain'),
'#default_value' => variable_get('auth0_domain', ''),
'#description' => t('Your Auth0 domain, you can see it in the auth0 dashboard.'),
'#required' => TRUE,
);
$form['auth0_client_id'] = array(
'#type' => 'textfield',
'#title' => t('Client id'),
'#default_value' => variable_get('auth0_client_id', ''),
'#description' => t('Application id, copy from the auth0 dashboard.'),
'#required' => TRUE,
);
$form['auth0_client_secret'] = array(
'#type' => 'textfield',
'#title' => t('Client secret'),
'#default_value' => variable_get('auth0_client_secret', ''),
'#description' => t('Application secret, copy from the auth0 dashboard.'),
'#required' => TRUE,
);
$form['auth0_secret_base64_encoded'] = array(
'#type' => 'checkbox',
'#title' => t('Client Secret is Base64 Encoded'),
'#default_value' => variable_get('auth0_secret_base64_encoded', FALSE),
'#description' => t('This is stated below the client secret in your Auth0 Dashboard for the client. If your client was created after September 2016, this should be false.'),
);
$form['auth0_jwt_signature_alg'] = array(
'#type' => 'select',
'#title' => t('JWT Signature Algorithm'),
'#options' => [
'HS256' => t('HS256'),
'RS256' => t('RS256'),
],
'#default_value' => variable_get('auth0_jwt_signature_alg', 'HS256'),
'#description' => t('Your JWT Signing Algorithm for the ID token. RS256 is recommended, but must be set in the advanced settings under oauth for this client.'),
'#required' => TRUE,
);
return system_settings_form($form);
}
function auth0_advanced_settings_form($form, &$form_state) {
if (!auth0_check_dependencies()) {
auth0_missing_dependencies_message();
}
$form['auth0_replace_forms'] = array(
'#type' => 'checkbox',
'#title' => t('Replace default Drupal login, registration, and password reset forms'),
'#default_value' => variable_get('auth0_replace_forms', TRUE),
'#description' => t('Uncheck this box to disable replacement of the default Drupal login, registration, and password reset forms with the Auth0 Lock login widget. This allows maintaining the option to login with a Drupal username and password.'),
);
$form['auth0_form_title'] = array(
'#type' => 'textfield',
'#title' => t('Form title'),
'#default_value' => variable_get('auth0_form_title', 'Sign In'),
'#description' => t('This is the title for the login widget.'),
);
$form['auth0_allow_signup'] = array(
'#type' => 'checkbox',
'#title' => t('Allow user signup'),
'#default_value' => variable_get('auth0_allow_signup', TRUE),
'#description' => t('If you have database connection you can allow users to signup using the Auth0 widget.'),
);
$form['auth0_widget_cdn'] = array(
'#type' => 'textfield',
'#title' => t('Widget CDN'),
'#default_value' => variable_get('auth0_widget_cdn', AUTH0_WIDGET_CDN),
'#description' => t('Point this to the latest widget available in the CDN.'),
);
$form['auth0_requires_email'] = array(
'#type' => 'checkbox',
'#title' => t('Require an e-mail account'),
'#default_value' => variable_get('auth0_requires_email', TRUE),
'#description' => t('Require the user to have an e-mail address to login.'),
);
$form['auth0_join_user_by_mail_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Link auth0 logins to drupal users by email address'),
'#default_value' => variable_get('auth0_join_user_by_mail_enabled', FALSE),
'#description' => t('If enabled, when a user logs into Drupal for the first time, the system will use the email
address of the Auth0 user to search for a drupal user with the same email address and setup a link to that
Drupal user account.
<br/>If not enabled, then a new Drupal user will be created even if a Drupal user with the same email address already exists.
'),
);
$form['auth0_sso'] = array(
'#type' => 'checkbox',
'#title' => t('SSO enabled'),
'#default_value' => variable_get('auth0_sso', FALSE),
'#description' => t('Enable Auth0 <a href="@url">Single Sign On</a> for this site.', array(
'@url' => 'https://auth0.com/docs/sso/single-sign-on',
)),
);
$form['auth0_login_css'] = array(
'#type' => 'textarea',
'#title' => t('Login widget CSS'),
'#default_value' => variable_get('auth0_login_css', AUTH_LOGIN_CSS),
'#description' => t('This CSS controls the widget look and feel.'),
);
$form['auth0_returnTo_app'] = array(
'#type' => 'checkbox',
'#title' => t('Use returnTo URLs at the App Level'),
'#default_value' => variable_get('auth0_returnTo_app', FALSE),
'#description' => t('Check this box to use the <a href="@url">returnTo URLs</a> at the Account Level', array(
'@url' => 'https://auth0.com/docs/logout#redirecting-users-after-logout',
)),
);
$form['auth0_lock_extra_settings'] = array(
'#type' => 'textarea',
'#title' => t('Lock extra setting'),
'#default_value' => variable_get('auth0_lock_extra_settings'),
'#description' => t('This should be a valid JSON file. This entire object will be passed to the lock options parameter.'),
);
$form['auth0_auto_register'] = array(
'#type' => 'checkbox',
'#title' => t('Auto Register Auth0 users (ignore site registration settings)'),
'#default_value' => variable_get('auth0_auto_register', FALSE),
'#description' => t('Enable this option if you want new auth0 users to automatically be activated within Drupal regardless of the global site visitor registration settings (e.g. requiring admin approval).'),
);
$form['auth0_claim_mapping'] = array(
'#type' => 'textarea',
'#title' => t('Mapping of Claims to Profile Fields (one per line):'),
'#cols' => 50,
'#rows' => 5,
'#default_value' => variable_get('auth0_claim_mapping'),
'#description' => t('Enter claim mappings here in the format <claim_name>|<profile_field_name> (one per line), e.g:
<br/>
<br/>given_name|field_first_name
<br/>family_name|field_last_name
<br/>
<br/>NOTE: the following Drupal fields are handled automatically and will be ignored if specified above:
<br/> uid, name, mail, init, is_new, status, pass
'),
);
$form['auth0_claim_to_use_for_role'] = array(
'#type' => 'textfield',
'#title' => t('Claim for Role Mapping:'),
'#default_value' => variable_get('auth0_claim_to_use_for_role'),
'#description' => t('Name of the claim to use to map to Drupal roles, e.g. roles. If the claim contains a list of values, all values will be used in the mappings below.'),
);
$form['auth0_role_mapping'] = array(
'#type' => 'textarea',
'#title' => t('Mapping of Claim Role Values to Drupal Roles (one per line)'),
'#default_value' => variable_get('auth0_role_mapping'),
'#description' => t('Enter role mappings here in the format <auth0 claim value>|<drupal role name> (one per line), e.g.:
<br/>
<br/>admin|administrator
<br/>poweruser|power users
<br/>
<br/>NOTE: for any drupal role in the mapping, if a user is not mapped to the role, the role will be removed from their profile.
Drupal roles not listed above will not be changed by this module.
'),
);
return system_settings_form($form);
}
function auth0_user_logout($account) {
if (variable_get("auth0_sso", FALSE)) {
session_destroy();
$domain = check_plain(variable_get("auth0_domain", ''));
if (variable_get("auth0_returnTo_app", FALSE)) {
$client = check_plain(variable_get("auth0_client_id", ''));
drupal_goto("https://{$domain}/v2/logout?returnTo=" . urlencode(url('<front>', array(
'absolute' => TRUE,
))) . "&client_id={$client}");
}
else {
drupal_goto("https://{$domain}/v2/logout?returnTo=" . urlencode(url('<front>', array(
'absolute' => TRUE,
))));
}
}
}
function auth0_form_alter(&$form, $form_state, $form_id) {
if (!variable_get('auth0_replace_forms', TRUE)) {
return;
}
if (($form_id == 'user_login_block' || $form_id == 'user_login') && auth0_enabled('login')) {
_auth0_form_replace_with_lock($form, 'signin');
}
if ($form_id == 'user_register_form' && auth0_enabled('signup')) {
_auth0_form_replace_with_lock($form, 'signup');
}
if ($form_id == 'user_pass' && auth0_enabled('reset')) {
_auth0_form_replace_with_lock($form, 'reset');
}
if (!variable_get('auth0_allow_signup', '')) {
if ($form_id == 'user_register_form' || $form_id == 'user_pass') {
drupal_goto('user/login');
}
}
}
function auth0_form_user_profile_form_alter(&$form, $form_state) {
$user = $form_state['user'];
if ($object = auth0_get_auth0_object_from_drupal_uid($user->uid)) {
if ($user->mail) {
$form['account']['mail']['#disabled'] = TRUE;
}
else {
$form['account']['mail']['#access'] = FALSE;
}
$form['account']['pass']['#access'] = FALSE;
if (isset($form['account']['current_pass'])) {
$form['account']['current_pass']['#access'] = FALSE;
}
}
}
function _auth0_generate_authorize_url($prompt) {
$query = array(
'redirect_uri' => url('auth0/callback', array(
'absolute' => TRUE,
'query' => drupal_get_destination(),
)),
'connection' => null,
'response_type' => 'code',
);
if (is_array($prompt)) {
$query = array_merge($query, $prompt);
}
if (!isset($_SESSION['auth0_session_started'])) {
$_SESSION['auth0_session_started'] = TRUE;
drupal_session_start();
}
$state = isset($query['state']) ? $query['state'] : drupal_get_token('auth0_state');
$response_type = $query['response_type'];
$redirect_uri = $query['redirect_uri'];
$connection = $query['connection'];
unset($query['state']);
unset($query['response_type']);
unset($query['redirect_uri']);
unset($query['connection']);
$additional_params = [];
$additional_params['scope'] = 'openid profile email';
$additional_params = array_merge($additional_params, $query);
$domain = check_plain(variable_get("auth0_domain", ''));
$client_id = check_plain(variable_get("auth0_client_id", ''));
$auth0Api = new Authentication($domain, $client_id);
return $auth0Api
->get_authorize_link($response_type, $redirect_uri, $connection, $state, $additional_params);
}
function _auth0_form_replace_with_lock(&$form, $mode = 'signin') {
foreach (element_children($form) as $child) {
unset($form[$child]);
}
$form['auth0'] = array(
'#type' => 'markup',
'#markup' => theme('auth0_lock', array(
'mode' => $mode,
)),
);
}
function template_preprocess_auth0_lock(&$vars) {
$vars['sso_enabled'] = (bool) variable_get("auth0_sso", FALSE);
$vars['domain'] = check_plain(variable_get("auth0_domain", ''));
$vars['client_id'] = check_plain(variable_get("auth0_client_id", ''));
$vars['lock_extra_settings'] = json_decode(variable_get("auth0_lock_extra_settings", ''), true);
if ($vars['lock_extra_settings'] === null) {
$vars['lock_extra_settings'] = array();
}
$vars['params'] = $vars['lock_extra_settings'];
$vars['params']['container'] = isset($vars['params']['container']) ? $vars['params']['container'] : 'auth0-login-form';
$vars['params']['sso'] = isset($vars['sso_enabled']) ? $vars['sso_enabled'] : false;
$vars['params']['auth'] = isset($vars['params']['auth']) ? $vars['params']['auth'] : array();
$vars['params']['auth']['redirectUrl'] = isset($vars['params']['auth']['redirectUrl']) ? $vars['params']['auth']['redirectUrl'] : url('auth0/callback', array(
'absolute' => TRUE,
'query' => drupal_get_destination(),
));
$vars['params']['auth']['responseType'] = isset($vars['params']['auth']['responseType']) ? $vars['params']['auth']['responseType'] : 'code';
$vars['params']['auth']['params'] = isset($vars['params']['auth']['params']) ? $vars['params']['auth']['params'] : array();
$vars['params']['auth']['params']['scope'] = isset($vars['params']['auth']['params']['scope']) ? $vars['params']['auth']['params']['scope'] : 'openid email';
if (!isset($_SESSION['auth0_session_started'])) {
$_SESSION['auth0_session_started'] = TRUE;
drupal_session_start();
}
$vars['params']['auth']['params']['state'] = isset($vars['params']['auth']['params']['state']) ? $vars['params']['auth']['params']['state'] : drupal_get_token('auth0_state');
if (auth0_enabled('signup')) {
$vars['params']['allowSignUp'] = TRUE;
}
else {
$vars['params']['allowSignUp'] = FALSE;
}
if (auth0_enabled('reset')) {
$vars['params']['disableResetAction'] = TRUE;
}
else {
$vars['params']['disableResetAction'] = FALSE;
}
drupal_alter('auth0_params', $vars['params']);
if (isset($vars['params']['auth']['params']['state'])) {
$query = array(
'redirect_uri' => $vars['params']['auth']['redirectUrl'],
'state' => $vars['params']['auth']['params']['state'],
);
}
else {
$query = array(
'redirect_uri' => $vars['params']['auth']['redirectUrl'],
);
}
if (variable_get("auth0_sso", FALSE)) {
$query['prompt'] = 'none';
$vars['login_link'] = l(t('Log in'), _auth0_generate_authorize_url($query));
return;
}
else {
$vars['login_link'] = l(t('Log in'), _auth0_generate_authorize_url($query));
}
if ($css = variable_get("auth0_login_css", AUTH_LOGIN_CSS)) {
drupal_add_css($css, array(
'type' => 'inline',
));
}
drupal_add_js(filter_var(variable_get('auth0_widget_cdn', AUTH0_WIDGET_CDN), FILTER_VALIDATE_URL), 'external');
drupal_add_js(array(
'auth0' => array(
'client_id' => $vars['client_id'],
'domain' => $vars['domain'],
'options' => $vars['params'],
),
), 'setting');
drupal_add_js(drupal_get_path('module', 'auth0') . '/auth0.lock.js');
}
function auth0_enabled($operation = '') {
if (!auth0_check_dependencies()) {
return FALSE;
}
$out = FALSE;
if (variable_get("auth0_domain", '') && variable_get("auth0_client_id", '') && variable_get('auth0_client_secret', '')) {
$out = TRUE;
if ($operation == 'signup' || $operation == 'reset') {
$out = (bool) variable_get('auth0_allow_signup', '');
}
}
drupal_alter('auth0_enabled', $out, $operation);
return $out;
}
function auth0_check_dependencies() {
if (class_exists('\\Auth0SDK\\Auth0')) {
return TRUE;
}
if (file_exists(DRUPAL_ROOT . '/' . drupal_get_path('module', 'auth0') . '/vendor/autoload.php')) {
require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'auth0') . '/vendor/autoload.php';
return TRUE;
}
return FALSE;
}
function auth0_missing_dependencies_message() {
drupal_set_message(t('Auth0 is not fully installed. See the module\'s INSTALL.txt file for installation instructions.', array(
'!url' => 'https://www.drupal.org/project/composer_manager',
)), 'warning');
}
function auth0_block_info() {
$blocks['auth0_lock'] = array(
'info' => t('Auth0 Lock widget'),
'cache' => DRUPAL_CACHE_GLOBAL,
);
return $blocks;
}
function auth0_block_view($delta = '') {
global $user;
$block = array();
switch ($delta) {
case 'auth0_lock':
if (!$user->uid) {
$block['subject'] = '';
$block['content'] = array(
'#type' => 'markup',
'#markup' => theme('auth0_lock', array(
'mode' => 'signin',
)),
);
}
break;
}
return $block;
}