def authenticate(self, username, password): """ This function will check the password passed in for the given userid against the database, and raise an exception if the authentication failse. :raise :class:`ensconce.exc.InvalidCredentials`: If username and/or password are invalid. """ try: user = operators.get_by_username(username) if not pwhash.compare(user.password, password): raise exc.InvalidCredentials() except exc.NoSuchEntity: log.exception("No matching user.") raise exc.InvalidCredentials()
def set_entity_attributes(entity, update_attributes, encrypted_attributes=None, hashed_attributes=None): """ A convenience method to set attributes (column values) on an entity and return the attributes which were actually modified. :param obj: The entity object being updated. :param update_attributes: A dict of attributes that should be set on this object. :param encrypted_attributes: A list of any attributes that are encrypted (and need to be set using the *_decrypted method variant). :return: A list of modified attributes (column names). :rtype: list """ if encrypted_attributes is None: encrypted_attributes = [] if hashed_attributes is None: hashed_attributes = [] for (attrib, value) in update_attributes.items(): if attrib not in encrypted_attributes and attrib not in hashed_attributes: setattr(entity, attrib, value) for attrib in encrypted_attributes: if attrib in update_attributes: value = update_attributes[attrib] decrypted_attrib = attrib + '_decrypted' if value != getattr(entity, decrypted_attrib): setattr(entity, decrypted_attrib, value) for attrib in hashed_attributes: if attrib in update_attributes: value = update_attributes[attrib] if value is None: setattr(entity, attrib, value) elif not pwhash.compare(getattr(entity, attrib), value): setattr(entity, attrib, pwhash.obscure(value)) # Using default hashtype # Use SQLAlchemy's cool get_history method: # http://docs.sqlalchemy.org/en/rel_0_7/orm/session.html?highlight=get_history#sqlalchemy.orm.attributes.History modified = [] for attrib in update_attributes.keys(): hist = attributes.get_history(entity, attrib) if hist.has_changes(): modified.append(attrib) return modified