def _process_user(provider, provider_id, token, secret, userdata): """ Processes a user to either login or add a new authentication to an existing user. """ # If there is already an authenticated user, # add this provider to that user. if current_user.is_authenticated: try: current_user.add_provider(provider, provider_id, token, secret, update=True) # Conflict - this added authentication already exists but is # associated with another user. except AuthExistsForUserException as e: return jsonify(status=409, message=e.message) # Otherwise... else: # Try to find an existing auth and user. auth = Auth.for_provider(provider, provider_id) # If one is found, update the auth. if auth: user = auth.user auth.update_token(token, secret) # Otherwise create a new user and auth. else: user = User(**userdata) auth = Auth(provider, provider_id, token, secret) auth.user = user db.session.add(user) db.session.add(auth) db.session.commit() login_user(user)
def test_add_provider(self): user = User(**self.userdata) user.add_provider(**self.authdata) auth = Auth.for_provider(self.authdata['provider'], self.authdata['provider_id']) self.assertEqual(Auth.query.count(), 1) self.assertEqual(auth.user, user)