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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/// wifiqr
/// A crate to transform Wifi credentials into a scannable QR code

extern crate image;
extern crate qrcodegen;

macro_rules! wifi_auth {
    // Derived from:
    // https://github.com/zxing/zxing/wiki/Barcode-Contents#wifi-network-config-android
    //
    // T: authentication type (WEP, WPA, 'nopass'). Can be ommitted for no password.
    // S: network SSID
    // P: wifi password. Can be ommitted if T is 'nopass'
    // H: Hidden SSID. Optional.
    (hidden) => ("WIFI:T:{};S:{};P:{};H:{};;");
    (nopass) => ("WIFI:T:nopass;S:{};;");
    (nopass_hidden) => ("WIFI:T:nopass;S:{};H:{};;");
    () => {
        "WIFI:T:{};S:{};P:{};;";
    };
}

#[cfg(test)]
mod tests {
    use super::code::Credentials;
    use super::code::{encode, make_svg, manual_encode};
    use qrcodegen::{QrCodeEcc, Version};

    /// Basic functionality test
    #[test]
    fn test_credentials() {
        assert_eq!(
            Credentials::new(Some("test"), Some("password"), Some("WPA2"), false, false).format().unwrap(),
            "WIFI:T:WPA2;S:test;P:password;;"
        );
    }

    /// Test credential escaping; per Zxing guidelines on how to format a `WIFI:` string
    #[test]
    fn test_credentials_escapes() {
        assert_eq!(
            Credentials::new(Some(r###""foo;bar\baz""###), 
                             Some("randompassword"), 
                             Some("wpa2"), 
                             false, false).format().unwrap(),
            r###"WIFI:T:WPA2;S:\"foo\;bar\\baz\";P:randompassword;;"###
        );
    }

    /// Exercise the automatic qr encoder against the manual encoder
    #[test]
    fn test_qrcodes() {
        let credentials = Credentials::new(Some("test"), Some("WPA"), Some("test"), false, false);

        assert_eq!(
            make_svg(&encode(&credentials).unwrap()),
            make_svg(&manual_encode(
                &credentials,
                QrCodeEcc::High,
                Version::new(2),
                Version::new(15),
                None,
            ))
        );
    }

    /// Ensure that the hidden flag is added if requested 
    #[test]
    fn test_hidden_ssid() {
        assert_eq!(Credentials::new(Some(r###""foo;bar\baz""###), 
                                    Some("randompassword"), 
                                    Some("WPA2"), true, false).format().unwrap(),
            r###"WIFI:T:WPA2;S:\"foo\;bar\\baz\";P:randompassword;H:true;;"###);
    }

    /// If a ssid isn't hidden, it shouldn't be set in the formatted string
    #[test]
    fn test_normal_ssid() {
        assert_eq!(Credentials::new(Some(r###""foo;bar\baz""###), 
                                    Some("randompassword"), 
                                    Some("WPA2"), false, false).format().unwrap(),
            r###"WIFI:T:WPA2;S:\"foo\;bar\\baz\";P:randompassword;;"###);
    }

    /// Require a password when wpa/wpa2 is requested
    #[test]
    fn test_nopassword_with_wpa2() {
        assert!(Credentials::new(Some(r###""foo;bar\baz""###), 
                                    Some(""), 
                                    Some("wpa"), 
                                    false, false).format().is_err(), 
        "wpa2 requires a password");
 
        assert!(Credentials::new(Some(r###""foo;bar\baz""###), 
                                    Some(""), 
                                    Some("wpa2"), 
                                    false, false).format().is_err(), 
        "wpa2 requires a password");
    }

    /// Require a password when using wep
    #[test]
    fn test_nopassword_with_wep() {
        assert!(Credentials::new(Some(r###""foo;bar\baz""###), 
                                    Some(""), 
                                    Some("wep"), 
                                    false, false).format().is_err(), 
        "wep requires a password");
    }

    #[test]
    fn test_nopassword_with_nopassword() {
        assert!(Credentials::new(Some("bane"), 
                                    Some(""), 
                                    Some("nopass"), 
                                    false, false).format().is_ok(), 
        "nopass specified with a blank password should work");
    }

    /// Test various auth (T) types, like WPA/WPA2
    #[test]
    fn test_auth_types() {
        // wep
        assert_eq!(
            Credentials::new(Some("test"), Some("password"), Some("wep"), false, false).format().unwrap(),
            "WIFI:T:WEP;S:test;P:password;;"
        );

        // wpa
        assert_eq!(
            Credentials::new(Some("test"), Some("password"), Some("WPA"), false, false).format().unwrap(),
            "WIFI:T:WPA;S:test;P:password;;"
        );

        // wpa2 -- note that the wifi string has WPA2 in caps. it seems that iOS devices are sensitive
        // to the T: parameter being lowercase (and will return 'no usable data found')
        assert_eq!(
            Credentials::new(Some("test"), Some("password"), Some("wpa2"), false, false).format().unwrap(),
            "WIFI:T:WPA2;S:test;P:password;;"
        );

        // wpa3
        assert_eq!(
            Credentials::new(Some("test"), Some("password"), Some("wpa3"), false, false).format().unwrap(),
            "WIFI:T:WPA3;S:test;P:password;;"
        );

        // nopass -- unlike wpa2/wpa3, etc, nopass is accepted by iOS devices uncapitalized
        assert_eq!(
            Credentials::new(Some("test"), Some(""), Some("nopass"), false, false).format().unwrap(),
            "WIFI:T:nopass;S:test;;"
        );
    }

    #[test]
    fn test_empty_passwords_with_nopass_encr() {
        assert!(Credentials::new(Some(r###""foo;bar\baz""###), 
                                    Some("password"), 
                                    Some("nopass"), 
                                    false, false).format().is_err(), 
        "nopass cannot be specified with a password");
    }

    /// ensure that nopass is set along with an empty password when it is requested by the user
    #[test]
    fn test_encr_nopass_with_empty_password() {
        assert_eq!(
            Credentials::new(Some("test"), Some(""), Some("nopass"), false, false).format().unwrap(),
            "WIFI:T:nopass;S:test;;"
        );
    }

    /// when quote is set, ensure that the result is quoted
    #[test]
    fn test_quoted_ssid_password() {
        assert_eq!(
            Credentials::new(Some("test"), Some("password"), Some("wpa2"), false, true).format().unwrap(),
            "WIFI:T:WPA2;S:\"test\";P:\"password\";;"
        );
    }
}

/// Wifi QR code generator
pub mod code {

    use std::convert::TryInto;

    use image::{ImageBuffer, LumaA};
    use qrcodegen::{DataTooLong, Mask, QrCode, QrCodeEcc, QrSegment};

    use imageproc::drawing::draw_filled_rect_mut;
    use imageproc::rect::Rect;

    #[derive(Debug)]
    pub struct Credentials {
        pub ssid: String,
        pub pass: String,
        pub encr: String,
        pub hidden: bool,
        pub quote: bool,
    }

    impl Credentials {
        pub fn new(
            mut _ssid: Option<&str>,
            mut _password: Option<&str>,
            mut _encr: Option<&str>,
            mut _hidden: bool,
            mut _quote: bool,
        ) -> Self {
            return Credentials {
                ssid: _ssid.unwrap().to_string(),
                encr: _encr.unwrap().to_string(),
                pass: _password.unwrap().to_string(),
                hidden: _hidden,
                quote: _quote,
            };
        }

        /// escape characters as in:
        /// https://github.com/zxing/zxing/wiki/Barcode-Contents#wifi-network-config-android
        /// Special characters `\`, `;`, `,` and `:` should be escaped with a backslash
        fn filter_credentials(&self, field: &str) -> String {
            // N.B. If performance problems ever crop up, this might be more performant
            // with regex replace_all 

            let mut filtered = field.to_string()
                .replace(r#"\"#, r#"\\"#)
                .replace(r#"""#, r#"\""#)
                .replace(r#";"#, r#"\;"#)
                .replace(r#":"#, r#"\:"#);

            if (filtered == self.ssid || filtered == self.pass) && self.quote {
                // println!("Adding quotes to SSID/Password -- quote is not set");
                filtered = format!("\"{}\"", field.to_string());
            }

            return filtered
        }

        /// the encryption field in the Wifi QR code fails on iOS devices if it is
        /// not provided in an uppercase format. Android devices are case insensitive,
        /// so the encryption field is passed through as uppercase now.
        fn filter_encr(&self, field: &str) -> String {
            if field != "nopass" && !self.encr.is_empty() {
                return field.to_string().to_uppercase();
            }
            return field.to_string();
        }

        /// Call the wifi_auth! macro to generate a qr-string and/or return any errors that 
        /// need to be raised to the caller. Note: format does not enforce an encryption type, it is
        /// up to the end user to use the right value if one is provided.
        pub fn format(&self) -> Result<String, &'static str> {
            // empty password ->
            //  * is password empty and ssid hidden? => set T:nopass and H:
            //  * is encryption type empty? => set nopass
            //  * hidden ssid? => add H:
            // plain format
            // unrecoverable errors:
            // * ssid has no password, but sets a T type
            // * sets a password, but sets T type to nopass

            if self.encr == "nopass" || self.encr.is_empty() {
                if !self.pass.is_empty() {
                    return Err("With nopass as the encryption type (or unset encryption type), 
                        the password field should be empty. (Encryption should probably be set 
                        to something like wpa2)")
                }
            }

            if self.pass.is_empty() || self.pass == "" {
                // Error condition: Password is empty, and the T (encr) type is not "nopass" / not empty
                if self.encr != "nopass" && !self.encr.is_empty() {
                    return Err("The encryption method requested requires a password. 
                        If you would like no password, set '--encr nopass'")
                }

                if self.encr.is_empty() || self.encr == "nopass" {
                    if self.hidden {
                        return Ok(format!(
                            wifi_auth!(nopass_hidden),
                            self.filter_credentials(&self.ssid),
                            &self.hidden,
                        ));
                    } else if self.pass == "" {
                        return Ok(format!(
                            wifi_auth!(nopass),
                            self.filter_credentials(&self.ssid),
                        ));
                    }
                }
            }

            if self.hidden {
                return Ok(format!(
                    wifi_auth!(hidden),
                    self.filter_credentials(&self.filter_encr(&self.encr)),
                    self.filter_credentials(&self.ssid),
                    self.filter_credentials(&self.pass),
                    &self.hidden,
                ))
            } else {
                return Ok(format!(
                    wifi_auth!(),
                    self.filter_credentials(&self.filter_encr(&self.encr)),
                    self.filter_credentials(&self.ssid),
                    self.filter_credentials(&self.pass)
                ))
            }
        }

        // Transform the QR Wifi connection string into a Vec<char> for use with manual_encode()
        pub fn format_vec(&self) -> Vec<char> {
            return Credentials::format(&self).unwrap().chars().collect();
        }
    }

    /// returns a new Credentials struct given Wifi credentials. This data is not validated,
    /// nor formatted into a QR code string. Call .format() on Credentials to do this.
    pub fn auth(_ssid: Option<&str>, _password: Option<&str>, _encr: Option<&str>, 
        _hidden: bool, _quote: bool) -> Credentials {
        return self::Credentials::new(_ssid, _password, _encr, _hidden, _quote);
    }

    /// generates a qrcode from a Credentials configuration 
    pub fn encode(config: &Credentials) -> Result<QrCode, DataTooLong> {
        let q = QrCode::encode_text(&config.format().unwrap(), QrCodeEcc::High)?;
        Ok(q)
    }

    /// manual_encode isn't intended for use externally, but exists to compare between the
    /// automated encoder and this manual_encode version
    /// https://docs.rs/qrcodegen/latest/src/qrcodegen/lib.rs.html#151
    pub fn manual_encode(config: &Credentials, error_level: QrCodeEcc, lowest_version: qrcodegen::Version, 
        highest_version: qrcodegen::Version, mask_level: Option<Mask>) -> QrCode {

        let wifi: Vec<char> = config.format_vec();
        let segs: Vec<QrSegment> = QrSegment::make_segments(&wifi);

        return QrCode::encode_segments_advanced(
            &segs,
            error_level,
            lowest_version,
            highest_version,
            mask_level,
            true,
        ).unwrap();
    }

    /// generates an svg string from a QrCode (output from the QR library)
    ///
    /// * qrcode: &QrCode
    ///
    pub fn make_svg(qrcode: &QrCode) -> String {
        return qrcode.to_svg_string(4);
    }

    /// generates a wifi qr code that is printed to a terminal/console for quick scanning
    /// parameters:
    /// - qrcode: encoded qrcode
    /// - quiet_zone: the border size to apply to the QR code (created with ASCII_BL_BLOCK)
    /// result:
    /// - this prints a block of text directly to the console
    pub fn console_qr(qrcode: &QrCode, quiet_zone: i32) {
        const ASCII_BL_BLOCK: &str = "  ";
        const ASCII_W_BLOCK: &str = "██";

        let x_zone = quiet_zone;
        let y_zone = quiet_zone;

        // paint top border -- y axis
        for _top_border in 0..y_zone {
            print!("{}", ASCII_BL_BLOCK);
            println!();
        }

        for y in 0..qrcode.size() {
            // paint left border -- x axis
            for _left_border in 0..x_zone {
                print!("{}", ASCII_BL_BLOCK);
            }

            // paint qr
            for x in 0..qrcode.size() {
                if qrcode.get_module(x, y) {
                    print!("{}", ASCII_W_BLOCK);
                } else {
                    print!("{}", ASCII_BL_BLOCK);
                }
            }

            // paint right border -- x axis
            for _right_border in 0..x_zone {
                print!("{}", ASCII_BL_BLOCK);
            }

            println!();
        }

        // paint bottom border -- y axis
        for _bottom_border in 0..y_zone {
            print!("{}", ASCII_BL_BLOCK);
            println!();
        }
    }

    /// returns an ImageBuffer<> that can be saved using save_image(), or passed on
    /// for further manipulation by the caller
    /// 
    ///
    /// * qrcode: Is an encoded qrcode
    /// 
    /// * scale: The scaling factor to apply to the qrcode
    /// 
    /// * border_size: How large to make the quiet zone
    pub fn make_image(qrcode: &QrCode, scale: i32, border_size: i32) -> ImageBuffer<LumaA<u8>, Vec<u8>> {
        let new_qr_size = qrcode.size() * scale;

        // --- Initialize to a white canvas with the alpha layer pre-set ---
        let mut image = ImageBuffer::from_pixel(
            (new_qr_size + border_size * 2).try_into().unwrap(),
            (new_qr_size + border_size * 2).try_into().unwrap(),
            LumaA([255, 255]),
        );

        // --- Draw QR w/scale ---
        for y in 0..new_qr_size {
            for x in 0..new_qr_size {
                if qrcode.get_module(x, y) {
                    draw_filled_rect_mut(
                        &mut image,
                        Rect::at(
                            (x * scale) + border_size as i32,
                            (y * scale) + border_size as i32,
                        ).of_size(scale as u32, scale as u32),
                        LumaA([0, 255]),
                    );
                } else {
                    draw_filled_rect_mut(
                        &mut image,
                        Rect::at(
                            (x * scale) + border_size as i32,
                            (y * scale) + border_size as i32,
                        ).of_size(scale as u32, scale as u32),
                        LumaA([255, 255]),
                    );
                }
            }
        }

        return image;
    }


    /// saves an image to a file
    ///
    /// * image: ImageBuffer<>
    ///
    /// * save_file: file path to save the image into
    pub fn save_image(image: &ImageBuffer<LumaA<u8>, Vec<u8>>, save_file: String) {
        let _ = image.save(save_file).unwrap();
    }
}