|
1 |
| -import { Middleware } from 'koa' |
2 |
| -import { EmailVerification, Integration } from '../config/sequelize' |
3 |
| - |
4 |
| -export const getStatus: Middleware = async ctx => { |
5 |
| - const webId = ctx.state.user |
6 |
| - |
7 |
| - const verified = await Integration.findAll({ where: { webId } }) |
8 |
| - |
9 |
| - const unverified = await EmailVerification.findAll({ where: { webId } }) |
10 |
| - |
11 |
| - ctx.response.body = { |
12 |
| - actor: webId, |
13 |
| - integrations: [ |
14 |
| - ...verified.map(s => ({ |
15 |
| - object: s.inbox, |
16 |
| - target: s.email, |
17 |
| - verified: true, |
18 |
| - })), |
19 |
| - ...unverified.map(s => ({ |
20 |
| - object: s.inbox, |
21 |
| - target: s.email, |
22 |
| - verified: false, |
23 |
| - })), |
24 |
| - ], |
25 |
| - } |
| 1 | +import { RdfQuery } from '@ldhop/core/dist/src' |
| 2 | +import { QueryAndStore } from '@ldhop/core/dist/src/QueryAndStore' |
| 3 | +import { fetchRdfDocument } from '@ldhop/core/dist/src/utils/helpers' |
| 4 | +import { getAuthenticatedFetch as getAuthenticatedFetch6x } from 'css-authn/dist/6.x' |
| 5 | +import { getAuthenticatedFetch as getAuthenticatedFetch7x } from 'css-authn/dist/7.x' |
| 6 | +import { readFile } from 'fs-extra' |
| 7 | +import { verify } from 'jsonwebtoken' |
| 8 | +import type { DefaultContext, DefaultState, Middleware } from 'koa' |
| 9 | +import { NamedNode, Quad } from 'n3' |
| 10 | +import { dct, rdfs, solid, space } from 'rdf-namespaces' |
| 11 | +import * as config from '../config' |
| 12 | + |
| 13 | +export const getVerifiedEmails = async (webId: string) => { |
| 14 | + const tokens = await findEmailVerificationTokens(webId) |
| 15 | + |
| 16 | + const pem = await readFile(config.jwt.key, { encoding: 'utf-8' }) |
| 17 | + |
| 18 | + const verifiedEmails = tokens |
| 19 | + .map(token => { |
| 20 | + try { |
| 21 | + return verify(token, pem) as { |
| 22 | + webId: string |
| 23 | + email: string |
| 24 | + emailVerified: boolean |
| 25 | + iss: string |
| 26 | + iat: number |
| 27 | + } |
| 28 | + } catch { |
| 29 | + return null |
| 30 | + } |
| 31 | + }) |
| 32 | + .filter(a => a?.emailVerified && a.webId === webId) |
| 33 | + .map(a => a!.email) |
| 34 | + |
| 35 | + return verifiedEmails |
| 36 | +} |
| 37 | + |
| 38 | +export const getStatus: Middleware< |
| 39 | + DefaultState, |
| 40 | + DefaultContext & { params: { webId: string } } |
| 41 | +> = async ctx => { |
| 42 | + const webId = ctx.params.webId |
| 43 | + |
| 44 | + const verifiedEmails = await getVerifiedEmails(webId) |
| 45 | + |
| 46 | + ctx.response.body = { emailVerified: verifiedEmails.length > 0 } |
26 | 47 | ctx.response.status = 200
|
27 | 48 | }
|
| 49 | + |
| 50 | +/** |
| 51 | + * To find verified email of a person |
| 52 | + * |
| 53 | + * - Go to person's webId |
| 54 | + * - Find public type index `(webId) - solid:publicTypeIndex -> (publicTypeIndex)`` |
| 55 | + * - Find instances of specific class defined in config (EMAIL_DISCOVERY_TYPE defaults to hospex:PersonalHospexDocument) |
| 56 | + * - Find settings in the relevant instance (webId) - space:preferencesFile -> (settings) |
| 57 | + * - In the settings, find (webId) - example:emailVerificationToken -> (JWT) |
| 58 | + */ |
| 59 | +const findEmailQuery: RdfQuery = [ |
| 60 | + // Go to person's webId and fetch extended profile documents, too |
| 61 | + { |
| 62 | + type: 'match', |
| 63 | + subject: '?person', |
| 64 | + predicate: rdfs.seeAlso, |
| 65 | + pick: 'object', |
| 66 | + target: '?extendedDocument', |
| 67 | + }, |
| 68 | + { type: 'add resources', variable: '?extendedDocument' }, |
| 69 | + // Find public type index |
| 70 | + { |
| 71 | + type: 'match', |
| 72 | + subject: '?person', |
| 73 | + predicate: solid.publicTypeIndex, |
| 74 | + pick: 'object', |
| 75 | + target: '?publicTypeIndex', |
| 76 | + }, |
| 77 | + // Find instances of specific class defined in config (EMAIL_DISCOVERY_TYPE) |
| 78 | + { |
| 79 | + type: 'match', |
| 80 | + subject: '?publicTypeIndex', |
| 81 | + predicate: dct.references, |
| 82 | + pick: 'object', |
| 83 | + target: '?typeRegistration', |
| 84 | + }, |
| 85 | + { |
| 86 | + type: 'match', |
| 87 | + subject: '?typeRegistration', |
| 88 | + predicate: solid.forClass, |
| 89 | + object: config.emailDiscoveryType, |
| 90 | + pick: 'subject', |
| 91 | + target: '?typeRegistrationForClass', |
| 92 | + }, |
| 93 | + { |
| 94 | + type: 'match', |
| 95 | + subject: '?typeRegistrationForClass', |
| 96 | + predicate: solid.instance, |
| 97 | + pick: 'object', |
| 98 | + target: `?classDocument`, |
| 99 | + }, |
| 100 | + { type: 'add resources', variable: '?classDocument' }, |
| 101 | + // Find settings |
| 102 | + { |
| 103 | + type: 'match', |
| 104 | + subject: '?person', |
| 105 | + predicate: space.preferencesFile, |
| 106 | + pick: 'object', |
| 107 | + target: '?settings', |
| 108 | + }, |
| 109 | + { type: 'add resources', variable: '?settings' }, |
| 110 | +] |
| 111 | + |
| 112 | +const findEmailVerificationTokens = async (webId: string) => { |
| 113 | + // initialize knowledge graph and follow your nose through it |
| 114 | + // according to the query |
| 115 | + const qas = new QueryAndStore(findEmailQuery, { person: new Set([webId]) }) |
| 116 | + await run(qas) |
| 117 | + |
| 118 | + // Find email verification tokens |
| 119 | + const objects = qas.store.getObjects( |
| 120 | + new NamedNode(webId), |
| 121 | + new NamedNode('https://example.com/emailVerificationToken'), |
| 122 | + null, |
| 123 | + ) |
| 124 | + |
| 125 | + return objects.map(o => o.value) |
| 126 | +} |
| 127 | + |
| 128 | +const fetchRdf = async (uri: string) => { |
| 129 | + const getAuthenticatedFetch = |
| 130 | + config.mailerCredentials.cssVersion === 6 |
| 131 | + ? getAuthenticatedFetch6x |
| 132 | + : getAuthenticatedFetch7x |
| 133 | + const authBotFetch = await getAuthenticatedFetch(config.mailerCredentials) |
| 134 | + |
| 135 | + const { data: quads } = await fetchRdfDocument(uri, authBotFetch) |
| 136 | + |
| 137 | + return quads |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Follow your nose through the linked data graph by query |
| 142 | + */ |
| 143 | +const run = async (qas: QueryAndStore) => { |
| 144 | + let missingResources = qas.getMissingResources() |
| 145 | + |
| 146 | + while (missingResources.length > 0) { |
| 147 | + let quads: Quad[] = [] |
| 148 | + const res = missingResources[0] |
| 149 | + try { |
| 150 | + quads = await fetchRdf(missingResources[0]) |
| 151 | + } catch (e) { |
| 152 | + // eslint-disable-next-line no-console |
| 153 | + console.error(e) |
| 154 | + } finally { |
| 155 | + qas.addResource(res, quads) |
| 156 | + missingResources = qas.getMissingResources() |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments