1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/accounts/utils.py
catborise 50fa42181b fix(ui): the settings index stays at hand on a phone; one wording in Turkish
On a phone the settings page opened with its group index on seven lines, and
the index was gone once the page, nine screens long, was scrolled. Below the lg
breakpoint it is now one row that scrolls sideways and sticks under the navbar
(navbar_height.js gives what sticks the navbar's height, which differs between
themes and grows when the phone menu opens).

Turkish said "VM" in 65 messages and "sanal makine" everywhere else; it now
says "sanal makine" throughout. Headings and buttons that still capitalised
every word follow the panel's sentence case, in English too (Change password,
SSH keys, Power off, ...). Wrong translations fixed: a password that is set
read as the command "Ayarla", the firmware setting was called a chip type,
Create flavor said Create user, the console viewport setting was called a port,
a DRBD typo, and host and interface are sunucu and arabirim as on every page.
A group's save button reads "Kaydet: <group>".
2026-11-22 16:43:34 +03:00

78 lines
3.1 KiB
Python

from decimal import Decimal
import segno
from cryptography.exceptions import UnsupportedAlgorithm
from cryptography.hazmat.primitives.serialization import load_ssh_public_key
from django.contrib.auth import get_user_model
from django.core.mail import send_mail
from django.core.signing import TimestampSigner
from django.db import transaction
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _
from django_otp import devices_for_user
from django_otp.plugins.otp_totp.models import TOTPDevice
def get_existing_totp_device(user):
"""The user's confirmed TOTP device, or None. Unlike get_user_totp_device
it never creates one, so an unauthenticated caller cannot turn a
password-only account into an OTP-required one."""
for device in devices_for_user(user, confirmed=True):
if isinstance(device, TOTPDevice):
return device
return None
def get_user_totp_device(user):
"""The user's TOTP device, made if they have none. The user's row is
locked meanwhile: two requests at once (a double click) make one device,
not two that both log in."""
with transaction.atomic():
get_user_model().objects.select_for_update().get(pk=user.pk)
for device in devices_for_user(user):
if isinstance(device, TOTPDevice):
return device
return user.totpdevice_set.create()
def validate_ssh_key(key):
"""An OpenSSH public key: its type, a whole key of that type (security
keys too) and an optional comment, which may hold spaces: one line, as
authorized_keys holds it."""
if "\n" in key.strip() or "\r" in key.strip():
return False
try:
load_ssh_public_key(" ".join(key.split(maxsplit=2)[:2]).encode())
except (ValueError, UnsupportedAlgorithm):
return False
return True
# the link a mail carries to its device's OTP QR code: the mail holds no secret.
# Links once named the user: this salt refuses those, whose id may be another's device.
OTP_LINK = TimestampSigner(salt="accounts.otp_qr.device")
OTP_LINK_MAX_AGE = 3600
def send_email_with_otp(request, device):
"""Mail the device's user a link to the page of its OTP QR code, good for
OTP_LINK_MAX_AGE: the QR code's secret is never in a mailbox. The token
is the URL's fragment, which no request carries: it is in no access log.
It names the device, so after an OTP reset it shows nothing."""
user = device.user
url = request.build_absolute_uri(reverse("accounts:otp_qr")) + "#" + OTP_LINK.sign(str(device.pk))
send_mail(
_("OTP QR code"),
_("Open this link within an hour to see the OTP QR code of account '%(user)s':\n\n%(url)s")
% {"user": user, "url": url},
None,
[user.email],
fail_silently=False,
)
def otp_qr_svg(device):
"""The device's QR code as inline SVG: 1.8 mm modules, error correction M, a 4-module border."""
qr = segno.make(device.config_url, error="m", mode="byte", encoding="utf-8", micro=False)
return mark_safe(qr.svg_inline(scale=Decimal("1.8"), unit="mm", border=4, dark="black", light="white"))