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
/// rust implementation for HTOP, TOTP and steam guard tow-factor-authentication
///
/// usage:
/// ```rust
/// use libr2fa::HOTPKey;
/// use libr2fa::HMACType;
/// use libr2fa::Key;
///
/// let mut hotp_key = HOTPKey {
/// key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
/// hmac_type: HMACType::SHA1,
/// ..Default::default()
/// };
///
/// let code = hotp_key.get_code().unwrap();
/// ```
use serde::{Deserialize, Serialize};
mod error;
mod hmac_type;
mod hotp;
mod totp;
mod uri;
pub use error::Error;
pub use hmac_type::HMACType;
pub use hotp::HOTPKey;
pub use totp::TOTPKey;
pub use uri::URI;
#[cfg(test)]
mod test;
/// KeyType is the type of the key
/// HOTP is the counter based key
/// TOTP is the time based key
/// STEAM is the steam guard key (TODO not implemented yet)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum KeyType {
HOTP,
#[default]
TOTP,
}
impl std::fmt::Display for KeyType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
KeyType::HOTP => write!(f, "hotp"),
KeyType::TOTP => write!(f, "totp"),
}
}
}
impl From<&str> for KeyType {
fn from(s: &str) -> Self {
match s.to_ascii_lowercase().as_str() {
"hotp" => KeyType::HOTP,
"totp" => KeyType::TOTP,
_ => KeyType::default(),
}
}
}
impl From<String> for KeyType {
fn from(s: String) -> Self {
KeyType::from(s.as_str())
}
}
/// Key is the interface for the keys
///
/// usage:
/// ```rust
/// use libr2fa::HOTPKey;
/// use libr2fa::HMACType;
/// use libr2fa::Key;
///
/// let mut hotp_key = HOTPKey {
/// key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
/// hmac_type: HMACType::SHA1,
/// ..Default::default()
/// };
///
/// let code = hotp_key.get_code().unwrap();
/// ```
pub trait Key {
/// get_code returns the code for the key
///
/// if it is HTOP key, it will increment the counter
fn get_code(&mut self) -> Result<String, error::Error>;
/// get the name of the key
///
/// ```rust
/// use libr2fa::HOTPKey;
/// use libr2fa::HMACType;
/// use libr2fa::Key;
///
/// let mut hotp_key = HOTPKey {
/// key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
/// hmac_type: HMACType::SHA1,
/// ..Default::default()
/// };
///
/// hotp_key.set_name("test");
///
/// assert_eq!(hotp_key.get_name(), "test")
/// ```
fn get_name(&self) -> &str;
/// get the recovery codes
///
/// ```rust
/// use libr2fa::HOTPKey;
/// use libr2fa::HMACType;
/// use libr2fa::Key;
///
/// let mut hotp_key = HOTPKey {
/// key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
/// hmac_type: HMACType::SHA1,
/// ..Default::default()
/// };
///
/// hotp_key.set_recovery_codes(&["test".to_string()]);
///
/// assert_eq!(hotp_key.get_recovery_codes(), &["test".to_string()])
///
/// ```
fn get_recovery_codes(&self) -> &[String];
/// get the type of the key
fn get_type(&self) -> KeyType;
/// set the name of the key
///
/// ```rust
/// use libr2fa::HOTPKey;
/// use libr2fa::HMACType;
/// use libr2fa::Key;
///
/// let mut hotp_key = HOTPKey {
/// name: "".to_string(),
/// key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
/// hmac_type: HMACType::SHA1,
/// ..Default::default()
/// };
///
/// hotp_key.set_name("test");
///
/// assert_eq!(hotp_key.get_name(), "test")
/// ```
fn set_name(&mut self, name: &str);
/// set the recovery codes
///
/// ```rust
/// use libr2fa::HOTPKey;
/// use libr2fa::HMACType;
/// use libr2fa::Key;
///
/// let mut hotp_key = HOTPKey {
/// key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
/// hmac_type: HMACType::SHA1,
/// ..Default::default()
/// };
///
/// hotp_key.set_recovery_codes(&["test".to_string()]);
///
/// assert_eq!(hotp_key.get_recovery_codes(), &["test".to_string()])
///
/// ```
fn set_recovery_codes(&mut self, recovery_codes: &[String]);
}
/// create a new key from the uri string
///
/// ```rust
/// use libr2fa::otpauth_from_uri;
/// use libr2fa::TOTPKey;
/// use libr2fa::HMACType;
/// use libr2fa::Key;
///
/// let totp_key1 = otpauth_from_uri("otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA256&digits=7&period=60");
/// if let Err(err) = totp_key1 {
/// panic!("{}", err);
/// }
/// let mut totp_key1 = totp_key1.unwrap();
///
/// let mut totp_key2 = TOTPKey {
/// name: "ACME Co:john.doe@email.com".to_string(),
/// key: "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ".to_string(),
/// digits: 7,
/// time_step: 60,
/// hmac_type: HMACType::SHA256,
/// issuer: Some("ACME Co".to_string()),
/// ..Default::default()
/// };
///
/// assert_eq!(totp_key1.get_name(), totp_key2.get_name());
/// assert_eq!(totp_key1.get_type(), totp_key2.get_type());
/// assert_eq!(totp_key1.get_code(), totp_key2.get_code());
/// ```
pub fn otpauth_from_uri(uri: &str) -> Result<Box<dyn Key>, Error> {
let uri_struct = URI::from(uri);
match uri_struct.key_type {
KeyType::HOTP => HOTPKey::from_uri_struct(&uri_struct),
KeyType::TOTP => TOTPKey::from_uri_struct(&uri_struct),
}
}
/// create a new key from the uri qrcode
///
/// ```rust
/// use libr2fa::otpauth_from_uri_qrcode;
/// use libr2fa::TOTPKey;
/// use libr2fa::HMACType;
/// use libr2fa::Key;
///
/// let totp_key1 = otpauth_from_uri_qrcode("public/uri_qrcode_test.png");
/// if let Err(err) = totp_key1 {
/// panic!("{}", err);
/// }
/// let mut totp_key1 = totp_key1.unwrap();
///
/// let mut totp_key2 = TOTPKey {
/// name: "ACME Co:john.doe@email.com".to_string(),
/// issuer: Some("ACME Co".to_string()),
/// key: "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ".to_string(),
/// digits: 7,
/// time_step: 60,
/// hmac_type: HMACType::SHA256,
/// ..Default::default()
/// };
///
/// assert_eq!(totp_key1.get_name(), totp_key2.get_name());
/// assert_eq!(totp_key1.get_type(), totp_key2.get_type());
/// assert_eq!(totp_key1.get_code(), totp_key2.get_code());
/// ```
pub fn otpauth_from_uri_qrcode(path: &str) -> Result<Box<dyn Key>, Error> {
let uri_struct = URI::from_qr_code(path)?;
match uri_struct.key_type {
KeyType::HOTP => HOTPKey::from_uri_struct(&uri_struct),
KeyType::TOTP => TOTPKey::from_uri_struct(&uri_struct),
}
}
pub trait OptAuthKey {
/// to uri struct
fn to_uri_struct(&self) -> URI;
/// get the uri for the key
fn get_uri(&self) -> String {
self.to_uri_struct().to_string()
}
/// get issuer
fn get_issuer(&self) -> Option<&str>;
/// create the key from the uri struct
fn from_uri_struct(uri: &URI) -> Result<Box<dyn Key>, Error>;
}