diff --git a/lib/galaxy/webapps/galaxy/controllers/admin.py b/lib/galaxy/webapps/galaxy/controllers/admin.py index 3ea74006c4b..510272c445d 100644 --- a/lib/galaxy/webapps/galaxy/controllers/admin.py +++ b/lib/galaxy/webapps/galaxy/controllers/admin.py @@ -16,8 +16,8 @@ from galaxy import ( from galaxy.actions.admin import AdminActions from galaxy.exceptions import ActionInputError, MessageException from galaxy.model import tool_shed_install as install_model -from galaxy.tool_shed.util.repository_util import get_ids_of_tool_shed_repositories_being_installed from galaxy.security.validate_user_input import validate_password +from galaxy.tool_shed.util.repository_util import get_ids_of_tool_shed_repositories_being_installed from galaxy.util import ( nice_size, sanitize_text, diff --git a/lib/tool_shed/util/admin_util.py b/lib/tool_shed/util/admin_util.py index 3175d2b74e1..d9d43b3e054 100644 --- a/lib/tool_shed/util/admin_util.py +++ b/lib/tool_shed/util/admin_util.py @@ -4,12 +4,12 @@ import time from sqlalchemy import false, func from galaxy import util, web +from galaxy.security.validate_user_input import validate_password from galaxy.util import inflector from galaxy.util.hash_util import new_secure_hash from galaxy.web.form_builder import CheckboxField from tool_shed.util.web_util import escape - log = logging.getLogger(__name__) compliance_log = logging.getLogger('COMPLIANCE') @@ -685,13 +685,8 @@ class Admin(object): user = get_user(trans, user_id) password = kwd.get('password', None) confirm = kwd.get('confirm', None) - if len(password) < 6: - message = "Use a password of at least 6 characters." - status = 'error' - break - elif password != confirm: - message = "Passwords do not match." - status = 'error' + message = validate_password(trans, password, confirm) + if message: break else: user.set_password_cleartext(password) diff --git a/lib/tool_shed/webapp/model/__init__.py b/lib/tool_shed/webapp/model/__init__.py index 78461817067..2e76778e2ba 100644 --- a/lib/tool_shed/webapp/model/__init__.py +++ b/lib/tool_shed/webapp/model/__init__.py @@ -8,6 +8,7 @@ from datetime import ( import tool_shed.repository_types.util as rt_util from galaxy import util from galaxy.model.orm.now import now +from galaxy.security.validate_user_input import validate_password_str from galaxy.util import unique_id from galaxy.util.bunch import Bunch from galaxy.util.dictifiable import Dictifiable @@ -63,6 +64,9 @@ class User(Dictifiable): total_disk_usage = property(get_disk_usage, set_disk_usage) def set_password_cleartext(self, cleartext): + message = validate_password_str(cleartext, cleartext) + if message: + raise Exception("Invalid password: %s" % message) """Set 'self.password' to the digest of 'cleartext'.""" self.password = new_secure_hash(text_type=cleartext) diff --git a/scripts/tool_shed/bootstrap_tool_shed/create_user_with_api_key.py b/scripts/tool_shed/bootstrap_tool_shed/create_user_with_api_key.py index f8facd69e6f..8cae502273b 100644 --- a/scripts/tool_shed/bootstrap_tool_shed/create_user_with_api_key.py +++ b/scripts/tool_shed/bootstrap_tool_shed/create_user_with_api_key.py @@ -4,7 +4,6 @@ from __future__ import print_function import logging import optparse import os -import re import sys from six.moves.configparser import ConfigParser @@ -14,16 +13,17 @@ sys.path.insert(1, os.path.join(os.path.dirname(__file__))) import tool_shed.webapp.config as tool_shed_config from galaxy.security.idencoding import IdEncodingHelper +from galaxy.security.validate_user_input import ( + validate_email_str, + validate_password_str, + validate_publicname_str +) from tool_shed.webapp.model import mapping from bootstrap_util import admin_user_info # noqa: I100,I201 log = logging.getLogger(__name__) -VALID_PUBLICNAME_RE = re.compile(r"^[a-z0-9\-]+$") -VALID_EMAIL_RE = re.compile(r"[^@]+@[^@]+\.[^@]+") - - class BootstrapApplication(object): """ Creates a basic Tool Shed application in order to discover the database connection and use SQL @@ -84,44 +84,15 @@ def create_user(app): def validate(email, password, username): - message = validate_email(email) - if not message: - message = validate_password(password) - if not message: - message = validate_publicname(username) + message = "\n".join([validate_email_str(email), + validate_password_str(password, password), + validate_publicname_str(username)]).rstrip() return message -def validate_email(email): - """Validates the email format.""" - message = '' - if not(VALID_EMAIL_RE.match(email)): - message = "Please enter a real email address." - elif len(email) > 255: - message = "Email address exceeds maximum allowable length." - return message - - -def validate_password(password): - if len(password) < 6: - return "Use a password of at least 6 characters" - return '' - - -def validate_publicname(username): - """Validates the public username.""" - if len(username) < 3: - return "Public name must be at least 3 characters in length" - if len(username) > 255: - return "Public name cannot be more than 255 characters in length" - if not(VALID_PUBLICNAME_RE.match(username)): - return "Public name must contain only lower-case letters, numbers and '-'" - return '' - - if __name__ == "__main__": parser = optparse.OptionParser(description='Create a user with API key.') - parser.add_option('-c', dest='config', action='store', help='.ini file to retried toolshed configuration from') + parser.add_option('-c', dest='config', action='store', help='.ini file to retrieve toolshed configuration from') (args, options) = parser.parse_args() ini_file = args.config config_parser = ConfigParser({'here': os.getcwd()})