From cd93464f015e24d3242997ef99a88f687d32e764 Mon Sep 17 00:00:00 2001 From: Rastopapola <35961376+Rastopapola@users.noreply.github.com> Date: Mon, 16 Aug 2021 13:53:18 +0200 Subject: [PATCH 1/4] Update user data (#62) * Update user data * adds updating logic to client.py * adds test * Update user data * Change Request by @GaretJax in https://github.com/divio/django-simple-sso/pull/62/files/367d54ebf06d41aa6db182338d152b3266dda12c#diff-156d4d0f83e2028594d28cfe5b57a3aaec983a3722c45c6f9aaf0c7619a37c1d --- simple_sso/sso_client/client.py | 7 ++++++ tests/test_core.py | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/simple_sso/sso_client/client.py b/simple_sso/sso_client/client.py index c82e688..f9f40a0 100644 --- a/simple_sso/sso_client/client.py +++ b/simple_sso/sso_client/client.py @@ -1,3 +1,4 @@ +from copy import copy from urllib.parse import urlparse, urlunparse, urljoin, urlencode from django.urls import re_path @@ -108,6 +109,12 @@ def get_user(self, access_token): def build_user(self, user_data): try: user = User.objects.get(username=user_data['username']) + # Update user data, excluding username changes + # Work on copied _tmp dict to keep an untouched user_data + user_data_tmp = copy(user_data) + del user_data_tmp['username'] + for _attr, _val in user_data_tmp.items(): + setattr(user, _attr, _val) except User.DoesNotExist: user = User(**user_data) user.set_unusable_password() diff --git a/tests/test_core.py b/tests/test_core.py index 6c525d8..ee63c97 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -106,6 +106,46 @@ def test_user_already_logged_in(self): for key in ['username', 'email', 'first_name', 'last_name']: self.assertEqual(getattr(client_user, key), getattr(server_user, key)) + def test_user_data_updated(self): + """ User data update test + + Tests whether sso server user data changes will be forwared to the client on the user's next login. + + """ + USERNAME = PASSWORD = 'myuser' + extra_data = { + "first_name": "bob", + "last_name": "bobster", + } + server_user = User.objects.create_user( + USERNAME, + 'bob@bobster.org', + PASSWORD, + **extra_data, + ) + self._get_consumer() + + with UserLoginContext(self, server_user): + # First login + # try logging in and auto-follow all 302s + self.client.get(reverse('simple-sso-login'), follow=True) + # check the user + client_user = get_user(self.client) + for key in ['username', 'email', 'first_name', 'last_name']: + self.assertEqual(getattr(client_user, key), getattr(server_user, key)) + + # User data changes + server_user.first_name = "Alice" + server_user.email = "alice@bobster.org" + server_user.save() + + with UserLoginContext(self, server_user): + # Second login + self.client.get(reverse('simple-sso-login'), follow=True) + client_user = get_user(self.client) + for key in ['username', 'email', 'first_name', 'last_name']: + self.assertEqual(getattr(client_user, key), getattr(server_user, key)) + def test_custom_keygen(self): # WARNING: The following test uses a key generator function that is # highly insecure and should never under any circumstances be used in From 0e9509a73732a08354ba59b5a5607b93086f3eb6 Mon Sep 17 00:00:00 2001 From: Jonathan Stoppani Date: Mon, 16 Aug 2021 13:56:13 +0200 Subject: [PATCH 2/4] Prepare release 1.1.0 --- CHANGELOG.rst | 6 ++++++ simple_sso/__init__.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 087ba29..074b7de 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,12 @@ Changelog ========= +1.1.0 (2021-08-16) +================== + +* Added support to update user-data on login (#61) + + 1.0.0 (2020-09-03) ================== diff --git a/simple_sso/__init__.py b/simple_sso/__init__.py index 1f356cc..1a72d32 100644 --- a/simple_sso/__init__.py +++ b/simple_sso/__init__.py @@ -1 +1 @@ -__version__ = '1.0.0' +__version__ = '1.1.0' From 364073b7c2cf5fe496118bb08384698aa361cd71 Mon Sep 17 00:00:00 2001 From: Jonathan Stoppani Date: Mon, 16 Aug 2021 14:14:54 +0200 Subject: [PATCH 3/4] Rename back to correct name --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 89eb5db..0ecb1fe 100644 --- a/setup.py +++ b/setup.py @@ -35,7 +35,7 @@ setup( - name='divio-simple-sso', + name='django-simple-sso', version=__version__, author='Divio AG', author_email='info@divio.com', From 2717082c1d5e9bca4ef19303d72b572e3cb67f44 Mon Sep 17 00:00:00 2001 From: Jonathan Stoppani Date: Wed, 14 Dec 2022 15:54:10 +0100 Subject: [PATCH 4/4] Increase max length of Token.redirect_to and release 1.2.0 --- CHANGELOG.rst | 5 +++++ simple_sso/__init__.py | 2 +- .../0003_token_redirect_to_max_length.py | 16 ++++++++++++++++ simple_sso/sso_server/models.py | 2 +- 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 simple_sso/sso_server/migrations/0003_token_redirect_to_max_length.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 074b7de..a091376 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,11 @@ Changelog ========= +1.2.0 (2022-12-14) +================== + +* Increased the max length of the Token.Token.redirect_to field to 1023 + 1.1.0 (2021-08-16) ================== diff --git a/simple_sso/__init__.py b/simple_sso/__init__.py index 1a72d32..58d478a 100644 --- a/simple_sso/__init__.py +++ b/simple_sso/__init__.py @@ -1 +1 @@ -__version__ = '1.1.0' +__version__ = '1.2.0' diff --git a/simple_sso/sso_server/migrations/0003_token_redirect_to_max_length.py b/simple_sso/sso_server/migrations/0003_token_redirect_to_max_length.py new file mode 100644 index 0000000..f77094c --- /dev/null +++ b/simple_sso/sso_server/migrations/0003_token_redirect_to_max_length.py @@ -0,0 +1,16 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sso_server', '0002_consumer_name_max_length'), + ] + + operations = [ + migrations.AlterField( + model_name='token', + name='redirect_to', + field=models.CharField(max_length=1023), + ), + ] diff --git a/simple_sso/sso_server/models.py b/simple_sso/sso_server/models.py index ccd7fee..3d258d1 100644 --- a/simple_sso/sso_server/models.py +++ b/simple_sso/sso_server/models.py @@ -67,7 +67,7 @@ class Token(models.Model): default=TokenSecretKeyGenerator('access_token') ) timestamp = models.DateTimeField(default=timezone.now) - redirect_to = models.CharField(max_length=255) + redirect_to = models.CharField(max_length=1023) user = models.ForeignKey( getattr(settings, 'AUTH_USER_MODEL', 'auth.User'), null=True,