Development
This commit is contained in:
parent
1a473296b7
commit
43c4cf380d
5 changed files with 71 additions and 62 deletions
|
|
@ -71,7 +71,8 @@ def _db_conn():
|
|||
vlan TEXT NOT NULL DEFAULT '',
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
date_set INTEGER NOT NULL,
|
||||
valid_for INTEGER DEFAULT NULL
|
||||
session_seconds INTEGER NOT NULL DEFAULT 0,
|
||||
expires_seconds INTEGER NOT NULL DEFAULT 0
|
||||
)
|
||||
""")
|
||||
conn.execute("""
|
||||
|
|
@ -103,33 +104,33 @@ def _hash_password(plaintext, digest_type):
|
|||
raise ValueError(f"Unknown digest_type: {digest_type}")
|
||||
|
||||
|
||||
def _parse_valid_for(value_str, unit_str):
|
||||
"""Return valid_for in seconds (int) or None for no expiry. Value of 0 means no expiry."""
|
||||
def _parse_session_seconds(value_str, unit_str):
|
||||
"""Return session_seconds (int). 0 means no limit."""
|
||||
unit_str = (unit_str or '').strip()
|
||||
if not value_str or not value_str.strip():
|
||||
return None
|
||||
return 0
|
||||
try:
|
||||
n = int(value_str.strip())
|
||||
if n <= 0:
|
||||
return None
|
||||
return 0
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
return 0
|
||||
multipliers = {'hours': 3600, 'days': 86400}
|
||||
mult = multipliers.get(unit_str)
|
||||
if mult is None:
|
||||
return None
|
||||
return 0
|
||||
return n * mult
|
||||
|
||||
|
||||
def _valid_for_to_display(valid_for):
|
||||
def _session_seconds_to_display(session_seconds):
|
||||
"""Return (value_str, unit_str) for form pre-population."""
|
||||
if valid_for is None:
|
||||
if not session_seconds:
|
||||
return '0', 'hours'
|
||||
if valid_for % 86400 == 0:
|
||||
return str(valid_for // 86400), 'days'
|
||||
if valid_for % 3600 == 0:
|
||||
return str(valid_for // 3600), 'hours'
|
||||
return str(valid_for // 3600 or 1), 'hours'
|
||||
if session_seconds % 86400 == 0:
|
||||
return str(session_seconds // 86400), 'days'
|
||||
if session_seconds % 3600 == 0:
|
||||
return str(session_seconds // 3600), 'hours'
|
||||
return str(session_seconds // 3600 or 1), 'hours'
|
||||
|
||||
|
||||
def _row_index():
|
||||
|
|
@ -184,10 +185,10 @@ def addedit():
|
|||
flash('802.1X credentials cannot be assigned to a VPN VLAN.', 'error')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
enabled = 'enabled' in request.form
|
||||
valid_for = _parse_valid_for(
|
||||
request.form.get('valid_for_value', ''),
|
||||
request.form.get('valid_for_unit', 'never'),
|
||||
enabled = 'enabled' in request.form
|
||||
session_seconds = _parse_session_seconds(
|
||||
request.form.get('session_seconds_value', ''),
|
||||
request.form.get('session_seconds_unit', 'hours'),
|
||||
)
|
||||
|
||||
if not username:
|
||||
|
|
@ -222,10 +223,10 @@ def addedit():
|
|||
conn.execute(
|
||||
"""UPDATE credentials
|
||||
SET username=?, password=?, description=?, user_type=?, digest_type=?,
|
||||
vlan=?, enabled=?, date_set=?, valid_for=?
|
||||
vlan=?, enabled=?, date_set=?, session_seconds=?
|
||||
WHERE id=?""",
|
||||
(username, stored_password, description, user_type, stored_digest_type,
|
||||
vlan, int(enabled), date_set, valid_for, existing['id']),
|
||||
vlan, int(enabled), date_set, session_seconds, existing['id']),
|
||||
)
|
||||
conn.commit()
|
||||
except sqlite3.IntegrityError:
|
||||
|
|
@ -251,10 +252,10 @@ def addedit():
|
|||
try:
|
||||
conn.execute(
|
||||
"""INSERT INTO credentials
|
||||
(username, password, description, user_type, digest_type, vlan, enabled, date_set, valid_for)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
||||
(username, password, description, user_type, digest_type, vlan, enabled, date_set, session_seconds, expires_seconds)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
||||
(username, hashed, description, user_type, digest_type,
|
||||
vlan, int(enabled), int(time.time()), valid_for),
|
||||
vlan, int(enabled), int(time.time()), session_seconds, 0),
|
||||
)
|
||||
conn.commit()
|
||||
except sqlite3.IntegrityError:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue