Skip to content

Commit dd900dd

Browse files
authored
Transition to complete after using downstream code (#393)
1 parent 60e08ee commit dd900dd

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

v-api/src/context/login.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ where
8484
Ok(attempts.pop())
8585
}
8686

87+
pub async fn complete_login_attempt(
88+
&self,
89+
attempt: LoginAttempt,
90+
) -> Result<LoginAttempt, StoreError> {
91+
let mut attempt: NewLoginAttempt = attempt.into();
92+
attempt.attempt_state = LoginAttemptState::Complete;
93+
LoginAttemptStore::upsert(&*self.storage, attempt).await
94+
}
95+
8796
pub async fn fail_login_attempt(
8897
&self,
8998
attempt: LoginAttempt,

v-api/src/endpoints/login/oauth/code.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ where
505505
tracing::debug!("Authorized code exchange");
506506

507507
// Lookup the request assigned to this code
508-
let attempt = ctx
508+
let mut attempt = ctx
509509
.login
510510
.get_login_attempt_for_code(&body.code)
511511
.await
@@ -533,6 +533,24 @@ where
533533

534534
tracing::debug!("Retrieved user information from remote provider");
535535

536+
// During fetch_user_info we revoke any downstream codes if possible, therefore At this point we
537+
// consider the login attempt to be consumed and can no longer be used. We state transition to
538+
// complete, even though we may fail further along in the handler. If a failure occurs then the
539+
// user will need to re-authenticate.
540+
attempt = ctx
541+
.login
542+
.complete_login_attempt(attempt)
543+
.await
544+
.map_err(|err| {
545+
tracing::error!(?err, "Failed to complete login attempt");
546+
OAuthError {
547+
error: OAuthErrorCode::ServerError,
548+
error_description: Some("An unexpected error occurred".to_string()),
549+
error_uri: None,
550+
state: None,
551+
}
552+
})?;
553+
536554
// Register this user as an API user if needed
537555
let (api_user_info, api_user_provider) = ctx
538556
.register_api_user(&ctx.builtin_registration_user(), info)

0 commit comments

Comments
 (0)