From 80fce3f115c0ba2fb064bfee8d81a2430dd07e64 Mon Sep 17 00:00:00 2001
From: 0xf8 <0xf8.dev@proton.me>
Date: Wed, 21 Jun 2023 18:08:23 -0400
Subject: [PATCH] add passwd command
---
src/dbtool/mod.rs | 5 +++++
src/dbtool/passwd.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 58 insertions(+)
diff --git a/src/dbtool/mod.rs b/src/dbtool/mod.rs
index 8814d01..51ee2da 100644
--- a/src/dbtool/mod.rs
+++ b/src/dbtool/mod.rs
@@ -17,6 +17,7 @@ use yggdrasil::*;
mod dump;
mod search;
+mod passwd;
mod add_account;
mod add_profile;
mod del_account;
@@ -54,6 +55,10 @@ pub async fn start(db: &Database) -> Result<()> {
"search" => search::Search::exec(args, &db).await?,
+ "setpasswd" |
+ "set-passwd" |
+ "passwd" => passwd::Passwd::exec(args, &db).await?,
+
"addaccount" |
"add-account" => add_account::AddAccount::exec(args, &db).await?,
diff --git a/src/dbtool/passwd.rs b/src/dbtool/passwd.rs
index e69de29..b9dc339 100644
--- a/src/dbtool/passwd.rs
+++ b/src/dbtool/passwd.rs
@@ -0,0 +1,53 @@
+/*
+ * Yggdrasil: Minecraft authentication server
+ * Copyright (C) 2023 0xf8.dev@proton.me
+ *
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with this program. If not, see .
+ */
+
+use std::str::FromStr;
+
+use anyhow::{bail, Result};
+use log::{info, warn};
+
+use yggdrasil::*;
+use yggdrasil::structs::account::Account;
+use yggdrasil::structs::profile::Profile;
+use yggdrasil::structs::token::Token;
+
+use crate::dbtool::Args;
+
+pub struct Passwd {}
+
+impl Passwd {
+ pub async fn exec(args: Args, db: &Database) -> Result<()> {
+ if args.arguments.len() < 1 { bail!("Not enough arguments. passwd ") }
+
+ // Get id
+ let id = i64::from_str(args.arguments.get(0).unwrap())?;
+
+ // Get account
+ let Some(account) = Account::from_id(db, id).await else {
+ bail!("Account(id = {id}) doesn't exist")
+ };
+
+ // Get password
+ let password = Input::password().await?;
+
+ info!("Password: ...{{{}}}", password.len());
+
+ // Change password
+ account.set_password(db, password).await?;
+
+ // Invalidate old tokens
+ Token::delete_all_from(db, account).await?;
+
+ info!("Changed successfully!");
+
+ Ok(())
+ }
+}