Start to implement OpenID authentication

This commit is contained in:
2024-04-25 19:25:08 +02:00
parent 48f24e6ca1
commit d8946eb462
12 changed files with 427 additions and 6 deletions

View File

@ -0,0 +1,47 @@
use actix_identity::Identity;
use actix_web::dev::Payload;
use actix_web::{Error, FromRequest, HttpMessage, HttpRequest};
use futures_util::future::{ready, Ready};
use std::fmt::Display;
pub struct AuthExtractor {
identity: Option<Identity>,
request: HttpRequest,
}
impl AuthExtractor {
/// Check whether the user is authenticated or not
pub fn is_authenticated(&self) -> bool {
self.identity.is_some()
}
/// Authenticate the user
pub fn authenticate(&self, id: impl Display) {
Identity::login(&self.request.extensions(), id.to_string())
.expect("Unable to set authentication!");
}
pub fn id(&self) -> Option<String> {
self.identity.as_ref().map(|i| i.id().unwrap())
}
pub fn sign_out(self) {
if let Some(i) = self.identity {
i.logout()
}
}
}
impl FromRequest for AuthExtractor {
type Error = Error;
type Future = Ready<Result<Self, Error>>;
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let identity: Option<Identity> = Identity::from_request(req, payload).into_inner().ok();
ready(Ok(Self {
identity,
request: req.clone(),
}))
}
}