forked from LanceGin/dotp
/
totp.dart
97 lines (86 loc) · 2.52 KB
/
totp.dart
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
///
/// @module : TOTP module to generate and verify TOTP password
/// @author : Gin (gin.lance.inside@hotmail.com)
///
import 'otp.dart';
import 'components/otp_type.dart';
import 'package:dart_otp/src/utils/generic_util.dart';
import 'package:dart_otp/src/components/otp_algorithm.dart';
///
/// TOTP class will generate the OTP (One Time Password) object with the current or given time.
class TOTP extends OTP {
/// Period in which should be generated new tokens.
int interval;
@override
OTPType get type => OTPType.TOTP;
@override
Map<String, dynamic> get extraUrlProperties => {"period": interval};
///
/// This constructor will create an TOTP instance.
///
/// All parameters are mandatory however [interval],
/// [digits] and [algorithm] have a default values, so can be ignored.
///
/// Will throw an exception if the line above isn't satisfied.
///
TOTP(
{String secret,
int digits = 6,
int interval = 30,
OTPAlgorithm algorithm = OTPAlgorithm.SHA1})
: super(secret: secret, digits: digits, algorithm: algorithm) {
this.interval = interval;
}
///
/// Generate the TOTP value with current time.
///
/// ```dart
/// TOTP totp = TOTP(secret: 'BASE32ENCODEDSECRET');
/// totp.now(); // => 432143
/// ```
///
String now() {
int _formatTime = Util.timeFormat(time: DateTime.now(), interval: interval);
return super.generateOTP(input: _formatTime);
}
///
/// Generate the OTP with a custom time.
///
/// All parameters are mandatory.
///
/// ```dart
/// TOTP totp = TOTP(secret: 'BASE32ENCODEDSECRET');
/// totp.value(date: DateTime.now()); // => 432143
/// ```
///
String value({DateTime date}) {
if (date == null) {
return null;
}
int _formatTime = Util.timeFormat(time: date, interval: interval);
return super.generateOTP(input: _formatTime);
}
///
/// Verifies the TOTP value passed in against the current time.
///
/// All parameters are mandatory.
///
/// ```dart
/// TOTP totp = TOTP(secret: 'BASE32ENCODEDSECRET');
/// totp.now(); // => 432143
/// // Verify for current time
/// totp.verify(otp: 432143); // => true
/// // Verify after 30s
/// totp.verify(otp: 432143); // => false
/// ```
///
bool verify({String otp, DateTime time}) {
if (otp == null) {
return false;
}
var _time = time ?? DateTime.now();
var _input = Util.timeFormat(time: _time, interval: interval);
String otpTime = super.generateOTP(input: _input);
return otp == otpTime;
}
}