about summary refs log tree commit diff
path: root/knowing/src/lib.rs
blob: 2a92f25d81ff62546dabb9d3a8d6e66ca20313a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
mod db;
mod id;
mod password;

use std::fmt;

use db::AuthDatabase;
pub use id::RandomId;
use password::{IntoHashedPassword, PasswordVerifier};
use somethings::{
	DatabaseError, DatabaseHandle,
	auth::{AsUserId, Session, SessionId, User, UserId},
};

pub async fn create_table(db: &DatabaseHandle) -> Result<(), Error> {
	db.auth_create_tables().await?;
	Ok(())
}

pub async fn user_register(
	db: &DatabaseHandle,
	name: &str,
	plaintext_password: String,
) -> Result<User, Error> {
	let id = UserId::rand();
	let hash = plaintext_password.hash_password();
	db.insert_user(&id, name, hash).await?;

	Ok(User::new(id, name.to_owned()))
}

pub async fn user_login(
	db: &DatabaseHandle,
	name: &str,
	plaintext_password: String,
) -> Result<Session, Error> {
	let userauth = db.get_userauth_by_name(name).await?;
	userauth.hash().verify_password(plaintext_password)?;

	let session_id = SessionId::rand();
	db.insert_session(&session_id, userauth.user().userid())
		.await?;

	Ok(Session::new(session_id, userauth.into()))
}

pub async fn get_session(db: &DatabaseHandle, sid_str: &str) -> Result<Session, Error> {
	let sid = sid_str.parse().map_err(|_| Error::InvalidSessionId)?;
	db.get_session_by_id(&sid).await
}

pub async fn remove_session(db: &DatabaseHandle, sid_str: &str) -> Result<(), Error> {
	let sid = sid_str.parse().map_err(|_| Error::InvalidSessionId)?;
	db.delete_session_by_id(&sid).await
}

#[derive(Debug)]
pub enum Error {
	AuthenticationFailed,
	InvalidSessionId,
	UserIsAnonymous,
	UserIsRegistered,

	DatabaseError(DatabaseError),
}
impl std::error::Error for Error {}

impl From<DatabaseError> for Error {
	fn from(dbe: DatabaseError) -> Self {
		Self::DatabaseError(dbe)
	}
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::AuthenticationFailed => write!(f, "failed to authenticate user"),
			Self::InvalidSessionId => write!(f, "SessionId invalid"),
			Self::UserIsAnonymous => {
				write!(f, "expected a registered user, but UserId is anonymous")
			}
			Self::UserIsRegistered => {
				write!(f, "expected an anonymous user, but UserId is registered")
			}
			Self::DatabaseError(dbe) => write!(f, "{dbe}"),
		}
	}
}