remove token from InvalidToken error

This commit is contained in:
ad hoc 2022-03-07 15:15:51 +01:00
parent 19da45c53b
commit ddd25bfe01
No known key found for this signature in database
GPG Key ID: 4F00A782990CC643
2 changed files with 10 additions and 15 deletions

View File

@ -5,7 +5,7 @@ pub enum AuthenticationError {
#[error("The Authorization header is missing. It must use the bearer authorization method.")] #[error("The Authorization header is missing. It must use the bearer authorization method.")]
MissingAuthorizationHeader, MissingAuthorizationHeader,
#[error("The provided API key is invalid.")] #[error("The provided API key is invalid.")]
InvalidToken(String), InvalidToken,
// Triggered on configuration error. // Triggered on configuration error.
#[error("An internal error has occurred. `Irretrievable state`.")] #[error("An internal error has occurred. `Irretrievable state`.")]
IrretrievableState, IrretrievableState,
@ -15,7 +15,7 @@ impl ErrorCode for AuthenticationError {
fn error_code(&self) -> Code { fn error_code(&self) -> Code {
match self { match self {
AuthenticationError::MissingAuthorizationHeader => Code::MissingAuthorizationHeader, AuthenticationError::MissingAuthorizationHeader => Code::MissingAuthorizationHeader,
AuthenticationError::InvalidToken(_) => Code::InvalidToken, AuthenticationError::InvalidToken => Code::InvalidToken,
AuthenticationError::IrretrievableState => Code::Internal, AuthenticationError::IrretrievableState => Code::Internal,
} }
} }

View File

@ -33,7 +33,7 @@ impl<P, D> GuardedData<P, D> {
P: Policy + 'static, P: Policy + 'static,
{ {
match Self::authenticate(auth, token, index).await? { match Self::authenticate(auth, token, index).await? {
(_, Some(filters)) => match data { Some(filters) => match data {
Some(data) => Ok(Self { Some(data) => Ok(Self {
data, data,
filters, filters,
@ -41,7 +41,7 @@ impl<P, D> GuardedData<P, D> {
}), }),
None => Err(AuthenticationError::IrretrievableState.into()), None => Err(AuthenticationError::IrretrievableState.into()),
}, },
(token, None) => Err(AuthenticationError::InvalidToken(token).into()), None => Err(AuthenticationError::InvalidToken.into()),
} }
} }
@ -49,7 +49,7 @@ impl<P, D> GuardedData<P, D> {
where where
P: Policy + 'static, P: Policy + 'static,
{ {
match Self::authenticate(auth, "", None).await?.1 { match Self::authenticate(auth, String::new(), None).await? {
Some(filters) => match data { Some(filters) => match data {
Some(data) => Ok(Self { Some(data) => Ok(Self {
data, data,
@ -62,18 +62,16 @@ impl<P, D> GuardedData<P, D> {
} }
} }
async fn authenticate<S>( async fn authenticate(
auth: AuthController, auth: AuthController,
token: S, token: String,
index: Option<String>, index: Option<String>,
) -> Result<(S, Option<AuthFilter>), ResponseError> ) -> Result<Option<AuthFilter>, ResponseError>
where where
P: Policy + 'static, P: Policy + 'static,
S: AsRef<str> + 'static + Send,
{ {
Ok(tokio::task::spawn_blocking(move || { Ok(tokio::task::spawn_blocking(move || {
let res = P::authenticate(auth, token.as_ref(), index.as_deref()); P::authenticate(auth, token.as_ref(), index.as_deref())
(token, res)
}) })
.await .await
.map_err(|e| ResponseError::from_msg(e.to_string(), Code::Internal))?) .map_err(|e| ResponseError::from_msg(e.to_string(), Code::Internal))?)
@ -114,10 +112,7 @@ impl<P: Policy + 'static, D: 'static + Clone> FromRequest for GuardedData<P, D>
index.map(String::from), index.map(String::from),
req.app_data::<D>().cloned(), req.app_data::<D>().cloned(),
)), )),
None => Box::pin(err(AuthenticationError::InvalidToken( None => Box::pin(err(AuthenticationError::InvalidToken.into())),
"unknown".to_string(),
)
.into())),
} }
} }
_otherwise => { _otherwise => {