From bc5fe110a71363ba53335783c39a407d49ff2c5a Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Thu, 13 Jul 2023 10:39:13 +0200 Subject: [PATCH] fix: include missing data when creating profiles --- src/asp.js | 31 +++++++++++++++++++++++++++---- src/openpgp.js | 38 ++++++++++++++------------------------ 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/asp.js b/src/asp.js index 2ed52b8..2b33ab1 100644 --- a/src/asp.js +++ b/src/asp.js @@ -19,7 +19,7 @@ import { base32, base64url } from 'rfc4648' import { Claim } from './claim.js' import { Persona } from './persona.js' import { Profile } from './profile.js' -import { ProfileType } from './enums.js' +import { ProfileType, PublicKeyEncoding, PublicKeyFetchMethod, PublicKeyType } from './enums.js' const SupportedCryptoAlg = ['EdDSA', 'ES256', 'ES256K', 'ES384', 'ES512'] @@ -67,7 +67,12 @@ export async function fetchASPE (uri) { throw new Error(`Error fetching Keybase key: ${e.message}`) } - return await parseProfileJws(profileJws, uri) + const profile = await parseProfileJws(profileJws, uri) + profile.publicKey.fetch.method = PublicKeyFetchMethod.ASPE + profile.publicKey.fetch.query = uri + profile.publicKey.fetch.resolvedUrl = profileUrl + + return profile } /** @@ -135,9 +140,27 @@ export async function parseProfileJws (profileJws, uri) { pe.setDescription(profileDescription) } - const pr = new Profile(ProfileType.ASP, uri, [pe]) + const profile = new Profile(ProfileType.ASP, uri, [pe]) + profile.publicKey.fingerprint = fp + profile.publicKey.encoding = PublicKeyEncoding.JWK + profile.publicKey.encodedKey = JSON.stringify(protectedHeader.jwk) + profile.publicKey.key = protectedHeader.jwk - return pr + switch (protectedHeader.alg) { + case 'ES256': + profile.publicKey.keyType = PublicKeyType.ES256 + break + + case 'EdDSA': + profile.publicKey.keyType = PublicKeyType.EDDSA + break + + default: + profile.publicKey.keyType = PublicKeyType.UNKNOWN + break + } + + return profile } /** diff --git a/src/openpgp.js b/src/openpgp.js index a4d199b..af5e9fc 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -66,10 +66,6 @@ export async function fetchHKP (identifier, keyserverDomain) { }) const profile = await parsePublicKey(publicKey) - profile.publicKey.keyType = PublicKeyType.OPENPGP - profile.publicKey.encoding = PublicKeyEncoding.ARMORED_PGP - profile.publicKey.encodedKey = publicKey.armor() - profile.publicKey.key = publicKey profile.publicKey.fetch.method = PublicKeyFetchMethod.HKP profile.publicKey.fetch.query = identifier @@ -108,10 +104,6 @@ export async function fetchWKD (identifier) { }) const profile = await parsePublicKey(publicKey) - profile.publicKey.keyType = PublicKeyType.OPENPGP - profile.publicKey.encoding = PublicKeyEncoding.ARMORED_PGP - profile.publicKey.encodedKey = publicKey.armor() - profile.publicKey.key = publicKey profile.publicKey.fetch.method = PublicKeyFetchMethod.WKD profile.publicKey.fetch.query = identifier @@ -155,10 +147,6 @@ export async function fetchKeybase (username, fingerprint) { }) const profile = await parsePublicKey(publicKey) - profile.publicKey.keyType = PublicKeyType.OPENPGP - profile.publicKey.encoding = PublicKeyEncoding.ARMORED_PGP - profile.publicKey.encodedKey = publicKey.armor() - profile.publicKey.key = publicKey profile.publicKey.fetch.method = PublicKeyFetchMethod.HTTP profile.publicKey.fetch.query = null profile.publicKey.fetch.resolvedUrl = keyLink @@ -189,10 +177,6 @@ export async function fetchPlaintext (rawKeyContent) { }) const profile = await parsePublicKey(publicKey) - profile.publicKey.keyType = PublicKeyType.OPENPGP - profile.publicKey.encoding = PublicKeyEncoding.ARMORED_PGP - profile.publicKey.encodedKey = publicKey.armor() - profile.publicKey.key = publicKey return profile } @@ -286,18 +270,18 @@ export async function fetch (identifier) { } /** - * Process a public key to get user data and claims + * Process a public key to get a profile * @function - * @param {PublicKey} publicKey - The public key to process + * @param {PublicKey} publicKey - The public key to parse * @returns {Promise} * @example * const key = doip.keys.fetchURI('hkp:alice@domain.tld'); - * const data = doip.keys.process(key); - * data.users[0].claims.forEach(claim => { + * const profile = doip.keys.parsePublicKey(key); + * profile.personas[0].claims.forEach(claim => { * console.log(claim.uri); * }); */ -async function parsePublicKey (publicKey) { +export async function parsePublicKey (publicKey) { if (!(publicKey && (publicKey instanceof PublicKey))) { throw new Error('Invalid public key') } @@ -334,8 +318,14 @@ async function parsePublicKey (publicKey) { personas.push(pe) }) - const pr = new Profile(ProfileType.OPENPGP, `openpgp4fpr:${fingerprint}`, personas) - pr.primaryPersonaIndex = primaryUser.index + const profile = new Profile(ProfileType.OPENPGP, `openpgp4fpr:${fingerprint}`, personas) + profile.primaryPersonaIndex = primaryUser.index - return pr + profile.publicKey.keyType = PublicKeyType.OPENPGP + profile.publicKey.fingerprint = fingerprint + profile.publicKey.encoding = PublicKeyEncoding.ARMORED_PGP + profile.publicKey.encodedKey = publicKey.armor() + profile.publicKey.key = publicKey + + return profile }