Skip to content

Commit

Permalink
Add token, user-info to actix extractor
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias de Bruijn <[email protected]>
  • Loading branch information
TobiasDeBruijn committed Dec 31, 2023
1 parent 8c4c810 commit fb733c1
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions wilford_oauth/src/actix/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use actix_web::dev::Payload;
use actix_web::http::StatusCode;
use actix_web::{web, FromRequest, HttpRequest};
use std::future::Future;
use std::ops::Deref;
use std::pin::Pin;
use tracing::warn;
use crate::user_info::UserInfo;

/// Extractor for Actix-Web.
/// This middleware extracts the Bearer token (provided in the `Authorization` header)
Expand All @@ -17,17 +17,23 @@ use tracing::warn;
/// # Panics
/// If no [actix_web::web::Data<WilfordConfig>] is stored in [actix_web::App::app_data].
pub struct WilfordAuth {
token_info: TokenInfo,
pub token_info: TokenInfo,
pub token: String,
wilford_host: String,
}

impl Deref for WilfordAuth {
type Target = TokenInfo;
impl WilfordAuth {
pub fn has_scope<S: AsRef<str>>(&self, scope: S) -> bool {
self.token_info.has_scope(scope.as_ref())
}

fn deref(&self) -> &Self::Target {
&self.token_info
pub async fn user_info(&self) -> reqwest::Result<UserInfo> {
UserInfo::request_info(&self.wilford_host, &self.token).await
}

}


impl FromRequest for WilfordAuth {
type Error = OAuth2Error;
type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>;
Expand All @@ -50,7 +56,11 @@ impl FromRequest for WilfordAuth {
},
})?;

Ok(Self { token_info })
Ok(Self {
token_info,
token,
wilford_host: wilford.wilford.clone(),
})
})
}
}
Expand Down

0 comments on commit fb733c1

Please sign in to comment.