summaryrefslogtreecommitdiffstats
path: root/src/OtpInterface.php
blob: e241d5d2c65287d7c34fbe4efc6aaa4918b23a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

namespace Otp;

/**
 * Interface for HOTP and TOTP
 *
 * HMAC-Based One-time Password(HOTP) algorithm specified in RFC 4226
 * @link https://tools.ietf.org/html/rfc4226
 *
 * Time-based One-time Password (TOTP) algorithm specified in RFC 6238
 * @link https://tools.ietf.org/html/rfc6238
 *
 * @author Christian Riesen <chris.riesen@gmail.com>
 * @link http://christianriesen.com
 * @license MIT License see LICENSE file
 */

interface OtpInterface
{
    /**
     * Returns OTP using the HOTP algorithm (counter based)
     *
     * @param string  $secret  Base32 Secret String
     * @param integer $counter Counter
     *
     * @return string One Time Password
     */
    function hotp($secret, $counter);

    /**
     * Returns OTP using the TOTP algorithm (time based)
     *
     * @param string  $secret      Base32 Secret String
     * @param integer $timecounter Optional: Uses current time if null
     *
     * @return string One Time Password
     */
    function totp($secret, $timecounter = null);

    /**
     * Checks Hotp against a key
     *
     * This is a helper function, but is here to ensure the Totp can be checked
     * in the same manner.
     *
     * @param string  $secret  Base32 Secret String
     * @param integer $counter Counter
     * @param string  $key     User supplied key
     *
     * @return boolean True if key is correct
     */
    function checkHotp($secret, $counter, $key);

    /**
     * Checks Hotp against a key for a provided counter window
     *
     * @param string  $secret        Base32 Secret String
     * @param integer $counter       Counter
     * @param string  $key           User supplied key
     * @param integer $counterwindow (optional) Size of the look-ahead window. Default value is 2
     *
     * @return int|boolean the counter if key is correct else false
     */
    function checkHotpResync($secret, $counter, $key, $counterwindow = 2);

    /**
     * Checks Totp agains a key
     *
     * @param string  $secret    Base32 Secret String
     * @param integer $key       User supplied key
     * @param integer $timedrift How large a drift to use beyond exact match
     *
     * @return boolean True if key is correct within time drift
     */
    function checkTotp($secret, $key, $timedrift = 1);
}