-
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 3 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 |
|---|---|---|
|
|
@@ -32,6 +32,9 @@ pub enum RouteError { | |
| #[error("User not found")] | ||
| UserNotFound, | ||
|
|
||
| #[error("User is not active")] | ||
| UserDeactivated, | ||
|
|
||
| #[error("Invalid scope")] | ||
| InvalidScope, | ||
| } | ||
|
|
@@ -46,6 +49,7 @@ impl IntoResponse for RouteError { | |
| let status = match self { | ||
| Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, | ||
| Self::UserNotFound => StatusCode::NOT_FOUND, | ||
| Self::UserDeactivated => StatusCode::GONE, | ||
| Self::InvalidScope => StatusCode::BAD_REQUEST, | ||
| }; | ||
| (status, sentry_event_id, Json(error)).into_response() | ||
|
|
@@ -114,6 +118,10 @@ pub async fn handler( | |
| .await? | ||
| .ok_or(RouteError::UserNotFound)?; | ||
|
|
||
| if actor_user.deactivated_at.is_some() { | ||
|
||
| return Err(RouteError::UserDeactivated); | ||
| } | ||
|
|
||
| let scope: Scope = params.scope.parse().map_err(|_| RouteError::InvalidScope)?; | ||
|
|
||
| // Create the personal session | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -357,6 +357,68 @@ 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 |
||
| .in_subquery( | ||
| Query::select() | ||
| .expr(Expr::col(( | ||
| PersonalSessions::Table, | ||
| PersonalSessions::PersonalSessionId, | ||
| ))) | ||
| .from(PersonalSessions::Table) | ||
| .left_join( | ||
| PersonalAccessTokens::Table, | ||
| Cond::all() | ||
| .add( | ||
| Expr::col(( | ||
| PersonalSessions::Table, | ||
| PersonalSessions::PersonalSessionId, | ||
| )) | ||
| .eq(Expr::col(( | ||
| PersonalAccessTokens::Table, | ||
| PersonalAccessTokens::PersonalSessionId, | ||
| ))), | ||
| ) | ||
| .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, | ||
|
|
||
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