Add Days field to the TUI cert form
This commit is contained in:
@@ -791,15 +791,17 @@ class CertFormResult:
|
||||
email: str
|
||||
password: str
|
||||
confirmed: bool
|
||||
days: str = ""
|
||||
|
||||
|
||||
_FORM_FIELDS = ("cn", "email", "password")
|
||||
_FORM_FIELDS = ("cn", "email", "days", "password")
|
||||
_FORM_LABELS = {
|
||||
"cn": "CN",
|
||||
"email": "Email",
|
||||
"days": "Days (blank = EasyRSA default)",
|
||||
"password": "Password",
|
||||
}
|
||||
_FORM_FIELD_Y = {"cn": 3, "email": 5, "password": 7}
|
||||
_FORM_FIELD_Y = {"cn": 3, "email": 5, "days": 7, "password": 9}
|
||||
|
||||
|
||||
def show_cert_form(
|
||||
@@ -807,6 +809,7 @@ def show_cert_form(
|
||||
cn: str = "",
|
||||
email: str = "",
|
||||
password: str = "",
|
||||
days: str = "",
|
||||
cn_readonly: bool = False,
|
||||
) -> CertFormResult:
|
||||
"""Modal cert-detail form.
|
||||
@@ -816,7 +819,7 @@ def show_cert_form(
|
||||
# Clamp to the screen so curses.newwin() can't raise "curses function
|
||||
# returned NULL" outright on a terminal narrower/shorter than the usual
|
||||
# 80x24 (e.g. a serial console with an unusually small viewport).
|
||||
h, w = min(13, sh), min(62, sw)
|
||||
h, w = min(15, sh), min(62, sw)
|
||||
try:
|
||||
win = curses.newwin(h, w, max(0, (sh - h) // 2), max(0, (sw - w) // 2))
|
||||
except curses.error:
|
||||
@@ -826,7 +829,7 @@ def show_cert_form(
|
||||
|
||||
_BTN_CONTINUE = "_continue"
|
||||
_BTN_CANCEL = "_cancel"
|
||||
_BTN_Y = 9
|
||||
_BTN_Y = 11
|
||||
_btn_x_cont = (w - 26) // 2 # "[ Continue ]"=12 + gap=4 + "[ Cancel ]"=10 = 26
|
||||
_btn_x_canc = _btn_x_cont + 16
|
||||
|
||||
@@ -835,18 +838,32 @@ def show_cert_form(
|
||||
fields: Dict[str, InputField] = {
|
||||
"cn": InputField(win, _FORM_FIELD_Y["cn"], 3, fw, initial=cn),
|
||||
"email": InputField(win, _FORM_FIELD_Y["email"], 3, fw, initial=email),
|
||||
"days": InputField(win, _FORM_FIELD_Y["days"], 3, fw, initial=days,
|
||||
allowed=string.digits),
|
||||
"password": InputField(win, _FORM_FIELD_Y["password"], 3, fw,
|
||||
initial=password if password else generate_password(),
|
||||
mask=True),
|
||||
}
|
||||
focus = 0
|
||||
error = ""
|
||||
|
||||
def _submit() -> CertFormResult:
|
||||
def _submit() -> Optional[CertFormResult]:
|
||||
nonlocal error
|
||||
try:
|
||||
# Validate with the same resolver the CLI and .conf use, so what
|
||||
# counts as valid never diverges between entry points.
|
||||
days_value = resolve_cert_days(fields["days"].value, "Days")
|
||||
except ConfigError:
|
||||
# The resolver's full sentence does not fit a 62-column window.
|
||||
error = "Days must be a positive number, or blank to inherit"
|
||||
return None
|
||||
error = ""
|
||||
final_cn = cn if cn_readonly else fields["cn"].value
|
||||
return CertFormResult(
|
||||
cn=final_cn,
|
||||
email=fields["email"].value,
|
||||
password=fields["password"].value,
|
||||
days=days_value,
|
||||
confirmed=True,
|
||||
)
|
||||
|
||||
@@ -887,8 +904,11 @@ def show_cert_form(
|
||||
pass
|
||||
|
||||
hint = "Tab=next F5=regen pwd Ctrl-G=confirm Esc=cancel"
|
||||
hint_attr = curses.color_pair(COLOR_DISABLED)
|
||||
if error:
|
||||
hint, hint_attr = error, curses.color_pair(COLOR_ERROR)
|
||||
try:
|
||||
win.addstr(h - 2, 2, hint[:w - 4], curses.color_pair(COLOR_DISABLED))
|
||||
win.addstr(h - 2, 2, hint[:w - 4], hint_attr)
|
||||
except curses.error:
|
||||
pass
|
||||
|
||||
@@ -913,8 +933,11 @@ def show_cert_form(
|
||||
curses.curs_set(0)
|
||||
return CertFormResult(cn=cn, email="", password="", confirmed=False)
|
||||
if key == 0x07: # Ctrl-G: immediate submit
|
||||
curses.curs_set(0)
|
||||
return _submit()
|
||||
result = _submit()
|
||||
if result is not None:
|
||||
curses.curs_set(0)
|
||||
return result
|
||||
continue
|
||||
if key in (9, curses.KEY_DOWN): # Tab / Down: next
|
||||
focus = (focus + 1) % len(active)
|
||||
continue
|
||||
@@ -934,9 +957,13 @@ def show_cert_form(
|
||||
)
|
||||
continue
|
||||
if key in (10, 13, curses.KEY_ENTER, ord(" ")):
|
||||
curses.curs_set(0)
|
||||
if current == _BTN_CONTINUE:
|
||||
return _submit()
|
||||
result = _submit()
|
||||
if result is not None:
|
||||
curses.curs_set(0)
|
||||
return result
|
||||
continue
|
||||
curses.curs_set(0)
|
||||
return CertFormResult(cn=cn, email="", password="", confirmed=False)
|
||||
else:
|
||||
if key in (10, 13, curses.KEY_ENTER):
|
||||
|
||||
Reference in New Issue
Block a user