Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 20cb5dd

Browse files
committed
Have more granular errors on the refresh token grant
1 parent 0bb34ed commit 20cb5dd

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

crates/handlers/src/oauth2/token.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use serde::Serialize;
4848
use serde_with::{serde_as, skip_serializing_none};
4949
use thiserror::Error;
5050
use tracing::debug;
51+
use ulid::Ulid;
5152
use url::Url;
5253

5354
use super::{generate_id_token, generate_token_pair};
@@ -96,6 +97,18 @@ pub(crate) enum RouteError {
9697
#[error("invalid grant")]
9798
InvalidGrant,
9899

100+
#[error("refresh token not found")]
101+
RefreshTokenNotFound,
102+
103+
#[error("refresh token {0} is invalid")]
104+
RefreshTokenInvalid(Ulid),
105+
106+
#[error("session {0} is invalid")]
107+
SessionInvalid(Ulid),
108+
109+
#[error("client id mismatch: expected {expected}, got {actual}")]
110+
ClientIDMismatch { expected: Ulid, actual: Ulid },
111+
99112
#[error("policy denied the request")]
100113
DeniedByPolicy(Vec<mas_policy::Violation>),
101114

@@ -152,7 +165,12 @@ impl IntoResponse for RouteError {
152165
),
153166
),
154167
),
155-
Self::InvalidGrant | Self::GrantNotFound => (
168+
Self::InvalidGrant
169+
| Self::RefreshTokenNotFound
170+
| Self::RefreshTokenInvalid(_)
171+
| Self::SessionInvalid(_)
172+
| Self::ClientIDMismatch { .. }
173+
| Self::GrantNotFound => (
156174
StatusCode::BAD_REQUEST,
157175
Json(ClientError::from(ClientErrorCode::InvalidGrant)),
158176
),
@@ -422,21 +440,28 @@ async fn refresh_token_grant(
422440
.oauth2_refresh_token()
423441
.find_by_token(&grant.refresh_token)
424442
.await?
425-
.ok_or(RouteError::InvalidGrant)?;
443+
.ok_or(RouteError::RefreshTokenNotFound)?;
426444

427445
let session = repo
428446
.oauth2_session()
429447
.lookup(refresh_token.session_id)
430448
.await?
431449
.ok_or(RouteError::NoSuchOAuthSession)?;
432450

433-
if !refresh_token.is_valid() || !session.is_valid() {
434-
return Err(RouteError::InvalidGrant);
451+
if !refresh_token.is_valid() {
452+
return Err(RouteError::RefreshTokenInvalid(refresh_token.id));
453+
}
454+
455+
if !session.is_valid() {
456+
return Err(RouteError::SessionInvalid(session.id));
435457
}
436458

437459
if client.id != session.client_id {
438460
// As per https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
439-
return Err(RouteError::InvalidGrant);
461+
return Err(RouteError::ClientIDMismatch {
462+
expected: session.client_id,
463+
actual: client.id,
464+
});
440465
}
441466

442467
let ttl = site_config.access_token_ttl;

0 commit comments

Comments
 (0)