Skip to content

Commit

Permalink
Rewrite Google Authenticator converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Mar 7, 2023
1 parent 7b10507 commit 97d55f1
Show file tree
Hide file tree
Showing 14 changed files with 1,456 additions and 397 deletions.
638 changes: 389 additions & 249 deletions core/Cargo.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ keyring = "1.2.0"
once_cell = "1.15.0"
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/tauri-plugin-single-instance" }
rand = "0.8.5"
google_authenticator_converter = { path = "crates/google_authenticator_converter" }

[features]
# by default Tauri runs in production mode
Expand All @@ -41,3 +42,9 @@ panic = "abort" # Strip expensive panic clean-up logic
codegen-units = 1 # Compile crates one after another so the compiler can optimize better
lto = true # Enables link to optimizations
opt-level = "s" # Optimize for binary size

[workspace]

members = [
"crates/*",
]
1 change: 1 addition & 0 deletions core/crates/google_authenticator_converter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
117 changes: 117 additions & 0 deletions core/crates/google_authenticator_converter/Cargo.lock

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

14 changes: 14 additions & 0 deletions core/crates/google_authenticator_converter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "google_authenticator_converter"
version = "0.1.0"
edition = "2021"
license = "MIT"
description= "Extract name, secret and issuer from a Google Authenticator migration QR code"

[dependencies]
base32 = "0.4.0"
base64 = "0.21.0"
urlencoding = "2.1.2"
protobuf = { version = "2" }
serde_json = { version = "1"}
serde = {version = "1", features = ["derive"] }
7 changes: 7 additions & 0 deletions core/crates/google_authenticator_converter/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 Lőrik Levente

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions core/crates/google_authenticator_converter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# google_authenticator_converter

- Extract name, secret and issuer from a Google Authenticator migration QR code

### Based on:

[google-authenticator-extractor
](https://github.com/zhangyuan/google-authenticator-extractor)
62 changes: 62 additions & 0 deletions core/crates/google_authenticator_converter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use base64::{engine::general_purpose, Engine as _};
use protobuf::Message;
use serde::Serialize;

mod proto;
#[derive(Debug, Serialize)]
pub struct Account {
name: String,
secret: String,
issuer: String,
}

impl Account {
pub fn new(name: String, secret: String, mut issuer: String) -> Account {
// If the issuer is empty, use the name as the issuer.
if issuer.is_empty() {
issuer = name.clone()
}

Account {
name,
secret,
issuer,
}
}
}

pub fn process_data(string: &str) -> Vec<Account> {
let encoded_data = extract_data_from_uri(string).unwrap();
let decoded_data = general_purpose::STANDARD.decode(encoded_data).unwrap();

let migration_payload =
proto::google_auth::MigrationPayload::parse_from_bytes(decoded_data.as_slice()).unwrap();

let otp_parameters = migration_payload.otp_parameters.into_vec();

let alphabet = base32::Alphabet::RFC4648 { padding: false };

let payloads: Vec<Account> = otp_parameters
.into_iter()
.map(|a| {
Account::new(
a.name,
base32::encode(alphabet, a.secret.as_slice()),
a.issuer,
)
})
.collect();

return payloads;
}

pub fn extract_data_from_uri(raw: &str) -> Result<String, Box<dyn std::error::Error>> {
let mut split = raw.split("data=");

if let Some(encoded_data) = split.next() {
let s = urlencoding::decode(encoded_data)?;
Ok(s.to_string())
} else {
Err("No data found in URI".into())
}
}
Loading

0 comments on commit 97d55f1

Please sign in to comment.