From dae15ae303a1e7df1dc2576230da91bd65e52bd7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Thu, 29 Mar 2012 18:11:30 -0300 Subject: [PATCH] Avoid refresh_token overwrite on further logins. Closes #299 --- README.rst | 13 +++++++++++-- doc/backends/oauth.rst | 6 +++++- doc/backends/openid.rst | 6 +++++- social_auth/backends/__init__.py | 15 ++++++++++++++- social_auth/backends/google.py | 2 +- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 9e14b06..fd361c3 100644 --- a/README.rst +++ b/README.rst @@ -614,7 +614,11 @@ Example:: GOOGLE_AX_EXTRA_DATA = [(..., ...)] Settings must be a list of tuples mapping value name in response and value -alias used to store. +alias used to store. A third value (boolean) is supported to, it's purpose is +to signal if the value should be discarded if it evaluates to ``False``, this +is to avoid replacing old (needed) values when they don't form part of current +response. If not present, then this check is avoided and the value will replace +any data. OAuth ^^^^^ @@ -639,7 +643,12 @@ Example:: FACEBOOK_EXTRA_DATA = [(..., ...)] Settings must be a list of tuples mapping value name in response and value -alias used to store. +alias used to store. A third value (boolean) is supported to, it's purpose is +to signal if the value should be discarded if it evaluates to ``False``, this +is to avoid replacing old (needed) values when they don't form part of current +response. If not present, then this check is avoided and the value will replace +any data. + Twitter ^^^^^^^ diff --git a/doc/backends/oauth.rst b/doc/backends/oauth.rst index f05b921..56d0c5d 100644 --- a/doc/backends/oauth.rst +++ b/doc/backends/oauth.rst @@ -21,7 +21,11 @@ Example:: FACEBOOK_EXTRA_DATA = [(..., ...)] Settings must be a list of tuples mapping value name in response and value -alias used to store. +alias used to store. A third value (boolean) is supported to, it's purpose is +to signal if the value should be discarded if it evaluates to ``False``, this +is to avoid replacing old (needed) values when they don't form part of current +response. If not present, then this check is avoided and the value will replace +any data. .. _OAuth: http://oauth.net/ diff --git a/doc/backends/openid.rst b/doc/backends/openid.rst index 2b8cd59..98d0007 100644 --- a/doc/backends/openid.rst +++ b/doc/backends/openid.rst @@ -22,7 +22,11 @@ Example:: GOOGLE_AX_EXTRA_DATA = [(..., ...)] Settings must be a list of tuples mapping value name in response and value -alias used to store. +alias used to store. A third value (boolean) is supported to, it's purpose is +to signal if the value should be discarded if it evaluates to ``False``, this +is to avoid replacing old (needed) values when they don't form part of current +response. If not present, then this check is avoided and the value will replace +any data. .. _OpenId: http://openid.net/ .. _OAuth: http://oauth.net/ diff --git a/social_auth/backends/__init__.py b/social_auth/backends/__init__.py index c38e60a..10f597b 100644 --- a/social_auth/backends/__init__.py +++ b/social_auth/backends/__init__.py @@ -219,7 +219,20 @@ class OAuthBackend(SocialAuthBackend): data = {'access_token': response.get('access_token', '')} name = self.name.replace('-', '_').upper() names = (self.EXTRA_DATA or []) + setting(name + '_EXTRA_DATA', []) - data.update((alias, response.get(name)) for name, alias in names) + for entry in names: + if len(entry) == 2: + (name, alias), discard = entry, False + elif len(entry) == 3: + name, alias, discard = entry + elif len(entry) == 1: + name = alias = entry + else: # ??? + continue + + value = response.get(name) + if discard and not value: + continue + data[alias] = value return data diff --git a/social_auth/backends/google.py b/social_auth/backends/google.py index d808e8a..2e0ccb6 100644 --- a/social_auth/backends/google.py +++ b/social_auth/backends/google.py @@ -70,7 +70,7 @@ class GoogleOAuth2Backend(GoogleOAuthBackend): """Google OAuth2 authentication backend""" name = 'google-oauth2' EXTRA_DATA = [ - ('refresh_token', 'refresh_token'), + ('refresh_token', 'refresh_token', True), ('expires_in', setting('SOCIAL_AUTH_EXPIRATION', 'expires')) ] -- 2.39.5