Skip to content

Commit

Permalink
Config struct started. User struct for db somewhat finished. Will nee…
Browse files Browse the repository at this point in the history
…d a separate type for Appstate.
  • Loading branch information
Stridsvagn69420 committed Jan 21, 2024
1 parent eeaf5c8 commit 15eb03a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::fmt;
use std::convert::TryFrom;
use serde::{Serialize, Deserialize};
use serde_json;

#[derive(Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Config {
/// Server Name
///
/// The Name of the Cyrkensia server.
pub name: String,

/// Bind Address
///
/// The IP address and Port to bind to.
/// On Unix platforms, this can also be a path to a Unix Domain Socket.
pub addr: String,

/// Database Path
///
/// Path to the folder that contains the
/// `artists.json`, `albums.json`, `tracks.json`, `users.json` and `maintainers.json` database files.
pub database: String
}

impl TryFrom<&str> for Config {
type Error = serde_json::Error;

fn try_from(value: &str) -> Result<Self, Self::Error> {
serde_json::from_str(value)
}
}

impl TryFrom<String> for Config {
type Error = serde_json::Error;

fn try_from(value: String) -> Result<Self, Self::Error> {
serde_json::from_str(&value)
}
}

impl TryFrom<&[u8]> for Config {
type Error = serde_json::Error;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
serde_json::from_slice(value)
}
}

impl fmt::Display for Config {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}@{} ({})", self.name, self.addr, self.database)
}
}
4 changes: 3 additions & 1 deletion src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// Contains database types and reader function
// Contains database types and reader function
mod user;
pub use user::User;
38 changes: 38 additions & 0 deletions src/database/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::fmt;
use argon2::{Argon2, PasswordHasher};
use argon2::password_hash::rand_core::OsRng;
use argon2::password_hash::{Result, SaltString};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct User {
/// Username
///
/// The plaintext username.
pub username: String,

/// Hashed Password
///
/// The Argon2 PHC Hash as a [String].
pub password: String
}

impl User {
/// New User
///
/// Creates a new User Account from a username and plaintext password that gets hashed.
pub fn new(user: &str, passwd: &[u8]) -> Result<Self> {
let salt = SaltString::generate(&mut OsRng);
let hash = Argon2::default().hash_password(passwd, &salt)?;
Ok(Self {
username: user.to_string(),
password: hash.to_string()
})
}
}

impl fmt::Display for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.username, self.password)
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg(feature = "database")]
pub mod database;

pub mod config;

0 comments on commit 15eb03a

Please sign in to comment.