Skip to content

Commit bd773be

Browse files
committed
refactor(sdk): bearer extractors read the layer and refuse tokens through Bearer and AuthRejection
1 parent bdc4b4d commit bd773be

2 files changed

Lines changed: 40 additions & 37 deletions

File tree

‎crates/auth-sdk/src/axum/bearer.rs‎

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ impl AuthRejection {
233233
}
234234
}
235235

236+
/// Refuses a token `validator` accepted, which the handler cannot use: it lacks a
237+
/// scope, or its authentication is too weak or too old.
238+
fn refused<X>(error: TokenError, validator: &ResourceValidator<X>) -> Self {
239+
Self::new(Reason::Token(Arc::new(error)), Some(validator))
240+
}
241+
236242
/// The token error behind the rejection, if any.
237243
#[must_use]
238244
pub fn token_error(&self) -> Option<&TokenError> {
@@ -299,24 +305,27 @@ impl IntoResponse for AuthRejection {
299305
}
300306
}
301307

302-
/// Reads the layer's result for an extractor requiring `scope`.
303-
fn token<X: Clone + Send + Sync + 'static>(
304-
parts: &Parts,
305-
scope: Option<Scopes>,
306-
) -> Result<(AccessToken<Scopes, X>, &ResourceValidator<X>), AuthRejection> {
307-
let bearer = parts
308-
.extensions
309-
.get::<Arc<Bearer<X>>>()
310-
.ok_or_else(|| AuthRejection::new::<X>(Reason::MissingLayer, None))?;
311-
let validator = &bearer.validator;
312-
match &bearer.outcome {
313-
Outcome::Missing => Err(AuthRejection::new(Reason::Missing(scope), Some(validator))),
314-
Outcome::Malformed => Err(AuthRejection::new(Reason::InvalidRequest, Some(validator))),
315-
Outcome::Validated(Err(error)) => Err(AuthRejection::new(
316-
Reason::Token(error.clone()),
317-
Some(validator),
318-
)),
319-
Outcome::Validated(Ok(token)) => Ok((token.as_ref().clone(), validator)),
308+
impl<X: Clone + Send + Sync + 'static> Bearer<X> {
309+
/// Reads the layer's result for an extractor requiring `scope`, with the
310+
/// validator that produced it.
311+
fn token(
312+
parts: &Parts,
313+
scope: Option<Scopes>,
314+
) -> Result<(AccessToken<Scopes, X>, &ResourceValidator<X>), AuthRejection> {
315+
let bearer = parts
316+
.extensions
317+
.get::<Arc<Self>>()
318+
.ok_or_else(|| AuthRejection::new::<X>(Reason::MissingLayer, None))?;
319+
let validator = &bearer.validator;
320+
match &bearer.outcome {
321+
Outcome::Missing => Err(AuthRejection::new(Reason::Missing(scope), Some(validator))),
322+
Outcome::Malformed => Err(AuthRejection::new(Reason::InvalidRequest, Some(validator))),
323+
Outcome::Validated(Err(error)) => Err(AuthRejection::new(
324+
Reason::Token(error.clone()),
325+
Some(validator),
326+
)),
327+
Outcome::Validated(Ok(token)) => Ok((token.as_ref().clone(), validator)),
328+
}
320329
}
321330
}
322331

@@ -340,7 +349,7 @@ impl<St: Send + Sync, X: Clone + Send + Sync + 'static> FromRequestParts<St> for
340349
parts: &mut Parts,
341350
_: &St,
342351
) -> impl Future<Output = Result<Self, AuthRejection>> + Send {
343-
std::future::ready(token(parts, None).map(|(token, _)| Self(token)))
352+
std::future::ready(Bearer::token(parts, None).map(|(token, _)| Self(token)))
344353
}
345354
}
346355

@@ -370,13 +379,14 @@ impl<St: Send + Sync, S: ScopeSet, X: Clone + Send + Sync + 'static> FromRequest
370379
parts: &mut Parts,
371380
_: &St,
372381
) -> impl Future<Output = Result<Self, AuthRejection>> + Send {
373-
std::future::ready(
374-
token(parts, Some(S::required())).and_then(|(token, validator)| {
375-
token.require::<S>().map(Self).map_err(|error| {
376-
AuthRejection::new(Reason::Token(Arc::new(error)), Some(validator))
377-
})
378-
}),
379-
)
382+
std::future::ready(Bearer::token(parts, Some(S::required())).and_then(
383+
|(token, validator)| {
384+
token
385+
.into_scoped()
386+
.map(Self)
387+
.map_err(|error| AuthRejection::refused(error, validator))
388+
},
389+
))
380390
}
381391
}
382392

@@ -412,12 +422,10 @@ where
412422
parts: &mut Parts,
413423
_: &St,
414424
) -> impl Future<Output = Result<Self, AuthRejection>> + Send {
415-
std::future::ready(token(parts, None).and_then(|(token, validator)| {
425+
std::future::ready(Bearer::token(parts, None).and_then(|(token, validator)| {
416426
token
417427
.require_authentication(P::REQUIREMENT, validator.now())
418-
.map_err(|error| {
419-
AuthRejection::new(Reason::Token(Arc::new(error)), Some(validator))
420-
})?;
428+
.map_err(|error| AuthRejection::refused(error, validator))?;
421429
Ok(Self(token, PhantomData))
422430
}))
423431
}

‎crates/auth-sdk/src/axum/login/extract.rs‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,13 @@ impl SignedIn {
125125
/// The [`StepUp`] asking for `P`'s requirement.
126126
pub fn require<P: AuthenticationPolicy>(&self) -> Result<(), StepUp> {
127127
let authentication = &self.claims().authentication;
128-
if TokenError::require_authentication(
128+
TokenError::require_authentication(
129129
P::REQUIREMENT,
130130
authentication.acr.as_ref(),
131131
authentication.auth_time,
132132
self.checked_at,
133133
)
134-
.is_ok()
135-
{
136-
Ok(())
137-
} else {
138-
Err(self.step_up(P::REQUIREMENT))
139-
}
134+
.map_err(|_| self.step_up(P::REQUIREMENT))
140135
}
141136

142137
/// Answers an API's `insufficient_user_authentication` challenge (RFC 9470) with

0 commit comments

Comments
 (0)