Skip to content

Commit dd18be0

Browse files
authored
Merge pull request #431 from jozefrebjak/develop
feat: add request info to all emails
2 parents 7e90fda + 2dd99db commit dd18be0

File tree

15 files changed

+170
-12
lines changed

15 files changed

+170
-12
lines changed

src/Authentication/Actions/Email2FA.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use CodeIgniter\HTTP\IncomingRequest;
88
use CodeIgniter\HTTP\RedirectResponse;
9+
use CodeIgniter\I18n\Time;
910
use CodeIgniter\Shield\Authentication\Authenticators\Session;
1011
use CodeIgniter\Shield\Entities\User;
1112
use CodeIgniter\Shield\Entities\UserIdentity;
@@ -72,11 +73,15 @@ public function handle(IncomingRequest $request)
7273
return redirect()->route('auth-action-show')->with('error', lang('Auth.need2FA'));
7374
}
7475

76+
$ipAddress = $request->getIPAddress();
77+
$userAgent = (string) $request->getUserAgent();
78+
$date = Time::now()->toDateTimeString();
79+
7580
// Send the user an email with the code
7681
$email = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
7782
$email->setTo($user->email);
7883
$email->setSubject(lang('Auth.email2FASubject'));
79-
$email->setMessage(view(setting('Auth.views')['action_email_2fa_email'], ['code' => $identity->secret]));
84+
$email->setMessage(view(setting('Auth.views')['action_email_2fa_email'], ['code' => $identity->secret, 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'date' => $date]));
8085

8186
if ($email->send(false) === false) {
8287
throw new RuntimeException('Cannot send email for user: ' . $user->email . "\n" . $email->printDebugger(['headers']));

src/Authentication/Actions/EmailActivator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CodeIgniter\HTTP\IncomingRequest;
99
use CodeIgniter\HTTP\RedirectResponse;
1010
use CodeIgniter\HTTP\Response;
11+
use CodeIgniter\I18n\Time;
1112
use CodeIgniter\Shield\Authentication\Authenticators\Session;
1213
use CodeIgniter\Shield\Entities\User;
1314
use CodeIgniter\Shield\Entities\UserIdentity;
@@ -43,11 +44,18 @@ public function show(): string
4344

4445
$code = $this->createIdentity($user);
4546

47+
/** @var IncomingRequest $request */
48+
$request = service('request');
49+
50+
$ipAddress = $request->getIPAddress();
51+
$userAgent = (string) $request->getUserAgent();
52+
$date = Time::now()->toDateTimeString();
53+
4654
// Send the email
4755
$email = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
4856
$email->setTo($userEmail);
4957
$email->setSubject(lang('Auth.emailActivateSubject'));
50-
$email->setMessage(view(setting('Auth.views')['action_email_activate_email'], ['code' => $code]));
58+
$email->setMessage(view(setting('Auth.views')['action_email_activate_email'], ['code' => $code, 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'date' => $date]));
5159

5260
if ($email->send(false) === false) {
5361
throw new RuntimeException('Cannot send email for user: ' . $user->email . "\n" . $email->printDebugger(['headers']));

src/Controllers/MagicLinkController.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Controllers\BaseController;
88
use CodeIgniter\Events\Events;
9+
use CodeIgniter\HTTP\IncomingRequest;
910
use CodeIgniter\HTTP\RedirectResponse;
1011
use CodeIgniter\I18n\Time;
1112
use CodeIgniter\Shield\Authentication\Authenticators\Session;
@@ -90,11 +91,18 @@ public function loginAction()
9091
'expires' => Time::now()->addSeconds(setting('Auth.magicLinkLifetime'))->format('Y-m-d H:i:s'),
9192
]);
9293

94+
/** @var IncomingRequest $request */
95+
$request = service('request');
96+
97+
$ipAddress = $request->getIPAddress();
98+
$userAgent = (string) $request->getUserAgent();
99+
$date = Time::now()->toDateTimeString();
100+
93101
// Send the user an email with the code
94102
$email = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
95103
$email->setTo($user->email);
96104
$email->setSubject(lang('Auth.magicLinkSubject'));
97-
$email->setMessage(view(setting('Auth.views')['magic-link-email'], ['token' => $token]));
105+
$email->setMessage(view(setting('Auth.views')['magic-link-email'], ['token' => $token, 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'date' => $date]));
98106

99107
if ($email->send(false) === false) {
100108
log_message('error', $email->printDebugger(['headers']));

src/Language/de/Auth.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
'userDoesNotExist' => 'Passwort wurde nicht geändert. Der Benutzer existiert nicht',
6565
'resetTokenExpired' => 'Tut mir leid. Ihr Reset-Token ist abgelaufen.',
6666

67+
// Email Globals
68+
'emailInfo' => 'Einige Informationen über die Person:',
69+
'emailIpAddress' => 'IP Adresse:',
70+
'emailDevice' => 'Gerät:',
71+
'emailDate' => 'Datum:',
72+
6773
// 2FA
6874
'email2FATitle' => 'Zwei-Faktor-Authentifizierung',
6975
'confirmEmailAddress' => 'Bestätigen Sie Ihre E-Mail-Adresse.',

src/Language/en/Auth.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
'userDoesNotExist' => 'Password was not changed. User does not exist',
6565
'resetTokenExpired' => 'Sorry. Your reset token has expired.',
6666

67+
// Email Globals
68+
'emailInfo' => 'Some information about the person:',
69+
'emailIpAddress' => 'IP Address:',
70+
'emailDevice' => 'Device:',
71+
'emailDate' => 'Date:',
72+
6773
// 2FA
6874
'email2FATitle' => 'Two Factor Authentication',
6975
'confirmEmailAddress' => 'Confirm your email address.',

src/Language/es/Auth.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
'userDoesNotExist' => 'No se ha cambiado la contraseña. No existe el usuario',
6565
'resetTokenExpired' => 'Lo sentimos. Tu token de reseteo ha caducado.',
6666

67+
// Email Globals
68+
'emailInfo' => 'Algunos datos sobre la persona:',
69+
'emailIpAddress' => 'Dirección IP:',
70+
'emailDevice' => 'Dispositivo:',
71+
'emailDate' => 'Fecha:',
72+
6773
// 2FA
6874
'email2FATitle' => 'Authenticación de Doble Factor',
6975
'confirmEmailAddress' => 'Confirma tu dirección de email.',

src/Language/fa/Auth.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
'userDoesNotExist' => 'رمز عبور تغییر نکرد. کاربر وجود ندارد.',
7474
'resetTokenExpired' => 'متاسفانه، توکن بازنشانی شما منقضی شده است.',
7575

76+
// Email Globals
77+
'emailInfo' => 'برخی از اطلاعات درخواست کننده:',
78+
'emailIpAddress' => 'آدرس ای پی:',
79+
'emailDevice' => 'دستگاه:',
80+
'emailDate' => 'زمان:',
81+
7682
// 2FA
7783
'email2FATitle' => 'احراز هویت دو عاملی',
7884
'confirmEmailAddress' => 'آدرس ایمیل خود را تایید کنید.',

src/Language/fr/Auth.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
'userDoesNotExist' => 'Le mot de passe n\'a pas été modifié. L\'utilisateur n\'existe pas',
6565
'resetTokenExpired' => 'Désolé. Votre jeton de réinitialisation a expiré.',
6666

67+
// Email Globals
68+
'emailInfo' => 'Quelques informations sur la personne:',
69+
'emailIpAddress' => 'Adresse IP:',
70+
'emailDevice' => 'Dispositif:',
71+
'emailDate' => 'Jour:',
72+
6773
// 2FA
6874
'email2FATitle' => 'Authentification à deux facteurs',
6975
'confirmEmailAddress' => 'Confirmer votre adresse email.',

src/Language/id/Auth.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
'userDoesNotExist' => 'Kata sandi tidak diubah. User tidak ditemukan',
6565
'resetTokenExpired' => 'Maaf, token setel ulang Anda sudah habis waktu.',
6666

67+
// Email Globals
68+
'emailInfo' => 'Beberapa informasi tentang seseorang:',
69+
'emailIpAddress' => 'Alamat IP:',
70+
'emailDevice' => 'Perangkat:',
71+
'emailDate' => 'Tanggal:',
72+
6773
// 2FA
6874
'email2FATitle' => 'Otentikasi Dua Faktor',
6975
'confirmEmailAddress' => 'Alamat email konfirmasi Anda.',

src/Language/ja/Auth.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
'userDoesNotExist' => 'パスワードは変更されていません。ユーザーは存在しません', // 'Password was not changed. User does not exist',
6565
'resetTokenExpired' => '申し訳ありません。リセットトークンの有効期限が切れました。', // 'Sorry. Your reset token has expired.',
6666

67+
// Email Globals
68+
'emailInfo' => '本人に関する情報:',
69+
'emailIpAddress' => 'IPアドレス:',
70+
'emailDevice' => 'デバイス:',
71+
'emailDate' => '日時:',
72+
6773
// 2FA
6874
'email2FATitle' => '二要素認証', // 'Two Factor Authentication',
6975
'confirmEmailAddress' => 'メールアドレスを確認してください。', // 'Confirm your email address.',

src/Language/sk/Auth.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
'userDoesNotExist' => 'Heslo nebolo zmenené. Používateľ neexistuje',
6565
'resetTokenExpired' => 'Prepáčte. Platnosť vášho resetovacieho tokenu vypršala.',
6666

67+
// Email Globals
68+
'emailInfo' => 'Niektoré informácie o osobe:',
69+
'emailIpAddress' => 'IP Adresa:',
70+
'emailDevice' => 'Zariadenie:',
71+
'emailDate' => 'Dátum:',
72+
6773
// 2FA
6874
'email2FATitle' => 'Dvojfaktorová autentifikácia',
6975
'confirmEmailAddress' => 'Potvrďte svoju e-mailovú adresu.',

src/Views/Email/email_2fa_email.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
1-
<p><?= lang('Auth.email2FAMailBody') ?> <b><?= $code ?></b></p>
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
3+
<head>
4+
<meta name="x-apple-disable-message-reformatting">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<meta name="format-detection" content="telephone=no, date=no, address=no, email=no">
7+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8+
<title><?= lang('Auth.email2FASubject') ?></title>
9+
</head>
10+
11+
<body>
12+
<p><?= lang('Auth.email2FAMailBody') ?></p>
13+
<div style="text-align: center">
14+
<h1><?= $code ?></h1>
15+
</div>
16+
<table role="presentation" border="0" cellpadding="0" cellspacing="0" style="width: 100%;" width="100%">
17+
<tbody>
18+
<tr>
19+
<td style="line-height: 20px; font-size: 20px; width: 100%; height: 20px; margin: 0;" align="left" width="100%" height="20">
20+
&#160;
21+
</td>
22+
</tr>
23+
</tbody>
24+
</table>
25+
<b><?= lang('Auth.emailInfo') ?></b>
26+
<p><?= lang('Auth.emailIpAddress') ?> <?= esc($ipAddress) ?></p>
27+
<p><?= lang('Auth.emailDevice') ?> <?= esc($userAgent) ?></p>
28+
<p><?= lang('Auth.emailDate') ?> <?= esc($date) ?></p>
29+
</body>
30+
31+
</html>
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1-
<p><?= lang('Auth.emailActivateMailBody') ?></p>
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
22

3-
<p><?= $code ?></p>
3+
<head>
4+
<meta name="x-apple-disable-message-reformatting">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<meta name="format-detection" content="telephone=no, date=no, address=no, email=no">
7+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8+
<title><?= lang('Auth.emailActivateSubject') ?></title>
9+
</head>
10+
11+
<body>
12+
<p><?= lang('Auth.emailActivateMailBody') ?></p>
13+
<div style="text-align: center">
14+
<h1><?= $code ?></h1>
15+
</div>
16+
<table role="presentation" border="0" cellpadding="0" cellspacing="0" style="width: 100%;" width="100%">
17+
<tbody>
18+
<tr>
19+
<td style="line-height: 20px; font-size: 20px; width: 100%; height: 20px; margin: 0;" align="left" width="100%" height="20">
20+
&#160;
21+
</td>
22+
</tr>
23+
</tbody>
24+
</table>
25+
<b><?= lang('Auth.emailInfo') ?></b>
26+
<p><?= lang('Auth.emailIpAddress') ?> <?= esc($ipAddress) ?></p>
27+
<p><?= lang('Auth.emailDevice') ?> <?= esc($userAgent) ?></p>
28+
<p><?= lang('Auth.emailDate') ?> <?= esc($date) ?></p>
29+
</body>
30+
31+
</html>

src/Views/Email/magic_link_email.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
1-
<p>
2-
<a href="<?= url_to('verify-magic-link') ?>?token=<?= $token ?>">
3-
<?= lang('Auth.login') ?>
4-
</a>
5-
</p>
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
3+
<head>
4+
<meta name="x-apple-disable-message-reformatting">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<meta name="format-detection" content="telephone=no, date=no, address=no, email=no">
7+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8+
<title><?= lang('Auth.magicLinkSubject') ?></title>
9+
</head>
10+
11+
<body>
12+
<table role="presentation" border="0" cellpadding="0" cellspacing="0" style="border-radius: 6px; border-collapse: separate !important;">
13+
<tbody>
14+
<tr>
15+
<td style="line-height: 24px; font-size: 16px; border-radius: 6px; margin: 0;" align="center" bgcolor="#0d6efd">
16+
<a href="<?= url_to('verify-magic-link') ?>?token=<?= $token ?>" style="color: #ffffff; font-size: 16px; font-family: Helvetica, Arial, sans-serif; text-decoration: none; border-radius: 6px; line-height: 20px; display: inline-block; font-weight: normal; white-space: nowrap; background-color: #0d6efd; padding: 8px 12px; border: 1px solid #0d6efd;"><?= lang('Auth.login') ?></a>
17+
</td>
18+
</tr>
19+
</tbody>
20+
</table>
21+
<table role="presentation" border="0" cellpadding="0" cellspacing="0" style="width: 100%;" width="100%">
22+
<tbody>
23+
<tr>
24+
<td style="line-height: 20px; font-size: 20px; width: 100%; height: 20px; margin: 0;" align="left" width="100%" height="20">
25+
&#160;
26+
</td>
27+
</tr>
28+
</tbody>
29+
</table>
30+
<b><?= lang('Auth.emailInfo') ?></b>
31+
<p><?= lang('Auth.emailIpAddress') ?> <?= esc($ipAddress) ?></p>
32+
<p><?= lang('Auth.emailDevice') ?> <?= esc($userAgent) ?></p>
33+
<p><?= lang('Auth.emailDate') ?> <?= esc($date) ?></p>
34+
</body>
35+
36+
</html>

tests/Controllers/ActionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function testEmailActivateShow(): void
236236
service('email')->archive['body']
237237
);
238238
$this->assertMatchesRegularExpression(
239-
'!<p>[0-9]{6}</p>!',
239+
'!<h1>[0-9]{6}</h1>!',
240240
service('email')->archive['body']
241241
);
242242
}

0 commit comments

Comments
 (0)