-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config struct started. User struct for db somewhat finished. Will nee…
…d a separate type for Appstate.
- Loading branch information
1 parent
eeaf5c8
commit 15eb03a
Showing
4 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[cfg(feature = "database")] | ||
pub mod database; | ||
|
||
pub mod config; |