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 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 context_value(): keys = 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.replace('-', '_'), assoc) for assoc in UserSocialAuth.get_social_auth_for_user(user)) return accounts
def tokens(self): """Return access_token stored in extra_data or None""" # Make import here to avoid recursive imports :-/ from core.social_auth.backends import get_backends backend = get_backends().get(self.provider) if backend: return backend.AUTH_BACKEND.tokens(self) else: return {}
def context_value(): keys = 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.replace('-', '_'), assoc) for assoc in UserSocialAuth.get_social_auth_for_user(user)) return accounts
def group_backend_by_type(items, key=lambda x: x): """Group items by backend type.""" # Beware of cyclical imports! from core.social_auth.backends import get_backends, OpenIdAuth, BaseOAuth, BaseOAuth2 result = defaultdict(list) backends = get_backends() for item in items: backend = backends[key(item)] if issubclass(backend, OpenIdAuth): result["openid"].append(item) elif issubclass(backend, BaseOAuth2): result["oauth2"].append(item) elif issubclass(backend, BaseOAuth): result["oauth"].append(item) return dict(result)