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]
impl LoginProvider for StaticLoginProvider {
async fn login(&self, username: &str, password: &str) -> Result<Credentials> {
tracing::debug!(user=%username, "login");
match self.users.get(username) {
None => bail!("User {} does not exist", username),
Some(u) => {
tracing::debug!(user=%username, "verify password");
if !verify_password(password, &u.password)? {
bail!("Wrong password");
}
tracing::debug!(user=%username, "fetch bucket");
let bucket = u
.bucket
.clone()
@ -43,6 +46,7 @@ impl LoginProvider for StaticLoginProvider {
"No bucket configured and no default bucket specieid"
))?;
tracing::debug!(user=%username, "fetch configuration");
let storage = StorageCredentials {
k2v_region: self.k2v_region.clone(),
s3_region: self.s3_region.clone(),
@ -51,6 +55,7 @@ impl LoginProvider for StaticLoginProvider {
bucket,
};
tracing::debug!(user=%username, "fetch keys");
let keys = match (&u.master_key, &u.secret_key) {
(Some(m), Some(s)) => {
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"),
};
tracing::debug!(user=%username, "logged");
Ok(Credentials { storage, keys })
}
}

View File

@ -26,8 +26,8 @@ impl Connection {
}
impl Service<Request> for Connection {
type Response = Response;
type Error = anyhow::Error;
type Future = BoxFuture<'static, Result<Self::Response>>;
type Error = boitalettres::errors::Error;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
@ -35,6 +35,7 @@ impl Service<Request> for Connection {
fn call(&mut self, req: Request) -> Self::Future {
tracing::debug!("Got request: {:#?}", req);
let mailstore = self.mailstore.clone();
Box::pin(async move {
use imap_codec::types::{
command::CommandBody,
@ -51,9 +52,22 @@ impl Service<Request> for Connection {
.with_body(body)
}
CommandBody::Login {
username: _,
password: _,
} => Response::ok("Logged in")?,
username,
password,
} => {
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.")?,
};