-
Notifications
You must be signed in to change notification settings - Fork 66
Revoke personal sessions when users are deactivated #5181
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
Changes from all commits
be40e1b
7d5e9b8
c5756d4
e648c8e
54c025f
80feaff
409f354
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -357,6 +357,73 @@ impl PersonalSessionRepository for PgPersonalSessionRepository<'_> { | |
| .map_err(DatabaseError::to_invalid_operation) | ||
| } | ||
|
|
||
| #[tracing::instrument( | ||
| name = "db.personal_session.revoke_bulk", | ||
| skip_all, | ||
| fields( | ||
| db.query.text, | ||
| ), | ||
| err, | ||
| )] | ||
| async fn revoke_bulk( | ||
| &mut self, | ||
| clock: &dyn Clock, | ||
| filter: PersonalSessionFilter<'_>, | ||
| ) -> Result<usize, Self::Error> { | ||
| let revoked_at = clock.now(); | ||
|
|
||
| let (sql, arguments) = Query::update() | ||
| .table(PersonalSessions::Table) | ||
| .value(PersonalSessions::RevokedAt, revoked_at) | ||
| .and_where( | ||
| Expr::col((PersonalSessions::Table, PersonalSessions::PersonalSessionId)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a chunky subquery. I assume you need this because of the JOIN; could you stick in a comment explaining that? Also we could be doing that in a CTE, but not convinced the code would be clearer
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added some comments yeah, it's a bit chunky, the sea-query doesn't help with that verbosity either, but hopefully the new comments make the intent clear |
||
| // Because filters apply to both the session and access token tables, | ||
| // Use a subquery to make it possible to use a JOIN | ||
| // onto the personal access token table. | ||
| .in_subquery( | ||
| Query::select() | ||
| .expr(Expr::col(( | ||
| PersonalSessions::Table, | ||
| PersonalSessions::PersonalSessionId, | ||
| ))) | ||
| .from(PersonalSessions::Table) | ||
| .left_join( | ||
| PersonalAccessTokens::Table, | ||
| Cond::all() | ||
| // Match session ID | ||
| .add( | ||
| Expr::col(( | ||
| PersonalSessions::Table, | ||
| PersonalSessions::PersonalSessionId, | ||
| )) | ||
| .eq(Expr::col(( | ||
| PersonalAccessTokens::Table, | ||
| PersonalAccessTokens::PersonalSessionId, | ||
| ))), | ||
| ) | ||
| // Only choose the active access token for each session | ||
| .add( | ||
| Expr::col(( | ||
| PersonalAccessTokens::Table, | ||
| PersonalAccessTokens::RevokedAt, | ||
| )) | ||
| .is_null(), | ||
| ), | ||
| ) | ||
| .apply_filter(filter) | ||
| .take(), | ||
| ), | ||
| ) | ||
| .build_sqlx(PostgresQueryBuilder); | ||
|
|
||
| let res = sqlx::query_with(&sql, arguments) | ||
| .traced() | ||
| .execute(&mut *self.conn) | ||
| .await?; | ||
|
|
||
| Ok(res.rows_affected().try_into().unwrap_or(usize::MAX)) | ||
| } | ||
|
|
||
| #[tracing::instrument( | ||
| name = "db.personal_session.list", | ||
| skip_all, | ||
|
|
@@ -433,13 +500,15 @@ impl PersonalSessionRepository for PgPersonalSessionRepository<'_> { | |
| .left_join( | ||
| PersonalAccessTokens::Table, | ||
| Cond::all() | ||
| // Match session ID | ||
| .add( | ||
| Expr::col((PersonalSessions::Table, PersonalSessions::PersonalSessionId)) | ||
| .eq(Expr::col(( | ||
| PersonalAccessTokens::Table, | ||
| PersonalAccessTokens::PersonalSessionId, | ||
| ))), | ||
| ) | ||
| // Only choose the active access token for each session | ||
| .add( | ||
| Expr::col((PersonalAccessTokens::Table, PersonalAccessTokens::RevokedAt)) | ||
| .is_null(), | ||
|
|
@@ -477,13 +546,15 @@ impl PersonalSessionRepository for PgPersonalSessionRepository<'_> { | |
| .left_join( | ||
| PersonalAccessTokens::Table, | ||
| Cond::all() | ||
| // Match session ID | ||
| .add( | ||
| Expr::col((PersonalSessions::Table, PersonalSessions::PersonalSessionId)) | ||
| .eq(Expr::col(( | ||
| PersonalAccessTokens::Table, | ||
| PersonalAccessTokens::PersonalSessionId, | ||
| ))), | ||
| ) | ||
| // Only choose the active access token for each session | ||
| .add( | ||
| Expr::col((PersonalAccessTokens::Table, PersonalAccessTokens::RevokedAt)) | ||
| .is_null(), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure why not :D