Просмотр исходного кода

ref(social auth): remove all social auth context processors (#16831)

josh 5 лет назад
Родитель
Сommit
6c997da9ff
3 измененных файлов с 0 добавлено и 139 удалено
  1. 0 4
      src/sentry/conf/server.py
  2. 0 101
      src/social_auth/context_processors.py
  3. 0 34
      src/social_auth/utils.py

+ 0 - 4
src/sentry/conf/server.py

@@ -297,10 +297,6 @@ TEMPLATES = [
                 "django.contrib.messages.context_processors.messages",
                 "django.contrib.messages.context_processors.messages",
                 "django.template.context_processors.csrf",
                 "django.template.context_processors.csrf",
                 "django.template.context_processors.request",
                 "django.template.context_processors.request",
-                "social_auth.context_processors.social_auth_by_name_backends",
-                "social_auth.context_processors.social_auth_backends",
-                "social_auth.context_processors.social_auth_by_type_backends",
-                "social_auth.context_processors.social_auth_login_redirect",
             ]
             ]
         },
         },
     }
     }

+ 0 - 101
src/social_auth/context_processors.py

@@ -1,101 +0,0 @@
-from __future__ import absolute_import
-
-from django.contrib.auth import REDIRECT_FIELD_NAME
-
-from social_auth.models import UserSocialAuth
-from social_auth.backends import get_backends
-from social_auth.utils import group_backend_by_type, LazyDict
-
-# Note: social_auth_backends, social_auth_by_type_backends and
-#       social_auth_by_name_backends don't play nice together.
-
-
-def social_auth_backends(request):
-    """Load Social Auth current user data to context.
-    Will add a output from backends_data to context under social_auth key.
-    """
-
-    def context_value():
-        return backends_data(request.user)
-
-    return {"social_auth": LazyDict(context_value)}
-
-
-def social_auth_by_type_backends(request):
-    """Load Social Auth current user data to context.
-    Will add a output from backends_data to context under social_auth key where
-    each entry will be grouped by backend type (oauth, oauth2).
-    """
-
-    def context_value():
-        data = backends_data(request.user)
-        data["backends"] = group_backend_by_type(data["backends"])
-        data["not_associated"] = group_backend_by_type(data["not_associated"])
-        data["associated"] = group_backend_by_type(
-            data["associated"], key=lambda assoc: assoc.provider
-        )
-        return data
-
-    return {"social_auth": LazyDict(context_value)}
-
-
-def social_auth_by_name_backends(request):
-    """Load Social Auth current user data to context.
-    Will add a social_auth object whose attribute names are the names of each
-    provider, e.g. social_auth.facebook would be the facebook association or
-    None, depending on the logged in user's current associations. Providers
-    with a hyphen have the hyphen replaced with an underscore, e.g.
-    google-oauth2 becomes google_oauth2 when referenced in templates.
-    """
-
-    def context_value():
-        keys = [key for key in get_backends().keys()]
-        accounts = dict(zip(keys, [None] * len(keys)))
-        user = request.user
-        if hasattr(user, "is_authenticated") and user.is_authenticated():
-            accounts.update(
-                (assoc.provider, assoc) for assoc in UserSocialAuth.get_social_auth_for_user(user)
-            )
-        return accounts
-
-    return {"social_auth": LazyDict(context_value)}
-
-
-def backends_data(user):
-    """Return backends data for given user.
-
-    Will return a dict with values:
-        associated: UserSocialAuth model instances for currently
-                    associated accounts
-        not_associated: Not associated (yet) backend names.
-        backends: All backend names.
-
-    If user is not authenticated, then first list is empty, and there's no
-    difference between the second and third lists.
-    """
-    available = get_backends().keys()
-    values = {"associated": [], "not_associated": available, "backends": available}
-
-    # user comes from request.user usually, on /admin/ it will be an instance
-    # of auth.User and this code will fail if a custom User model was defined
-    if hasattr(user, "is_authenticated") and user.is_authenticated():
-        associated = UserSocialAuth.get_social_auth_for_user(user)
-        not_associated = list(set(available) - set(assoc.provider for assoc in associated))
-        values["associated"] = associated
-        values["not_associated"] = not_associated
-    return values
-
-
-def social_auth_login_redirect(request):
-    """Load current redirect to context."""
-    redirect_value = request.GET.get(REDIRECT_FIELD_NAME)
-    if redirect_value:
-        redirect_querystring = REDIRECT_FIELD_NAME + "=" + redirect_value
-    else:
-        redirect_querystring = ""
-
-    return {
-        "REDIRECT_FIELD_NAME": REDIRECT_FIELD_NAME,
-        "REDIRECT_FIELD_VALUE": redirect_value,
-        "redirect_querystring": redirect_querystring,
-    }

+ 0 - 34
src/social_auth/utils.py

@@ -5,11 +5,9 @@ import logging
 from importlib import import_module
 from importlib import import_module
 
 
 from cgi import parse_qsl
 from cgi import parse_qsl
-from collections import defaultdict
 from django.conf import settings
 from django.conf import settings
 from django.db.models import Model
 from django.db.models import Model
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes.models import ContentType
-from django.utils.functional import empty, SimpleLazyObject
 from six.moves.urllib.parse import urlencode, urlparse, urlunparse
 from six.moves.urllib.parse import urlencode, urlparse, urlunparse
 from six.moves.urllib.request import urlopen
 from six.moves.urllib.request import urlopen
 
 
@@ -41,24 +39,6 @@ def sanitize_log_data(secret, data=None, leave_characters=LEAVE_CHARS):
     return replace_secret
     return replace_secret
 
 
 
 
-def group_backend_by_type(items, key=lambda x: x):
-    """Group items by backend type."""
-
-    # Beware of cyclical imports!
-    from social_auth.backends import get_backends, BaseOAuth, BaseOAuth2
-
-    result = defaultdict(list)
-    backends = get_backends()
-
-    for item in items:
-        backend = backends[key(item)]
-        if issubclass(backend, BaseOAuth2):
-            result["oauth2"].append(item)
-        elif issubclass(backend, BaseOAuth):
-            result["oauth"].append(item)
-    return dict(result)
-
-
 def setting(name, default=None):
 def setting(name, default=None):
     """Return setting value for given name or default value."""
     """Return setting value for given name or default value."""
     return getattr(settings, name, default)
     return getattr(settings, name, default)
@@ -131,20 +111,6 @@ def url_add_parameters(url, params):
     return url
     return url
 
 
 
 
-class LazyDict(SimpleLazyObject):
-    """Lazy dict initialization."""
-
-    def __getitem__(self, name):
-        if self._wrapped is empty:
-            self._setup()
-        return self._wrapped[name]
-
-    def __setitem__(self, name, value):
-        if self._wrapped is empty:
-            self._setup()
-        self._wrapped[name] = value
-
-
 def dsa_urlopen(*args, **kwargs):
 def dsa_urlopen(*args, **kwargs):
     """Like urllib2.urlopen but sets a timeout defined by
     """Like urllib2.urlopen but sets a timeout defined by
     SOCIAL_AUTH_URLOPEN_TIMEOUT setting if defined (and not already in
     SOCIAL_AUTH_URLOPEN_TIMEOUT setting if defined (and not already in