Skip to content

Commit 975e7de

Browse files
committed
Make sure to consume the device grant to avoid replays
1 parent 0fee1b7 commit 975e7de

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

crates/handlers/src/oauth2/token.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,11 @@ async fn device_code_grant(
692692
// Start the session
693693
let mut session = repo
694694
.oauth2_session()
695-
.add_from_browser_session(rng, clock, client, &browser_session, grant.scope)
695+
.add_from_browser_session(rng, clock, client, &browser_session, grant.scope.clone())
696+
.await?;
697+
698+
repo.oauth2_device_code_grant()
699+
.exchange(clock, grant, &session)
696700
.await?;
697701

698702
// XXX: should we get the user agent from the device code grant instead?
@@ -1348,6 +1352,19 @@ mod tests {
13481352
// We asked for the openid scope, so we should have an ID token
13491353
assert!(response.id_token.is_some());
13501354

1355+
// Calling it again should fail
1356+
let request =
1357+
Request::post(mas_router::OAuth2TokenEndpoint::PATH).form(serde_json::json!({
1358+
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
1359+
"device_code": grant.device_code,
1360+
"client_id": client_id,
1361+
}));
1362+
let response = state.request(request).await;
1363+
response.assert_status(StatusCode::BAD_REQUEST);
1364+
1365+
let ClientError { error, .. } = response.json();
1366+
assert_eq!(error, ClientErrorCode::InvalidGrant);
1367+
13511368
// Do another grant and make it expire
13521369
let request = Request::post(mas_router::OAuth2DeviceAuthorizationEndpoint::PATH).form(
13531370
serde_json::json!({

0 commit comments

Comments
 (0)