authkeyline
Parse, check and safely build OpenSSH authorized_keys lines into typed records,
the way sshd reads them. Zero dependencies.
Install
pip install authkeyline
Usage
The examples use a made-up Ed25519 key (its 32 bytes are 0 to 31); every block runs as part of the test suite and prints what its comments say.
from authkeyline import parse_line
BLOB = "AAAAC3NzaC1lZDI1NTE5AAAAIAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4f"
key = parse_line(
f'restrict,command="/usr/bin/backup --all",from="10.0.0.1,10.0.0.2" ssh-ed25519 {BLOB} alice@host'
)
print(key.key_type, key.comment)
print(key.values("from"))
print(key.has_flag("RESTRICT"))
print(key.fingerprint())
print(key.fingerprint("md5"))
# ssh-ed25519 alice@host
# ('10.0.0.1,10.0.0.2',)
# True
# SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA
# MD5:0f:a2:0a:d7:38:3e:65:45:08:6b:63:84:1c:ff:dc:ba
Option values come back exactly as sshd sees them. Only \" is an escape inside the
quotes; every other backslash is kept:
from authkeyline import parse_line
BLOB = "AAAAC3NzaC1lZDI1NTE5AAAAIAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4f"
key = parse_line(f'command="echo \\"hi\\" c:\\\\temp",environment="LANG=C" ssh-ed25519 {BLOB}')
print(key.options[0].value)
print(key.environment())
# echo "hi" c:\\temp
# {'LANG': 'C'}
parse_line refuses what sshd refuses, with the reason: unknown options, unquoted
values, a repeated command, a malformed permitopen, environment or expiry-time,
an unknown key type, a blob that is not base64, whose embedded type differs from the line,
whose Ed25519 key is not 32 bytes, whose ECDSA point is off the curve, or whose RSA modulus
is under 1024 bits:
from authkeyline import AuthKeyError, parse_line
BLOB = "AAAAC3NzaC1lZDI1NTE5AAAAIAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4f"
for line in [f"command=ls ssh-ed25519 {BLOB}", f"ssh-rsa {BLOB}", "no-pty ssh-ed25519 AAAA"]:
try:
parse_line(line)
except AuthKeyError as error:
print(error)
# value of 'command' must be in double quotes
# line says 'ssh-rsa' but the key blob holds 'ssh-ed25519'
# key blob is truncated
Building lines safely
build_line writes a record back and refuses anything that would not read back as the
same record: unknown option names, values with line breaks or a trailing backslash, a
comment that breaks the line. Values cannot smuggle in other options or keys.
from authkeyline import AuthKeyError, KeyOption, build_line, parse_line
import dataclasses
BLOB = "AAAAC3NzaC1lZDI1NTE5AAAAIAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4f"
public = parse_line(f"ssh-ed25519 {BLOB} deploy")
restricted = dataclasses.replace(
public, options=(KeyOption("restrict"), KeyOption("command", 'rsync --server "a b"'))
)
print(build_line(restricted))
try:
build_line(dataclasses.replace(public, comment="x\nssh-ed25519 AAAA intruder"))
except AuthKeyError as error:
print(error)
# restrict,command="rsync --server \"a b\"" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4f deploy
# comment holds a NUL or a line break
Whole files
from authkeyline import check_file, parse_file
BLOB = "AAAAC3NzaC1lZDI1NTE5AAAAIAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4f"
text = f"# team keys\nssh-ed25519 {BLOB} one\n\nbogus ssh-ed25519 {BLOB} two\n"
print(check_file(text))
print(len(parse_file(text.replace("bogus ", ""))))
# [LineProblem(line=4, message="unknown option 'bogus'")]
# 2
parse_file raises on the first bad line with error.line set; check_file lists them
all. Blank lines and # comments are skipped, as sshd does. To read a real file, pass
its text: parse_file(Path.home().joinpath(".ssh/authorized_keys").read_text()).
Command line
authkeyline ~/.ssh/authorized_keys # one line per key, like ssh-keygen -l
authkeyline -E md5 authorized_keys # MD5 fingerprints
authkeyline --json authorized_keys # options, sizes, certificate details
authkeyline --check authorized_keys # only report broken lines
Broken lines go to standard error as FILE:LINE: reason and the exit status is 1; a file
that cannot be read gives status 2. The listing matches ssh-keygen -l byte for byte,
comments in any encoding included (the test suite compares them).
API
| Name | What it does |
|---|---|
parse_line(line) |
One line to an AuthorizedKey, checked like sshd; raises AuthKeyError |
parse_file(text) |
Every key of a file; the first bad line raises with .line set |
check_file(text) |
Every broken line as a LineProblem(line, message) |
build_line(key) |
A record back to a line that reads back the same, or AuthKeyError |
AuthorizedKey |
options, key_type, key_base64, comment; public_key(), fingerprint(), values(), has_flag(), environment(), key_bytes() |
KeyOption |
name as written and value (None for a flag) |
PublicKey |
key_type, short_type, bits, application, certificate, fingerprint() |
Certificate |
cert_type, serial, key_id, principals, validity, critical options, extensions, ca_key, signature_type |
LineProblem |
line number and message |
KEY_TYPES |
The key type names sshd accepts, certificates included |
AuthKeyError |
A ValueError with reason and line |
What it follows
- The options grammar and rules of OpenSSH's
sshauthopt_parseand the option list of sshd(8): case-insensitive names, quoted values,no-forms, singlecommand,principalsandfrom,permitopenandpermitlistentargets,tunnel,expiry-time,environment(first value of a name wins). - The key type table of OpenSSH (
ssh-ed25519,sk-ssh-ed25519@openssh.com,ecdsa-sha2-nistp256/384/521,sk-ecdsa-sha2-nistp256@openssh.com,ssh-rsa,ssh-mldsa44-ed25519, their certificates, and thersa-sha2-256/512names that sshd reads as RSA keys), the blob layouts of RFC 4253, RFC 5656 and RFC 8709, and the certificate format of draft-miller-ssh-cert. - Fingerprints are computed over the bare key, as
ssh-keygen -ldoes, so a certificate has the fingerprint of its key.
Where it differs from sshd, on purpose:
- Certificate signatures are not verified (that needs a crypto library), so a certificate
with a forged signature parses while sshd skips it. The signature type is checked
against the CA key. Certificates are rare in
authorized_keys; CA keys markedcert-authorityare plain keys and fully checked. from=patterns are kept as text, not matched against a client.- Port names such as
host:sshinpermitopenare refused, since sshd resolves them through the local services database, which differs per system. build_linekeeps comments and values to one line of text without NUL or CR. sshd would cut a line at a NUL and ignore a CR in the comment, andparse_linereads such lines the same way, but a written line never contains them.
Why
An authorized_keys line packs an options field, a key type, a base64 blob and a comment
into one string, and the options field has its own grammar. Splitting on whitespace or
commas gets it wrong, and a builder that quotes values naively lets a value add options or
whole keys. This library reads a line as sshd does (with the differences listed above),
says why sshd would skip it, and writes lines that cannot be steered.
License
MIT. See LICENSE.
Links
- Website: https://lianes8studio.com
- GitHub: https://github.com/JaviLianes8
- LinkedIn: https://www.linkedin.com/in/jlianes/
Metadata
Release files for authkeyline 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| authkeyline-0.2.0.tar.gz | 40.6 kB | Without provenance | Details for authkeyline-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| authkeyline-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for authkeyline-0.2.0-py3-none-any.whl |
Total release size: 63.6 kB
Release files / authkeyline-0.2.0.tar.gz
| Download URL | authkeyline-0.2.0.tar.gz |
|---|---|
| Size | 40.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b067c2040e87c46c11e268f48e0fd10cd272507e8f9f94e9dac09456893353a2
|
|
BLAKE2b-256 checksum How to use checksums |
2afd3c5f9028cc7e31bf8643945180e64574f2cd1015da1d9c912688f106f8dc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / authkeyline-0.2.0-py3-none-any.whl
| Download URL | authkeyline-0.2.0-py3-none-any.whl |
|---|---|
| Size | 23.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
48fba934977c73505f9a5d104ff623a1190eed191cdfc22c91e181388e88a16c
|
|
BLAKE2b-256 checksum How to use checksums |
891f3410b502623c3547d65e37f0f7aec1e460c489adfbc4fdc202b552d84951
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for authkeyline 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.