def switch_user(backend, switch_user=False, user=None, social=None, *args, **kwargs): if switch_user and social: logger.warn( '[authplus.social_pipeline.switch_user] switch to user %s' % user.email) # Do we have a voter who's twitter_id matches the incoming user_id? # Is social.user_id the Twitter id? # # social.actions.do_complete will not login second user because of prior user, so we'll do it here. # user.social_user = social user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__) _do_login(backend, user, social) # store last login backend name in session backend.strategy.session_set('social_auth_last_login_backend', social.provider)
def switch_user(backend, switch_user=False, user=None, social=None, *args, **kwargs): if switch_user and social: # # social.actions.do_complete will not login second user because of prior user, so we'll do it here. # #user.social_user = social #user.backend = "%s.%s" % (strategy.backend.__module__, strategy.backend.__class__.__name__) _do_login(backend, user, social) # store last login backend name in session backend.strategy.session_set('social_auth_last_login_backend', social.provider)
def switch_user(backend, switch_user=False, user=None, social=None, *args, **kwargs): if switch_user and social: logger.warn('[authplus.social_pipeline.switch_user] switch to user %s' % user.email) # Do we have a voter who's twitter_id matches the incoming user_id? # Is social.user_id the Twitter id? # # social.actions.do_complete will not login second user because of prior user, so we'll do it here. # user.social_user = social user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__) _do_login(backend, user, social) # store last login backend name in session backend.strategy.session_set('social_auth_last_login_backend', social.provider)
def social_register(request): auth_token = request.data.get('access_token', None) backend = request.data.get('backend', None) if auth_token and backend: try: user = auth_by_token(request, backend) except Exception, err: return Response(str(err), status=400) if user: uri = '' strategy = load_strategy(request) backend = load_backend(strategy, backend, uri) _do_login(backend , user, strategy) return Response( "User logged in", status=status.HTTP_200_OK ) else: return Response("Bad Credentials", status=403)
def post(self, request): serializer = self.social_serializer(data=request.DATA, files=request.FILES) if serializer.is_valid(): backend = serializer.data['backend'] else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) strategy = load_strategy(request=request, backend=backend) try: kwargs = dict({(k, i) for k, i in serializer.data.items() if k != 'backend'}) user = request.user kwargs['user'] = user.is_authenticated() and user or None user = strategy.backend.do_auth(**kwargs) except AuthAlreadyAssociated: data = { 'error_code': 'social_already_accociated', 'status': 'Auth associated with another user.', } return Response(data, status=status.HTTP_403_FORBIDDEN) if not user: data = { 'error_code': 'social_no_user', 'status': 'No associated user.', } return Response(data, status=status.HTTP_403_FORBIDDEN) if not user.is_active: data = { 'error_code': 'social_inactive', 'status': 'Associated user is inactive.', } return Response(data, status=status.HTTP_403_FORBIDDEN) _do_login(strategy, user) if not self.user_serializer: msg = 'SocialAuthView.user_serializer should be a serializer.' raise ImproperlyConfigured(msg) serializer = self.user_serializer(user) return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request): serializer = self.social_serializer(data=request.DATA, files=request.FILES) if serializer.is_valid(): backend = serializer.data['backend'] else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) strategy = load_strategy(request=request, backend=backend) try: kwargs = dict({(k, i) for k, i in serializer.data.items() if k != 'backend'}) user = request.user kwargs['user'] = user.is_authenticated() and user or None user = strategy.backend.do_auth(**kwargs) except AuthAlreadyAssociated: data = { 'error_code': 'social_already_accociated', 'status': 'Auth associated with another user.', } return Response(data, status=status.HTTP_403_FORBIDDEN) if not user: data = { 'error_code': 'social_no_user', 'status': 'No associated user.', } return Response(data, status=status.HTTP_403_FORBIDDEN) if not user.is_active: data = { 'error_code': 'social_inactive', 'status': 'Associated user is inactive.', } return Response(data, status=status.HTTP_403_FORBIDDEN) _do_login(strategy, user) if not self.user_serializer: msg = 'SocialAuthView.user_seriliser should be a serializer.' raise ImproperlyConfigured(msg) serializer = self.user_serializer(user) return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request): serializer = self.social_serializer(data=request.DATA, files=request.FILES) if serializer.is_valid(): backend = serializer.data['backend'] else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) strategy = load_strategy(request=request, backend=backend) # First create a user c = 0 while True: random_email = "tmp{}@test.com".format(c) try: User.objects.get(email=random_email) except User.DoesNotExist: break c += 1 user = strategy.create_user(email=random_email) # Now try logging into auth. try: kwargs = dict({(k, i) for k, i in serializer.data.items() if k != 'backend'}) kwargs['user'] = user user = strategy.backend.do_auth(**kwargs) # Throw error if they already have signed up. except (AuthAlreadyAssociated, AssertionError): data = { 'error_code': 'social_already_accociated', 'status': 'Auth associated with another user.', } return Response(data, status=status.HTTP_403_FORBIDDEN) _do_login(strategy, user) serializer = self.user_serializer(user) return Response(serializer.data, status=status.HTTP_201_CREATED)
def social_register(request): auth_token = request.data.get('access_token', None) backend = request.data.get('backend', None) if auth_token and backend: try: user = auth_by_token(request, backend) except Exception, err: return Response(str(err), status=400) if user: strategy = load_strategy(request) uri = '' backend = load_backend(strategy, backend, uri) _do_login(backend, user, strategy) print("apunto de salir") pdb.set_trace() data = { 'name': user.username, 'id': user.id, 'status': user.status } return Response(data, status=status.HTTP_200_OK) else: return Response("Bad Credentials", status=403)
def login_func(backend, user, social_user): if user.last_login != datetime.fromtimestamp(0): _do_login(backend, user, social_user)