use std::collections::HashMap;
use std::fmt;
use std::sync::Mutex;
use async_trait::async_trait;
use base64::Engine;
use serde_json::{Map, Value};
use crate::auth::{AuthContext, SessionValidator};
use crate::identity::{
Authenticated, Authenticator, AuthenticatorChain, AuthnRejection, Issuer, Presentation,
Principal,
};
use crate::oidc::{Audience, AudienceClaim, OidcConfig, OidcError, OidcValidator, VerifiedToken};
use crate::IssuerUrl;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ProviderName(String);
impl ProviderName {
pub fn try_new(name: impl Into<String>) -> Result<Self, AdapterError> {
let name = name.into();
if crate::identity::valid_provider_name(&name) {
Ok(Self(name))
} else {
Err(AdapterError::ProviderName(name))
}
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for ProviderName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AdapterError {
ProviderName(String),
Issuer(String),
Audience(String),
ServiceUrl(String),
DuplicateProvider(String),
}
impl fmt::Display for AdapterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ProviderName(n) => {
write!(f, "provider name `{n}` must be 1-32 of a-z, 0-9 and -")
}
Self::Issuer(i) => write!(f, "issuer `{i}` is not an https URL (or http on loopback)"),
Self::Audience(a) => write!(f, "audience `{a}` is not valid"),
Self::ServiceUrl(u) => write!(f, "service URL `{u}` does not parse"),
Self::DuplicateProvider(n) => {
write!(f, "two adapters are named `{n}`: each provider needs its own name")
}
}
}
}
impl std::error::Error for AdapterError {}
pub const WORKOS_API: &str = "https://api.workos.com";
#[derive(Debug, Clone, Default)]
pub struct ClaimMap {
pub name: Vec<String>,
pub roles: Option<String>,
}
#[derive(Debug, Clone)]
pub enum Namespace {
Idp,
Oidc(ProviderName),
}
pub struct OidcAdapter {
provider: ProviderName,
namespace: Namespace,
claims: ClaimMap,
issuer: String,
validator: OidcValidator,
}
impl OidcAdapter {
pub fn with_validator(
provider: ProviderName,
namespace: Namespace,
claims: ClaimMap,
validator: OidcValidator,
) -> Self {
let issuer = validator
.config()
.issuer()
.as_url()
.as_str()
.trim_end_matches('/')
.to_string();
Self { provider, namespace, claims, issuer, validator }
}
pub fn plexus_idp_with(validator: OidcValidator) -> Self {
Self::with_validator(
ProviderName("plexus-idp".into()),
Namespace::Idp,
ClaimMap::default(),
validator,
)
}
pub fn auth0_with(validator: OidcValidator, roles_claim: Option<&str>) -> Self {
Self::with_validator(
ProviderName("auth0".into()),
Namespace::Oidc(ProviderName("auth0".into())),
ClaimMap {
name: vec!["nickname".into(), "name".into(), "email".into()],
roles: roles_claim.map(str::to_string),
},
validator,
)
}
pub fn workos_with(validator: OidcValidator) -> Self {
Self::with_validator(
ProviderName("workos".into()),
Namespace::Oidc(ProviderName("workos".into())),
ClaimMap { name: vec!["email".into()], roles: None },
validator,
)
}
pub fn workos_config(client_id: &str, base: Option<&str>) -> Result<OidcConfig, AdapterError> {
let base = base.unwrap_or(WORKOS_API).trim_end_matches('/');
let issuer = format!("{base}/user_management/{client_id}");
Ok(Self::config(&issuer, client_id)?.with_audience_claim(AudienceClaim::ClientId))
}
pub fn config(issuer: &str, audience: &str) -> Result<OidcConfig, AdapterError> {
let url = issuer.parse().map_err(|_| AdapterError::Issuer(issuer.to_string()))?;
let issuer_url = IssuerUrl::try_new(url).map_err(|_| AdapterError::Issuer(issuer.to_string()))?;
let audience = Audience::try_new(audience).map_err(|_| AdapterError::Audience(audience.to_string()))?;
Ok(OidcConfig::new(issuer_url, audience))
}
#[cfg(feature = "oidc-reqwest")]
pub fn plexus_idp(issuer: &str, audience: &str) -> Result<Self, AdapterError> {
Ok(Self::plexus_idp_with(OidcValidator::new(Self::config(issuer, audience)?)))
}
#[cfg(feature = "oidc-reqwest")]
pub fn auth0(domain: &str, audience: &str, roles_claim: Option<&str>) -> Result<Self, AdapterError> {
Ok(Self::auth0_with(OidcValidator::new(Self::config(domain, audience)?), roles_claim))
}
#[cfg(feature = "oidc-reqwest")]
pub fn workos(client_id: &str, base: Option<&str>) -> Result<Self, AdapterError> {
Ok(Self::workos_with(OidcValidator::new(Self::workos_config(client_id, base)?)))
}
#[cfg(feature = "oidc-reqwest")]
pub fn generic(
provider: &str,
issuer: &str,
audience: &str,
claims: ClaimMap,
) -> Result<Self, AdapterError> {
let name = ProviderName::try_new(provider)?;
Ok(Self::with_validator(
name.clone(),
Namespace::Oidc(name),
claims,
OidcValidator::new(Self::config(issuer, audience)?),
))
}
pub fn provider(&self) -> &ProviderName {
&self.provider
}
fn identity(&self, token: &VerifiedToken) -> Result<Authenticated, AuthnRejection> {
let sub = token.user().user_id();
let principal = match &self.namespace {
Namespace::Idp => Principal::new(Issuer::Idp, sub),
Namespace::Oidc(provider) => Principal::oidc(provider.as_str(), sub),
}
.map_err(|e| AuthnRejection::Malformed(e.to_string()))?;
let roles = match &self.claims.roles {
Some(claim) => strings(token.claim(claim)),
None => token.roles().to_vec(),
};
let name = self
.claims
.name
.iter()
.find_map(|c| token.claim(c).and_then(Value::as_str))
.or_else(|| token.username())
.unwrap_or(sub)
.to_string();
let mut meta = Map::new();
meta.insert("name".into(), Value::String(name.clone()));
meta.insert("username".into(), Value::String(name));
meta.insert("provider".into(), Value::String(self.provider.to_string()));
meta.insert("auth_method".into(), Value::String("oidc".into()));
meta.insert("expires_at".into(), Value::from(token.user().expires_at()));
if let Some(t) = token.tenant_id() {
meta.insert("org_id".into(), Value::String(t.as_str().to_string()));
meta.insert("tenant_id".into(), Value::String(t.as_str().to_string()));
}
Ok(Authenticated::new(principal, token.session_id(), roles, Value::Object(meta)))
}
}
fn strings(value: Option<&Value>) -> Vec<String> {
value
.and_then(Value::as_array)
.map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect())
.unwrap_or_default()
}
fn unverified_issuer(token: &str) -> Option<String> {
let mut parts = token.split('.');
let (_, payload, _) = (parts.next()?, parts.next()?, parts.next()?);
let bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(payload).ok()?;
let claims: Value = serde_json::from_slice(&bytes).ok()?;
Some(claims.get("iss")?.as_str()?.trim_end_matches('/').to_string())
}
fn rejection(e: OidcError) -> AuthnRejection {
match e {
OidcError::Expired => AuthnRejection::Expired,
OidcError::InvalidSignature => AuthnRejection::SignatureInvalid,
OidcError::Discovery(_) | OidcError::JwksUnavailable(_) => AuthnRejection::Backend(e.to_string()),
other => AuthnRejection::Malformed(other.to_string()),
}
}
#[async_trait]
impl Authenticator for OidcAdapter {
fn name(&self) -> &str {
self.provider.as_str()
}
fn accepts(&self, presentation: &Presentation) -> bool {
match presentation {
Presentation::Bearer { token } => unverified_issuer(token).as_deref() == Some(self.issuer.as_str()),
_ => false,
}
}
async fn authenticate(&self, presentation: &Presentation) -> Result<Authenticated, AuthnRejection> {
if !self.accepts(presentation) {
return Err(AuthnRejection::unsupported(self.name(), presentation));
}
let Presentation::Bearer { token } = presentation else {
return Err(AuthnRejection::unsupported(self.name(), presentation));
};
let verified = self.validator.validate_token(token).await.map_err(rejection)?;
self.identity(&verified)
}
}
const WINDOW_PAST_SECS: u64 = 60;
const WINDOW_FUTURE_SECS: u64 = 30;
pub const NIP98_DEFAULT_LIFETIME_SECS: u64 = 12 * 60 * 60;
pub struct Nip98Adapter {
urls: Vec<url::Url>,
method: String,
roles: HashMap<String, Vec<String>>,
seen: Mutex<HashMap<String, u64>>,
lifetime_secs: u64,
}
impl Nip98Adapter {
pub fn new<I, S>(urls: I) -> Result<Self, AdapterError>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let urls = urls
.into_iter()
.map(|u| url::Url::parse(u.as_ref()).map_err(|_| AdapterError::ServiceUrl(u.as_ref().to_string())))
.collect::<Result<Vec<_>, _>>()?;
Ok(Self {
urls,
method: "GET".into(),
roles: HashMap::new(),
seen: Mutex::new(HashMap::new()),
lifetime_secs: NIP98_DEFAULT_LIFETIME_SECS,
})
}
pub fn with_lifetime(mut self, secs: u64) -> Self {
self.lifetime_secs = secs;
self
}
pub async fn authenticate_request(&self, token: &str, body: &[u8]) -> Result<Authenticated, AuthnRejection> {
self.verify(&Presentation::Bearer { token: token.to_string() }, Some(body))
}
pub fn for_method(mut self, method: &str) -> Self {
self.method = method.to_ascii_uppercase();
self
}
pub fn with_roles(mut self, pubkey_hex: &str, roles: Vec<String>) -> Self {
self.roles.insert(pubkey_hex.to_ascii_lowercase(), roles);
self
}
}
fn nip98_event(value: &str) -> Option<nostr::event::Event> {
let encoded = value.trim();
let encoded = encoded.strip_prefix("Nostr ").unwrap_or(encoded);
let engines = [
&base64::engine::general_purpose::STANDARD,
&base64::engine::general_purpose::URL_SAFE,
&base64::engine::general_purpose::URL_SAFE_NO_PAD,
];
let json = engines.iter().find_map(|e| e.decode(encoded).ok())?;
let event = nostr::event::Event::from_json(&json).ok()?;
(event.kind == nostr::event::Kind::HttpAuth).then_some(event)
}
#[async_trait]
impl Authenticator for Nip98Adapter {
fn name(&self) -> &str {
"nostr"
}
fn accepts(&self, presentation: &Presentation) -> bool {
matches!(presentation, Presentation::Bearer { token } if nip98_event(token).is_some())
}
async fn authenticate(&self, presentation: &Presentation) -> Result<Authenticated, AuthnRejection> {
self.verify(presentation, None)
}
}
impl Nip98Adapter {
fn verify(&self, presentation: &Presentation, body: Option<&[u8]>) -> Result<Authenticated, AuthnRejection> {
use nostr::nips::nip19::ToBech32;
use sha2::Digest;
let event = match presentation {
Presentation::Bearer { token } => nip98_event(token),
_ => None,
}
.ok_or_else(|| AuthnRejection::unsupported(self.name(), presentation))?;
let tag = |name: &str| {
event.tags.iter().find_map(|t| match t.as_slice() {
[n, v, ..] if n == name => Some(v.clone()),
_ => None,
})
};
let u = tag("u").and_then(|u| url::Url::parse(&u).ok());
if !u.as_ref().is_some_and(|u| self.urls.contains(u)) {
return Err(AuthnRejection::Malformed("NIP-98: `u` is not this service".into()));
}
if !tag("method").is_some_and(|m| m.eq_ignore_ascii_case(&self.method)) {
return Err(AuthnRejection::Malformed(format!("NIP-98: `method` is not {}", self.method)));
}
let now = nostr::types::Timestamp::now().as_secs();
let at = event.created_at.as_secs();
if at + WINDOW_PAST_SECS < now || at > now + WINDOW_FUTURE_SECS {
return Err(AuthnRejection::Expired);
}
event.verify().map_err(|_| AuthnRejection::SignatureInvalid)?;
if let Some(body) = body {
let want: String = sha2::Sha256::digest(body).iter().map(|b| format!("{b:02x}")).collect();
match tag("payload") {
Some(p) if p.eq_ignore_ascii_case(&want) => {}
Some(_) => return Err(AuthnRejection::Malformed("NIP-98: `payload` is not this body's hash".into())),
None if body.is_empty() => {}
None => return Err(AuthnRejection::Malformed("NIP-98: a body needs a `payload` tag".into())),
}
}
let id = event.id.to_hex();
{
let mut seen = self
.seen
.lock()
.map_err(|_| AuthnRejection::Backend("NIP-98 replay ledger unavailable".into()))?;
seen.retain(|_, until| *until > now);
if seen
.insert(id.clone(), now + WINDOW_PAST_SECS + WINDOW_FUTURE_SECS + 30)
.is_some()
{
return Err(AuthnRejection::Malformed("NIP-98: this event was already used".into()));
}
}
let hex = event.pubkey.to_hex();
let principal = Principal::new(Issuer::Nostr, hex.clone())
.map_err(|e| AuthnRejection::Malformed(e.to_string()))?;
let name = event.pubkey.to_bech32().unwrap_or_else(|_| hex.clone());
let mut meta = Map::new();
meta.insert("name".into(), Value::String(name.clone()));
meta.insert("username".into(), Value::String(name));
meta.insert("provider".into(), Value::String("nostr".into()));
meta.insert("auth_method".into(), Value::String("nip98".into()));
meta.insert("expires_at".into(), Value::from(now.saturating_add(self.lifetime_secs)));
let roles = self.roles.get(&hex).cloned().unwrap_or_default();
Ok(Authenticated::new(principal, id, roles, Value::Object(meta)))
}
}
pub const KEY_PROOF_PREFIX: &str = "plexus-key.";
pub const KEY_PROOF_CONTEXT: &str = "plexus-key-login/v1";
pub const KEY_PROOF_LIFETIME_SECS: i64 = 300;
const KEY_WINDOW_PAST_SECS: i64 = 60;
const KEY_WINDOW_FUTURE_SECS: i64 = 30;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct KeyProofJson {
v: u64,
key: String,
aud: String,
agent: String,
nonce: String,
ts: i64,
sig: String,
}
pub fn key_proof_message(aud: &str, agent: &str, nonce: &str, ts: i64) -> String {
format!("{KEY_PROOF_CONTEXT}\n{aud}\n{agent}\n{nonce}\n{ts}")
}
pub fn valid_agent_id(agent: &str) -> bool {
(1..=64).contains(&agent.len())
&& agent.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'))
}
pub fn key_principal(public_key: &[u8; 32]) -> Principal {
Principal::new(Issuer::Key, hex(public_key)).expect("32 bytes are 64 lowercase hex")
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
fn unhex_32(s: &str) -> Option<[u8; 32]> {
if s.len() != 64 {
return None;
}
let mut out = [0u8; 32];
for (i, chunk) in s.as_bytes().chunks(2).enumerate() {
out[i] = u8::from_str_radix(std::str::from_utf8(chunk).ok()?, 16).ok()?;
}
Some(out)
}
pub fn sign_key_proof(seed: &[u8; 32], aud: &str, agent: &str, nonce: &[u8; 32], ts: i64) -> String {
use ed25519_dalek::Signer;
let b64 = &base64::engine::general_purpose::URL_SAFE_NO_PAD;
let signing = ed25519_dalek::SigningKey::from_bytes(seed);
let nonce = b64.encode(nonce);
let sig = signing.sign(key_proof_message(aud, agent, &nonce, ts).as_bytes());
let proof = KeyProofJson {
v: 1,
key: hex(signing.verifying_key().as_bytes()),
aud: aud.to_string(),
agent: agent.to_string(),
nonce,
ts,
sig: b64.encode(sig.to_bytes()),
};
let json = serde_json::to_vec(&proof).expect("a key proof serializes");
format!("{KEY_PROOF_PREFIX}{}", b64.encode(json))
}
fn key_proof(value: &str) -> Option<KeyProofJson> {
let encoded = value.trim().strip_prefix(KEY_PROOF_PREFIX)?;
let json = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(encoded).ok()?;
serde_json::from_slice(&json).ok()
}
fn signed_challenge(key_id: &str, challenge: &str, signature: &str) -> Option<KeyProofJson> {
let mut lines = challenge.split('\n');
if lines.next()? != KEY_PROOF_CONTEXT {
return None;
}
let (aud, agent, nonce, ts) = (lines.next()?, lines.next()?, lines.next()?, lines.next()?);
if lines.next().is_some() {
return None;
}
let ts: i64 = ts.parse().ok()?;
if ts.to_string() != challenge.rsplit('\n').next()? {
return None;
}
Some(KeyProofJson {
v: 1,
key: key_id.to_string(),
aud: aud.to_string(),
agent: agent.to_string(),
nonce: nonce.to_string(),
ts,
sig: signature.to_string(),
})
}
type KeyAndNonce = ([u8; 32], [u8; 32]);
pub struct KeyProofAdapter {
audiences: Vec<String>,
seen: Mutex<HashMap<KeyAndNonce, i64>>,
clock: Box<dyn Fn() -> i64 + Send + Sync>,
}
impl fmt::Debug for KeyProofAdapter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("KeyProofAdapter").field("audiences", &self.audiences).finish()
}
}
fn system_now() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0)
}
fn audience_form(aud: &str) -> &str {
aud.trim_end_matches('/')
}
impl KeyProofAdapter {
pub fn new<I, S>(audiences: I) -> Result<Self, AdapterError>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut out = Vec::new();
for a in audiences {
let a = a.as_ref().trim();
match url::Url::parse(a) {
Ok(u) if matches!(u.scheme(), "ws" | "wss") => {}
_ => return Err(AdapterError::ServiceUrl(a.to_string())),
}
let a = audience_form(a).to_string();
if !out.contains(&a) {
out.push(a);
}
}
Ok(Self { audiences: out, seen: Mutex::new(HashMap::new()), clock: Box::new(system_now) })
}
pub fn with_clock(mut self, clock: impl Fn() -> i64 + Send + Sync + 'static) -> Self {
self.clock = Box::new(clock);
self
}
pub fn audiences(&self) -> &[String] {
&self.audiences
}
fn proof_of(presentation: &Presentation) -> Option<KeyProofJson> {
match presentation {
Presentation::Bearer { token } => key_proof(token),
Presentation::SignedChallenge { scheme, key_id, challenge, signature } if scheme == "ed25519" => {
signed_challenge(key_id, challenge, signature)
}
_ => None,
}
}
fn verify(&self, proof: KeyProofJson) -> Result<Authenticated, AuthnRejection> {
use ed25519_dalek::{Signature, VerifyingKey};
let b64 = &base64::engine::general_purpose::URL_SAFE_NO_PAD;
let bad = |why: &str| AuthnRejection::Malformed(format!("key proof: {why}"));
if proof.v != 1 {
return Err(bad("`v` is not 1"));
}
let principal = Principal::new(Issuer::Key, proof.key.clone()).map_err(|e| bad(&e.to_string()))?;
let key_bytes = unhex_32(&proof.key).ok_or_else(|| bad("`key` is not 32 bytes of hex"))?;
let key = VerifyingKey::from_bytes(&key_bytes).map_err(|_| bad("`key` is not an Ed25519 public key"))?;
if !valid_agent_id(&proof.agent) {
return Err(bad("`agent` must be 1-64 of A-Z, a-z, 0-9, `.`, `_` and `-`"));
}
let nonce: [u8; 32] = b64
.decode(&proof.nonce)
.ok()
.and_then(|n| n.try_into().ok())
.ok_or_else(|| bad("`nonce` is not 32 bytes of base64url"))?;
if !self.audiences.iter().any(|a| a == audience_form(&proof.aud)) {
return Err(bad("`aud` is not this server"));
}
let now = (self.clock)();
if proof.ts + KEY_WINDOW_PAST_SECS < now || proof.ts > now + KEY_WINDOW_FUTURE_SECS {
return Err(AuthnRejection::Expired);
}
let sig: [u8; 64] = b64
.decode(&proof.sig)
.ok()
.and_then(|s| s.try_into().ok())
.ok_or_else(|| bad("`sig` is not 64 bytes of base64url"))?;
let message = key_proof_message(&proof.aud, &proof.agent, &proof.nonce, proof.ts);
key.verify_strict(message.as_bytes(), &Signature::from_bytes(&sig))
.map_err(|_| AuthnRejection::SignatureInvalid)?;
{
let mut seen = self
.seen
.lock()
.map_err(|_| AuthnRejection::Backend("key proof replay ledger unavailable".into()))?;
seen.retain(|_, until| *until >= now);
if seen.insert((key_bytes, nonce), proof.ts + KEY_WINDOW_PAST_SECS).is_some() {
return Err(bad("this proof was already used"));
}
}
let mut meta = Map::new();
meta.insert("agent".into(), Value::String(proof.agent.clone()));
meta.insert("name".into(), Value::String(proof.agent));
meta.insert("provider".into(), Value::String("key".into()));
meta.insert("auth_method".into(), Value::String("key".into()));
meta.insert("expires_at".into(), Value::from(proof.ts + KEY_PROOF_LIFETIME_SECS));
Ok(Authenticated::new(principal, proof.nonce, Vec::new(), Value::Object(meta)))
}
}
#[async_trait]
impl Authenticator for KeyProofAdapter {
fn name(&self) -> &str {
"key"
}
fn accepts(&self, presentation: &Presentation) -> bool {
match presentation {
Presentation::Bearer { token } => token.trim().starts_with(KEY_PROOF_PREFIX),
Presentation::SignedChallenge { scheme, .. } => scheme == "ed25519",
_ => false,
}
}
async fn authenticate(&self, presentation: &Presentation) -> Result<Authenticated, AuthnRejection> {
if !self.accepts(presentation) {
return Err(AuthnRejection::unsupported(self.name(), presentation));
}
let proof = Self::proof_of(presentation)
.ok_or_else(|| AuthnRejection::Malformed("key proof: not plexus-key.<base64url JSON> of a proof".into()))?;
self.verify(proof)
}
}
pub struct Carriers {
chain: AuthenticatorChain,
cookies: Vec<String>,
}
impl Carriers {
pub fn cookie_and_bearer(chain: AuthenticatorChain) -> Self {
Self { chain, cookies: vec!["access_token".into(), "nostr".into()] }
}
pub fn bearer_only(chain: AuthenticatorChain) -> Self {
Self { chain, cookies: Vec::new() }
}
pub fn try_cookie_and_bearer(chain: AuthenticatorChain) -> Result<Self, AdapterError> {
unique_names(&chain)?;
Ok(Self::cookie_and_bearer(chain))
}
pub fn try_bearer_only(chain: AuthenticatorChain) -> Result<Self, AdapterError> {
unique_names(&chain)?;
Ok(Self::bearer_only(chain))
}
pub async fn authenticate(&self, value: &str) -> Result<Authenticated, AuthnRejection> {
if self.cookies.is_empty() && looks_like_cookie_header(value) {
return Err(AuthnRejection::Malformed(
"a Cookie header is not a credential here (bearer only)".into(),
));
}
let mut last = None;
for name in &self.cookies {
if let Some(token) = cookie(value, name) {
match self.chain.authenticate(&Presentation::Bearer { token: token.to_string() }).await {
Ok(ok) => return Ok(ok),
Err(e) => last = Some(e),
}
}
}
if let Some(e) = last {
return Err(e);
}
self.chain.authenticate(&Presentation::Bearer { token: value.trim().to_string() }).await
}
}
fn unique_names(chain: &AuthenticatorChain) -> Result<(), AdapterError> {
let mut names = std::collections::HashSet::new();
for name in chain.names() {
if !names.insert(name) {
return Err(AdapterError::DuplicateProvider(name.to_string()));
}
}
Ok(())
}
fn looks_like_cookie_header(value: &str) -> bool {
let v = value.trim();
v.contains(';') || v.trim_end_matches('=').contains('=')
}
fn cookie<'a>(header: &'a str, name: &str) -> Option<&'a str> {
header.split(';').find_map(|pair| {
let (n, v) = pair.trim().split_once('=')?;
(n == name && !v.is_empty()).then_some(v.trim())
})
}
#[async_trait]
impl SessionValidator for Carriers {
async fn validate(&self, value: &str) -> Option<AuthContext> {
match self.authenticate(value).await {
Ok(ok) => Some(ok.into_auth_context()),
Err(e) => {
tracing::debug!(target: "plexus::auth", error = %e, "no adapter admitted the caller");
None
}
}
}
}