Commit 31ed54bf authored by rgaudin's avatar rgaudin
Browse files

Fixed #118: CLI and --config arguments checked

Moved checked into utils ; shared between cli and GUI
parent 93e6959b
......@@ -14,7 +14,9 @@ from util import CancelEvent
from util import get_free_space_in_dir
from util import human_readable_size, get_cache
from util import CLILogger, b64decode
from util import check_user_inputs
import tzlocal
import humanfriendly
CANCEL_TIMEOUT = 5
......@@ -120,8 +122,8 @@ for catalog in YAML_CATALOGS:
languages = [code for code, language in data.ideascube_languages]
defaults = {
'name': "mybox",
'timezone': "Europe/Paris",
'name': "Kiwix Plug",
'timezone': str(tzlocal.get_localzone()),
'language': "en",
'size': "8GB",
'build_dir': ".",
......@@ -198,7 +200,7 @@ if args.catalog:
if args.admin_account:
admin_account = { "login": args.admin_account[0], "pwd": args.admin_account[1] }
else:
admin_account = None
admin_account = {"login": "admin", "pwd": "admin-password"}
# parse requested size
try:
......@@ -209,6 +211,32 @@ except Exception:
else:
args.human_size = human_readable_size(args.size, False)
# check arguments
valid_project_name, valid_language, \
valid_timezone, valid_wifi_pwd, \
valid_admin_login, valid_admin_pwd, valid_zim = check_user_inputs(
project_name=args.name,
language=args.language,
timezone=args.timezone,
wifi_pwd=args.wifi_pwd,
admin_login=admin_account['login'],
admin_pwd=admin_account['pwd'],
zim_install=args.zim_install)
for key, is_valid in {
'name': valid_project_name,
'language': valid_language,
'timezone': valid_timezone,
'wifi_pwd': valid_wifi_pwd,
'admin_login': valid_admin_login,
'admin_password': valid_admin_pwd,
'zim_install': valid_zim,
}.items():
if not is_valid:
print("Invalid argument for `{key}`".format(key=key))
sys.exit(1)
# display configuration and offer time to cancel
print("Kiwix-plug installer configuration:")
keys = args.__dict__.keys()
......
......@@ -18,9 +18,9 @@ from util import human_readable_size, ONE_GB, ONE_MiB
from util import get_free_space_in_dir
from util import relpathto
from util import b64encode, b64decode
from util import check_user_inputs
import data
import iso639
import string
import humanfriendly
import webbrowser
gi.require_version('Gtk', '3.0')
......@@ -879,50 +879,60 @@ class Application:
def run_installation_button_clicked(self, button):
all_valid = True
# capture input
project_name = self.component.project_name_entry.get_text()
allowed_chars = set(string.ascii_uppercase + string.ascii_lowercase + string.digits + '-' + ' ')
condition = len(project_name) >= 1 and len(project_name) <= 64 and set(project_name) <= allowed_chars
validate_label(self.component.project_name_label, condition)
self.component.project_name_constraints_revealer.set_reveal_child(not condition)
all_valid = all_valid and condition
language_id = self.component.language_combobox.get_active()
language = data.ideascube_languages[language_id][0]
condition = language_id != -1
validate_label(self.component.language_label, condition)
all_valid = all_valid and condition
language = data.ideascube_languages[
self.component.language_combobox.get_active()][0]
timezone = self.component.timezone_tree_store[
self.component.timezone_combobox.get_active()][0]
wifi_pwd = None if self.component.wifi_password_switch.get_state() \
else self.component.wifi_password_entry.get_text()
admin_login = self.component.admin_account_login_entry.get_text()
admin_pwd = self.component.admin_account_pwd_entry.get_text()
zim_install = [zim[0] for zim in self.component.zim_list_store
if zim[8]]
# validate inputs
valid_project_name, valid_language, \
valid_timezone, valid_wifi_pwd, \
valid_admin_login, valid_admin_pwd = check_user_inputs(
project_name=self.component.project_name_entry.get_text(),
language=data.ideascube_languages[
self.component.language_combobox.get_active()][0],
timezone=self.component.timezone_tree_store[
self.component.timezone_combobox.get_active()][0],
wifi_pwd=None
if self.component.wifi_password_switch.get_state()
else self.component.wifi_password_entry.get_text(),
admin_login=self.
component.admin_account_login_entry.get_text(),
admin_pwd=self.component.admin_account_pwd_entry.get_text())
# project name
validate_label(self.component.project_name_label, valid_project_name)
self.component.project_name_constraints_revealer.set_reveal_child(
not valid_project_name)
all_valid = all_valid and valid_project_name
timezone_id = self.component.timezone_combobox.get_active()
timezone = self.component.timezone_tree_store[timezone_id][0]
condition = timezone_id != -1
validate_label(self.component.timezone_label, condition)
all_valid = all_valid and condition
if self.component.wifi_password_switch.get_state():
wifi_pwd = None
condition = True
else:
wifi_pwd = self.component.wifi_password_entry.get_text()
condition = len(wifi_pwd) <= 31 and set(wifi_pwd) <= set(string.ascii_uppercase + string.ascii_lowercase + string.digits)
self.component.wifi_password_constraints_revealer.set_reveal_child(not condition)
validate_label(self.component.wifi_password_label, condition)
all_valid = all_valid and condition
# language
validate_label(self.component.language_label, valid_language)
all_valid = all_valid and valid_language
admin_account = {
"login": self.component.admin_account_login_entry.get_text(),
"pwd": self.component.admin_account_pwd_entry.get_text(),
}
login_condition = len(admin_account["login"]) <= 31 and set(admin_account["login"]) <= set(string.ascii_uppercase + string.ascii_lowercase + string.digits + '-' + '_')
pwd_condition = len(admin_account["pwd"]) <= 31 and set(admin_account["pwd"]) <= set(string.ascii_uppercase + string.ascii_lowercase + string.digits + '-' + '_') and (admin_account["pwd"] != admin_account["login"])
# timezone
validate_label(self.component.timezone_label, valid_timezone)
all_valid = all_valid and valid_timezone
validate_label(self.component.admin_account_login_label, login_condition)
validate_label(self.component.admin_account_pwd_label, pwd_condition)
all_valid = all_valid and pwd_condition and login_condition
# wifi passwd
validate_label(self.component.wifi_password_label, valid_wifi_pwd)
self.component.wifi_password_constraints_revealer.set_reveal_child(
not valid_wifi_pwd)
all_valid = all_valid and valid_wifi_pwd
zim_install = []
for zim in self.component.zim_list_store:
if zim[8]:
zim_install.append(zim[0])
# admin account
validate_label(self.component.admin_account_login_label,
valid_admin_login)
validate_label(self.component.admin_account_pwd_label, valid_admin_pwd)
all_valid = all_valid and valid_admin_login and valid_admin_login
output_size = self.get_output_size()
if self.component.output_stack.get_visible_child_name() == "sd_card":
......@@ -1005,7 +1015,7 @@ class Application:
favicon=favicon,
css=css,
build_dir=build_dir,
admin_account=admin_account,
admin_account={"login": admin_login, "pwd": admin_pwd},
done_callback=lambda error: GLib.idle_add(self.installation_done, error))
self.component.window.hide()
......
......@@ -5,6 +5,7 @@ import threading
import signal
import sys
import base64
import string
import hashlib
import tempfile
import ctypes
......@@ -12,9 +13,12 @@ import platform
import datetime
import collections
import pytz
from path import Path
import humanfriendly
import data
ONE_MiB = 2 ** 20
ONE_GiB = 2 ** 30
ONE_GB = int(1e9)
......@@ -387,3 +391,36 @@ def ensure_zip_exfat_compatible(fpath):
except Exception as exp:
return False, [str(exp)]
return len(bad_fnames) == 0, bad_fnames
def check_user_inputs(project_name, language, timezone,
admin_login, admin_pwd, wifi_pwd=None):
allowed_chars = set(string.ascii_uppercase + string.ascii_lowercase
+ string.digits + '-' + ' ')
valid_project_name = len(project_name) >= 1 \
and len(project_name) <= 64 \
and set(project_name) <= allowed_chars
valid_language = language in dict(data.ideascube_languages).keys()
valid_timezone = timezone in pytz.common_timezones
valid_wifi_pwd = len(wifi_pwd) <= 31 \
and set(wifi_pwd) <= set(string.ascii_uppercase
+ string.ascii_lowercase + string.digits
+ '-' + '_') \
if wifi_pwd is not None else True
valid_admin_login = len(admin_login) <= 31 \
and set(admin_login) <= set(string.ascii_uppercase
+ string.ascii_lowercase
+ string.digits + '-' + '_')
valid_admin_pwd = len(admin_pwd) <= 31 \
and set(admin_pwd) <= set(string.ascii_uppercase
+ string.ascii_lowercase
+ string.digits + '-' + '_') \
and (admin_pwd != admin_login)
return valid_project_name, valid_language, valid_timezone, \
valid_wifi_pwd, valid_admin_login, valid_admin_pwd
......@@ -666,8 +666,8 @@ It can be modified after installation using the file:
<object class="GtkLabel" id="no_id_23">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Must contains only 0 to 31 ASCII letters 'A' to 'z'
and digits '0' to '9'</property>
<property name="label" translatable="yes">Must contains only 0 to 31 ASCII letters 'A' to 'z',
digits '0' to '9', hyphen '-' and underscore '_'</property>
<attributes>
<attribute name="foreground" value="#efef29292929"/>
</attributes>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment