Skip to content

Commit

Permalink
Add OTP notification for email
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Aug 16, 2024
1 parent b8ca334 commit 0bcf9af
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 32 deletions.
34 changes: 18 additions & 16 deletions resources/lang/en.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
{
"Secure your account": "Secure your account",
"Your account has been secured with two factor authentication": "Your account has been secured with two factor authentication",
"Activate": "Activate",
"Add additional security to your account using two factor authentication": "Add additional security to your account using two factor authentication",
"You have three options to confirm your identity, please choose one of the options below to continue": "You have three options to confirm your identity, please choose one of the options below to continue",
"Two-Factor Authentication enabled": "Two-Factor Authentication enabled",
"Or scan the QR code with your authenticator app": "Or scan the QR code with your authenticator app",
"The secret key to setup the authenticator app is": "The secret key to setup the authenticator app is",
"Save these recovery codes in a secure place as they can be used to recover access to your account if you lose your device.": "Save these recovery codes in a secure place as they can be used to recover access to your account if you lose your device.",
"Authenticate with your code": "Authenticate with your code",
"Cancel": "Cancel",
"Code": "Code",
"Confirm": "Confirm",
"Deactivate": "Deactivate",
"Login": "Login",
"Authenticate with your code": "Authenticate with your code",
"Or scan the QR code with your authenticator app": "Or scan the QR code with your authenticator app",
"Password Confirmation": "Password Confirmation",
"Recovery code": "Recovery code",
"Resend": "Resend",
"Reset Password": "Reset Password",
"Save these recovery codes in a secure place as they can be used to recover access to your account if you lose your device.": "Save these recovery codes in a secure place as they can be used to recover access to your account if you lose your device.",
"Secure your account": "Secure your account",
"Submit": "Submit",
"Resend": "Resend",
"Successfully resend the OTP code": "Successfully resend the OTP code",
"Recovery code": "Recovery code",
"Password Confirmation": "Password Confirmation",
"Confirm": "Confirm",
"Verify": "Verify",
"Code": "Code",
"The secret key to setup the authenticator app is": "The secret key to setup the authenticator app is",
"Two-Factor Authentication": "Two-Factor Authentication",
"Activate": "Activate",
"Deactivate": "Deactivate"
"Two-Factor Authentication enabled": "Two-Factor Authentication enabled",
"Verify": "Verify",
"You have three options to confirm your identity, please choose one of the options below to continue": "You have three options to confirm your identity, please choose one of the options below to continue",
"Your account has been secured with two factor authentication": "Your account has been secured with two factor authentication",
"Your security code for :app": "Your security code for :app",
"Your security code is": "Your security code is"
}
34 changes: 18 additions & 16 deletions resources/lang/nl.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
{
"Secure your account": "Beveilig uw account",
"Your account has been secured with two factor authentication": "Uw account is beveiligd met tweestaps-verificatie",
"Activate": "Activeer",
"Add additional security to your account using two factor authentication": "Voeg extra beveiliging toe aan uw account met tweestaps-verificatie",
"You have three options to confirm your identity, please choose one of the options below to continue": "U heeft drie opties om uw identiteit te bevestigen, kies een van de onderstaande opties om door te gaan",
"Two-Factor Authentication enabled": "Tweestaps-verificatie ingeschakeld",
"Or scan the QR code with your authenticator app": "Of scan de QR-code met uw authenticator-app",
"The secret key to setup the authenticator app is": "De geheime sleutel om de authenticator-app in te stellen is",
"Save these recovery codes in a secure place as they can be used to recover access to your account if you lose your device.": "Bewaar deze herstelcodes op een veilige plaats, omdat ze kunnen worden gebruikt om toegang tot uw account te herstellen als u uw apparaat verliest.",
"Authenticate with your code": "Verifieer met uw authenticatiecode",
"Cancel": "Annuleren",
"Code": "Code",
"Confirm": "Bevestigen",
"Deactivate": "Deactiveer",
"Login": "Inloggen",
"Authenticate with your code": "Verifieer met uw authenticatiecode",
"Or scan the QR code with your authenticator app": "Of scan de QR-code met uw authenticator-app",
"Password Confirmation": "Wachtwoordbevestiging",
"Recovery code": "Herstelcode",
"Resend": "Opnieuw Verzenden",
"Reset Password": "Wachtwoord Resetten",
"Save these recovery codes in a secure place as they can be used to recover access to your account if you lose your device.": "Bewaar deze herstelcodes op een veilige plaats, omdat ze kunnen worden gebruikt om toegang tot uw account te herstellen als u uw apparaat verliest.",
"Secure your account": "Beveilig uw account",
"Submit": "Verzenden",
"Resend": "Opnieuw Verzenden",
"Successfully resend the OTP code": "OTP-code succesvol opnieuw verzonden",
"Recovery code": "Herstelcode",
"Password Confirmation": "Wachtwoordbevestiging",
"Confirm": "Bevestigen",
"Verify": "Verifiëren",
"Code": "Code",
"The secret key to setup the authenticator app is": "De geheime sleutel om de authenticator-app in te stellen is",
"Two-Factor Authentication": "Tweestaps-verificatie",
"Activate": "Activeer",
"Deactivate": "Deactiveer"
"Two-Factor Authentication enabled": "Tweestaps-verificatie ingeschakeld",
"Verify": "Verifiëren",
"You have three options to confirm your identity, please choose one of the options below to continue": "U heeft drie opties om uw identiteit te bevestigen, kies een van de onderstaande opties om door te gaan",
"Your account has been secured with two factor authentication": "Uw account is beveiligd met tweestaps-verificatie",
"Your security code for :app": "Uw beveiligingscode voor :app",
"Your security code is": "Uw beveiligingscode is"
}
21 changes: 21 additions & 0 deletions src/Actions/GenerateOTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Vormkracht10\TwoFactorAuth\Actions;

use PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException;
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;
use PragmaRX\Google2FA\Google2FA;

class GenerateOTP
{
/**
* @throws IncompatibleWithGoogleAuthenticatorException
* @throws SecretKeyTooShortException
* @throws InvalidCharactersException
*/
public static function for(string $secret): string
{
return app(Google2FA::class)->getCurrentOtp($secret);
}
}
28 changes: 28 additions & 0 deletions src/Listeners/SendTwoFactorCodeListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Vormkracht10\TwoFactorAuth\Listeners;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Vormkracht10\TwoFactorAuth\Notifications\SendOTP;
use Laravel\Fortify\Events\TwoFactorAuthenticationEnabled;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;

class SendTwoFactorCodeListener
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}

/**
* Handle the event.
*/
public function handle(TwoFactorAuthenticationChallenged|TwoFactorAuthenticationEnabled $event): void
{
$event->user->notify(app(SendOTP::class));

Check failure on line 26 in src/Listeners/SendTwoFactorCodeListener.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method notify() on an unknown class App\Models\User.
}
}
75 changes: 75 additions & 0 deletions src/Notifications/SendOTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Vormkracht10\TwoFactorAuth\Notifications;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Vormkracht10\TwoFactorAuth\Actions\GenerateOTP;
use Vormkracht10\TwoFactorAuth\Enums\TwoFactorType;
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;
use PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException;

class SendOTP extends Notification implements ShouldQueue
{
use Queueable;

/**
* Create a new notification instance.
*/
public function __construct()
{
//
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return $notifiable->two_factor_type === TwoFactorType::email ? ['mail'] : [];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject(__('Your security code for :app', ['app' => config('app.name')]))
->line(__('Your security code is') . ' ' . $this->getTwoFactorCode($notifiable));
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}

/**
* @throws IncompatibleWithGoogleAuthenticatorException
* @throws SecretKeyTooShortException
* @throws InvalidCharactersException
*/
public function getTwoFactorCode(User $notifiable): ?string

Check failure on line 65 in src/Notifications/SendOTP.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $notifiable of method Vormkracht10\TwoFactorAuth\Notifications\SendOTP::getTwoFactorCode() has invalid type App\Models\User.
{
if (!$notifiable->two_factor_secret) {

Check failure on line 67 in src/Notifications/SendOTP.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $two_factor_secret on an unknown class App\Models\User.
return null;
}

return GenerateOTP::for(
decrypt($notifiable->two_factor_secret)
);
}
}

0 comments on commit 0bcf9af

Please sign in to comment.