Merge pull request #18 from constantoine/add_tests Add tests for generate_current and check_current
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
diff --git a/.gitignore b/.gitignore
index 96ef6c0..315330e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
/target
Cargo.lock
+.vscode/settings.json
+cobertura.xml
diff --git a/Cargo.toml b/Cargo.toml
index c3cf279..9696ff3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "totp-rs"
-version = "1.2.1"
+version = "1.3.0"
authors = ["Cleo Rebert <cleo.rebert@gmail.com>"]
edition = "2021"
readme = "README.md"
diff --git a/README.md b/README.md
index 967f72f..fe0630c 100644
--- a/README.md
+++ b/README.md
@@ -17,7 +17,7 @@ With optional feature "serde_support", library-defined types will be Deserialize
Add it to your `Cargo.toml`:
```toml
[dependencies]
-totp-rs = "~1.2"
+totp-rs = "~1.3"
```
You can then do something like:
```Rust
@@ -31,12 +31,9 @@ let totp = TOTP::new(
30,
"supersecret",
);
-let time = SystemTime::now()
- .duration_since(SystemTime::UNIX_EPOCH).unwrap()
- .as_secs();
let url = totp.get_url("user@example.com", "my-org.com");
println!("{}", url);
-let token = totp.generate(time);
+let token = totp.generate_current().unwrap();
println!("{}", token);
```
@@ -45,7 +42,7 @@ println!("{}", token);
Add it to your `Cargo.toml`:
```toml
[dependencies.totp-rs]
-version = "~1.2"
+version = "~1.3"
features = ["qr"]
```
You can then do something like:
@@ -67,6 +64,6 @@ println!("{}", code);
Add it to your `Cargo.toml`:
```toml
[dependencies.totp-rs]
-version = "~1.2"
+version = "~1.3"
features = ["serde_support"]
```
diff --git a/src/lib.rs b/src/lib.rs
index be0d599..64ecda5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -19,12 +19,9 @@
//! 30,
//! "supersecret",
//! );
-//! let time = SystemTime::now()
-//! .duration_since(SystemTime::UNIX_EPOCH).unwrap()
-//! .as_secs();
//! let url = totp.get_url("user@example.com", "my-org.com");
//! println!("{}", url);
-//! let token = totp.generate(time);
+//! let token = totp.generate_current().unwrap();
//! println!("{}", token);
//! ```
//!
@@ -165,7 +162,7 @@ impl<T: AsRef<[u8]>> TOTP<T> {
)
}
- /// Will generate a token according to the provided timestamp in seconds
+ /// Will generate a token given the provided timestamp in seconds
pub fn generate(&self, time: u64) -> String {
let result: &[u8] = &self.sign(time);
let offset = (result.last().unwrap() & 15) as usize;
@@ -183,7 +180,7 @@ impl<T: AsRef<[u8]>> TOTP<T> {
Ok(self.generate(t))
}
- /// Will check if token is valid by current time, accounting [skew](struct.TOTP.html#structfield.skew)
+ /// Will check if token is valid given the provided timestamp in seconds, accounting [skew](struct.TOTP.html#structfield.skew)
pub fn check(&self, token: &str, time: u64) -> bool {
let basestep = time / self.step - (self.skew as u64);
for i in 0..self.skew * 2 + 1 {
@@ -223,6 +220,8 @@ impl<T: AsRef<[u8]>> TOTP<T> {
}
/// Will return a qrcode to automatically add a TOTP as a base64 string. Needs feature `qr` to be enabled!
+ /// Result will be in the form of a string containing a base64-encoded png, which you can embed in HTML without needing
+ /// To store the png as a file.
///
/// # Errors
///
@@ -248,20 +247,8 @@ impl<T: AsRef<[u8]>> TOTP<T> {
// Draw the border
for x in 0..image_size {
for y in 0..image_size {
- if y < 8*4 || y >= image_size - 8*4 {
- canvas.put_pixel(
- x,
- y,
- Luma([255]),
- );
- continue;
- }
- if x < 8*4 || x >= image_size - 8*4 {
- canvas.put_pixel(
- x,
- y,
- Luma([255]),
- );
+ if (y < 8*4 || y >= image_size - 8*4) || (x < 8*4 || x >= image_size - 8*4) {
+ canvas.put_pixel(x, y, Luma([255]));
}
}
}
@@ -378,12 +365,21 @@ mod tests {
}
#[test]
- fn generates_token() {
+ fn generate_token() {
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 1, "TestSecret");
assert_eq!(totp.generate(1000).as_str(), "718996");
}
#[test]
+ fn generate_token_current() {
+ let totp = TOTP::new(Algorithm::SHA1, 6, 1, 1, "TestSecret");
+ let time = SystemTime::now()
+ .duration_since(SystemTime::UNIX_EPOCH).unwrap()
+ .as_secs();
+ assert_eq!(totp.generate(time).as_str(), totp.generate_current().unwrap());
+ }
+
+ #[test]
fn generates_token_sha256() {
let totp = TOTP::new(Algorithm::SHA256, 6, 1, 1, "TestSecret");
assert_eq!(totp.generate(1000).as_str(), "480200");
@@ -405,6 +401,13 @@ mod tests {
}
#[test]
+ fn checks_token_current() {
+ let totp = TOTP::new(Algorithm::SHA1, 6, 0, 1, "TestSecret");
+ assert!(totp.check_current(&totp.generate_current().unwrap()).unwrap());
+ assert!(!totp.check_current("bogus").unwrap());
+ }
+
+ #[test]
fn checks_token_with_skew() {
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 1, "TestSecret");
assert!(