]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/peertube-crypto.ts
Refractor user quota SQL queries
[github/Chocobozzz/PeerTube.git] / server / helpers / peertube-crypto.ts
index 6d50e446f59a124488f2eb392e3212142d426e69..5c182961d4a8a41e1413ea1d33bc99df1a191178 100644 (file)
@@ -1,20 +1,8 @@
-import * as jsig from 'jsonld-signatures'
-
-import {
-  PRIVATE_RSA_KEY_SIZE,
-  BCRYPT_SALT_SIZE
-} from '../initializers'
-import {
-  bcryptComparePromise,
-  bcryptGenSaltPromise,
-  bcryptHashPromise,
-  createPrivateKey,
-  getPublicKey,
-  jsonldSignPromise,
-  jsonldVerifyPromise
-} from './core-utils'
+import { BCRYPT_SALT_SIZE, PRIVATE_RSA_KEY_SIZE } from '../initializers'
+import { ActorModel } from '../models/activitypub/actor'
+import { bcryptComparePromise, bcryptGenSaltPromise, bcryptHashPromise, createPrivateKey, getPublicKey } from './core-utils'
+import { jsig } from './custom-jsonld-signature'
 import { logger } from './logger'
-import { AccountInstance } from '../models/account/account-interface'
 
 async function createPrivateAndPublicKeys () {
   logger.info('Generating a RSA key...')
@@ -25,18 +13,18 @@ async function createPrivateAndPublicKeys () {
   return { privateKey: key, publicKey }
 }
 
-function isSignatureVerified (fromAccount: AccountInstance, signedDocument: object) {
+function isSignatureVerified (fromActor: ActorModel, signedDocument: object) {
   const publicKeyObject = {
     '@context': jsig.SECURITY_CONTEXT_URL,
-    '@id': fromAccount.url,
+    '@id': fromActor.url,
     '@type':  'CryptographicKey',
-    owner: fromAccount.url,
-    publicKeyPem: fromAccount.publicKey
+    owner: fromActor.url,
+    publicKeyPem: fromActor.publicKey
   }
 
   const publicKeyOwnerObject = {
     '@context': jsig.SECURITY_CONTEXT_URL,
-    '@id': fromAccount.url,
+    '@id': fromActor.url,
     publicKey: [ publicKeyObject ]
   }
 
@@ -45,20 +33,21 @@ function isSignatureVerified (fromAccount: AccountInstance, signedDocument: obje
     publicKeyOwner: publicKeyOwnerObject
   }
 
-  return jsonldVerifyPromise(signedDocument, options)
+  return jsig.promises.verify(signedDocument, options)
     .catch(err => {
-      logger.error('Cannot check signature.', err)
+      logger.error('Cannot check signature.', { err })
       return false
     })
 }
 
-function signObject (byAccount: AccountInstance, data: any) {
+function signObject (byActor: ActorModel, data: any) {
   const options = {
-    privateKeyPem: byAccount.privateKey,
-    creator: byAccount.url
+    privateKeyPem: byActor.privateKey,
+    creator: byActor.url,
+    algorithm: 'RsaSignature2017'
   }
 
-  return jsonldSignPromise(data, options)
+  return jsig.promises.sign(data, options)
 }
 
 function comparePassword (plainPassword: string, hashPassword: string) {