fix: include missing data when creating profiles

This commit is contained in:
Yarmo Mackenbach 2023-07-13 10:39:13 +02:00
parent 77bfb03ea6
commit bc5fe110a7
No known key found for this signature in database
GPG key ID: 3C57D093219103A3
2 changed files with 41 additions and 28 deletions

View file

@ -19,7 +19,7 @@ import { base32, base64url } from 'rfc4648'
import { Claim } from './claim.js' import { Claim } from './claim.js'
import { Persona } from './persona.js' import { Persona } from './persona.js'
import { Profile } from './profile.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'] 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}`) 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) 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
} }
/** /**

View file

@ -66,10 +66,6 @@ export async function fetchHKP (identifier, keyserverDomain) {
}) })
const profile = await parsePublicKey(publicKey) 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.method = PublicKeyFetchMethod.HKP
profile.publicKey.fetch.query = identifier profile.publicKey.fetch.query = identifier
@ -108,10 +104,6 @@ export async function fetchWKD (identifier) {
}) })
const profile = await parsePublicKey(publicKey) 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.method = PublicKeyFetchMethod.WKD
profile.publicKey.fetch.query = identifier profile.publicKey.fetch.query = identifier
@ -155,10 +147,6 @@ export async function fetchKeybase (username, fingerprint) {
}) })
const profile = await parsePublicKey(publicKey) 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.method = PublicKeyFetchMethod.HTTP
profile.publicKey.fetch.query = null profile.publicKey.fetch.query = null
profile.publicKey.fetch.resolvedUrl = keyLink profile.publicKey.fetch.resolvedUrl = keyLink
@ -189,10 +177,6 @@ export async function fetchPlaintext (rawKeyContent) {
}) })
const profile = await parsePublicKey(publicKey) 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 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 * @function
* @param {PublicKey} publicKey - The public key to process * @param {PublicKey} publicKey - The public key to parse
* @returns {Promise<Profile>} * @returns {Promise<Profile>}
* @example * @example
* const key = doip.keys.fetchURI('hkp:alice@domain.tld'); * const key = doip.keys.fetchURI('hkp:alice@domain.tld');
* const data = doip.keys.process(key); * const profile = doip.keys.parsePublicKey(key);
* data.users[0].claims.forEach(claim => { * profile.personas[0].claims.forEach(claim => {
* console.log(claim.uri); * console.log(claim.uri);
* }); * });
*/ */
async function parsePublicKey (publicKey) { export async function parsePublicKey (publicKey) {
if (!(publicKey && (publicKey instanceof PublicKey))) { if (!(publicKey && (publicKey instanceof PublicKey))) {
throw new Error('Invalid public key') throw new Error('Invalid public key')
} }
@ -334,8 +318,14 @@ async function parsePublicKey (publicKey) {
personas.push(pe) personas.push(pe)
}) })
const pr = new Profile(ProfileType.OPENPGP, `openpgp4fpr:${fingerprint}`, personas) const profile = new Profile(ProfileType.OPENPGP, `openpgp4fpr:${fingerprint}`, personas)
pr.primaryPersonaIndex = primaryUser.index 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
} }