Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
GraxMonzo committed Jan 16, 2021
1 parent c7a09bd commit 2417d71
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 47 deletions.
43 changes: 0 additions & 43 deletions .github/workflows/run-tests.yml

This file was deleted.

10 changes: 6 additions & 4 deletions src/HasOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

trait HasOTP
{
protected int $digits = 6;

protected OTPOptions $otpOptions;

abstract public function getOTPOptions(): OTPOptions;
Expand Down Expand Up @@ -46,7 +44,9 @@ public function authenticate($code, $options = [])
{
$this->otpOptions = $this->getOTPOptions();
$otpField = $this->otpOptions->otpField;
$totp = new TOTP($this->$otpField, ['digits' => $this->digits]);
$digits = $this->otpOptions->digits;

$totp = new TOTP($this->$otpField, compact('digits'));
if ($drift = $options['drift']) {
return $totp->verify($code, $drift);
} else {
Expand All @@ -58,11 +58,13 @@ public function otpCode($options = [])
{
$this->otpOptions = $this->getOTPOptions();
$otpField = $this->otpOptions->otpField;
$digits = $this->otpOptions->digits;

if (is_array($options)) {
$time = $options['time'] ?? time();
} else {
$time = $options;
}
return (new TOTP($this->$otpField, ['digits' => $this->digits]))->at($time);
return (new TOTP($this->$otpField, compact('digits')))->at($time);
}
}
9 changes: 9 additions & 0 deletions src/OTPOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class OTPOptions
{
public string $otpField;

public int $digits = 6;

public bool $generateOTPOnCreate = true;

public static function create(): self
Expand All @@ -19,4 +21,11 @@ public function saveOTPTo(string $fieldName): self

return $this;
}

public function withDigits(int $digits): self
{
$this->digits = $digits;

return $this;
}
}

0 comments on commit 2417d71

Please sign in to comment.