Skip to content

Commit ecfad0e

Browse files
Merge pull request #55 from brighthive/HIVE-1298-force-log-out
feat: Expire and revoke deactivated user tokens (Hive 1298)
2 parents 2ce1ffe + 47b5360 commit ecfad0e

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

authserver/api/user.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ def _deactivate(self, user_id: str):
197197
user.can_login = False
198198
user.date_last_updated = datetime.utcnow()
199199

200+
tokens = OAuth2Token.query.filter_by(user_id=user.id).all()
201+
for token in tokens:
202+
token.revoked = True
203+
token.expires_in = 0
204+
200205
clients = OAuth2Client.query.filter_by(user_id=user_id).all()
201206
for client in clients:
202207
client.client_secret = None

tests/api/test_user_resource.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_post_user_deactivate_invalid_user_id(self, mocker, client):
154154
equal("No resource with identifier '123bad' found.")
155155
)
156156

157-
def test_post_user_deactivate(self, mocker, client, user, oauth_client):
157+
def test_post_user_deactivate(self, mocker, client, user, oauth_client, oauth_token):
158158
mocker.patch(
159159
"authlib.integrations.flask_oauth2.ResourceProtector.acquire_token",
160160
return_value=True,
@@ -175,6 +175,10 @@ def test_post_user_deactivate(self, mocker, client, user, oauth_client):
175175
"/clients/{}".format(oauth_client.id), headers={})
176176
expect(response.json["response"]["client_secret"]).to(be(None))
177177

178+
# Assert that the user's token has been revoked and expired
179+
expect(oauth_token.revoked).to(be(True))
180+
expect(oauth_token.is_access_token_expired()).to(be(True))
181+
178182
# Clean up (n.b., clean up should happen in the conftest – between each test.)
179183
client.delete(f"/users/{user.id}", headers={})
180184

tests/conftest.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import json
44
import os
5-
from time import sleep
5+
from time import sleep, time
66
from uuid import uuid4
77

88
import pytest
@@ -13,7 +13,7 @@
1313

1414
from authserver import create_app
1515
from authserver.config import ConfigurationFactory
16-
from authserver.db import db, User, OAuth2Client
16+
from authserver.db import db, User, OAuth2Client, OAuth2Token
1717
from authserver.utilities import PostgreSQLContainer
1818

1919

@@ -113,3 +113,18 @@ def oauth_client(user):
113113
db.session.add(oauth_client)
114114

115115
return oauth_client
116+
117+
118+
@pytest.fixture
119+
def oauth_token(user):
120+
data = {
121+
"access_token": str(uuid4()).replace("-", ""),
122+
"issued_at": int(time()) - 865000,
123+
"expires_in": 965000,
124+
"user_id": user.id
125+
}
126+
token = OAuth2Token(**data)
127+
128+
db.session.add(token)
129+
130+
return token

0 commit comments

Comments
 (0)