declare new authentication related errors

This commit is contained in:
marin postma 2021-06-24 16:53:20 +02:00
parent fbd58f2eec
commit 3b601f615a
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9
3 changed files with 34 additions and 22 deletions

View File

@ -10,23 +10,6 @@ use meilisearch_error::{Code, ErrorCode};
use milli::UserError; use milli::UserError;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, thiserror::Error)]
pub enum AuthenticationError {
#[error("You must have an authorization token")]
MissingAuthorizationHeader,
#[error("Invalid API key")]
InvalidToken(String),
}
impl ErrorCode for AuthenticationError {
fn error_code(&self) -> Code {
match self {
AuthenticationError::MissingAuthorizationHeader => Code::MissingAuthorizationHeader,
AuthenticationError::InvalidToken(_) => Code::InvalidToken,
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ResponseError { pub struct ResponseError {

View File

@ -0,0 +1,26 @@
use meilisearch_error::{Code, ErrorCode};
#[derive(Debug, thiserror::Error)]
pub enum AuthenticationError {
#[error("You must have an authorization token")]
MissingAuthorizationHeader,
#[error("Invalid API key")]
InvalidToken(String),
// Triggered on configuration error.
#[error("Irretrievable state")]
IrretrievableState,
#[error("Unknown authentication policy")]
UnknownPolicy,
}
impl ErrorCode for AuthenticationError {
fn error_code(&self) -> Code {
match self {
AuthenticationError::MissingAuthorizationHeader => Code::MissingAuthorizationHeader,
AuthenticationError::InvalidToken(_) => Code::InvalidToken,
AuthenticationError::IrretrievableState => Code::Internal,
AuthenticationError::UnknownPolicy => Code::Internal,
}
}
}

View File

@ -1,3 +1,5 @@
mod error;
use std::any::{Any, TypeId}; use std::any::{Any, TypeId};
use std::collections::HashMap; use std::collections::HashMap;
use std::marker::PhantomData; use std::marker::PhantomData;
@ -7,7 +9,8 @@ use actix_web::FromRequest;
use futures::future::err; use futures::future::err;
use futures::future::{ok, Ready}; use futures::future::{ok, Ready};
use crate::error::{AuthenticationError, ResponseError}; use crate::error::ResponseError;
use error::AuthenticationError;
macro_rules! create_policies { macro_rules! create_policies {
($($name:ident), *) => { ($($name:ident), *) => {
@ -151,7 +154,7 @@ impl<P: Policy + 'static, D: 'static + Clone> FromRequest for GuardedData<P, D>
data, data,
_marker: PhantomData, _marker: PhantomData,
}), }),
None => todo!("Data not configured"), None => err(AuthenticationError::IrretrievableState.into()),
}, },
AuthConfig::Auth(policies) => match policies.get::<P>() { AuthConfig::Auth(policies) => match policies.get::<P>() {
Some(policy) => match req.headers().get("x-meili-api-key") { Some(policy) => match req.headers().get("x-meili-api-key") {
@ -162,7 +165,7 @@ impl<P: Policy + 'static, D: 'static + Clone> FromRequest for GuardedData<P, D>
data, data,
_marker: PhantomData, _marker: PhantomData,
}), }),
None => todo!("Data not configured"), None => err(AuthenticationError::IrretrievableState.into()),
} }
} else { } else {
err(AuthenticationError::InvalidToken(String::from("hello")).into()) err(AuthenticationError::InvalidToken(String::from("hello")).into())
@ -170,10 +173,10 @@ impl<P: Policy + 'static, D: 'static + Clone> FromRequest for GuardedData<P, D>
} }
None => err(AuthenticationError::MissingAuthorizationHeader.into()), None => err(AuthenticationError::MissingAuthorizationHeader.into()),
}, },
None => todo!("no policy found"), None => err(AuthenticationError::UnknownPolicy.into()),
}, },
}, },
None => todo!(), None => err(AuthenticationError::IrretrievableState.into()),
} }
} }
} }