mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-23 15:51:06 +01:00
derive identity error impls
This commit is contained in:
parent
1ed893a08c
commit
441d604c00
@ -22,6 +22,7 @@ actix-session = "0.7"
|
|||||||
actix-utils = "3"
|
actix-utils = "3"
|
||||||
actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies"] }
|
actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies"] }
|
||||||
|
|
||||||
|
derive_more = "0.99.7"
|
||||||
futures-core = "0.3.7"
|
futures-core = "0.3.7"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
||||||
|
@ -1,156 +1,70 @@
|
|||||||
//! Failure modes of identity operations.
|
//! Failure modes of identity operations.
|
||||||
|
|
||||||
use std::fmt;
|
|
||||||
|
|
||||||
use actix_session::{SessionGetError, SessionInsertError};
|
use actix_session::{SessionGetError, SessionInsertError};
|
||||||
use actix_web::{cookie::time::error::ComponentRange, http::StatusCode, ResponseError};
|
use actix_web::{cookie::time::error::ComponentRange, http::StatusCode, ResponseError};
|
||||||
|
use derive_more::{Display, Error, From};
|
||||||
|
|
||||||
/// Error that can occur during login attempts.
|
/// Error that can occur during login attempts.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Display, Error, From)]
|
||||||
|
#[display(fmt = "{_0}")]
|
||||||
pub struct LoginError(SessionInsertError);
|
pub struct LoginError(SessionInsertError);
|
||||||
|
|
||||||
impl fmt::Display for LoginError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(f, "{}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for LoginError {
|
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
||||||
Some(&self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ResponseError for LoginError {
|
impl ResponseError for LoginError {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
StatusCode::UNAUTHORIZED
|
StatusCode::UNAUTHORIZED
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<SessionInsertError> for LoginError {
|
|
||||||
fn from(error: SessionInsertError) -> Self {
|
|
||||||
Self(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Error encountered when working with a session that has expired.
|
/// Error encountered when working with a session that has expired.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Display, Error)]
|
||||||
pub struct SessionExpiryError(ComponentRange);
|
#[display(fmt = "The given session has expired and is no longer valid")]
|
||||||
|
pub struct SessionExpiryError(#[error(not(source))] pub(crate) ComponentRange);
|
||||||
impl fmt::Display for SessionExpiryError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
f.write_str("The given session has expired and is no longer valid")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for SessionExpiryError {}
|
|
||||||
|
|
||||||
/// The identity information has been lost.
|
/// The identity information has been lost.
|
||||||
///
|
///
|
||||||
/// Seeing this error in user code indicates a bug in actix-identity.
|
/// Seeing this error in user code indicates a bug in actix-identity.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[display(
|
||||||
|
fmt = "The identity information in the current session has disappeared after having been \
|
||||||
|
successfully validated. This is likely to be a bug."
|
||||||
|
)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct LostIdentityError;
|
pub struct LostIdentityError;
|
||||||
|
|
||||||
impl fmt::Display for LostIdentityError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
f.write_str(
|
|
||||||
"The identity information in the current session has disappeared \
|
|
||||||
after having been successfully validated. This is likely to be a bug.",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for LostIdentityError {
|
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
||||||
Some(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// There is no identity information attached to the current session.
|
/// There is no identity information attached to the current session.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[display(fmt = "There is no identity information attached to the current session")]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct MissingIdentityError;
|
pub struct MissingIdentityError;
|
||||||
|
|
||||||
impl fmt::Display for MissingIdentityError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
f.write_str("There is no identity information attached to the current session.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for MissingIdentityError {
|
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
||||||
Some(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Errors that can occur while retrieving an identity.
|
/// Errors that can occur while retrieving an identity.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Display, Error, From)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum GetIdentityError {
|
pub enum GetIdentityError {
|
||||||
/// The session has expired.
|
/// The session has expired.
|
||||||
|
#[display(fmt = "{_0}")]
|
||||||
SessionExpiryError(SessionExpiryError),
|
SessionExpiryError(SessionExpiryError),
|
||||||
|
|
||||||
/// No identity is found in a session.
|
/// No identity is found in a session.
|
||||||
|
#[display(fmt = "{_0}")]
|
||||||
MissingIdentityError(MissingIdentityError),
|
MissingIdentityError(MissingIdentityError),
|
||||||
|
|
||||||
/// Failed to accessing the session store.
|
/// Failed to accessing the session store.
|
||||||
|
#[display(fmt = "{_0}")]
|
||||||
SessionGetError(SessionGetError),
|
SessionGetError(SessionGetError),
|
||||||
|
|
||||||
/// Identity info was lost after being validated.
|
/// Identity info was lost after being validated.
|
||||||
///
|
///
|
||||||
/// Seeing this error indicates a bug in actix-identity.
|
/// Seeing this error indicates a bug in actix-identity.
|
||||||
|
#[display(fmt = "{_0}")]
|
||||||
LostIdentityError(LostIdentityError),
|
LostIdentityError(LostIdentityError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for GetIdentityError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::SessionExpiryError(err) => write!(f, "{err}"),
|
|
||||||
Self::MissingIdentityError(err) => write!(f, "{err}"),
|
|
||||||
Self::SessionGetError(err) => write!(f, "{err}"),
|
|
||||||
Self::LostIdentityError(err) => write!(f, "{err}"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for GetIdentityError {
|
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
||||||
match self {
|
|
||||||
Self::SessionExpiryError(err) => Some(err),
|
|
||||||
Self::MissingIdentityError(err) => Some(err),
|
|
||||||
Self::SessionGetError(err) => Some(err),
|
|
||||||
Self::LostIdentityError(err) => Some(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ResponseError for GetIdentityError {
|
impl ResponseError for GetIdentityError {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
StatusCode::UNAUTHORIZED
|
match self {
|
||||||
}
|
Self::LostIdentityError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
}
|
_ => StatusCode::UNAUTHORIZED,
|
||||||
|
}
|
||||||
impl From<LostIdentityError> for GetIdentityError {
|
|
||||||
fn from(error: LostIdentityError) -> Self {
|
|
||||||
Self::LostIdentityError(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<MissingIdentityError> for GetIdentityError {
|
|
||||||
fn from(error: MissingIdentityError) -> Self {
|
|
||||||
Self::MissingIdentityError(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<ComponentRange> for GetIdentityError {
|
|
||||||
fn from(error: ComponentRange) -> Self {
|
|
||||||
Self::SessionExpiryError(SessionExpiryError(error))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<SessionGetError> for GetIdentityError {
|
|
||||||
fn from(source: SessionGetError) -> Self {
|
|
||||||
Self::SessionGetError(source)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,9 @@ use actix_web::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::LogoutBehaviour,
|
config::LogoutBehaviour,
|
||||||
error::{GetIdentityError, LoginError, LostIdentityError, MissingIdentityError},
|
error::{
|
||||||
|
GetIdentityError, LoginError, LostIdentityError, MissingIdentityError, SessionExpiryError,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A verified user identity. It can be used as a request extractor.
|
/// A verified user identity. It can be used as a request extractor.
|
||||||
@ -212,7 +214,8 @@ impl Identity {
|
|||||||
.session
|
.session
|
||||||
.get(LOGIN_UNIX_TIMESTAMP_KEY)?
|
.get(LOGIN_UNIX_TIMESTAMP_KEY)?
|
||||||
.map(OffsetDateTime::from_unix_timestamp)
|
.map(OffsetDateTime::from_unix_timestamp)
|
||||||
.transpose()?)
|
.transpose()
|
||||||
|
.map_err(SessionExpiryError)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn last_visited_at(&self) -> Result<Option<OffsetDateTime>, GetIdentityError> {
|
pub(crate) fn last_visited_at(&self) -> Result<Option<OffsetDateTime>, GetIdentityError> {
|
||||||
@ -221,7 +224,8 @@ impl Identity {
|
|||||||
.session
|
.session
|
||||||
.get(LAST_VISIT_UNIX_TIMESTAMP_KEY)?
|
.get(LAST_VISIT_UNIX_TIMESTAMP_KEY)?
|
||||||
.map(OffsetDateTime::from_unix_timestamp)
|
.map(OffsetDateTime::from_unix_timestamp)
|
||||||
.transpose()?)
|
.transpose()
|
||||||
|
.map_err(SessionExpiryError)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_last_visited_at(&self) -> Result<(), LoginError> {
|
pub(crate) fn set_last_visited_at(&self) -> Result<(), LoginError> {
|
||||||
|
Loading…
Reference in New Issue
Block a user