WIP login

This commit is contained in:
Quentin 2022-06-03 14:00:19 +02:00
parent 8f3a34da33
commit 43f443c7a8
Signed by: quentin
GPG key ID: E9602264D639FF68
2 changed files with 25 additions and 5 deletions

View file

@ -29,12 +29,15 @@ impl StaticLoginProvider {
#[async_trait] #[async_trait]
impl LoginProvider for StaticLoginProvider { impl LoginProvider for StaticLoginProvider {
async fn login(&self, username: &str, password: &str) -> Result<Credentials> { async fn login(&self, username: &str, password: &str) -> Result<Credentials> {
tracing::debug!(user=%username, "login");
match self.users.get(username) { match self.users.get(username) {
None => bail!("User {} does not exist", username), None => bail!("User {} does not exist", username),
Some(u) => { Some(u) => {
tracing::debug!(user=%username, "verify password");
if !verify_password(password, &u.password)? { if !verify_password(password, &u.password)? {
bail!("Wrong password"); bail!("Wrong password");
} }
tracing::debug!(user=%username, "fetch bucket");
let bucket = u let bucket = u
.bucket .bucket
.clone() .clone()
@ -43,6 +46,7 @@ impl LoginProvider for StaticLoginProvider {
"No bucket configured and no default bucket specieid" "No bucket configured and no default bucket specieid"
))?; ))?;
tracing::debug!(user=%username, "fetch configuration");
let storage = StorageCredentials { let storage = StorageCredentials {
k2v_region: self.k2v_region.clone(), k2v_region: self.k2v_region.clone(),
s3_region: self.s3_region.clone(), s3_region: self.s3_region.clone(),
@ -51,6 +55,7 @@ impl LoginProvider for StaticLoginProvider {
bucket, bucket,
}; };
tracing::debug!(user=%username, "fetch keys");
let keys = match (&u.master_key, &u.secret_key) { let keys = match (&u.master_key, &u.secret_key) {
(Some(m), Some(s)) => { (Some(m), Some(s)) => {
let master_key = Key::from_slice(&base64::decode(m)?) let master_key = Key::from_slice(&base64::decode(m)?)
@ -69,6 +74,7 @@ impl LoginProvider for StaticLoginProvider {
_ => bail!("Either both master and secret key or none of them must be specified for user"), _ => bail!("Either both master and secret key or none of them must be specified for user"),
}; };
tracing::debug!(user=%username, "logged");
Ok(Credentials { storage, keys }) Ok(Credentials { storage, keys })
} }
} }

View file

@ -26,8 +26,8 @@ impl Connection {
} }
impl Service<Request> for Connection { impl Service<Request> for Connection {
type Response = Response; type Response = Response;
type Error = anyhow::Error; type Error = boitalettres::errors::Error;
type Future = BoxFuture<'static, Result<Self::Response>>; type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
@ -35,6 +35,7 @@ impl Service<Request> for Connection {
fn call(&mut self, req: Request) -> Self::Future { fn call(&mut self, req: Request) -> Self::Future {
tracing::debug!("Got request: {:#?}", req); tracing::debug!("Got request: {:#?}", req);
let mailstore = self.mailstore.clone();
Box::pin(async move { Box::pin(async move {
use imap_codec::types::{ use imap_codec::types::{
command::CommandBody, command::CommandBody,
@ -51,9 +52,22 @@ impl Service<Request> for Connection {
.with_body(body) .with_body(body)
} }
CommandBody::Login { CommandBody::Login {
username: _, username,
password: _, password,
} => Response::ok("Logged in")?, } => {
let (u, p) = match (String::try_from(username), String::try_from(password)) {
(Ok(u), Ok(p)) => (u, p),
_ => { return Response::bad("Invalid characters") }
};
tracing::debug!(user = %u, "command.login");
let creds = match mailstore.login_provider.login(&u, &p).await {
Err(_) => { return Response::no("[AUTHENTICATIONFAILED] Authentication failed.") }
Ok(c) => c,
};
Response::ok("Logged in")?
}
_ => Response::bad("Error in IMAP command received by server.")?, _ => Response::bad("Error in IMAP command received by server.")?,
}; };