Skip to content

Commit

Permalink
Update LaravelOtpGenerator.php
Browse files Browse the repository at this point in the history
rename AugmentedOTP as per old original class to keep some relevance

Update LaravelOtpGenerator.php

Update LaravelOtpGenerator.php

Create AugmentedOTP.php

Delete LaravelOtpGenerator.php

Update AugmentedOTP.php

Revert "Update AugmentedOTP.php"

This reverts commit aca58e6.

Revert "Create AugmentedOTP.php"

This reverts commit fb8a6c4.

Update composer.json

Update composer.json

Revert "Update LaravelOtpGenerator.php"

This reverts commit 2ca076a.

Update LaravelOtpGenerator.php

AugmentedOTP

Update LaravelOtpGenerator.php

Update LaravelOtpGenerator.php

Fix styling

Update LaravelOtpGenerator.php

Update LaravelOtpGenerator.php

Update LaravelOtpGenerator.php

Update LaravelOtpGenerator.php

Update LaravelOtpGenerator.php

Update LaravelOtpGenerator.php

Update LaravelOtpGenerator.php

Update composer.json

f

Fix styling

Update LaravelOtpGenerator.php
  • Loading branch information
msonowal committed Jul 31, 2022
1 parent dd41052 commit 11ae2b8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"clarity-tech",
"laravel",
"laravel-otp-generator",
"msonowal"
"msonowal",
"AugmentedOTP"
],
"homepage": "/~https://github.com/clarity-tech/laravel-otp-generator",
"license": "MIT",
Expand All @@ -19,7 +20,8 @@
"require": {
"php": "^8.1",
"spatie/laravel-package-tools": "^1.9.2",
"illuminate/contracts": "^9.0"
"illuminate/contracts": "^9.0",
"illuminate/http": "^9.0"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down Expand Up @@ -58,7 +60,7 @@
"ClarityTech\\LaravelOtpGenerator\\LaravelOtpGeneratorServiceProvider"
],
"aliases": {
"LaravelOtpGenerator": "ClarityTech\\LaravelOtpGenerator\\Facades\\LaravelOtpGenerator"
"AugmentedOTP": "ClarityTech\\LaravelOtpGenerator\\Facades\\AugmentedOTP"
}
}
},
Expand Down
14 changes: 14 additions & 0 deletions src/Facades/AugmentedOTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace ClarityTech\LaravelOtpGenerator\Facades;

use ClarityTech\LaravelOtpGenerator\LaravelOtpGenerator;
use Illuminate\Support\Facades\Facade;

class AugmentedOTP extends Facade
{
protected static function getFacadeAccessor()
{
return LaravelOtpGenerator::class;
}
}
42 changes: 35 additions & 7 deletions src/LaravelOtpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,62 @@

use ClarityTech\LaravelOtpGenerator\Exceptions\OTPExpiredException;
use ClarityTech\LaravelOtpGenerator\Exceptions\OTPInvalidException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class LaravelOtpGenerator
{
protected string $_key;

protected int $_expiry;

protected bool $_debug;

/**
* Create AugmentedOTP object by the identifer/ key
*/
public function __construct(string $key)
public function __construct(protected ?string $_key = null)
{
$this->setKey($key);
$this->debug = (bool) config('otp-generator.debug', false);
$this->_debug = (bool) config('otp-generator.debug', false);
$this->_expiry = (int) config('otp-generator.expiry', 15 * 60);
}

public static function create(string $key): static
{
return new static($key);
}

/**
* Create a instance with key from the ip and a mobile no to simple usage
* TODO: we can create a interface for implementation to the end user
*/
public static function fromRequest(Request $request, string $mobileNo): static
{
$key = 'augmentedOTP-'.sha1(implode('|', [$request->ip(), $mobileNo]));

$instance = static::create($key);

return $instance;
}

public function generateWithKey(string $key)
{
$otp = $this->withKey($key)
->generate();

return $otp;
}

public function getKey()
{
if (is_null($this->_key)) {
throw new \InvalidArgumentException('Please set a key to identify the OTP for verifying later.');
}

return $this->_key;
}

public function setKey(string $suffix)
public function withKey(string $suffix)
{
$this->_key = snake_case(class_basename(self::class).$suffix);
$this->_key = 'augmentedOTP-'.$suffix;

return $this;
}
Expand Down

0 comments on commit 11ae2b8

Please sign in to comment.