Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(api-keys): avoid db scan in find_subject_by_key #4718

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions core/api-keys/src/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,34 +133,7 @@ impl Identities {
.collect::<Vec<_>>();
Ok((IdentityApiKeyId::from(record.id), record.subject_id, scopes))
} else {
// look up by encrypted key and set hashed_key
let record = sqlx::query!(
r#"WITH updated_key AS (
UPDATE identity_api_keys k
SET last_used_at = NOW(), hashed_key = digest($1, 'sha256')
FROM identities i
WHERE k.identity_id = i.id
AND k.revoked = false
AND k.encrypted_key = crypt($1, k.encrypted_key)
AND (k.expires_at > NOW() OR k.expires_at IS NULL)
RETURNING k.id, i.subject_id, k.scopes
)
SELECT id, subject_id, scopes FROM updated_key"#,
code
)
.fetch_optional(&self.pool)
.await?;

if let Some(record) = record {
let scopes = record
.scopes
.into_iter()
.map(|s| s.parse::<Scope>().expect("Invalid scope"))
.collect::<Vec<_>>();
Ok((IdentityApiKeyId::from(record.id), record.subject_id, scopes))
} else {
Err(IdentityError::NoActiveKeyFound)
}
Err(IdentityError::NoActiveKeyFound)
}
}

Expand Down
Loading