-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improve introspection cache (#567)
Add introspection cache to axum and use `moka` as cache implementation. BREAKING CHANGE: This removes the "roles" enum from the introspected user. It is possible to achieve the same mechanism with the following work around: ```rust enum Role { Admin, Client } trait MyExtIntrospectedUser { fn role(&self, role: Role) -> Option<..>; } impl MyExtIntrospectedUser for IntrospectedUser { fn role(&self, role: Role) -> Option<..> { // convenience impl here } } ```
- Loading branch information
Showing
9 changed files
with
379 additions
and
117 deletions.
There are no files selected for viewing
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,30 +1,39 @@ | ||
use axum::extract::FromRef; | ||
use openidconnect::IntrospectionUrl; | ||
use std::sync::Arc; | ||
|
||
#[cfg(feature = "introspection_cache")] | ||
use crate::oidc::introspection::cache::IntrospectionCache; | ||
use crate::oidc::introspection::AuthorityAuthentication; | ||
|
||
/// State which must be present for extractor to work, | ||
/// compare [axum's official documentation](https://docs.rs/axum/0.6.4/axum/extract/struct.State.html#for-library-authors). | ||
/// Use [IntrospectionStateBuilder](super::IntrospectionStateBuilder) to configure the respective parameters. | ||
/// | ||
/// If a custom state is used, then [FromRef](axum::extract::FromRef) must be implemented, | ||
/// to make the necessary state available. | ||
/// | ||
/// ``` | ||
/// use axum::extract::FromRef; | ||
/// use zitadel::axum::introspection::IntrospectionState; | ||
/// struct UserState { | ||
/// introspection_state: IntrospectionState | ||
/// } | ||
/// | ||
/// impl FromRef<UserState> for IntrospectionState { | ||
/// fn from_ref(input: &UserState) -> Self { | ||
/// input.introspection_state.clone() | ||
/// } | ||
/// } | ||
#[derive(Clone, Debug)] | ||
pub struct IntrospectionState { | ||
pub(crate) config: IntrospectionConfig, | ||
pub(crate) config: Arc<IntrospectionConfig>, | ||
} | ||
|
||
impl IntrospectionState { | ||
pub fn config(&self) -> &IntrospectionConfig { | ||
&self.config | ||
} | ||
} | ||
|
||
/// Configuration that must be inject into the axum application state. Used by the | ||
/// [IntrospectionStateBuilder](super::IntrospectionStateBuilder). This struct is also used to create the [IntrospectionState](IntrospectionState) | ||
#[derive(Debug, Clone)] | ||
pub struct IntrospectionConfig { | ||
#[derive(Debug)] | ||
pub(crate) struct IntrospectionConfig { | ||
pub(crate) authority: String, | ||
pub(crate) authentication: AuthorityAuthentication, | ||
pub(crate) introspection_uri: IntrospectionUrl, | ||
} | ||
|
||
impl FromRef<IntrospectionState> for IntrospectionConfig { | ||
fn from_ref(input: &IntrospectionState) -> Self { | ||
input.config.clone() | ||
} | ||
#[cfg(feature = "introspection_cache")] | ||
pub(crate) cache: Option<Box<dyn IntrospectionCache>>, | ||
} |
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
Oops, something went wrong.