forked from Joystream/orion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
277 lines (252 loc) · 8.5 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import './config'
import request from 'supertest'
import { app } from '../index'
import { globalEm } from '../../utils/globalEm'
import { Account, Membership } from '../../model'
import assert from 'assert'
import { components } from '../generated/api-types'
import { Keyring } from '@polkadot/keyring'
import { KeyringPair } from '@polkadot/keyring/types'
import { ConfigVariable, config } from '../../utils/config'
import { u8aToHex } from '@polkadot/util'
import { JOYSTREAM_ADDRESS_PREFIX } from '@joystream/types'
import { uniqueId } from '../../utils/crypto'
import { ScryptOptions, createCipheriv, createDecipheriv, randomBytes, scrypt } from 'crypto'
import { SESSION_COOKIE_NAME } from '../../utils/auth'
import { SimpleRateLimit, resetAllLimits } from '../rateLimits'
export const keyring = new Keyring({ type: 'sr25519', ss58Format: JOYSTREAM_ADDRESS_PREFIX })
export type AccountAccessData = {
accountId: string
joystreamAccountId: string
email: string
password: string
seed: string
}
export type LoggedInAccountInfo = AccountAccessData & {
sessionId: string
sessionIdRaw: string
}
export type EncryptionArtifacts = components['schemas']['EncryptionArtifacts']
export async function scryptHash(
data: string,
salt: Buffer | string,
keylen = 32,
options: ScryptOptions = { N: 32768, r: 8, p: 1, maxmem: 64 * 1024 * 1024 }
): Promise<Buffer> {
return new Promise((resolve, reject) => {
scrypt(data, salt, keylen, options, (err, derivedKey) => {
if (err) {
reject(err)
} else {
resolve(derivedKey)
}
})
})
}
export function aes256CbcEncrypt(data: string, key: Buffer, iv: Buffer): string {
const cipher = createCipheriv('aes-256-cbc', key, iv)
let encrypted = cipher.update(data, 'utf8', 'hex')
encrypted += cipher.final('hex')
return encrypted
}
export function aes256CbcDecrypt(
encryptedData: string,
key: Buffer | string,
iv: Buffer | string
): string {
const decipher = createDecipheriv('aes-256-cbc', key, iv)
let decrypted = decipher.update(encryptedData, 'hex', 'utf8')
decrypted += decipher.final('utf8')
return decrypted
}
export async function calculateLookupKey(email: string, password: string): Promise<string> {
return (await scryptHash(`lookupKey:${email}:${password}`, 'lookupKeySalt')).toString('hex')
}
export async function prepareEncryptionArtifacts(
seed: string,
email: string,
password: string
): Promise<EncryptionArtifacts> {
const id = await calculateLookupKey(email, password)
const cipherIv = randomBytes(16)
const cipherKey = await scryptHash(`cipherKey:${email}:${password}`, cipherIv)
const encryptedSeed = aes256CbcEncrypt(seed, cipherKey, cipherIv)
return {
id,
cipherIv: cipherIv.toString('hex'),
encryptedSeed,
}
}
export async function decryptSeed(
email: string,
password: string,
{ cipherIv, encryptedSeed }: EncryptionArtifacts
): Promise<string> {
const cipherIvBuf = Buffer.from(cipherIv, 'hex')
const cipherKey = await scryptHash(`cipherKey:${email}:${password}`, cipherIvBuf)
return aes256CbcDecrypt(encryptedSeed, cipherKey, cipherIvBuf)
}
export const DEFAULT_PASSWORD = 'TestPassword123!'
export async function signedAction<T extends components['schemas']['ActionExecutionRequestData']>(
data: Omit<T['payload'], 'gatewayName' | 'joystreamAccountId' | 'timestamp'>,
keypair: KeyringPair
): Promise<T> {
const em = await globalEm
const gatewayName = await config.get(ConfigVariable.AppName, em)
const payload: T['payload'] = {
gatewayName,
joystreamAccountId: keypair.address,
timestamp: Date.now(),
...data,
}
const signature = u8aToHex(keypair.sign(JSON.stringify(payload)))
return {
payload,
signature,
} as T
}
async function insertFakeMember(controllerAccount: string) {
const em = await globalEm
const handle = uniqueId()
return em.getRepository(Membership).save({
createdAt: new Date(),
id: uniqueId(),
controllerAccount,
handle: uniqueId(),
handleRaw: '0x' + Buffer.from(handle).toString('hex'),
totalChannelsCreated: 0,
})
}
export async function createAccount(
email = `test.${uniqueId()}@example.com`,
password = DEFAULT_PASSWORD,
seed?: string
): Promise<AccountAccessData> {
seed = seed || uniqueId()
const keypair = keyring.addFromUri(`//${seed}`)
const em = await globalEm
const membership = await insertFakeMember(keypair.address)
const anonSessionId = await anonymousAuth()
const createAccountReqData = await signedAction<
components['schemas']['CreateAccountRequestData']
>(
{
action: 'createAccount',
email,
memberId: membership.id,
encryptionArtifacts: await prepareEncryptionArtifacts(seed, email, password),
},
keypair
)
await request(app)
.post('/api/v1/account')
.set('Content-Type', 'application/json')
.set('Cookie', `${SESSION_COOKIE_NAME}=${anonSessionId}`)
.send(createAccountReqData)
.expect(200)
const account = await em.getRepository(Account).findOneBy({ email })
assert(account, 'Account not found')
return { accountId: account.id, joystreamAccountId: keypair.address, email, password, seed }
}
export async function confirmEmail(token: string, expectedStatus: number): Promise<void> {
await request(app)
.post('/api/v1/confirm-email')
.set('Content-Type', 'application/json')
.send({ token })
.expect(expectedStatus)
}
export async function requestEmailConfirmationToken(
email: string,
expectedStatus: number
): Promise<void> {
await request(app)
.post('/api/v1/request-email-confirmation-token')
.set('Content-Type', 'application/json')
.send({ email })
.expect(expectedStatus)
}
export async function createAccountAndSignIn(
email = `test.${uniqueId()}@example.com`,
password = DEFAULT_PASSWORD,
seed?: string
): Promise<LoggedInAccountInfo> {
const accountData = await createAccount(email, password, seed)
return await signIn(accountData)
}
export async function signIn(accountData: AccountAccessData): Promise<LoggedInAccountInfo> {
const keypair = keyring.addFromUri(`//${accountData.seed}`)
const loginReqData = await signedAction<components['schemas']['LoginRequestData']>(
{
action: 'login',
},
keypair
)
console.log('Login request data:', loginReqData)
const loginResp = await request(app)
.post('/api/v1/login')
.set('Content-Type', 'application/json')
.send(loginReqData)
.expect(200)
const sessionId = extractSessionId(loginResp)
const sessionIdRaw = sessionId.split('.')[0].split(':')[1]
return {
sessionId: extractSessionId(loginResp),
sessionIdRaw,
...accountData,
}
}
function extractSessionId(response: request.Response): string {
const setCookieHeader = response.get('Set-Cookie')
assert(setCookieHeader, 'Set-Cookie header not found')
const [setCookieHeaderStr] = setCookieHeader
const [, sessionId] = setCookieHeaderStr.match(new RegExp(`${SESSION_COOKIE_NAME}=([^;]+)`)) || []
assert(sessionId, 'Session id not found')
return sessionId
}
export async function anonymousAuth(): Promise<string> {
const response = await request(app)
.post('/api/v1/anonymous-auth')
.set('Content-Type', 'application/json')
.expect(200)
return extractSessionId(response)
}
export async function verifyRateLimit(
requestGenerator:
| ((i: number) => { req: request.Test; status: number })
| ((i: number) => Promise<{ req: request.Test; status: number }>),
rateLimit: SimpleRateLimit | undefined,
resetAfterwards = true
) {
assert(rateLimit, 'Rate limit not set')
let remaining = rateLimit.limit
let reset = rateLimit.windowMinutes * 60
let i = 0
while (remaining > 0) {
const { req, status } = await requestGenerator(i++)
const resp = await req.expect(status)
const limitInHeader = resp.get('ratelimit-limit')
const resetInHeader = resp.get('ratelimit-reset')
const remainingInHeader = resp.get('ratelimit-remaining')
assert.equal(
limitInHeader,
rateLimit.limit.toString(),
'Limit header does not match the configured limit'
)
assert(
parseInt(resetInHeader) <= reset,
'Reset time in header has increased since the last request or is greater than the configured reset time'
)
assert(
parseInt(remainingInHeader) < remaining,
'Number of remaining requests in header is not decreasing'
)
remaining = parseInt(remainingInHeader)
reset = parseInt(resetInHeader)
}
const { req } = await requestGenerator(i)
await req.expect(429)
if (resetAfterwards) {
resetAllLimits('::ffff:127.0.0.1')
resetAllLimits('127.0.0.1')
}
}