From 64cffe69525a3173b781ed646d85202e7f1b1763 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 11 May 2022 17:03:39 +0900 Subject: [PATCH 1/6] fix: add user null check --- src/Authentication/Actions/Email2FA.php | 5 +++++ src/Authentication/Actions/EmailActivator.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/Authentication/Actions/Email2FA.php b/src/Authentication/Actions/Email2FA.php index 0cd66e98e..4cb2078c5 100644 --- a/src/Authentication/Actions/Email2FA.php +++ b/src/Authentication/Actions/Email2FA.php @@ -5,6 +5,7 @@ use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\Shield\Models\UserIdentityModel; +use RuntimeException; /** * Class Email2FA @@ -58,6 +59,10 @@ public function handle(IncomingRequest $request) return redirect()->route('auth-action-show')->with('error', lang('Auth.invalidEmail')); } + if ($user === null) { + throw new RuntimeException('Cannot get the User.'); + } + /** @var UserIdentityModel $identityModel */ $identityModel = model(UserIdentityModel::class); diff --git a/src/Authentication/Actions/EmailActivator.php b/src/Authentication/Actions/EmailActivator.php index ad1f6625d..6520df2cc 100644 --- a/src/Authentication/Actions/EmailActivator.php +++ b/src/Authentication/Actions/EmailActivator.php @@ -6,6 +6,7 @@ use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\Shield\Models\UserIdentityModel; +use RuntimeException; class EmailActivator implements ActionInterface { @@ -18,6 +19,10 @@ public function show(): string { $user = auth()->user(); + if ($user === null) { + throw new RuntimeException('Cannot get the User.'); + } + /** @var UserIdentityModel $identityModel */ $identityModel = model(UserIdentityModel::class); From 4bdaa35edf1f075984367a7330b328ed715cbb23 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 11 May 2022 17:04:31 +0900 Subject: [PATCH 2/6] fix: add check to see if the email sent --- src/Authentication/Actions/Email2FA.php | 8 ++++++-- src/Authentication/Actions/EmailActivator.php | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Authentication/Actions/Email2FA.php b/src/Authentication/Actions/Email2FA.php index 4cb2078c5..cd2210e57 100644 --- a/src/Authentication/Actions/Email2FA.php +++ b/src/Authentication/Actions/Email2FA.php @@ -74,13 +74,17 @@ public function handle(IncomingRequest $request) // Send the user an email with the code helper('email'); - $email = emailer(); - $email->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') + $emailer = emailer(); + $ret = $emailer->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') ->setTo($user->getAuthEmail()) ->setSubject(lang('Auth.email2FASubject')) ->setMessage(view(setting('Auth.views')['action_email_2fa_email'], ['code' => $identity->secret])) ->send(); + if ($ret === false) { + throw new RuntimeException('Cannot send email for user: ' . $user->getAuthEmail()); + } + return view(setting('Auth.views')['action_email_2fa_verify']); } diff --git a/src/Authentication/Actions/EmailActivator.php b/src/Authentication/Actions/EmailActivator.php index 6520df2cc..f9943c8f4 100644 --- a/src/Authentication/Actions/EmailActivator.php +++ b/src/Authentication/Actions/EmailActivator.php @@ -43,13 +43,17 @@ public function show(): string // Send the email helper('email'); - $email = emailer(); - $email->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') + $emailer = emailer(); + $ret = $emailer->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') ->setTo($user->getAuthEmail()) ->setSubject(lang('Auth.emailActivateSubject')) ->setMessage(view(setting('Auth.views')['action_email_activate_email'], ['code' => $code])) ->send(); + if ($ret === false) { + throw new RuntimeException('Cannot send email for user: ' . $user->getAuthEmail()); + } + // Display the info page return view(setting('Auth.views')['action_email_activate_show'], ['user' => $user]); } From 5537b19d39ee8bf570271b2391b24a5fabd07094 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 11 May 2022 17:15:13 +0900 Subject: [PATCH 3/6] refactor: add library specific Exceptions and use them --- src/Authentication/Actions/Email2FA.php | 2 +- src/Authentication/Actions/EmailActivator.php | 2 +- src/Exceptions/LogicException.php | 7 +++++++ src/Exceptions/RuntimeException.php | 7 +++++++ 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 src/Exceptions/LogicException.php create mode 100644 src/Exceptions/RuntimeException.php diff --git a/src/Authentication/Actions/Email2FA.php b/src/Authentication/Actions/Email2FA.php index cd2210e57..f0fe448b5 100644 --- a/src/Authentication/Actions/Email2FA.php +++ b/src/Authentication/Actions/Email2FA.php @@ -4,8 +4,8 @@ use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RedirectResponse; +use CodeIgniter\Shield\Exceptions\RuntimeException; use CodeIgniter\Shield\Models\UserIdentityModel; -use RuntimeException; /** * Class Email2FA diff --git a/src/Authentication/Actions/EmailActivator.php b/src/Authentication/Actions/EmailActivator.php index f9943c8f4..ccd2e5c4b 100644 --- a/src/Authentication/Actions/EmailActivator.php +++ b/src/Authentication/Actions/EmailActivator.php @@ -5,8 +5,8 @@ use CodeIgniter\Exceptions\PageNotFoundException; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RedirectResponse; +use CodeIgniter\Shield\Exceptions\RuntimeException; use CodeIgniter\Shield\Models\UserIdentityModel; -use RuntimeException; class EmailActivator implements ActionInterface { diff --git a/src/Exceptions/LogicException.php b/src/Exceptions/LogicException.php new file mode 100644 index 000000000..c59b59096 --- /dev/null +++ b/src/Exceptions/LogicException.php @@ -0,0 +1,7 @@ + Date: Wed, 11 May 2022 21:24:11 +0900 Subject: [PATCH 4/6] refactor: remove tmp var and rename var name --- src/Authentication/Actions/Email2FA.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Authentication/Actions/Email2FA.php b/src/Authentication/Actions/Email2FA.php index f0fe448b5..5ce92fb8f 100644 --- a/src/Authentication/Actions/Email2FA.php +++ b/src/Authentication/Actions/Email2FA.php @@ -74,14 +74,13 @@ public function handle(IncomingRequest $request) // Send the user an email with the code helper('email'); - $emailer = emailer(); - $ret = $emailer->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') + $return = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') ->setTo($user->getAuthEmail()) ->setSubject(lang('Auth.email2FASubject')) ->setMessage(view(setting('Auth.views')['action_email_2fa_email'], ['code' => $identity->secret])) ->send(); - if ($ret === false) { + if ($return === false) { throw new RuntimeException('Cannot send email for user: ' . $user->getAuthEmail()); } From 44c2570434eba932b07729198c9e1760a28a2cc4 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 13 May 2022 19:39:58 +0900 Subject: [PATCH 5/6] refactor: remove temp var and rename var name Co-authored-by: MGatner --- src/Authentication/Actions/EmailActivator.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Authentication/Actions/EmailActivator.php b/src/Authentication/Actions/EmailActivator.php index ccd2e5c4b..c1c864372 100644 --- a/src/Authentication/Actions/EmailActivator.php +++ b/src/Authentication/Actions/EmailActivator.php @@ -43,8 +43,7 @@ public function show(): string // Send the email helper('email'); - $emailer = emailer(); - $ret = $emailer->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') + $return = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') ->setTo($user->getAuthEmail()) ->setSubject(lang('Auth.emailActivateSubject')) ->setMessage(view(setting('Auth.views')['action_email_activate_email'], ['code' => $code])) From 15500eed8be94fd8baab9e5c55481b3ed2d42804 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 13 May 2022 19:40:12 +0900 Subject: [PATCH 6/6] refactor: rename var name Co-authored-by: MGatner --- src/Authentication/Actions/EmailActivator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Authentication/Actions/EmailActivator.php b/src/Authentication/Actions/EmailActivator.php index c1c864372..3f9cd0a0e 100644 --- a/src/Authentication/Actions/EmailActivator.php +++ b/src/Authentication/Actions/EmailActivator.php @@ -49,7 +49,7 @@ public function show(): string ->setMessage(view(setting('Auth.views')['action_email_activate_email'], ['code' => $code])) ->send(); - if ($ret === false) { + if ($return === false) { throw new RuntimeException('Cannot send email for user: ' . $user->getAuthEmail()); }