From 8a4dbae68f1157112b2e4a256f403a01452e6b1d Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 9 Apr 2021 17:05:21 +0200 Subject: [PATCH] auth_fc: always fill user_info on FcAccount creation (#49797) --- src/authentic2_auth_fc/backends.py | 6 +++++- src/authentic2_auth_fc/models.py | 5 ++++- src/authentic2_auth_fc/views.py | 16 ++++++++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/authentic2_auth_fc/backends.py b/src/authentic2_auth_fc/backends.py index 72685a8b..92eef007 100644 --- a/src/authentic2_auth_fc/backends.py +++ b/src/authentic2_auth_fc/backends.py @@ -61,7 +61,11 @@ class FcBackend(ModelBackend): user.save() try: models.FcAccount.objects.create( - user=user, sub=sub, order=0, token=json.dumps(kwargs['token']) + user=user, + sub=sub, + order=0, + token=json.dumps(kwargs['token']), + user_info=json.dumps(user_info or {}), ) except IntegrityError: # uniqueness check failed, as the user is new, it can only means that the sub is not unique diff --git a/src/authentic2_auth_fc/models.py b/src/authentic2_auth_fc/models.py index 3516ee10..3dc79a24 100644 --- a/src/authentic2_auth_fc/models.py +++ b/src/authentic2_auth_fc/models.py @@ -102,7 +102,10 @@ class FcAccount(models.Model): return json.loads(self.token) def get_user_info(self): - return json.loads(self.user_info) + if self.user_info: + return json.loads(self.user_info) + else: + return {} def __str__(self): user_info = self.get_user_info() diff --git a/src/authentic2_auth_fc/views.py b/src/authentic2_auth_fc/views.py index 662396d5..002ed736 100644 --- a/src/authentic2_auth_fc/views.py +++ b/src/authentic2_auth_fc/views.py @@ -398,7 +398,13 @@ class LoginOrLinkView(PopupViewMixin, FcOAuthSessionViewMixin, View): # Prevent to add a link with an FC account already linked with another user. try: self.fc_account, created = models.FcAccount.objects.get_or_create( - sub=self.sub, user=request.user, order=0, defaults={'token': json.dumps(self.token)} + sub=self.sub, + user=request.user, + order=0, + defaults={ + 'token': json.dumps(self.token), + 'user_info': json.dumps(self.user_info), + }, ) except IntegrityError: # unique index check failed, find why. @@ -450,7 +456,13 @@ class LoginOrLinkView(PopupViewMixin, FcOAuthSessionViewMixin, View): if not user.fc_accounts.exists(): try: self.fc_account, created = models.FcAccount.objects.get_or_create( - defaults={'token': json.dumps(self.token)}, sub=self.sub, user=user, order=0 + defaults={ + 'token': json.dumps(self.token), + 'user_info': json.dumps(self.user_info), + }, + sub=self.sub, + user=user, + order=0, ) except IntegrityError: # unique index check failed, find why. -- 2.30.1