From fb775b16094c20f021a2285e8c29a828c6ec56a9 Mon Sep 17 00:00:00 2001 From: Andreas Axelsson Date: Mon, 7 Mar 2022 16:22:11 +0800 Subject: [PATCH 1/3] [auth] Provide a fallback mechanic for old-style logins - The identity storage model has changed and all logins should be using an identity marker. Old users that have been added before this, will not have the marker and while the idea is to migrate the IDs in the DB we still need a fallback to where these may have been created during the upgrade, or where the upgrade for some reason fail to take all the correct identities into account. --- driftbase/auth/authenticate.py | 15 +++++-- driftbase/tests/test_auth.py | 78 ++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/driftbase/auth/authenticate.py b/driftbase/auth/authenticate.py index b5f756b5..20b32465 100644 --- a/driftbase/auth/authenticate.py +++ b/driftbase/auth/authenticate.py @@ -20,7 +20,7 @@ def abort_unauthorized(description): def authenticate_with_provider(auth_info): """ Supported schemas: - provider: "uuid", provider_details: { key, secret } -> key + provider: "uuid", provider_details: { key, secret } -> uuid:key provider: "device_id", provider_details: { uuid:username, password } -> uuid:username provider: "user+pass", provider_details: { username, password } -> username provider: "viveport", provider_details: { username, password } -> viveport:username @@ -38,7 +38,8 @@ def authenticate_with_provider(auth_info): try: identity = authenticate('uuid:' + provider_details['key'], provider_details['secret'], - automatic_account_creation) + automatic_account_creation, + allow_fallback_to_non_identity=True) except KeyError: abort(http.client.BAD_REQUEST, "Bad Request. Missing expected argument.") @@ -86,7 +87,7 @@ def authenticate_with_provider(auth_info): return identity -def authenticate(username, password, automatic_account_creation=True): +def authenticate(username, password, automatic_account_creation=True, allow_fallback_to_non_identity=False): """basic authentication""" identity_type = "" create_roles = [] @@ -105,6 +106,14 @@ def authenticate(username, password, automatic_account_creation=True): .filter(UserIdentity.name == username) \ .first() + # attempt to locate a legacy non-specific version of the identity, in case it hasn't yet been migrated + if my_identity is None and not is_old and allow_fallback_to_non_identity: + legacy_identity = g.db.query(UserIdentity) \ + .filter(UserIdentity.name == lst[1]) \ + .first() + if legacy_identity is not None: + my_identity = legacy_identity + try: service_user = g.conf.tier.get('service_user') if not service_user and g.conf.tenant: diff --git a/driftbase/tests/test_auth.py b/driftbase/tests/test_auth.py index 17a42d17..9a53505c 100644 --- a/driftbase/tests/test_auth.py +++ b/driftbase/tests/test_auth.py @@ -1,6 +1,8 @@ import http.client as http_client +import jwt from mock import patch, MagicMock +from drift.core.extensions.jwt import JWT_ALGORITHM from drift.systesthelper import setup_tenant, remove_tenant from drift.utils import get_config from driftbase.systesthelper import DriftBaseTestCase @@ -264,3 +266,79 @@ def test_uuid_methods_resolve_to_same_user(self): self.assertEqual(user1['user_id'], user2['user_id']) self.assertEqual(user2['identity_id'], user3['identity_id']) self.assertEqual(user2['user_id'], user3['user_id']) + + def test_userpass_auth_fallback(self): + # Create or login + data1 = { + "username": "foo", + "password": "bar", + "automatic_account_creation": True + } + resp1 = self.post('/auth', data=data1, expected_status_code=http_client.OK).json() + data2 = { + "provider": "user+pass", + "username": "foo", + "password": "bar", + "automatic_account_creation": True + } + resp2 = self.post('/auth', data=data2, expected_status_code=http_client.OK).json() + data3 = { + "provider": "user+pass", + "provider_details": { + "username": "foo", + "password": "bar" + }, + "automatic_account_creation": True + } + resp3 = self.post('/auth', data=data3, expected_status_code=http_client.OK).json() + options = {"verify_signature": False} + token1 = jwt.decode(resp1['token'], algorithms=[JWT_ALGORITHM], options=options) + token2 = jwt.decode(resp2['token'], algorithms=[JWT_ALGORITHM], options=options) + token3 = jwt.decode(resp3['token'], algorithms=[JWT_ALGORITHM], options=options) + self.assertEqual(token1['user_id'], token2['user_id']) + self.assertEqual(token1['player_id'], token2['player_id']) + self.assertEqual(token1['identity_id'], token2['identity_id']) + self.assertEqual(token1['user_id'], token3['user_id']) + self.assertEqual(token1['player_id'], token3['player_id']) + self.assertEqual(token1['identity_id'], token3['identity_id']) + + def test_uuid_auth_fallback(self): + # Will be converted to user+pass + data1 = { + "username": "foo", + "password": "bar", + "automatic_account_creation": True + } + resp1 = self.post('/auth', data=data1, expected_status_code=http_client.OK).json() + # Will be treated as uuid + data2 = { + "provider": "uuid", + "provider_details": { + "key": "foo", + "secret": "bar" + }, + "username": "foo", + "password": "bar", + "automatic_account_creation": True + } + resp2 = self.post('/auth', data=data2, expected_status_code=http_client.OK).json() + # Will be treated as uuid + data3 = { + "provider": "uuid", + "provider_details": { + "key": "foo", + "secret": "bar" + }, + "automatic_account_creation": True + } + resp3 = self.post('/auth', data=data3, expected_status_code=http_client.OK).json() + options = {"verify_signature": False} + token1 = jwt.decode(resp1['token'], algorithms=[JWT_ALGORITHM], options=options) + token2 = jwt.decode(resp2['token'], algorithms=[JWT_ALGORITHM], options=options) + token3 = jwt.decode(resp3['token'], algorithms=[JWT_ALGORITHM], options=options) + self.assertEqual(token1['user_id'], token2['user_id']) + self.assertEqual(token1['player_id'], token2['player_id']) + self.assertEqual(token1['identity_id'], token2['identity_id']) + self.assertEqual(token1['user_id'], token3['user_id']) + self.assertEqual(token1['player_id'], token3['player_id']) + self.assertEqual(token1['identity_id'], token3['identity_id']) From 0b4a42c176b098df14b4fcbd6244fe3ef6302505 Mon Sep 17 00:00:00 2001 From: Andreas Axelsson Date: Mon, 7 Mar 2022 16:45:02 +0800 Subject: [PATCH 2/3] Migrate legacy identities on demand --- driftbase/auth/authenticate.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/driftbase/auth/authenticate.py b/driftbase/auth/authenticate.py index 20b32465..f2a7de9a 100644 --- a/driftbase/auth/authenticate.py +++ b/driftbase/auth/authenticate.py @@ -113,6 +113,9 @@ def authenticate(username, password, automatic_account_creation=True, allow_fall .first() if legacy_identity is not None: my_identity = legacy_identity + # migrate the identity to new-style on demand + my_identity.name = username + my_identity.identity_type = identity_type try: service_user = g.conf.tier.get('service_user') From 204d830ec18331bd18d1eef46bee1502b8489f47 Mon Sep 17 00:00:00 2001 From: Andreas Axelsson Date: Tue, 8 Mar 2022 23:55:19 +0800 Subject: [PATCH 3/3] Test user_name too for uuid fallback --- driftbase/tests/test_auth.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/driftbase/tests/test_auth.py b/driftbase/tests/test_auth.py index 9a53505c..c1476488 100644 --- a/driftbase/tests/test_auth.py +++ b/driftbase/tests/test_auth.py @@ -296,9 +296,11 @@ def test_userpass_auth_fallback(self): token2 = jwt.decode(resp2['token'], algorithms=[JWT_ALGORITHM], options=options) token3 = jwt.decode(resp3['token'], algorithms=[JWT_ALGORITHM], options=options) self.assertEqual(token1['user_id'], token2['user_id']) + self.assertEqual(token1['user_name'], token2['user_name']) self.assertEqual(token1['player_id'], token2['player_id']) self.assertEqual(token1['identity_id'], token2['identity_id']) self.assertEqual(token1['user_id'], token3['user_id']) + self.assertEqual(token1['user_name'], token3['user_name']) self.assertEqual(token1['player_id'], token3['player_id']) self.assertEqual(token1['identity_id'], token3['identity_id'])