Skip to content

Commit

Permalink
Generate random App Passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Jun 30, 2024
1 parent 467a93c commit 96f87b4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ form_urlencoded = "1.1.0"
serde_urlencoded = "0.7.1"
totp-rs = { version = "5.5.1", features = ["otpauth", "qr", "gen_secret"] }
web-time = "1.1.0"
rand = "0.8.5"

[features]
demo = []
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use pages::{
account::{app_password::{AppPasswordCreate, AppPasswords}, mfa::ManageMfa},
account::{
app_password::{AppPasswordCreate, AppPasswords},
mfa::ManageMfa,
},
config::edit::DEFAULT_SETTINGS_URL,
manage::spam::{SpamTest, SpamTrain},
};
Expand Down
24 changes: 18 additions & 6 deletions src/pages/account/app_password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ use chrono_humanize::HumanTime;
use leptos::*;
use leptos_router::{use_navigate, use_query_map};
use pwhash::sha512_crypt;
use rand::Rng;
use serde::{Deserialize, Serialize};

use crate::{
components::{
form::{
button::Button,
input::{InputPassword, InputText},
input::InputText,
Form, FormButtonBar, FormElement, FormItem, FormSection,
},
icon::{IconAdd, IconTrash},
Expand Down Expand Up @@ -374,9 +375,20 @@ pub fn AppPasswordCreate() -> impl IntoView {

let (pending, set_pending) = create_signal(false);

let data = expect_context::<Arc<Schemas>>()
.build_form("app-password")
.into_signal();
let mut data = expect_context::<Arc<Schemas>>().build_form("app-password");

// Generate a random Application Password
let mut app_password = String::with_capacity(19);
for _ in 0..20 {
app_password.push(rand::thread_rng().gen_range(b'a'..=b'z') as char);
// Add a space every 4 characters
if app_password.len() % 5 == 0 {
app_password.push(' ');
}
}
data.set("password", app_password);

let data = data.into_signal();

let save_changes = create_action(move |(name, password): &(String, String)| {
let auth = auth.get();
Expand All @@ -390,7 +402,7 @@ pub fn AppPasswordCreate() -> impl IntoView {
.with_authorization(&auth)
.with_body(vec![AccountAuthRequest::AddAppPassword {
name: STANDARD.encode(format!("{}${}", name, Utc::now().to_rfc3339())),
password: sha512_crypt::hash(password).unwrap()
password: sha512_crypt::hash(password).unwrap(),
}])
.unwrap()
.send::<()>()
Expand All @@ -417,7 +429,7 @@ pub fn AppPasswordCreate() -> impl IntoView {
<InputText element=FormElement::new("name", data)/>
</FormItem>
<FormItem label="Password">
<InputPassword element=FormElement::new("password", data)/>
<InputText element=FormElement::new("password", data)/>
</FormItem>

</FormSection>
Expand Down

0 comments on commit 96f87b4

Please sign in to comment.