Skip to content

Commit

Permalink
Make sure to consume the device grant to avoid replays
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Dec 11, 2024
1 parent 1dbed14 commit dcf739c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion crates/handlers/src/oauth2/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,11 @@ async fn device_code_grant(
// Start the session
let mut session = repo
.oauth2_session()
.add_from_browser_session(rng, clock, client, &browser_session, grant.scope)
.add_from_browser_session(rng, clock, client, &browser_session, grant.scope.clone())
.await?;

repo.oauth2_device_code_grant()
.exchange(clock, grant, &session)
.await?;

// XXX: should we get the user agent from the device code grant instead?
Expand Down Expand Up @@ -1605,6 +1609,19 @@ mod tests {
// We asked for the openid scope, so we should have an ID token
assert!(response.id_token.is_some());

// Calling it again should fail
let request =
Request::post(mas_router::OAuth2TokenEndpoint::PATH).form(serde_json::json!({
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
"device_code": grant.device_code,
"client_id": client_id,
}));
let response = state.request(request).await;
response.assert_status(StatusCode::BAD_REQUEST);

let ClientError { error, .. } = response.json();
assert_eq!(error, ClientErrorCode::InvalidGrant);

// Do another grant and make it expire
let request = Request::post(mas_router::OAuth2DeviceAuthorizationEndpoint::PATH).form(
serde_json::json!({
Expand Down

0 comments on commit dcf739c

Please sign in to comment.