def run(self, username, password, email, admin='false'): # force type conversion to boolean user_type = 'administrator' if admin.lower() == 'true' else 'user' userdata = { 'username': username, 'password': password, 'email': email, 'user_type': user_type, app.config['LAST_UPDATED']: utcnow(), } with app.test_request_context('/users', method='POST'): if userdata.get('password', None) and not is_hashed(userdata.get('password')): userdata['password'] = get_hash(userdata.get('password'), app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)) user = superdesk.get_resource_service('users').find_one(username=userdata.get('username'), req=None) if user: logger.info('updating user %s' % (userdata)) superdesk.get_resource_service('users').patch(user.get('_id'), userdata) return userdata else: logger.info('creating user %s' % (userdata)) userdata[app.config['DATE_CREATED']] = userdata[app.config['LAST_UPDATED']] superdesk.get_resource_service('users').post([userdata]) logger.info('user saved %s' % (userdata)) return userdata
def run(self, username, password, email, admin=False): # force type conversion to boolean user_type = 'administrator' if admin else 'user' userdata = { 'username': username, 'password': password, 'email': email, 'user_type': user_type, 'is_active': admin, 'needs_activation': not admin } with app.test_request_context('/users', method='POST'): if userdata.get('password', None) and not is_hashed(userdata.get('password')): userdata['password'] = get_hash(userdata.get('password'), app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)) user = superdesk.get_resource_service('users').find_one(username=userdata.get('username'), req=None) if user: logger.info('user already exists %s' % (userdata)) else: logger.info('creating user %s' % (userdata)) superdesk.get_resource_service('users').post([userdata]) logger.info('user saved %s' % (userdata)) return userdata
def run(self, username, password, email, admin=False, support=False): # force type conversion to boolean user_type = 'administrator' if admin else 'user' userdata = { 'username': username, 'password': password, 'email': email, 'user_type': user_type, 'is_active': admin, 'is_support': support, 'needs_activation': not admin } with app.test_request_context('/users', method='POST'): if userdata.get('password', None) and not is_hashed(userdata.get('password')): userdata['password'] = get_hash(userdata.get('password'), app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)) user = superdesk.get_resource_service('users').find_one(username=userdata.get('username'), req=None) if user: logger.info('user already exists %s' % (userdata)) else: logger.info('creating user %s' % (userdata)) superdesk.get_resource_service('users').post([userdata]) logger.info('user saved %s' % (userdata)) return userdata
def update_password(self, user_id, password): """Update the user password. Returns true if successful. """ user = self.find_one(req=None, _id=user_id) if not user: raise SuperdeskApiError.unauthorizedError("User not found") if not self.is_user_active(user): raise UserInactiveError() updates = { "password": get_hash(password, app.config.get("BCRYPT_GENSALT_WORK_FACTOR", 12)), "password_changed_on": utcnow(), app.config["LAST_UPDATED"]: utcnow(), } if self.user_is_waiting_activation(user): updates["needs_activation"] = False self.patch(user_id, updates=updates)
def run(self, username, password, email, admin=False, support=False): # force type conversion to boolean user_type = "administrator" if admin else "user" userdata = { "username": username, "password": password, "email": email, "user_type": user_type, "is_active": admin, "is_support": support, "needs_activation": not admin, } with app.test_request_context("/users", method="POST"): if userdata.get("password", None) and not is_hashed(userdata.get("password")): userdata["password"] = get_hash( userdata.get("password"), app.config.get("BCRYPT_GENSALT_WORK_FACTOR", 12) ) user = superdesk.get_resource_service("users").find_one(username=userdata.get("username"), req=None) if user: logger.info("user already exists %s" % (userdata)) else: logger.info("creating user %s" % (userdata)) superdesk.get_resource_service("users").post([userdata]) logger.info("user saved %s" % (userdata)) return userdata
def update_password(self, user_id, password): """Update the user password. Returns true if successful. """ user = self.find_one(req=None, _id=user_id) if not user: raise SuperdeskApiError.unauthorizedError('User not found') if not self.is_user_active(user): raise UserInactiveError() updates = { 'password': get_hash(password, app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)), app.config['LAST_UPDATED']: utcnow() } if self.user_is_waiting_activation(user): updates['needs_activation'] = False self.patch(user_id, updates=updates)
def on_create(self, docs): super().on_create(docs) for doc in docs: if doc.get('password', None) and not is_hashed(doc.get('password')): doc['password'] = get_hash( doc.get('password'), app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12))
def on_create(self, docs): super().on_create(docs) for doc in docs: if doc.get("password", None) and not is_hashed(doc.get("password")): doc["password"] = get_hash( doc.get("password"), app.config.get("BCRYPT_GENSALT_WORK_FACTOR", 12))
def run(self): users = superdesk.get_resource_service("auth_users").get(req=None, lookup={}) for user in users: pwd = user.get("password") if not is_hashed(pwd): updates = {} hashed = get_hash(user["password"], app.config.get("BCRYPT_GENSALT_WORK_FACTOR", 12)) user_id = user.get("_id") updates["password"] = hashed superdesk.get_resource_service("users").patch(user_id, updates=updates)
def run(self): users = superdesk.get_resource_service('auth_users').get(req=None, lookup={}) for user in users: pwd = user.get('password') if not is_hashed(pwd): updates = {} hashed = get_hash(user['password'], app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)) user_id = user.get('_id') updates['password'] = hashed superdesk.get_resource_service('users').patch(user_id, updates=updates)
def update_password(self, user_id, password): """ Update the user password. Returns true if successful. """ user = self.find_one(req=None, _id=user_id) if not user: raise SuperdeskError(payload='Invalid user.') if not self.user_is_active(user): raise SuperdeskError(status_code=403, message='Updating password is forbidden.') updates = {} updates['password'] = get_hash(password, app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)) updates[app.config['LAST_UPDATED']] = utcnow() if self.user_is_waiting_activation(user): updates['needs_activation'] = False self.patch(user_id, updates=updates)
def update_password(self, user_id, password): """Update the user password. Returns true if successful. """ user = self.find_one(req=None, _id=user_id) if not user: raise SuperdeskApiError.unauthorizedError('User not found') if not self.is_user_active(user): raise UserInactiveError() updates = {'password': get_hash(password, app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)), app.config['LAST_UPDATED']: utcnow()} if self.user_is_waiting_activation(user): updates['needs_activation'] = False self.patch(user_id, updates=updates)
def test_is_user_valid_empty_password(client): password = '******'.encode('utf-8') assert not _is_password_valid(password, { '_id': 'foo', 'email': '*****@*****.**' }) assert not _is_password_valid(password, { '_id': 'foo', 'email': '*****@*****.**', 'password': None }) assert not _is_password_valid(password, { '_id': 'foo', 'email': '*****@*****.**', 'password': '' }) assert _is_password_valid(password, { '_id': 'foo', 'email': '*****@*****.**', 'password': get_hash('foo', 10) })
def update_password(self, user_id, password): """ Update the user password. Returns true if successful. """ user = self.find_one(req=None, _id=user_id) if not user: raise SuperdeskError(payload='Invalid user.') if not self.user_is_active(user): raise SuperdeskError(status_code=403, message='Updating password is forbidden.') updates = {} updates['password'] = get_hash( password, app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)) updates[app.config['LAST_UPDATED']] = utcnow() if self.user_is_waiting_activation(user): updates['needs_activation'] = False self.patch(user_id, updates=updates)
def _get_password_hash(self, password): return get_hash(password, app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12))
def on_create(self, docs): super().on_create(docs) for doc in docs: if doc.get('password', None) and not is_hashed(doc.get('password')): doc['password'] = get_hash(doc.get('password'), app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12))
def update_user_password(self, user_id, password): updates = {} updates['password'] = get_hash(password, app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)) updates[app.config['LAST_UPDATED']] = utcnow() superdesk.get_resource_service('users').patch(user_id, updates=updates)