def renew_session(self, force=False, max_age=None): """Renews the session id if its expiration time has passed. :param force: True to force the session id to be renewed, False to check if the expiration time has passed. :returns: None. """ if not force: # Only renew the session id if it is too old. expires = datetime.timedelta(seconds=max_age) force = (self.session_updated + expires < datetime.datetime.now()) if force: self.session_id = create_session_id() self.session_updated = datetime.datetime.now() self.put()
def create(cls, username, auth_id, **data): data['key_name'] = rand_key(22) data['username'] = username data['auth_id'] = auth_id # Generate an initial session id. data['session_id'] = create_session_id() data['confirmation_token'] = rand_key(22) if 'password_hash' in data: # Password is already hashed. data['password'] = data.pop('password_hash') elif 'password' in data: # Password is not hashed: generate a hash. data['password'] = generate_password_hash(data['password']) if cls.get_by_auth_id(auth_id) is not None: return None return cls._create(data)
def create(cls, username, auth_id, **kwargs): """Creates a new user and returns it. If the username already exists, returns None. :param username: Unique username. :param auth_id: Authentication id, according the the authentication method used. :param kwargs: Additional entity attributes. :returns: The newly created user or None if the username already exists. """ kwargs['username'] = username kwargs['key_name'] = username kwargs['auth_id'] = auth_id # Generate an initial session id. kwargs['session_id'] = create_session_id() if 'password_hash' in kwargs: # Password is already hashed. kwargs['password'] = kwargs.pop('password_hash') elif 'password' in kwargs: # Password is not hashed: generate a hash. kwargs['password'] = generate_password_hash(kwargs['password']) def txn(): if cls.get_by_username(username) is not None: # Username already exists. return None user = cls(**kwargs) user.put() return user return db.run_in_transaction(txn)
def test_create_session_id(self): self.assertEqual(len(create_session_id()), 32)