-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae6857a
commit b5bf10e
Showing
9 changed files
with
107 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::{ops::Deref, pin::Pin}; | ||
|
||
use actix_web::{FromRequest, HttpMessage, error::ErrorUnauthorized}; | ||
use futures_util::Future; | ||
|
||
use crate::models::dto_models::ResponseDTO; | ||
|
||
use super::models::AuthenticationInfo; | ||
|
||
pub struct Authenticated { | ||
value: AuthenticationInfo, | ||
} | ||
|
||
impl Authenticated { | ||
pub fn new(auth_info: AuthenticationInfo) -> Self { | ||
Authenticated { value: auth_info } | ||
} | ||
} | ||
|
||
impl Deref for Authenticated { | ||
type Target = AuthenticationInfo; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.value | ||
} | ||
} | ||
|
||
impl FromRequest for Authenticated { | ||
type Error = actix_web::Error; | ||
type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>> + Send>>; | ||
|
||
fn from_request(req: &actix_web::HttpRequest, _: &mut actix_web::dev::Payload) -> Self::Future { | ||
let auth_info = req.extensions().get::<AuthenticationInfo>().cloned(); | ||
|
||
return Box::pin(async { | ||
match auth_info { | ||
Some(auth_info) => Ok(Authenticated::new(auth_info)), | ||
None => { | ||
Err(ErrorUnauthorized(ResponseDTO::new("Unauthorized").message("Authentication required for this resource"))) | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
use actix_web::{Responder, HttpResponse}; | ||
use actix_web::{HttpResponse, Responder}; | ||
|
||
use crate::models::dto_models::ResponseDTO; | ||
|
||
pub mod healthcheck; | ||
|
||
pub async fn authenticated_route() -> impl Responder { | ||
log::error!("Authenticated route"); | ||
return HttpResponse::Ok().json(ResponseDTO::new("This is the authenticated route")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters