Skip to content

Commit 52abc34

Browse files
committed
chore: remove otp reminder email
fix: add to otp email reset password request if user has blank password Change-Id: Ica3d73103be9d0d93dd7e74d4b7974d50588d008
1 parent d38c25e commit 52abc34

11 files changed

+77
-242
lines changed

app/Jobs/GenerateOTPRegistrationReminder.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

app/Mail/OAuth2PasswordlessOTPMail.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,34 @@ class OAuth2PasswordlessOTPMail extends Mailable
5050
public $subject;
5151

5252
/**
53-
* OAuth2PasswordlessOTPMail constructor.
53+
* @var string|null
54+
*/
55+
public $reset_password_link;
56+
57+
/**
58+
* @var int|null
59+
*/
60+
public $reset_password_link_lifetime;
61+
62+
/**
5463
* @param string $to
5564
* @param string $otp
5665
* @param int $lifetime
66+
* @param string|null $reset_password_link
5767
*/
5868
public function __construct
5969
(
6070
string $to,
6171
string $otp,
62-
int $lifetime
72+
int $lifetime,
73+
string $reset_password_link = null
6374
)
6475
{
6576
$this->email = trim($to);
6677
$this->otp = trim($otp);
6778
$this->lifetime = $lifetime / 60;
79+
$this->reset_password_link = $reset_password_link;
80+
$this->reset_password_link_lifetime = Config::get("auth.password_reset_lifetime")/60;
6881
}
6982
/**
7083
* Build the message.

app/Mail/OTPRegistrationReminderEmail.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

app/Services/Auth/IUserService.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,4 @@ public function initializeUser(int $user_id):?User;
130130
*/
131131
public function updateRegistrationRequest(int $id, array $payload):UserRegistrationRequest;
132132

133-
/**
134-
* @param int $user_id
135-
* @return void
136-
* @throws \Exception
137-
*/
138-
public function sendOTPRegistrationReminder(int $user_id);
139133
}

app/Services/Auth/UserService.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14-
1514
use App\Events\UserPasswordResetSuccessful;
1615
use App\Jobs\PublishUserCreated;
1716
use App\Jobs\PublishUserUpdated;
@@ -23,7 +22,6 @@
2322
use App\libs\Auth\Repositories\ISpamEstimatorFeedRepository;
2423
use App\libs\Auth\Repositories\IUserPasswordResetRequestRepository;
2524
use App\libs\Auth\Repositories\IUserRegistrationRequestRepository;
26-
use App\Mail\OTPRegistrationReminderEmail;
2725
use App\Mail\UserEmailVerificationRequest;
2826
use App\Mail\UserEmailVerificationSuccess;
2927
use App\Mail\UserPasswordResetRequestMail;
@@ -604,24 +602,4 @@ public function initializeUser(int $user_id): ?User
604602
return $user;
605603
});
606604
}
607-
608-
/**
609-
* @param int $user_id
610-
* @return void
611-
* @throws \Exception
612-
*/
613-
public function sendOTPRegistrationReminder(int $user_id){
614-
$this->tx_service->transaction(function() use($user_id) {
615-
Log::debug(sprintf("UserService::sendOTPRegistrationReminder %s", $user_id));
616-
$user = $this->user_repository->getById($user_id);
617-
if( !$user instanceof User)
618-
throw new EntityNotFoundException(sprintf("User %s not found.", $user_id));
619-
620-
if ($user->hasPasswordSet())
621-
throw new ValidationException(sprintf("User %s already has password set.", $user->getId()));
622-
623-
$request = $this->generatePasswordResetRequest($user->getEmail());
624-
Mail::queue(new OTPRegistrationReminderEmail($user, $request->getResetLink()));
625-
});
626-
}
627605
}

app/Strategies/OTP/OTPChannelEmailStrategy.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,42 @@
1313
**/
1414

1515
use App\Mail\OAuth2PasswordlessOTPMail;
16+
use App\Services\Auth\IUserService;
17+
use Auth\User;
1618
use Illuminate\Support\Facades\Log;
1719
use Illuminate\Support\Facades\Mail;
1820
use Models\OAuth2\OAuth2OTP;
21+
use Auth\Repositories\IUserRepository;
1922
/**
2023
* Class OTPChannelEmailStrategy
2124
* @package App\Strategies\OTP
2225
*/
2326
final class OTPChannelEmailStrategy
2427
implements IOTPChannelStrategy
2528
{
29+
/**
30+
* @var IUserRepository
31+
*/
32+
private $user_repository;
33+
34+
/**
35+
* @var IUserService
36+
*/
37+
private $user_service;
2638

39+
/**
40+
* @param IUserService $user_service
41+
* @param IUserRepository $user_repository
42+
*/
43+
public function __construct
44+
(
45+
IUserService $user_service,
46+
IUserRepository $user_repository
47+
)
48+
{
49+
$this->user_repository = $user_repository;
50+
$this->user_service = $user_service;
51+
}
2752
/**
2853
* @param IOTPTypeBuilderStrategy $typeBuilderStrategy
2954
* @param OAuth2OTP $otp
@@ -34,13 +59,30 @@ public function send(IOTPTypeBuilderStrategy $typeBuilderStrategy, OAuth2OTP $ot
3459
$value = $typeBuilderStrategy->generate($otp);
3560
// send email
3661
try{
62+
$reset_password_link = null;
63+
$user = $this->user_repository->getByEmailOrName($otp->getUserName());
64+
if($user instanceof User && !$user->hasPasswordSet()){
65+
// create a password reset request
66+
Log::debug
67+
(
68+
sprintf
69+
(
70+
"OTPChannelEmailStrategy::send user %s has no password set",
71+
$user->getId()
72+
)
73+
);
74+
$request = $this->user_service->generatePasswordResetRequest($user->getEmail());
75+
$reset_password_link = $request->getResetLink();
76+
}
77+
3778
Mail::queue
3879
(
3980
new OAuth2PasswordlessOTPMail
4081
(
4182
$otp->getUserName(),
4283
$value,
43-
$otp->getLifetime()
84+
$otp->getLifetime(),
85+
$reset_password_link
4486
)
4587
);
4688
}

app/Strategies/OTP/OTPChannelStrategyFactory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* limitations under the License.
1313
**/
1414

15+
use App\Services\Auth\IUserService;
16+
use Auth\Repositories\IUserRepository;
17+
use Illuminate\Support\Facades\App;
1518
use OAuth2\Exceptions\InvalidOAuth2Request;
1619
use OAuth2\OAuth2Protocol;
1720
/**
@@ -24,7 +27,11 @@ public static function build(string $connection):IOTPChannelStrategy{
2427

2528
switch ($connection) {
2629
case OAuth2Protocol::OAuth2PasswordlessConnectionEmail:
27-
return new OTPChannelEmailStrategy();
30+
return new OTPChannelEmailStrategy
31+
(
32+
App::make(IUserService::class),
33+
App::make(IUserRepository::class),
34+
);
2835
case OAuth2Protocol::OAuth2PasswordlessConnectionInline:
2936
return new OTPChannelNullStrategy();
3037
}

app/libs/Auth/AuthService.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* limitations under the License.
1313
**/
1414

15-
use App\Jobs\GenerateOTPRegistrationReminder;
1615
use App\libs\OAuth2\Exceptions\ReloadSessionException;
1716
use App\libs\OAuth2\Repositories\IOAuth2OTPRepository;
1817
use App\Services\AbstractService;
@@ -278,10 +277,6 @@ public function loginWithOTP(OAuth2OTP $otpClaim, ?Client $client = null, bool $
278277

279278
Auth::login($user, $remember);
280279

281-
if (!$user->hasPasswordSet() && !$new_user) {
282-
// trigger background job
283-
GenerateOTPRegistrationReminder::dispatch($user);
284-
}
285280
Log::debug(sprintf("AuthService::loginWithOTP user %s logged in.", $user->getId()));
286281
return $otp;
287282
});

resources/views/emails/oauth2_passwordless_otp.blade.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
<div style="font-family:open Sans Helvetica, Arial, sans-serif;font-size:16px;line-height:1;text-align:center;color:#000000;">Thanks! <br/><br/>{{Config::get('app.tenant_name')}} Support Team</div>
3232
</td>
3333
</tr>
34+
@if(!empty($reset_password_link))
35+
<tr>
36+
<td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
37+
<div style="font-family:open Sans Helvetica, Arial, sans-serif;font-size:16px;line-height:1;text-align:justify;color:#000000;">
38+
In order to login more quickly in the future you can <a href="{!! $reset_password_link !!}" target="_blank">set a password</a>(this link expires in {!! $reset_password_link_lifetime !!} min but you can always use the <a
39+
href="{!! URL::action("Auth\ForgotPasswordController@showLinkRequestForm") !!}?email={!! $email !!}"
40+
target="_blank">reset your password</a> option to get a new one).
41+
</div>
42+
</td>
43+
</tr>
44+
@endif
3445
</tbody>
3546
</table>
3647
@stop

resources/views/emails/oauth2_passwordless_otp_reg_reminder.blade.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)