Skip to content

Commit fb83c31

Browse files
fix(oauth): only remove the related tokens (#81677)
Before this fix, if someone deleted 1 authorization for one org we would delete all tokens for that app even if they're related to another org. This was not an issue before because our application authorization was user level, and becomes a problem now that some of them become (org, user) level.
1 parent 6e4c21a commit fb83c31

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/sentry/api/endpoints/api_authorizations.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def delete(self, request: Request) -> Response:
5050

5151
with outbox_context(transaction.atomic(using=router.db_for_write(ApiToken)), flush=False):
5252
for token in ApiToken.objects.filter(
53-
user_id=request.user.id, application=auth.application_id
53+
user_id=request.user.id,
54+
application=auth.application_id,
55+
scoping_organization_id=auth.organization_id,
5456
):
5557
token.delete()
5658

tests/sentry/api/endpoints/test_api_authorizations.py

+26
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,29 @@ def test_simple(self):
5151
self.get_success_response(authorization=auth.id, status_code=204)
5252
assert not ApiAuthorization.objects.filter(id=auth.id).exists()
5353
assert not ApiToken.objects.filter(id=token.id).exists()
54+
55+
def test_with_org(self):
56+
org1 = self.organization
57+
org2 = self.create_organization(owner=self.user, slug="test-org-2")
58+
app_with_org = ApiApplication.objects.create(
59+
name="test-app", owner=self.user, requires_org_level_access=True
60+
)
61+
org1_auth = ApiAuthorization.objects.create(
62+
application=app_with_org, user=self.user, organization_id=org1.id
63+
)
64+
org2_auth = ApiAuthorization.objects.create(
65+
application=app_with_org, user=self.user, organization_id=org2.id
66+
)
67+
org1_token = ApiToken.objects.create(
68+
application=app_with_org, user=self.user, scoping_organization_id=org1.id
69+
)
70+
org2_token = ApiToken.objects.create(
71+
application=app_with_org, user=self.user, scoping_organization_id=org2.id
72+
)
73+
74+
self.get_success_response(authorization=org1_auth.id, status_code=204)
75+
assert not ApiAuthorization.objects.filter(id=org1_auth.id).exists()
76+
assert not ApiToken.objects.filter(id=org1_token.id).exists()
77+
78+
assert ApiAuthorization.objects.filter(id=org2_auth.id).exists()
79+
assert ApiToken.objects.filter(id=org2_token.id).exists()

0 commit comments

Comments
 (0)