mirror of
https://codeberg.org/pfzetto/axum-oidc
synced 2025-12-08 06:05:16 +01:00
Use AuthenticationContextClass, IssuerUrl and Scope instead of strings
This commit is contained in:
parent
542fe66313
commit
3acdd41a9a
3 changed files with 22 additions and 22 deletions
|
|
@ -1,7 +1,9 @@
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use http::Uri;
|
use http::Uri;
|
||||||
use openidconnect::{Audience, ClientId, ClientSecret, IssuerUrl};
|
use openidconnect::{
|
||||||
|
Audience, AuthenticationContextClass, ClientId, ClientSecret, IssuerUrl, Scope,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{error::Error, AdditionalClaims, Client, OidcClient, ProviderMetadata};
|
use crate::{error::Error, AdditionalClaims, Client, OidcClient, ProviderMetadata};
|
||||||
|
|
||||||
|
|
@ -21,8 +23,8 @@ pub struct Builder<AC: AdditionalClaims, Credentials, Client, HttpClient, Redire
|
||||||
http_client: HttpClient,
|
http_client: HttpClient,
|
||||||
redirect_url: RedirectUrl,
|
redirect_url: RedirectUrl,
|
||||||
end_session_endpoint: Option<Uri>,
|
end_session_endpoint: Option<Uri>,
|
||||||
scopes: Vec<Box<str>>,
|
scopes: Vec<Scope>,
|
||||||
auth_context_class: Option<Box<str>>,
|
auth_context_class: Option<AuthenticationContextClass>,
|
||||||
untrusted_audiences: Vec<Audience>,
|
untrusted_audiences: Vec<Audience>,
|
||||||
_ac: PhantomData<AC>,
|
_ac: PhantomData<AC>,
|
||||||
}
|
}
|
||||||
|
|
@ -41,7 +43,7 @@ impl<AC: AdditionalClaims> Builder<AC, (), (), (), ()> {
|
||||||
http_client: (),
|
http_client: (),
|
||||||
redirect_url: (),
|
redirect_url: (),
|
||||||
end_session_endpoint: None,
|
end_session_endpoint: None,
|
||||||
scopes: vec![Box::from("openid")],
|
scopes: vec![Scope::new("openid".to_string())],
|
||||||
auth_context_class: None,
|
auth_context_class: None,
|
||||||
untrusted_audiences: Vec::new(),
|
untrusted_audiences: Vec::new(),
|
||||||
_ac: PhantomData,
|
_ac: PhantomData,
|
||||||
|
|
@ -58,20 +60,20 @@ impl<AC: AdditionalClaims> OidcClient<AC> {
|
||||||
|
|
||||||
impl<AC: AdditionalClaims, CREDS, CLIENT, HTTP, RURL> Builder<AC, CREDS, CLIENT, HTTP, RURL> {
|
impl<AC: AdditionalClaims, CREDS, CLIENT, HTTP, RURL> Builder<AC, CREDS, CLIENT, HTTP, RURL> {
|
||||||
/// add a scope to existing (default) scopes
|
/// add a scope to existing (default) scopes
|
||||||
pub fn add_scope(mut self, scope: impl Into<Box<str>>) -> Self {
|
pub fn add_scope(mut self, scope: Scope) -> Self {
|
||||||
self.scopes.push(scope.into());
|
self.scopes.push(scope);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// replace scopes (including default)
|
/// replace scopes (including default)
|
||||||
pub fn with_scopes(mut self, scopes: impl Iterator<Item = impl Into<Box<str>>>) -> Self {
|
pub fn with_scopes(mut self, scopes: Vec<Scope>) -> Self {
|
||||||
self.scopes = scopes.map(|x| x.into()).collect::<Vec<_>>();
|
self.scopes = scopes;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// authenticate with Authentication Context Class Reference
|
/// authenticate with Authentication Context Class Reference
|
||||||
pub fn with_auth_context_class(mut self, acr: impl Into<Box<str>>) -> Self {
|
pub fn with_auth_context_class(mut self, acr: AuthenticationContextClass) -> Self {
|
||||||
self.auth_context_class = Some(acr.into());
|
self.auth_context_class = Some(acr);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -212,14 +214,13 @@ impl<AC: AdditionalClaims> Builder<AC, ClientCredentials, (), HttpClient, Redire
|
||||||
/// discover issuer details
|
/// discover issuer details
|
||||||
pub async fn discover(
|
pub async fn discover(
|
||||||
self,
|
self,
|
||||||
issuer: String,
|
issuer: IssuerUrl,
|
||||||
) -> Result<
|
) -> Result<
|
||||||
Builder<AC, ClientCredentials, OpenidconnectClient<AC>, HttpClient, RedirectUrl>,
|
Builder<AC, ClientCredentials, OpenidconnectClient<AC>, HttpClient, RedirectUrl>,
|
||||||
Error,
|
Error,
|
||||||
> {
|
> {
|
||||||
let issuer_url = IssuerUrl::new(issuer)?;
|
|
||||||
let http_client = self.http_client.0.clone();
|
let http_client = self.http_client.0.clone();
|
||||||
let provider_metadata = ProviderMetadata::discover_async(issuer_url, &http_client);
|
let provider_metadata = ProviderMetadata::discover_async(issuer, &http_client);
|
||||||
|
|
||||||
Self::manual(self, provider_metadata.await?)
|
Self::manual(self, provider_metadata.await?)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
src/lib.rs
10
src/lib.rs
|
|
@ -12,9 +12,9 @@ use openidconnect::{
|
||||||
CoreResponseMode, CoreResponseType, CoreRevocableToken, CoreRevocationErrorResponse,
|
CoreResponseMode, CoreResponseType, CoreRevocableToken, CoreRevocationErrorResponse,
|
||||||
CoreSubjectIdentifierType, CoreTokenIntrospectionResponse, CoreTokenType,
|
CoreSubjectIdentifierType, CoreTokenIntrospectionResponse, CoreTokenType,
|
||||||
},
|
},
|
||||||
AccessToken, Audience, ClientId, CsrfToken, EmptyExtraTokenFields, EndpointMaybeSet,
|
AccessToken, Audience, AuthenticationContextClass, ClientId, CsrfToken, EmptyExtraTokenFields,
|
||||||
EndpointNotSet, EndpointSet, IdTokenFields, Nonce, PkceCodeVerifier, RefreshToken,
|
EndpointMaybeSet, EndpointNotSet, EndpointSet, IdTokenFields, Nonce, PkceCodeVerifier,
|
||||||
StandardErrorResponse, StandardTokenResponse,
|
RefreshToken, Scope, StandardErrorResponse, StandardTokenResponse,
|
||||||
};
|
};
|
||||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||||
|
|
||||||
|
|
@ -102,12 +102,12 @@ pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
|
||||||
/// OpenID Connect Client
|
/// OpenID Connect Client
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct OidcClient<AC: AdditionalClaims> {
|
pub struct OidcClient<AC: AdditionalClaims> {
|
||||||
scopes: Vec<Box<str>>,
|
scopes: Vec<Scope>,
|
||||||
client_id: ClientId,
|
client_id: ClientId,
|
||||||
client: Client<AC>,
|
client: Client<AC>,
|
||||||
http_client: reqwest::Client,
|
http_client: reqwest::Client,
|
||||||
end_session_endpoint: Option<Uri>,
|
end_session_endpoint: Option<Uri>,
|
||||||
auth_context_class: Option<Box<str>>,
|
auth_context_class: Option<AuthenticationContextClass>,
|
||||||
untrusted_audiences: Vec<Audience>,
|
untrusted_audiences: Vec<Audience>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,8 @@ use tower_sessions::Session;
|
||||||
|
|
||||||
use openidconnect::{
|
use openidconnect::{
|
||||||
core::{CoreAuthenticationFlow, CoreErrorResponseType, CoreGenderClaim, CoreJsonWebKey},
|
core::{CoreAuthenticationFlow, CoreErrorResponseType, CoreGenderClaim, CoreJsonWebKey},
|
||||||
AccessToken, AccessTokenHash, AuthenticationContextClass, CsrfToken, IdTokenClaims,
|
AccessToken, AccessTokenHash, CsrfToken, IdTokenClaims, IdTokenVerifier, Nonce,
|
||||||
IdTokenVerifier, Nonce, OAuth2TokenResponse, PkceCodeChallenge, RefreshToken,
|
OAuth2TokenResponse, PkceCodeChallenge, RefreshToken,
|
||||||
RequestTokenError::ServerResponse,
|
RequestTokenError::ServerResponse,
|
||||||
Scope, TokenResponse, UserInfoClaims,
|
Scope, TokenResponse, UserInfoClaims,
|
||||||
};
|
};
|
||||||
|
|
@ -143,8 +143,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(acr) = oidcclient.auth_context_class {
|
if let Some(acr) = oidcclient.auth_context_class {
|
||||||
auth = auth
|
auth = auth.add_auth_context_value(acr);
|
||||||
.add_auth_context_value(AuthenticationContextClass::new(acr.into()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auth.set_pkce_challenge(pkce_challenge).url()
|
auth.set_pkce_challenge(pkce_challenge).url()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue