Move to eslint
[oweals/peertube.git] / server / helpers / custom-jsonld-signature.ts
1 import * as AsyncLRU from 'async-lru'
2 import * as jsonld from 'jsonld'
3 import { logger } from './logger'
4
5 const CACHE = {
6   'https://w3id.org/security/v1': {
7     '@context': {
8       id: '@id',
9       type: '@type',
10
11       dc: 'http://purl.org/dc/terms/',
12       sec: 'https://w3id.org/security#',
13       xsd: 'http://www.w3.org/2001/XMLSchema#',
14
15       EcdsaKoblitzSignature2016: 'sec:EcdsaKoblitzSignature2016',
16       Ed25519Signature2018: 'sec:Ed25519Signature2018',
17       EncryptedMessage: 'sec:EncryptedMessage',
18       GraphSignature2012: 'sec:GraphSignature2012',
19       LinkedDataSignature2015: 'sec:LinkedDataSignature2015',
20       LinkedDataSignature2016: 'sec:LinkedDataSignature2016',
21       CryptographicKey: 'sec:Key',
22
23       authenticationTag: 'sec:authenticationTag',
24       canonicalizationAlgorithm: 'sec:canonicalizationAlgorithm',
25       cipherAlgorithm: 'sec:cipherAlgorithm',
26       cipherData: 'sec:cipherData',
27       cipherKey: 'sec:cipherKey',
28       created: { '@id': 'dc:created', '@type': 'xsd:dateTime' },
29       creator: { '@id': 'dc:creator', '@type': '@id' },
30       digestAlgorithm: 'sec:digestAlgorithm',
31       digestValue: 'sec:digestValue',
32       domain: 'sec:domain',
33       encryptionKey: 'sec:encryptionKey',
34       expiration: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' },
35       expires: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' },
36       initializationVector: 'sec:initializationVector',
37       iterationCount: 'sec:iterationCount',
38       nonce: 'sec:nonce',
39       normalizationAlgorithm: 'sec:normalizationAlgorithm',
40       owner: { '@id': 'sec:owner', '@type': '@id' },
41       password: 'sec:password',
42       privateKey: { '@id': 'sec:privateKey', '@type': '@id' },
43       privateKeyPem: 'sec:privateKeyPem',
44       publicKey: { '@id': 'sec:publicKey', '@type': '@id' },
45       publicKeyBase58: 'sec:publicKeyBase58',
46       publicKeyPem: 'sec:publicKeyPem',
47       publicKeyWif: 'sec:publicKeyWif',
48       publicKeyService: { '@id': 'sec:publicKeyService', '@type': '@id' },
49       revoked: { '@id': 'sec:revoked', '@type': 'xsd:dateTime' },
50       salt: 'sec:salt',
51       signature: 'sec:signature',
52       signatureAlgorithm: 'sec:signingAlgorithm',
53       signatureValue: 'sec:signatureValue'
54     }
55   }
56 }
57
58 const nodeDocumentLoader = jsonld.documentLoaders.node()
59
60 const lru = new AsyncLRU({
61   max: 10,
62   load: (url, cb) => {
63     if (CACHE[url] !== undefined) {
64       logger.debug('Using cache for JSON-LD %s.', url)
65
66       return cb(null, {
67         contextUrl: null,
68         document: CACHE[url],
69         documentUrl: url
70       })
71     }
72
73     nodeDocumentLoader(url)
74       .then(value => cb(null, value))
75       .catch(err => cb(err))
76   }
77 })
78
79 jsonld.documentLoader = (url) => {
80   return new Promise((res, rej) => {
81     lru.get(url, (err, value) => {
82       if (err) return rej(err)
83
84       return res(value)
85     })
86   })
87 }
88
89 export { jsonld }