Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump base32 #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rsa_stoken = ["stoken"]
[dependencies]
rust-crypto = "^0"
byteorder = "^1.4"
base32 = "^0"
base32 = "^0.5"
toml = "^0"
serde = { version = "^1.0", features = ["derive"] }
structopt = "^0"
Expand Down
10 changes: 1 addition & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::totp::TokenAlgorithm;
use crate::{TotpConfigError, TotpResult};
use serde::{self, Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize, Clone)]
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct Config {
totp: HashMap<String, TotpOptions>,
}
Expand Down Expand Up @@ -73,14 +73,6 @@ impl TotpOptions {
}
}

impl Default for Config {
fn default() -> Self {
Config {
totp: HashMap::new(),
}
}
}

impl Config {
pub fn lookup(&self, name: &str) -> TotpResult<&TotpOptions> {
Ok(self
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn add_totp_secret<P: AsRef<Path>>(
name: &str,
secret: String,
) -> TotpResult<()> {
base32::decode(base32::Alphabet::RFC4648 { padding: false }, &secret)
base32::decode(base32::Alphabet::Rfc4648 { padding: false }, &secret)
.expect("Invalid base32 OTP secret");

add_secret(&config, config_dir, name, secret, TokenAlgorithm::TotpSha1).map(|_| ())
Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn add_secret<P: AsRef<Path>>(
secret: String,
algorithm: TokenAlgorithm,
) -> TotpResult<Config> {
let totp_options = secrets::store_secret(&name, &secret, algorithm)?;
let totp_options = secrets::store_secret(name, &secret, algorithm)?;
let mut config: Config = config.clone();
config.insert(name.to_string(), totp_options);
let string = toml::to_string(&config)?;
Expand Down
2 changes: 1 addition & 1 deletion src/otpcli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() -> Result<(), Box<dyn Error>> {
Ok(())
}
Command::AddSecret { name, secret } => {
otp::add_totp_secret(config, config_dir, &name, secret.replace(" ", ""))?;
otp::add_totp_secret(config, config_dir, &name, secret.replace(' ', ""))?;
Ok(())
}
#[cfg(feature = "ras_stoken")]
Expand Down
9 changes: 5 additions & 4 deletions src/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};

use super::secrets;

static ALPHABET: base32::Alphabet = base32::Alphabet::RFC4648 { padding: false };
static ALPHABET: base32::Alphabet = base32::Alphabet::Rfc4648 { padding: false };

/// [RFC6238-timestep]: https://tools.ietf.org/html/rfc6238#section-5.2
/// [RFC6238 recommended][RFC6238-timestep] time step duration of 30 seconds.
Expand All @@ -29,6 +29,7 @@ pub enum TokenAlgorithm {

impl Copy for TokenAlgorithm {}

#[allow(dead_code)]
trait AsDigest {
fn as_digest(&self) -> Box<dyn Digest>;
}
Expand Down Expand Up @@ -63,13 +64,13 @@ impl AsDigest for TokenAlgorithm {
///
/// ```
pub fn standard_totp(name: &str, options: &TotpOptions) -> TotpResult<String> {
let secret = secrets::get_secret(name, &options)?;
let secret = secrets::get_secret(name, options)?;
generate_sha1_code(secret)
}

/// Cleans a base32 secret by removing spaces and making sure it's upper-cased.
pub fn clean_secret(secret: &str) -> String {
secret.replace(" ", "").to_uppercase()
secret.replace(' ', "").to_uppercase()
}

/// Generate a SHA1 TOTP code
Expand All @@ -91,7 +92,7 @@ pub fn generate_sha1_code(secret: String) -> TotpResult<String> {
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Can't get time since UNIX_EPOCH?");

let clean_secret = secret.replace(" ", "").to_uppercase();
let clean_secret = secret.replace(' ', "").to_uppercase();
let secret = base32::decode(ALPHABET, &clean_secret)
.ok_or(TotpError("Failed to decode secret from base32"))?;

Expand Down