| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace OTPHP; |
| 6 |
|
| 7 |
interface OTPInterface |
| 8 |
{ |
| 9 |
public const DEFAULT_DIGITS = 6; |
| 10 |
|
| 11 |
/** |
| 12 |
* Upper bound for the "digits" parameter. The RFC 4226 dynamic truncation |
| 13 |
* yields a 31-bit value (max 2 147 483 647, i.e. 10 digits); above this |
| 14 |
* bound the extra digits carry no entropy and "10 ** digits" overflows |
| 15 |
* PHP's integer range, leading to a DivisionByZeroError during generation. |
| 16 |
*/ |
| 17 |
public const MAX_DIGITS = 10; |
| 18 |
|
| 19 |
public const DEFAULT_DIGEST = 'sha1'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Create a OTP object from an existing secret. |
| 23 |
* |
| 24 |
* @param non-empty-string $secret |
| 25 |
*/ |
| 26 |
public static function createFromSecret(string $secret): self; |
| 27 |
|
| 28 |
/** |
| 29 |
* Create a new OTP object. A random 64 bytes secret will be generated. |
| 30 |
*/ |
| 31 |
public static function generate(): self; |
| 32 |
|
| 33 |
/** |
| 34 |
* @param non-empty-string $secret |
| 35 |
* |
| 36 |
* @deprecated Deprecated since v11.4, use {@see self::withSecret()} instead |
| 37 |
*/ |
| 38 |
public function setSecret(string $secret): void; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param non-empty-string $secret |
| 42 |
*/ |
| 43 |
public function withSecret(string $secret): self; |
| 44 |
|
| 45 |
/** |
| 46 |
* @deprecated Deprecated since v11.4, use {@see self::withDigits()} instead |
| 47 |
*/ |
| 48 |
public function setDigits(int $digits): void; |
| 49 |
|
| 50 |
public function withDigits(int $digits): self; |
| 51 |
|
| 52 |
/** |
| 53 |
* @param non-empty-string $digest |
| 54 |
* |
| 55 |
* @deprecated Deprecated since v11.4, use {@see self::withDigest()} instead |
| 56 |
*/ |
| 57 |
public function setDigest(string $digest): void; |
| 58 |
|
| 59 |
/** |
| 60 |
* @param non-empty-string $digest |
| 61 |
*/ |
| 62 |
public function withDigest(string $digest): self; |
| 63 |
|
| 64 |
/** |
| 65 |
* Generate the OTP at the specified input. |
| 66 |
* |
| 67 |
* @param 0|positive-int $input |
| 68 |
* |
| 69 |
* @return non-empty-string Return the OTP at the specified timestamp |
| 70 |
*/ |
| 71 |
public function at(int $input): string; |
| 72 |
|
| 73 |
/** |
| 74 |
* Verify that the OTP is valid with the specified input. If no input is provided, the input is set to a default |
| 75 |
* value or false is returned. |
| 76 |
* |
| 77 |
* @param non-empty-string $otp |
| 78 |
* @param null|0|positive-int $input |
| 79 |
* @param null|0|positive-int $window |
| 80 |
*/ |
| 81 |
public function verify(string $otp, null|int $input = null, null|int $window = null): bool; |
| 82 |
|
| 83 |
/** |
| 84 |
* @return non-empty-string The secret of the OTP |
| 85 |
*/ |
| 86 |
public function getSecret(): string; |
| 87 |
|
| 88 |
/** |
| 89 |
* @param non-empty-string $label The label of the OTP |
| 90 |
* |
| 91 |
* @deprecated Deprecated since v11.4, use {@see self::withLabel()} instead |
| 92 |
*/ |
| 93 |
public function setLabel(string $label): void; |
| 94 |
|
| 95 |
/** |
| 96 |
* @param non-empty-string $label The label of the OTP |
| 97 |
*/ |
| 98 |
public function withLabel(string $label): self; |
| 99 |
|
| 100 |
/** |
| 101 |
* @return non-empty-string|null The label of the OTP |
| 102 |
*/ |
| 103 |
public function getLabel(): null|string; |
| 104 |
|
| 105 |
/** |
| 106 |
* @return non-empty-string|null The issuer |
| 107 |
*/ |
| 108 |
public function getIssuer(): ?string; |
| 109 |
|
| 110 |
/** |
| 111 |
* @param non-empty-string $issuer |
| 112 |
* |
| 113 |
* @deprecated Deprecated since v11.4, use {@see self::withIssuer()} instead |
| 114 |
*/ |
| 115 |
public function setIssuer(string $issuer): void; |
| 116 |
|
| 117 |
/** |
| 118 |
* @param non-empty-string $issuer |
| 119 |
*/ |
| 120 |
public function withIssuer(string $issuer): self; |
| 121 |
|
| 122 |
/** |
| 123 |
* @return bool If true, the issuer will be added as a parameter in the provisioning URI |
| 124 |
*/ |
| 125 |
public function isIssuerIncludedAsParameter(): bool; |
| 126 |
|
| 127 |
/** |
| 128 |
* @deprecated Deprecated since v11.4, use {@see self::withIssuerIncludedAsParameter()} instead |
| 129 |
*/ |
| 130 |
public function setIssuerIncludedAsParameter(bool $issuer_included_as_parameter): void; |
| 131 |
|
| 132 |
public function withIssuerIncludedAsParameter(bool $issuer_included_as_parameter): self; |
| 133 |
|
| 134 |
/** |
| 135 |
* @return positive-int Number of digits in the OTP |
| 136 |
*/ |
| 137 |
public function getDigits(): int; |
| 138 |
|
| 139 |
/** |
| 140 |
* @return non-empty-string Digest algorithm used to calculate the OTP. Spec-compliant, interoperable values are 'sha1', 'sha256' and 'sha512'. Any digest shorter than 19 bytes (e.g. 'md5') is rejected as it cannot satisfy the RFC 4226 dynamic truncation. |
| 141 |
*/ |
| 142 |
public function getDigest(): string; |
| 143 |
|
| 144 |
/** |
| 145 |
* @param non-empty-string $parameter |
| 146 |
*/ |
| 147 |
public function getParameter(string $parameter): mixed; |
| 148 |
|
| 149 |
/** |
| 150 |
* @param non-empty-string $parameter |
| 151 |
*/ |
| 152 |
public function hasParameter(string $parameter): bool; |
| 153 |
|
| 154 |
/** |
| 155 |
* @return array<non-empty-string, mixed> |
| 156 |
*/ |
| 157 |
public function getParameters(): array; |
| 158 |
|
| 159 |
/** |
| 160 |
* @param non-empty-string $parameter |
| 161 |
* |
| 162 |
* @deprecated Deprecated since v11.4, use {@see self::withParameter()} instead |
| 163 |
*/ |
| 164 |
public function setParameter(string $parameter, mixed $value): void; |
| 165 |
|
| 166 |
/** |
| 167 |
* @param non-empty-string $parameter |
| 168 |
*/ |
| 169 |
public function withParameter(string $parameter, mixed $value): self; |
| 170 |
|
| 171 |
/** |
| 172 |
* Get the provisioning URI. |
| 173 |
* |
| 174 |
* @return non-empty-string |
| 175 |
*/ |
| 176 |
public function getProvisioningUri(): string; |
| 177 |
|
| 178 |
/** |
| 179 |
* Get the provisioning URI. |
| 180 |
* |
| 181 |
* @param non-empty-string $uri The Uri of the QRCode generator with all parameters. This Uri MUST contain a placeholder that will be replaced by the method. |
| 182 |
* @param non-empty-string $placeholder the placeholder to be replaced in the QR Code generator URI |
| 183 |
*/ |
| 184 |
public function getQrCodeUri(string $uri, string $placeholder): string; |
| 185 |
} |