2022-07-21 03:50:22 +02:00
|
|
|
use std::{error::Error, fmt};
|
2018-05-30 16:43:39 +03:00
|
|
|
|
2022-07-21 03:50:22 +02:00
|
|
|
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
|
2018-05-30 16:43:39 +03:00
|
|
|
|
2022-07-21 03:50:22 +02:00
|
|
|
use crate::headers::www_authenticate::{Challenge, WwwAuthenticate};
|
2018-05-30 16:43:39 +03:00
|
|
|
|
2019-05-15 17:38:55 +03:00
|
|
|
/// Authentication error returned by authentication extractors.
|
2018-05-30 16:43:39 +03:00
|
|
|
///
|
2022-07-21 03:50:22 +02:00
|
|
|
/// Different extractors may extend `AuthenticationError` implementation in order to provide access
|
|
|
|
/// inner challenge fields.
|
2018-05-30 16:43:39 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct AuthenticationError<C: Challenge> {
|
|
|
|
challenge: C,
|
|
|
|
status_code: StatusCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: Challenge> AuthenticationError<C> {
|
2019-05-15 17:38:55 +03:00
|
|
|
/// Creates new authentication error from the provided `challenge`.
|
|
|
|
///
|
|
|
|
/// By default returned error will resolve into the `HTTP 401` status code.
|
2018-05-30 16:43:39 +03:00
|
|
|
pub fn new(challenge: C) -> AuthenticationError<C> {
|
|
|
|
AuthenticationError {
|
|
|
|
challenge,
|
|
|
|
status_code: StatusCode::UNAUTHORIZED,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 17:38:55 +03:00
|
|
|
/// Returns mutable reference to the inner challenge instance.
|
2018-05-30 16:43:39 +03:00
|
|
|
pub fn challenge_mut(&mut self) -> &mut C {
|
|
|
|
&mut self.challenge
|
|
|
|
}
|
|
|
|
|
2019-05-15 17:38:55 +03:00
|
|
|
/// Returns mutable reference to the inner status code.
|
|
|
|
///
|
2022-07-21 03:50:22 +02:00
|
|
|
/// Can be used to override returned status code, but by default this lib tries to stick to the
|
|
|
|
/// RFC, so it might be unreasonable.
|
2018-05-30 16:43:39 +03:00
|
|
|
pub fn status_code_mut(&mut self) -> &mut StatusCode {
|
|
|
|
&mut self.status_code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: Challenge> fmt::Display for AuthenticationError<C> {
|
2020-01-14 13:31:20 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-05-15 17:38:55 +03:00
|
|
|
fmt::Display::fmt(&self.status_code, f)
|
2018-05-30 16:43:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 03:50:22 +02:00
|
|
|
impl<C: Challenge + 'static> Error for AuthenticationError<C> {}
|
|
|
|
|
|
|
|
impl<C: Challenge + 'static> ResponseError for AuthenticationError<C> {
|
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
self.status_code
|
|
|
|
}
|
2018-05-30 16:43:39 +03:00
|
|
|
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
2022-07-21 03:50:22 +02:00
|
|
|
HttpResponse::build(self.status_code())
|
2021-03-21 23:50:26 +01:00
|
|
|
.insert_header(WwwAuthenticate(self.challenge.clone()))
|
2018-05-30 16:43:39 +03:00
|
|
|
.finish()
|
|
|
|
}
|
2020-05-18 21:30:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-03-29 11:46:13 +01:00
|
|
|
use actix_web::Error;
|
|
|
|
|
2020-05-18 21:30:14 -05:00
|
|
|
use super::*;
|
|
|
|
use crate::headers::www_authenticate::basic::Basic;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_status_code_is_preserved_across_error_conversions() {
|
2022-07-21 03:50:22 +02:00
|
|
|
let ae = AuthenticationError::new(Basic::default());
|
2020-05-18 21:30:14 -05:00
|
|
|
let expected = ae.status_code;
|
|
|
|
|
|
|
|
// Converting the AuthenticationError into a ResponseError should preserve the status code.
|
2022-07-21 03:50:22 +02:00
|
|
|
let err = Error::from(ae);
|
|
|
|
let res_err = err.as_response_error();
|
|
|
|
assert_eq!(expected, res_err.status_code());
|
2020-05-18 21:30:14 -05:00
|
|
|
}
|
2018-05-30 16:43:39 +03:00
|
|
|
}
|