wifi_qr_code/lib.rs
1#![deny(missing_docs)]
2
3//! Wifi QR codes are a way to encode wifi connection information and credentials into a QR code so that it can be scanned. They are supported via the latest Android and iOS phones, as well as other platforms.
4//!
5//! It is important to take into account that QR codes do not provide any security mechanisms that would prevent someone from just reading the code and recovering the password for the network. Android requires that you re-authenticate before it will display the QR code on the screen to make sure the user is allowed to share that information, for example.
6
7pub use qrcode_generator::QrCodeEcc;
8
9use std::io::Write;
10
11/// Encode credentials as a matrix of boolean values. This is useful when manually generating an image.
12///
13/// # Examples
14///
15/// ```
16/// use wifi_qr_code::QrCodeEcc;
17/// use wifi_qr_code::{AuthenticationType, Visibility, WifiCredentials};
18///
19/// let wifi_credentials = WifiCredentials {
20/// ssid: String::from("example ssid"),
21/// authentication_type: AuthenticationType::WPA(String::from("example password")),
22/// visibility: Visibility::Hidden,
23/// };
24/// wifi_qr_code::encode_as_matrix(&wifi_credentials, QrCodeEcc::Medium);
25/// ```
26pub fn encode_as_matrix(
27 wifi_credentials: &WifiCredentials,
28 qr_code_error_checking: QrCodeEcc,
29) -> Result<Vec<Vec<bool>>, std::io::Error> {
30 qrcode_generator::to_matrix(wifi_credentials.encode(), qr_code_error_checking)
31}
32
33/// Encode credentials as raw image data. This is useful when generating the QR code and then manipulating it with an image library.
34///
35/// # Examples
36///
37/// ```
38/// use wifi_qr_code::QrCodeEcc;
39/// use wifi_qr_code::{AuthenticationType, Visibility, WifiCredentials};
40///
41/// let wifi_credentials = WifiCredentials {
42/// ssid: String::from("example ssid"),
43/// authentication_type: AuthenticationType::WPA(String::from("example password")),
44/// visibility: Visibility::Hidden,
45/// };
46/// wifi_qr_code::encode_as_image(&wifi_credentials, QrCodeEcc::Medium, 100);
47/// ```
48pub fn encode_as_image(
49 wifi_credentials: &WifiCredentials,
50 qr_code_error_checking: QrCodeEcc,
51 image_size: usize,
52) -> Result<Vec<u8>, std::io::Error> {
53 qrcode_generator::to_image(
54 wifi_credentials.encode(),
55 qr_code_error_checking,
56 image_size,
57 )
58}
59
60/// Encode credentials as a PNG image.
61///
62/// # Examples
63///
64/// ```
65/// use wifi_qr_code::QrCodeEcc;
66/// use wifi_qr_code::{AuthenticationType, Visibility, WifiCredentials};
67///
68/// use std::fs::File;
69///
70/// let wifi_credentials = WifiCredentials {
71/// ssid: String::from("example ssid"),
72/// authentication_type: AuthenticationType::WPA(String::from("example password")),
73/// visibility: Visibility::Hidden,
74/// };
75/// let png_file = File::create("wifi_qr.png").expect("Failed to create example PNG file.");
76/// wifi_qr_code::encode_as_png(&wifi_credentials, QrCodeEcc::Medium, 100, png_file);
77/// ```
78pub fn encode_as_png(
79 wifi_credentials: &WifiCredentials,
80 qr_code_error_checking: QrCodeEcc,
81 image_size: usize,
82 writer: impl Write,
83) -> Result<(), std::io::Error> {
84 qrcode_generator::to_png(
85 wifi_credentials.encode(),
86 qr_code_error_checking,
87 image_size,
88 writer,
89 )
90}
91
92/// Encode credentials as an SVG image.
93///
94/// # Examples
95///
96/// ```
97/// use wifi_qr_code::QrCodeEcc;
98/// use wifi_qr_code::{AuthenticationType, Visibility, WifiCredentials};
99///
100/// use std::fs::File;
101///
102/// let wifi_credentials = WifiCredentials {
103/// ssid: String::from("example ssid"),
104/// authentication_type: AuthenticationType::WPA(String::from("example password")),
105/// visibility: Visibility::Hidden,
106/// };
107/// let svg_file = File::create("wifi_qr.svg").expect("Failed to create example SVG file.");
108/// wifi_qr_code::encode_as_svg(&wifi_credentials, QrCodeEcc::Medium, 100, Some("Example Wifi QR Code"), svg_file);
109/// ```
110pub fn encode_as_svg(
111 wifi_credentials: &WifiCredentials,
112 qr_code_error_checking: QrCodeEcc,
113 image_size: usize,
114 description: Option<&str>,
115 writer: impl Write,
116) -> Result<(), std::io::Error> {
117 qrcode_generator::to_svg(
118 wifi_credentials.encode(),
119 qr_code_error_checking,
120 image_size,
121 description,
122 writer,
123 )
124}
125
126/// Declare whether the network is authenticated via WEP with a password, WPA with a password, or if the network is open.
127pub enum AuthenticationType {
128 /// WEP authentication is an older family of protocols. It is not particularly secure and wireless access points should use a more modern methods such as the WPA family of authentication protocols.
129 WEP(String),
130 /// WPA authentication is a more modern family of protocols. Typically, wireless networks will use WPA2 as their protocol implementation.
131 WPA(String),
132 /// No password / open access is particularly rare because it is possible for malicious actors to read all unencrypted traffic going across the network.
133 NoPassword,
134}
135
136impl AuthenticationType {
137 fn encode(&self) -> String {
138 match self {
139 Self::WEP(password) => format!("T:WEP;P:{};", escape(password)),
140 Self::WPA(password) => format!("T:WPA;P:{};", escape(password)),
141 Self::NoPassword => String::from("T:nopass;"),
142 }
143 }
144}
145
146/// Declare whether the network is broadcasting its availability.
147pub enum Visibility {
148 /// Visible wifi networks display in lists of networks when a device scans an area.
149 Visible,
150 /// Hidden wifi networks do not show up on scans and must be known by their SSID to be accessed.
151 Hidden,
152}
153
154impl Visibility {
155 fn encode(&self) -> String {
156 match self {
157 Self::Visible => String::from("H:false;"),
158 Self::Hidden => String::from("H:true;"),
159 }
160 }
161}
162
163/// The credentials needed to completely connect to a wifi network.
164pub struct WifiCredentials {
165 /// The SSID of a wifi network is the name used to access it.
166 pub ssid: String,
167 /// The authentication type of a wifi network determines the protocol used to access it and the password required to properly authenticate to it.
168 pub authentication_type: AuthenticationType,
169 /// The visibility of a wifi network determines if it can be seen by any device or if it must be known by SSID beforehand.
170 pub visibility: Visibility,
171}
172
173impl WifiCredentials {
174 /// Encode the credentials into the form expected for a wifi QR Code. Special characters (i.e. ";,:\) will be escaped in the output.
175 ///
176 /// # Examples
177 ///
178 /// ```
179 /// use wifi_qr_code::{AuthenticationType, Visibility, WifiCredentials};
180 ///
181 /// let wifi_credentials = WifiCredentials {
182 /// ssid: String::from("example ssid"),
183 /// authentication_type: AuthenticationType::WPA(String::from("example password")),
184 /// visibility: Visibility::Hidden,
185 /// };
186 /// assert_eq!("WIFI:S:example ssid;T:WPA;P:example password;H:true;;", wifi_credentials.encode());
187 /// ```
188 pub fn encode(&self) -> String {
189 format!(
190 "WIFI:{}{}{};",
191 self.encode_ssid(),
192 self.authentication_type.encode(),
193 self.visibility.encode()
194 )
195 }
196
197 fn encode_ssid(&self) -> String {
198 format!("S:{};", escape(&self.ssid))
199 }
200}
201
202fn escape(input: &str) -> String {
203 String::from(input)
204 .replace(r#"\"#, r#"\\"#)
205 .replace(r#"""#, r#"\""#)
206 .replace(";", r#"\;"#)
207 .replace(",", r#"\,"#)
208 .replace(":", r#"\:"#)
209}
210
211#[cfg(test)]
212mod tests {
213 use super::*;
214
215 #[test]
216 fn it_encodes_valid_wifi_forms() {
217 // WIFI:S:<SSID>;T:<WPA|WEP|>;P:<password>;H:<true|false|>;
218 let wifi_credentials = WifiCredentials {
219 ssid: String::from("test ssid"),
220 authentication_type: AuthenticationType::WEP(String::from("test password")),
221 visibility: Visibility::Visible,
222 };
223 assert_eq!(
224 "WIFI:S:test ssid;T:WEP;P:test password;H:false;;",
225 &wifi_credentials.encode()
226 );
227 let wifi_credentials = WifiCredentials {
228 ssid: String::from("test ssid"),
229 authentication_type: AuthenticationType::WPA(String::from("test password")),
230 visibility: Visibility::Hidden,
231 };
232 assert_eq!(
233 "WIFI:S:test ssid;T:WPA;P:test password;H:true;;",
234 &wifi_credentials.encode()
235 );
236 let wifi_credentials = WifiCredentials {
237 ssid: String::from("test ssid"),
238 authentication_type: AuthenticationType::NoPassword,
239 visibility: Visibility::Visible,
240 };
241 assert_eq!(
242 "WIFI:S:test ssid;T:nopass;H:false;;",
243 &wifi_credentials.encode()
244 );
245 }
246
247 #[test]
248 fn it_properly_handles_escaped_characters() {
249 let wifi_credentials = WifiCredentials {
250 ssid: String::from(r#"special_characters ";,:\"#),
251 authentication_type: AuthenticationType::WEP(String::from(
252 r#"special_characters ";,:\"#,
253 )),
254 visibility: Visibility::Visible,
255 };
256 assert_eq!(
257 r#"WIFI:S:special_characters \"\;\,\:\\;T:WEP;P:special_characters \"\;\,\:\\;H:false;;"#,
258 &wifi_credentials.encode()
259 );
260 }
261}