diff --git a/auth.php b/auth.php index fa91355..ace54ec 100644 --- a/auth.php +++ b/auth.php @@ -93,10 +93,14 @@ class auth_plugin_emailotp extends auth_plugin_base { 'auth' => $this->authtype, 'deleted' => 0, ]))) { - $this->redirect($username, $this->gen_otp($username) - ? notification::NOTIFY_SUCCESS - : notification::NOTIFY_ERROR - ); + if ($this->gen_otp($username)) { + \auth_emailotp\event\otp_generated::create(array( + 'other' => array('email' => $username), + ))->trigger(); + $this->redirect($username, notification::NOTIFY_SUCCESS); + } else { + $this->redirect($username, notification::NOTIFY_ERROR); + } } // OTP exits but validation failed - reset if revoke threshold is set. if (isset($_SESSION[self::COMPONENT_NAME])) { @@ -105,9 +109,12 @@ class auth_plugin_emailotp extends auth_plugin_base { $_SESSION[self::COMPONENT_NAME]['login_failed_count'] >= $this->config->revokethreshold) { unset($_SESSION[self::COMPONENT_NAME]); \core\notification::add( - (string)new lang_string('otpinvalidated', self::COMPONENT_NAME, null, $CFG->lang), + (string)new lang_string('otprevoked', self::COMPONENT_NAME, null, $CFG->lang), notification::NOTIFY_WARNING ); + \auth_emailotp\event\otp_revoked::create(array( + 'other' => array('email' => $username), + ))->trigger(); } } return false; diff --git a/classes/event/otp_generated.php b/classes/event/otp_generated.php new file mode 100644 index 0000000..6ac2d08 --- /dev/null +++ b/classes/event/otp_generated.php @@ -0,0 +1,88 @@ +. + +/** + * Event when one-time password is generated. + * + * @package auth_emailotp + * @copyright 2020 Pawel Suwinski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace auth_emailotp\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Event when one-time password is generated. + * + * @package auth_emailotp + * @copyright 2020 Pawel Suwinski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class otp_generated extends \core\event\base { + + protected static $crud = 'c'; + + /** + * {@inheritdoc} + */ + protected function init() { + $this->data['crud'] = static::$crud; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->context = \context_system::instance(); + } + + /** + * {@inheritdoc} + */ + public static function get_name() { + return get_string('eventotp'.substr(static::class, strrpos(static::class, '_') + 1), + 'auth_emailotp'); + } + + /** + * {@inheritdoc} + */ + public function get_description() { + return sprintf('Password %s for \'%s\'', $this->action, + $this->other['email']); + } + + /** + * {@inheritdoc} + */ + protected function get_legacy_logdata() { + return array(SITEID, 'auth_emailotp', $this->action, '', + $this->other['email']); + } + + /** + * Custom validation. + * + * @throws \coding_exception + */ + protected function validate_data() { + parent::validate_data(); + if (!isset($this->other['email'])) { + throw new \coding_exception('The \'email\' value must be set in other.'); + } + } + + public static function get_other_mapping() { + return false; + } +} diff --git a/classes/event/otp_revoked.php b/classes/event/otp_revoked.php new file mode 100644 index 0000000..14001a6 --- /dev/null +++ b/classes/event/otp_revoked.php @@ -0,0 +1,38 @@ +. + +/** + * Event when one-time password is revoked. + * + * @package auth_emailotp + * @copyright 2020 Pawel Suwinski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace auth_emailotp\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Event when one-time password is revoked. + * + * @package auth_emailotp + * @copyright 2020 Pawel Suwinski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class otp_revoked extends otp_generated { + protected static $crud = 'd'; +} diff --git a/lang/en/auth_emailotp.php b/lang/en/auth_emailotp.php index f4476ab..6b87c64 100644 --- a/lang/en/auth_emailotp.php +++ b/lang/en/auth_emailotp.php @@ -23,20 +23,22 @@ */ $string['pluginname'] = 'Email OTP'; +$string['eventotpgenerated'] = 'Password generated'; +$string['eventotprevoked'] = 'Password revoked'; $string['otpgeneratedsubj'] = 'One-time password'; $string['otpgeneratedtext'] = 'One-time password for current session: {$a->password}'; $string['otpsentsuccess'] = 'One-time password was sent to given email.'; $string['otpsenterror'] = 'An error occurred while sending one-time password.'; $string['otpsentinfo'] = 'One-time password for current session was already generated and sent to email.'; -$string['otpinvalidated'] = 'Previously generated password has been revoked due to exceeding the login failure threshold.'; -$string['optperioderror'] = 'Minim period after which another password can be generated not preserved. Try again later.'; +$string['otprevoked'] = 'Previously generated password has been revoked due to exceeding the login failure threshold.'; +$string['otpperioderror'] = 'Minim period after which another password can be generated not preserved. Try again later.'; $string['revokethreshold'] = 'Revoke threshold'; $string['revokethreshold_help'] = 'Login failures limit causing revoke of the generated password (0 - unlimited).'; $string['minrequestperiod'] = 'Minium period'; $string['minrequestperiod_help'] = 'A time in seconds after which another password can be generated.'; $string['fieldsmapping'] = 'User profile fields mapping'; $string['fieldsmapping_pattern'] = 'Pattern'; -$string['fieldsmapping_pattern_help'] = 'Capturing groups PCRE patttern.'; +$string['fieldsmapping_pattern_help'] = 'Capturing groups PCRE pattern.'; $string['fieldsmapping_mapping'] = 'Mapping'; $string['fieldsmapping_mapping_help'] = 'Mapping expressions.'; $string['fieldsmapping_help'] = <<<'EOT' diff --git a/lang/pl/auth_emailotp.php b/lang/pl/auth_emailotp.php index f1a8863..f1e55e5 100644 --- a/lang/pl/auth_emailotp.php +++ b/lang/pl/auth_emailotp.php @@ -23,13 +23,15 @@ */ $string['pluginname'] = 'Email OTP'; +$string['eventotpgenerated'] = 'Hasło wynegerowano'; +$string['eventotprevoked'] = 'Hasło unieważniono'; $string['otpgeneratedsubj'] = 'Hasło jednorazowe'; $string['otpgeneratedtext'] = 'Hasło jednorazowe dla bieżącej sesji: {$a->password}'; $string['otpsentsuccess'] = 'Hasło jednorazowe zostało wysłane na podany adres email.'; $string['otpsenterror'] = 'Wystąpił błąd podczas wysyłania hasła jednorazowego.'; $string['otpsentinfo'] = 'Hasło jednorazowe dla bieżącej sesji już zostało wygenerowane i wysłane.'; $string['otpinvalidated'] = 'Poprzednio wygenerowane hasło zostało unieważnione z powodu przekroczenia limitu niepoprawnych logowań.'; -$string['optperioderror'] = 'Nie zachowany minimalny odstęp, po którym kolejne hasło może być wygenerowane. Spróbuj ponownie później.'; +$string['otpperioderror'] = 'Nie zachowany minimalny odstęp, po którym kolejne hasło może być wygenerowane. Spróbuj ponownie później.'; $string['revokethreshold'] = 'Próg nieważnienia'; $string['revokethreshold_help'] = 'Limit nieudanych logowań unieważniających wygenerowane hasło (0 - bez limitu).'; $string['minrequestperiod'] = 'Minimalny odstęp';