prasanth-j /
otpify
| 1 | <?php |
||
| 2 | |||
| 3 | namespace PrasanthJ\Otpify; |
||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | use PrasanthJ\Otpify\Models\Otp; |
||
| 7 | |||
| 8 | class Otpify |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Generates a new token. |
||
| 12 | * |
||
| 13 | * @param string $identifier |
||
| 14 | * @param int|null $userId |
||
| 15 | * @param string|null $otpType |
||
| 16 | * @param int|null $digits |
||
| 17 | * @param int|null $validity |
||
| 18 | * |
||
| 19 | * @return array<string,mixed|string> |
||
| 20 | */ |
||
| 21 | public static function generate(string $identifier, int $userId = null, string $otpType = null, int $digits = null, int $validity = null) |
||
| 22 | { |
||
| 23 | if ($digits === null) { |
||
| 24 | $digits = config('otpify.digits'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 25 | } |
||
| 26 | |||
| 27 | if ($validity === null) { |
||
| 28 | $validity = config('otpify.validity'); |
||
| 29 | } |
||
| 30 | |||
| 31 | Otp::where([ |
||
| 32 | ['identifier', $identifier], |
||
| 33 | ['otp_type', $otpType] |
||
| 34 | ])->delete(); |
||
| 35 | |||
| 36 | if (($digits >= 4) && ($digits <= 12)) { |
||
| 37 | $token = rand(pow(10, $digits - 1), pow(10, $digits) - 1); |
||
| 38 | |||
| 39 | Otp::create([ |
||
| 40 | 'user_id' => $userId, |
||
| 41 | 'identifier' => $identifier, |
||
| 42 | 'token' => $token, |
||
| 43 | 'validity' => $validity, |
||
| 44 | 'otp_type' => $otpType |
||
| 45 | ]); |
||
| 46 | |||
| 47 | return [ |
||
| 48 | 'status' => 'success', |
||
| 49 | 'token' => $token, |
||
| 50 | 'message' => 'OTP genetated successfully' |
||
| 51 | ]; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Validates the generated token. |
||
| 57 | * |
||
| 58 | * @param string $identifier |
||
| 59 | * @param string $token |
||
| 60 | * @param string|null $otpType |
||
| 61 | * |
||
| 62 | * @return array<string,string> |
||
| 63 | */ |
||
| 64 | public static function validate(string $identifier, string $token, string $otpType = null) |
||
| 65 | { |
||
| 66 | $otp = Otp::where([ |
||
| 67 | ['identifier', $identifier], |
||
| 68 | ['otp_type', $otpType] |
||
| 69 | ])->first(); |
||
| 70 | |||
| 71 | if ($otp == null) { |
||
| 72 | |||
| 73 | return [ |
||
| 74 | 'status' => 'error', |
||
| 75 | 'message' => 'OTP does not exist' |
||
| 76 | ]; |
||
| 77 | } else { |
||
| 78 | if (($otp->token == $token) && ($otp->verified == false)) { |
||
|
0 ignored issues
–
show
|
|||
| 79 | $carbon = new Carbon(); |
||
| 80 | $now = $carbon->now(); |
||
| 81 | $validity = $otp->created_at->addMinutes($otp->validity); |
||
| 82 | |||
| 83 | if (strtotime($validity) < strtotime($now)) { |
||
| 84 | |||
| 85 | return [ |
||
| 86 | 'status' => 'error', |
||
| 87 | 'message' => 'OTP Expired' |
||
| 88 | ]; |
||
| 89 | } else { |
||
| 90 | $otp->verified = true; |
||
| 91 | $otp->update(); |
||
| 92 | |||
| 93 | return [ |
||
| 94 | 'status' => 'success', |
||
| 95 | 'message' => 'OTP is valid' |
||
| 96 | ]; |
||
| 97 | } |
||
| 98 | } elseif (($otp->token == $token) && ($otp->verified == true)) { |
||
|
0 ignored issues
–
show
|
|||
| 99 | |||
| 100 | return [ |
||
| 101 | 'status' => 'info', |
||
| 102 | 'message' => 'OTP already verified' |
||
| 103 | ]; |
||
| 104 | } else { |
||
| 105 | |||
| 106 | return [ |
||
| 107 | 'status' => 'warning', |
||
| 108 | 'message' => 'OTP invalid' |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 |