Commit 9f2ea107570e44612a12d29a1d31cd942dc3dd87

Cléo Rebert 2023-09-10T21:34:47

Update changelog, README, comments and subcrate version Signed-off-by: Cléo Rebert <cleo.rebert@gmail.com>

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9290a58..2e0b8f4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,13 @@
+# [5.3.0](https://github.com/constantoine/totp-rs/releases/tag/v4.2) (10/09/2023)
+### What's new
+- Creation of a new `qrcodegen-image` subcrate to handle image creation, as the wrapper is actually nice and could be used in placed not related to `totp-rs`. (#61)
+
+### Changes
+- `TOTP::get_qr` was deprecated in favour of `TOTP::get_qr_base64` and `TOTP::get_qr_png`. 
+
+### Special thanks
+* [@tmpfs](https://github.com/tmpfs) for their work on #60 and implementation in #61.
+
 # [5.2.0](https://github.com/constantoine/totp-rs/releases/tag/v5.2.0) (10/08/2023)
 ### Changes
 - Updated `url` crate to `2.4`.
diff --git a/Cargo.toml b/Cargo.toml
index d2657ff..3a44b6f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -39,4 +39,4 @@ url = { version = "2.4", optional = true }
 constant_time_eq = "0.2"
 rand = { version = "0.8", features = ["std_rng", "std"], optional = true, default-features = false }
 zeroize = { version = "1.6", features = ["alloc", "derive"], optional = true }
-qrcodegen-image = { version = "0.1", features = ["base64"], optional = true, path = "qrcodegen-image" }
\ No newline at end of file
+qrcodegen-image = { version = "1.0", features = ["base64"], optional = true, path = "qrcodegen-image" }
\ No newline at end of file
diff --git a/README.md b/README.md
index b5c0262..527cf65 100644
--- a/README.md
+++ b/README.md
@@ -92,7 +92,7 @@ fn main() {
 Add it to your `Cargo.toml`:
 ```toml
 [dependencies.totp-rs]
-version = "^5.0"
+version = "^5.3"
 features = ["qr"]
 ```
 You can then do something like:
@@ -109,8 +109,8 @@ fn main() {
         Some("Github".to_string()),
         "constantoine@github.com".to_string(),
     ).unwrap();
-    let code = totp.get_qr()?;
-    println!("{}", code);   
+    let qr_code = totp.get_qr_base64()?;
+    println!("{}", qr_code);   
 }
 ```
 
@@ -147,7 +147,7 @@ fn main() {
 Add it to your `Cargo.toml`:
 ```toml
 [dependencies.totp-rs]
-version = "^5.0"
+version = "^5.3"
 features = ["gen_secret"]
 ```
 You can then do something like:
@@ -164,8 +164,8 @@ fn main() {
         Some("Github".to_string()),
         "constantoine@github.com".to_string(),
     ).unwrap();
-    let code = totp.get_qr()?;
-    println!("{}", code);   
+    let qr_code = totp.get_qr_base64()?;
+    println!("{}", qr_code);   
 }
 ```
 Which is equivalent to
@@ -182,8 +182,8 @@ fn main() {
         Some("Github".to_string()),
         "constantoine@github.com".to_string(),
     ).unwrap();
-    let code = totp.get_qr()?;
-    println!("{}", code);   
+    let qr_code = totp.get_qr_base64()?;
+    println!("{}", qr_code);   
 }
 ```
 
@@ -223,7 +223,7 @@ fn main() {
 Add it to your `Cargo.toml`:
 ```toml
 [dependencies.totp-rs]
-version = "^5.0"
+version = "^5.3"
 features = ["qr"]
 ```
 You can then do something like:
@@ -234,7 +234,7 @@ fn main() {
     let totp = TOTP::new_steam(
         Secret::Encoded("KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ".to_string()).to_bytes().unwrap(),
     ).unwrap();
-    let code = totp.get_qr()?;
-    println!("{}", code);   
+    let qr_code = totp.get_qr_base64()?;
+    println!("{}", qr_code);     
 }
 ```
\ No newline at end of file
diff --git a/qrcodegen-image/Cargo.toml b/qrcodegen-image/Cargo.toml
index 5927334..5bcb45a 100644
--- a/qrcodegen-image/Cargo.toml
+++ b/qrcodegen-image/Cargo.toml
@@ -1,12 +1,12 @@
 [package]
 name = "qrcodegen-image"
-version = "0.1.0"
+version = "1.0.0"
 edition = "2021"
 authors = ["Cleo Rebert <cleo.rebert@gmail.com>"]
 rust-version = "1.61"
 readme = "README.md"
 license = "MIT"
-description = "Draw QR codes to a PNG canvas."
+description = "Draw QR codes to a PNG canvas. Wrapper around the qrcodegen crate."
 repository = "https://github.com/constantoine/totp-rs"
 homepage = "https://github.com/constantoine/totp-rs"
 
diff --git a/src/lib.rs b/src/lib.rs
index 699601b..14d1ecd 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -42,7 +42,7 @@
 //! ).unwrap();
 //! let url = totp.get_url();
 //! println!("{}", url);
-//! let code = totp.get_qr().unwrap();
+//! let code = totp.get_qr_base64().unwrap();
 //! println!("{}", code);
 //! # }
 //! ```
@@ -646,6 +646,15 @@ impl TOTP {
 
 #[cfg(feature = "qr")]
 impl TOTP {
+    #[deprecated(
+        since = "5.3.0",
+        note = "get_qr was forcing the use of png as a base64. Use get_qr_base64 or get_qr_png instead. Will disappear in 6.0."
+    )]
+    pub fn get_qr(&self) -> Result<String, String> {
+        let url = self.get_url();
+        qrcodegen_image::draw_base64(&url)
+    }
+
     /// 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.
@@ -657,10 +666,25 @@ impl TOTP {
     /// Which would be too long for some browsers anyway.
     ///
     /// It will also return an error in case it can't encode the qr into a png. This shouldn't happen unless either the qrcode library returns malformed data, or the image library doesn't encode the data correctly
-    pub fn get_qr(&self) -> Result<String, String> {
+    pub fn get_qr_base64(&self) -> Result<String, String> {
         let url = self.get_url();
         qrcodegen_image::draw_base64(&url)
     }
+
+    /// Will return a qrcode to automatically add a TOTP as a byte array. Needs feature `qr` to be enabled!
+    /// Result will be in the form of a png file as bytes.
+    ///
+    /// # Errors
+    ///
+    /// This will return an error in case the URL gets too long to encode into a QR code.
+    /// This would require the get_url method to generate an url bigger than 2000 characters,
+    /// Which would be too long for some browsers anyway.
+    ///
+    /// It will also return an error in case it can't encode the qr into a png. This shouldn't happen unless either the qrcode library returns malformed data, or the image library doesn't encode the data correctly
+    pub fn get_qr_png(&self) -> Result<Vec<u8>, String> {
+        let url = self.get_url();
+        qrcodegen_image::draw_png(&url)
+    }
 }
 
 #[cfg(test)]
@@ -1225,7 +1249,24 @@ mod tests {
 
     #[test]
     #[cfg(feature = "qr")]
-    fn generates_qr_ok() {
+    fn generates_qr_base64_ok() {
+        let totp = TOTP::new(
+            Algorithm::SHA1,
+            6,
+            1,
+            1,
+            "TestSecretSuperSecret".as_bytes().to_vec(),
+            Some("Github".to_string()),
+            "constantoine@github.com".to_string(),
+        )
+        .unwrap();
+        let qr = totp.get_qr_base64();
+        assert!(qr.is_ok());
+    }
+
+    #[test]
+    #[cfg(feature = "qr")]
+    fn generates_qr_png_ok() {
         let totp = TOTP::new(
             Algorithm::SHA1,
             6,
@@ -1236,7 +1277,7 @@ mod tests {
             "constantoine@github.com".to_string(),
         )
         .unwrap();
-        let qr = totp.get_qr();
+        let qr = totp.get_qr_png();
         assert!(qr.is_ok());
     }
 }