@gen.coroutine
  def _PrepareAuthUser(self, user_dict, ident_dict, device_dict):
    """Validates incoming user, identity, and device information in preparation for login,
    register, or link action. Derives user id and name and sets them into the user dict.
    """
    # Create json_attrs from the user_dict returned by the auth service.
    ident_dict['json_attrs'] = user_dict

    # Check whether identity is already created.
    identity = yield gen.Task(Identity.Query, self._client, ident_dict['key'], None, must_exist=False)

    # Ensure that user id and device id are allocated.
    current_user = self.get_current_user()

    # Find or allocate the user id.
    if ['login', 'login_reset'] in self._action:
      # Require identity to already be linked to an account.
      if identity is not None and identity.user_id is not None:
        user = yield gen.Task(User.Query, self._client, identity.user_id, None, must_exist=False)
      else:
        user = None

      if user is None:
        raise PermissionError(NO_USER_ACCOUNT, account=Identity.GetDescription(ident_dict['key']))

      if not user.IsRegistered():
        # Cannot log into an unregistered account.
        raise PermissionError(LOGIN_REQUIRES_REGISTER)

      user_dict['user_id'] = identity.user_id
    elif self._action == 'register':
      if identity is not None and identity.user_id is not None:
        # Identity should already be bound to a user, so only proceed if registering a prospective user.
        user = yield gen.Task(User.Query, self._client, identity.user_id, None, must_exist=False)
        if user is None or user.IsRegistered():
          # User can be None if there's a DB corruption, or if it's still in the process of
          # creation. Treat this case the same as if the user exists but is already registered. 
          raise PermissionError(ALREADY_REGISTERED, account=Identity.GetDescription(identity.key))

        user_dict['user_id'] = user.user_id
      else:
        # Construct a prospective user with newly allocated user id and web device id.
        user_id, webapp_dev_id = yield User.AllocateUserAndWebDeviceIds(self._client)
        user_dict['user_id'] = user_id

        request = {'headers': {'synchronous': True},
                   'user_id': user_id,
                   'webapp_dev_id': webapp_dev_id,
                   'identity_key': ident_dict['key'],
                   'reason': 'register'}
        yield gen.Task(Operation.CreateAndExecute,
                       self._client,
                       user_id,
                       webapp_dev_id,
                       'CreateProspectiveOperation.Execute',
                       request)

        user = yield gen.Task(User.Query, self._client, user_id, None)
        identity = yield gen.Task(Identity.Query, self._client, ident_dict['key'], None)

      if options.options.freeze_new_accounts:
        raise web.HTTPError(403, _FREEZE_NEW_ACCOUNTS_MESSAGE)
    else:
      assert self._action == 'link', self._action
      if current_user is None:
        # This case should never happen in the mobile or web clients, since they will not offer
        # the option to link if the user is not already logged in. But it could happen with a
        # direct API call.
        raise PermissionError(MERGE_REQUIRES_LOGIN)

      if not current_user.IsRegistered():
        raise web.HTTPError(403, _CANNOT_LINK_TO_PROSPECTIVE)

      if identity is not None and identity.user_id is not None and current_user.user_id != identity.user_id:
        raise PermissionError(ALREADY_LINKED, account=Identity.GetDescription(ident_dict['key']))

      # Ensure that the new identity is created.
      if identity is None:
        identity = Identity.CreateFromKeywords(key=ident_dict['key'])
        yield gen.Task(identity.Update, self._client)

      user = current_user
      user_dict['user_id'] = current_user.user_id

    assert user, user_dict
    assert identity, ident_dict

    if device_dict is not None:
      if 'device_id' in device_dict:
        # If device_id was specified, it must be owned by the calling user.
        if 'user_id' in user_dict:
          # Raise error if the device specified in the device dict is not owned by the calling user.
          device = yield gen.Task(Device.Query, self._client, user_dict['user_id'], device_dict['device_id'],
                                  None, must_exist=False)
          if device is None:
            raise web.HTTPError(403, 'user %d does not own device %d' %
                                (user_dict['user_id'], device_dict['device_id']))
        else:
          logging.warning('device_id cannot be set when user does not yet exist: %s' % device_dict)
          raise web.HTTPError(403, _CANNOT_SET_DEVICE_FOR_USER_MESSAGE)

    raise gen.Return(user)
dataset/ETHPy150Open viewfinderco/viewfinder/backend/www/auth.py/AuthHandler._PrepareAuthUser/swapped operands of `in`