|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) 2021. Hilmi Erdem Keren |
|
4
|
|
|
* license MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Erdemkeren\Otp; |
|
8
|
|
|
|
|
9
|
|
|
use Erdemkeren\Otp\Contracts\EncryptorContract; |
|
10
|
|
|
use Erdemkeren\Otp\Contracts\FormatContract; |
|
11
|
|
|
use Erdemkeren\Otp\Contracts\FormatManagerContract; |
|
12
|
|
|
use Erdemkeren\Otp\Contracts\TokenRepositoryContract; |
|
13
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
|
14
|
|
|
use UnexpectedValueException; |
|
15
|
|
|
|
|
16
|
|
|
class OtpService |
|
17
|
|
|
{ |
|
18
|
|
|
public function __construct( |
|
19
|
|
|
private FormatManagerContract $manager, |
|
20
|
|
|
private EncryptorContract $encryptor, |
|
21
|
|
|
private TokenRepositoryContract $repository, |
|
22
|
|
|
) { |
|
23
|
|
|
// |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function retrieveByCipherText(string $cipherText): ?OtpToken |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->repository->retrieveByCipherText($cipherText); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function create(int|string $authenticableId, string $format = 'default'): OtpToken |
|
32
|
|
|
{ |
|
33
|
|
|
$format = $this->getFormat($format); |
|
34
|
|
|
|
|
35
|
|
|
return tap(new OtpToken([ |
|
36
|
|
|
'format' => $format->name(), |
|
37
|
|
|
'plain_text' => $plainText = $format->generator()(), |
|
38
|
|
|
'cipher_text' => $this->encryptor->encrypt($plainText), |
|
39
|
|
|
'expiry_time' => 300, |
|
40
|
|
|
'authenticable_id' => $authenticableId, |
|
41
|
|
|
]), fn (OtpToken $otpToken) => $this->repository->persist($otpToken)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function save(OtpToken $token): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->repository->persist($token); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function extend(OtpToken $token, int $secs): OtpToken |
|
50
|
|
|
{ |
|
51
|
|
|
$extended = $token->extend($secs); |
|
52
|
|
|
|
|
53
|
|
|
$this->repository->persist($extended); |
|
54
|
|
|
|
|
55
|
|
|
return $extended; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function refresh(OtpToken $token): OtpToken |
|
59
|
|
|
{ |
|
60
|
|
|
$refreshed = $token->refresh(); |
|
61
|
|
|
|
|
62
|
|
|
$this->repository->persist($refreshed); |
|
63
|
|
|
|
|
64
|
|
|
return $refreshed; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function invalidate(OtpToken $token): OtpToken |
|
68
|
|
|
{ |
|
69
|
8 |
|
$invalidated = $token->invalidate(); |
|
70
|
|
|
|
|
71
|
|
|
$this->repository->persist($invalidated); |
|
72
|
|
|
|
|
73
|
|
|
return $invalidated; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
8 |
|
public function addFormat(FormatContract $format): void |
|
77
|
8 |
|
{ |
|
78
|
8 |
|
$this->manager->register($format); |
|
79
|
8 |
|
} |
|
80
|
|
|
|
|
81
|
8 |
|
public function sendOtpNotification(object $notifiable, OtpToken $token): void |
|
82
|
1 |
|
{ |
|
83
|
1 |
|
$notifiable->notify( |
|
84
|
|
|
$this->getFormat($token->format())->createNotification($token) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
8 |
|
|
|
88
|
8 |
|
public function sendNewOtp(Authenticatable $user): void |
|
89
|
1 |
|
{ |
|
90
|
1 |
|
$token = $this->create($user->getAuthIdentifier()); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
if (! method_exists($user, 'notify')) { |
|
93
|
|
|
throw new UnexpectedValueException( |
|
94
|
8 |
|
'The otp owner should be an instance of notifiable or implement the notify method.' |
|
95
|
1 |
|
); |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->sendOtpNotification($user, $token); |
|
99
|
|
|
} |
|
100
|
8 |
|
|
|
101
|
8 |
|
private function getFormat(string $format): FormatContract |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->manager->get($format); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|