// An "SSH keys" settings page: a pasted key checked and shown as GitHub shows it.
//
//   quillo run ssh_keys.qwe      then open http://localhost:8080/keys

use "ssh_keys.qwer"

let KEYS = read_file("ssh_keys_sample.pub")?

route GET /keys = ssh_authorized_keys(KEYS)? | map((k) => { title: k.comment, type: k.type, bits: k.bits, fingerprint: k.fingerprint })

route POST /keys { form: { key: str } } {
    let k = ssh_key_info(key)
    if is_err(k) { reply 400 k.error }
    if k.weak { reply 400 "Use an Ed25519 key, or RSA of 2048 bits or more." }
    if any(split(KEYS, "\n") | filter((l) => trim(l) != ""), (l) => ssh_key_same(l, key)?) { reply 409 "That key is already added." }
    { added: k.fingerprint }
}

route GET /check {
    let first = split(KEYS, "\n")[0]
    {
        info: ssh_key_info(first)?,
        same_with_other_comment: ssh_key_same(first, replace(first, "ada@laptop", "ada@desktop"))?,
        private_refused: ssh_key_info("-----BEGIN OPENSSH PRIVATE KEY-----").error,
    }
}
