aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/peertube-crypto.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-25 16:03:33 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-26 09:11:38 +0200
commitf5028693a896a3076dd286ac0030e3d8f78f5ebf (patch)
tree09144ed6357e49ea575fb110247f933283ad235e /server/helpers/peertube-crypto.ts
parenteb08047657e739bcd9e592d76307befa3998482b (diff)
downloadPeerTube-f5028693a896a3076dd286ac0030e3d8f78f5ebf.tar.gz
PeerTube-f5028693a896a3076dd286ac0030e3d8f78f5ebf.tar.zst
PeerTube-f5028693a896a3076dd286ac0030e3d8f78f5ebf.zip
Use async/await in lib and initializers
Diffstat (limited to 'server/helpers/peertube-crypto.ts')
-rw-r--r--server/helpers/peertube-crypto.ts103
1 files changed, 48 insertions, 55 deletions
diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts
index 89aef99c4..47f0243e7 100644
--- a/server/helpers/peertube-crypto.ts
+++ b/server/helpers/peertube-crypto.ts
@@ -1,5 +1,4 @@
1import * as crypto from 'crypto' 1import * as crypto from 'crypto'
2import * as Promise from 'bluebird'
3import { join } from 'path' 2import { join } from 'path'
4 3
5import { 4import {
@@ -41,7 +40,7 @@ function checkSignature (publicKey: string, data: string, hexSignature: string)
41 return isValid 40 return isValid
42} 41}
43 42
44function sign (data: string|Object) { 43async function sign (data: string|Object) {
45 const sign = crypto.createSign(SIGNATURE_ALGORITHM) 44 const sign = crypto.createSign(SIGNATURE_ALGORITHM)
46 45
47 let dataString: string 46 let dataString: string
@@ -52,33 +51,33 @@ function sign (data: string|Object) {
52 dataString = JSON.stringify(data) 51 dataString = JSON.stringify(data)
53 } catch (err) { 52 } catch (err) {
54 logger.error('Cannot sign data.', err) 53 logger.error('Cannot sign data.', err)
55 return Promise.resolve('') 54 return ''
56 } 55 }
57 } 56 }
58 57
59 sign.update(dataString, 'utf8') 58 sign.update(dataString, 'utf8')
60 59
61 return getMyPrivateCert().then(myKey => { 60 const myKey = await getMyPrivateCert()
62 return sign.sign(myKey, SIGNATURE_ENCODING) 61 return await sign.sign(myKey, SIGNATURE_ENCODING)
63 })
64} 62}
65 63
66function comparePassword (plainPassword: string, hashPassword: string) { 64function comparePassword (plainPassword: string, hashPassword: string) {
67 return bcryptComparePromise(plainPassword, hashPassword) 65 return bcryptComparePromise(plainPassword, hashPassword)
68} 66}
69 67
70function createCertsIfNotExist () { 68async function createCertsIfNotExist () {
71 return certsExist().then(exist => { 69 const exist = await certsExist()
72 if (exist === true) { 70 if (exist === true) {
73 return undefined 71 return undefined
74 } 72 }
75 73
76 return createCerts() 74 return await createCerts()
77 })
78} 75}
79 76
80function cryptPassword (password: string) { 77async function cryptPassword (password: string) {
81 return bcryptGenSaltPromise(BCRYPT_SALT_SIZE).then(salt => bcryptHashPromise(password, salt)) 78 const salt = await bcryptGenSaltPromise(BCRYPT_SALT_SIZE)
79
80 return await bcryptHashPromise(password, salt)
82} 81}
83 82
84function getMyPrivateCert () { 83function getMyPrivateCert () {
@@ -105,51 +104,45 @@ export {
105 104
106// --------------------------------------------------------------------------- 105// ---------------------------------------------------------------------------
107 106
108function certsExist () { 107async function certsExist () {
109 const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) 108 const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
110 109
111 // If there is an error the certificates do not exist 110 // If there is an error the certificates do not exist
112 return accessPromise(certPath) 111 try {
113 .then(() => true) 112 await accessPromise(certPath)
114 .catch(() => false) 113
114 return true
115 } catch {
116 return false
117 }
115} 118}
116 119
117function createCerts () { 120async function createCerts () {
118 return certsExist().then(exist => { 121 const exist = await certsExist()
119 if (exist === true) { 122 if (exist === true) {
120 const errorMessage = 'Certs already exist.' 123 const errorMessage = 'Certs already exist.'
121 logger.warning(errorMessage) 124 logger.warning(errorMessage)
122 throw new Error(errorMessage) 125 throw new Error(errorMessage)
123 } 126 }
124 127
125 logger.info('Generating a RSA key...') 128 logger.info('Generating a RSA key...')
126 129
127 const privateCertPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) 130 const privateCertPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
128 const genRsaOptions = { 131 const genRsaOptions = {
129 'out': privateCertPath, 132 'out': privateCertPath,
130 '2048': false 133 '2048': false
131 } 134 }
132 return opensslExecPromise('genrsa', genRsaOptions) 135
133 .then(() => { 136 await opensslExecPromise('genrsa', genRsaOptions)
134 logger.info('RSA key generated.') 137 logger.info('RSA key generated.')
135 logger.info('Managing public key...') 138 logger.info('Managing public key...')
136 139
137 const publicCertPath = join(CONFIG.STORAGE.CERT_DIR, 'peertube.pub') 140 const publicCertPath = join(CONFIG.STORAGE.CERT_DIR, 'peertube.pub')
138 const rsaOptions = { 141 const rsaOptions = {
139 'in': privateCertPath, 142 'in': privateCertPath,
140 'pubout': true, 143 'pubout': true,
141 'out': publicCertPath 144 'out': publicCertPath
142 } 145 }
143 return opensslExecPromise('rsa', rsaOptions) 146
144 .then(() => logger.info('Public key managed.')) 147 await opensslExecPromise('rsa', rsaOptions)
145 .catch(err => {
146 logger.error('Cannot create public key on this pod.')
147 throw err
148 })
149 })
150 .catch(err => {
151 logger.error('Cannot create private key on this pod.')
152 throw err
153 })
154 })
155} 148}