(query)
| 29 | * @yield {String|undefined} Armored key with matching uid. Undefined if no key was found. |
| 30 | */ |
| 31 | export async function lookup(query) { |
| 32 | let armoredKey; |
| 33 | if (!query) { |
| 34 | throw new Error('openpgpKeyServer: Skipping lookup without query.'); |
| 35 | } |
| 36 | const response = await self.fetch(url(query)); |
| 37 | if (response.status === 200) { |
| 38 | armoredKey = await response.text(); |
| 39 | } |
| 40 | if (!armoredKey) { |
| 41 | return; |
| 42 | } |
| 43 | // Only the userid matching the email should be imported. |
| 44 | // This avoids usability problems and potential security issues |
| 45 | // when unreleated userids are also part of the key. |
| 46 | let key; |
| 47 | try { |
| 48 | key = await readKey({armoredKey}); |
| 49 | } catch (e) { |
| 50 | throw new Error(`openpgpKeyServer: Failed to parse response '${armoredKey}': ${e.message}`); |
| 51 | } |
| 52 | const filtered = filterUserIdsByEmail(key, query.email); |
| 53 | if (!filtered.users.length) { |
| 54 | throw new Error(`openpgpKeyServer: Response '${armoredKey}': contained no matching userIds.`); |
| 55 | } |
| 56 | console.log(`openpgpKeyServer: fetched key: '${filtered.getFingerprint()}'`); |
| 57 | const result = { |
| 58 | armored: filtered.armor(), |
| 59 | date: new Date() |
| 60 | }; |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Helper function to create a url with the proper path for an api request. |
nothing calls this directly
no test coverage detected