diff --git a/auth.php b/auth.php index 510d3cf..a328835 100644 --- a/auth.php +++ b/auth.php @@ -33,6 +33,7 @@ use core\output\notification; * Email OTP authentication plugin. * * @see self::user_login() + * @see self::get_user_field() * @package auth_emailotp * @copyright 2020 Pawel Suwinski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -63,9 +64,9 @@ class auth_plugin_emailotp extends auth_plugin_base { } /** - * Matches only valid and allowed email as username. Validates credentials - * and password if exists in current session or generates ones for session - * time on empty password treated as one-time password request. + * Matches only valid email from allowed domains. Validates credentials and + * password if exists in current session or generates ones for session time + * on empty password treated as one-time password request. * * @param string $username The username * @param string $password The password @@ -73,7 +74,10 @@ class auth_plugin_emailotp extends auth_plugin_base { */ public function user_login($username, $password) { global $CFG, $DB; - if (!validate_email($username) || email_is_not_allowed($username)) { + $email = validate_email($username) + ? $username // Email as username or signup on first login. + : $this->get_user_field($username, 'email'); // Standard login, existing user. + if (empty($email) || email_is_not_allowed($email)) { return false; } // OTP already generated and base credentials matches. @@ -87,17 +91,12 @@ class auth_plugin_emailotp extends auth_plugin_base { } // OTP request - do not proceed on preventaccountcreation when user not exits. if (!isset($_SESSION[self::COMPONENT_NAME]) && empty($password) && ( - empty($CFG->authpreventaccountcreation) || $DB->get_field('user', 'id', [ - 'username' => $username, - 'mnethostid' => $CFG->mnet_localhost_id, - 'auth' => $this->authtype, - 'deleted' => 0, - ]))) { - if (!$this->min_request_period_fulfilled($username)) { + empty($CFG->authpreventaccountcreation) || $this->get_user_field($username, 'id'))) { + if (!$this->min_request_period_fulfilled($email)) { $this->redirect($username, 'otpperiod', notification::NOTIFY_WARNING); - } else if ($this->gen_otp($username)) { + } else if ($this->gen_otp($username, $email)) { \auth_emailotp\event\otp_generated::create(array( - 'other' => array('email' => $username), + 'other' => array('email' => $email), ))->trigger(); $this->redirect($username, 'otpsent', notification::NOTIFY_SUCCESS); } else { @@ -114,37 +113,25 @@ class auth_plugin_emailotp extends auth_plugin_base { notification::NOTIFY_WARNING ); \auth_emailotp\event\otp_revoked::create(array( - 'other' => array('email' => $username), + 'other' => array('email' => $email), ))->trigger(); } } return false; } - /** - * {@inheritdoc} - */ public function is_synchronised_with_external() { return false; } - /** - * {@inheritdoc} - */ public function is_internal() { return false; } - /** - * {@inheritdoc} - */ public function can_be_manually_set() { return true; } - /** - * {@inheritdoc} - */ public function user_authenticated_hook(&$user, $username, $password) { // Destroy credentials - is already used. if (isset($_SESSION[self::COMPONENT_NAME])) { @@ -153,7 +140,13 @@ class auth_plugin_emailotp extends auth_plugin_base { } /** - * {@inheritdoc} + * get_userinfo + * + * Signup and user creation on first login takes place only in case of + * using email address as username. + * + * @param string $username + * @return array */ public function get_userinfo($username) { $this->get_custom_user_profile_fields(); @@ -195,9 +188,10 @@ class auth_plugin_emailotp extends auth_plugin_base { * gen_otp * * @param string $username + * @param string $email * @return bool */ - protected function gen_otp(string $username) { + protected function gen_otp(string $username, string $email) { global $CFG; $newpassword = generate_password(); $_SESSION[self::COMPONENT_NAME] = array( @@ -209,7 +203,7 @@ class auth_plugin_emailotp extends auth_plugin_base { 'id' => -1, // Fake due email_to_user() requirements. 'auth' => $this->authtype, 'username' => $username, - 'email' => $username, + 'email' => $email, 'password' => $newpassword, ); return email_to_user($user, core_user::get_support_user(), @@ -257,4 +251,22 @@ class auth_plugin_emailotp extends auth_plugin_base { ) ) === 0; } + + /** + * get_user_field + * + * @see moodle_database::get_field() + * @param string $username + * @param string $field + * @return mixed + */ + protected function get_user_field(string $username, string $field) { + global $CFG, $DB; + return $DB->get_field('user', $field, array( + 'username' => $username, + 'mnethostid' => $CFG->mnet_localhost_id, + 'auth' => $this->authtype, + 'deleted' => 0, + )); + } } diff --git a/lang/en/auth_emailotp.php b/lang/en/auth_emailotp.php index 83b306a..e12907f 100644 --- a/lang/en/auth_emailotp.php +++ b/lang/en/auth_emailotp.php @@ -37,7 +37,7 @@ $string['revokethreshold_help'] = 'Login failures limit causing revoke of the ge $string['minrequestperiod'] = 'Minium period'; $string['minrequestperiod_help'] = 'A time in seconds after which another password can be generated (0 - unrestricted). Enabled logstore required.'; $string['logstorerequired'] = 'Notice: no working logstore! Enable logstore or set time to 0.'; -$string['fieldsmapping'] = 'User profile fields mapping'; +$string['fieldsmapping'] = 'User profile fields mapping on signup'; $string['fieldsmapping_pattern'] = 'Pattern'; $string['fieldsmapping_pattern_help'] = 'Capturing groups PCRE pattern.'; $string['fieldsmapping_mapping'] = 'Mapping'; diff --git a/lang/pl/auth_emailotp.php b/lang/pl/auth_emailotp.php index f52e8b4..fef1334 100644 --- a/lang/pl/auth_emailotp.php +++ b/lang/pl/auth_emailotp.php @@ -37,7 +37,7 @@ $string['revokethreshold_help'] = 'Limit nieudanych logowań unieważniających $string['minrequestperiod'] = 'Minimalny odstęp'; $string['minrequestperiod_help'] = 'Czas w sekundach, po którym kolejne hasło może być wygenerowane (0 - nieograniczony). Wymaga działającego loggera.'; $string['logstorerequired'] = 'Uwaga: logger nieaktywny! Aktywuj logger albo ustaw czas na 0.'; -$string['fieldsmapping'] = 'Mapowanie pól profilu użytkownika'; +$string['fieldsmapping'] = 'Mapowanie pól profilu użytkownika podczas rejestracji'; $string['fieldsmapping_pattern'] = 'Wzorzec'; $string['fieldsmapping_pattern_help'] = 'Grupujące wyrażenie regularne PCRE.'; $string['fieldsmapping_mapping'] = 'Mapowanie';