1 | <?php |
||
24 | class Otp extends BaseObject |
||
25 | { |
||
26 | /** |
||
27 | * TOTP is a time based one-time password. It lives only for a few seconds (the period). You just have to be sure that the clock of your server and your device are synchronized. This is the most common OTP. |
||
28 | */ |
||
29 | const TOTP = 0; |
||
30 | |||
31 | /** |
||
32 | * HOTP is a counter based one-time password. Every time a password is used, the counter is updated. You have to verify that the server and the device are synchronized. |
||
33 | */ |
||
34 | const HOTP = 1; |
||
35 | |||
36 | /** |
||
37 | * @var string type of otp |
||
38 | * |
||
39 | * @see self::HOTP |
||
40 | * @see self::TOTP |
||
41 | */ |
||
42 | public $type = self::TOTP; |
||
43 | |||
44 | /** |
||
45 | * @var int otp digits. Default the number is 6 |
||
46 | */ |
||
47 | public $digits = 6; |
||
48 | |||
49 | /** |
||
50 | * @var string digest algorithm. Default is 'sha1' you can use any algorithm listed by hash_algos(). Note that most applications only support md5, sha1, sha256 and sha512. |
||
51 | */ |
||
52 | public $digest = 'sha1'; |
||
53 | |||
54 | /** |
||
55 | * @var string the template of qr code uri server. Please note that this URI MUST contain a placeholder {PROVISIONING_URI} for the OTP Provisioning URI. |
||
56 | */ |
||
57 | public $qrCodeUriTemplate = 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl={PROVISIONING_URI}'; |
||
58 | |||
59 | /** |
||
60 | * Generate an otp digits. |
||
61 | * |
||
62 | * @param string $secretKey the secret key use to generate an otp |
||
63 | * @return string an otp generated by secret key given. |
||
64 | * |
||
65 | * @throws NotSupportedException |
||
66 | */ |
||
67 | 4 | public function generate(string $secretKey) |
|
71 | |||
72 | /** |
||
73 | * Validate an otp digits. |
||
74 | * |
||
75 | * @param string $secretKey the secret key use to validate an otp. |
||
76 | * @param string $otp need to verify |
||
77 | * @return bool weather an otp given is valid |
||
78 | * |
||
79 | * @throws NotSupportedException |
||
80 | */ |
||
81 | 3 | public function validate(string $secretKey, string $otp) |
|
85 | |||
86 | /** |
||
87 | * Get qr code for authenticator like google authenticator. |
||
88 | * |
||
89 | * @param string $secretKey the secret key an authenticator use to generating an otp. |
||
90 | * @param array $params list of information use to show on an authenticator app. |
||
91 | * |
||
92 | * Example: |
||
93 | * ```php |
||
94 | * ['issuer' => 'VXM', 'label' => '[email protected]', 'image' => 'https://google.com'] |
||
95 | * ``` |
||
96 | * @return string the qr code uri |
||
97 | * @throws NotSupportedException |
||
98 | */ |
||
99 | 3 | public function getQrCodeUri(string $secretKey, array $params) |
|
109 | |||
110 | /** |
||
111 | * Create an otp instance. |
||
112 | * |
||
113 | * @param string $secretKey the secret key use to create an otp instance. |
||
114 | * |
||
115 | * @return HOTP|TOTP an object instance for generate and validate otp. |
||
116 | * @throws NotSupportedException |
||
117 | */ |
||
118 | 8 | protected function createInstance(string $secretKey) |
|
130 | |||
131 | } |
||
132 |