aurweb.routers.accounts: strip host out of ssh pubkeys

We must store the paired key, otherwise aurweb-git-auth
will fail.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-06-27 03:54:13 -07:00
parent 97c1247b57
commit 83f93c8dbb

View file

@ -1,4 +1,5 @@
import copy import copy
import logging
import typing import typing
from datetime import datetime from datetime import datetime
@ -24,6 +25,7 @@ from aurweb.scripts.notify import ResetKeyNotification
from aurweb.templates import make_variable_context, render_template from aurweb.templates import make_variable_context, render_template
router = APIRouter() router = APIRouter()
logger = logging.getLogger(__name__)
@router.get("/passreset", response_class=HTMLResponse) @router.get("/passreset", response_class=HTMLResponse)
@ -402,6 +404,10 @@ async def account_register_post(request: Request,
if PK: if PK:
# Get the second element in the PK, which is the actual key. # Get the second element in the PK, which is the actual key.
pubkey = PK.strip().rstrip() pubkey = PK.strip().rstrip()
parts = pubkey.split(" ")
if len(parts) == 3:
# Remove the host part.
pubkey = parts[0] + " " + parts[1]
fingerprint = get_fingerprint(pubkey) fingerprint = get_fingerprint(pubkey)
user.ssh_pub_key = SSHPubKey(UserID=user.ID, user.ssh_pub_key = SSHPubKey(UserID=user.ID,
PubKey=pubkey, PubKey=pubkey,
@ -522,15 +528,19 @@ async def account_edit_post(request: Request,
if PK: if PK:
# Get the second token in the public key, which is the actual key. # Get the second token in the public key, which is the actual key.
pubkey = PK.strip().rstrip() pubkey = PK.strip().rstrip()
parts = pubkey.split(" ")
if len(parts) == 3:
# Remove the host part.
pubkey = parts[0] + " " + parts[1]
fingerprint = get_fingerprint(pubkey) fingerprint = get_fingerprint(pubkey)
if not user.ssh_pub_key: if not user.ssh_pub_key:
# No public key exists, create one. # No public key exists, create one.
user.ssh_pub_key = SSHPubKey(UserID=user.ID, user.ssh_pub_key = SSHPubKey(UserID=user.ID,
PubKey=PK, PubKey=pubkey,
Fingerprint=fingerprint) Fingerprint=fingerprint)
elif user.ssh_pub_key.Fingerprint != fingerprint: elif user.ssh_pub_key.PubKey != pubkey:
# A public key already exists, update it. # A public key already exists, update it.
user.ssh_pub_key.PubKey = PK user.ssh_pub_key.PubKey = pubkey
user.ssh_pub_key.Fingerprint = fingerprint user.ssh_pub_key.Fingerprint = fingerprint
elif user.ssh_pub_key: elif user.ssh_pub_key:
# Else, if the user has a public key already, delete it. # Else, if the user has a public key already, delete it.