Skip to content

Commit c539cb8

Browse files
committed
feat: 🎸 Easier import of events from client
feat: 🎸 Easier import of events from client fix: πŸ› Updated dependencies style: πŸ’„ Fixed linting errors due to eslint update
1 parent e6b4531 commit c539cb8

15 files changed

+568
-530
lines changed

β€Ž__tests__/client.test.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const createClient = require('../lib/client')
22
const { createMemoryStore } = require('../lib/memoryStore')
33
const axios = require('axios')
44
const { generateKey } = require('../lib/crypto')
5-
const { JWT } = require('@panva/jose')
5+
const { JWT } = require('jose')
66
const { CONNECT_TO_OPERATOR_START, CONNECT_TO_OPERATOR } = require('../lib/events')
77
jest.mock('axios')
88

β€Ž__tests__/connection.test.jsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const createClient = require('../lib/client')
22
const { createMemoryStore } = require('../lib/memoryStore')
3-
const { JWT } = require('@panva/jose')
3+
const { JWT } = require('jose')
44
const { connectionInitHandler, connectionEventHandler } = require('./../lib/connection')
55
const { sign } = require('../lib/jwt')
66
const { generateKey, toPublicKey } = require('../lib/crypto')
@@ -64,7 +64,7 @@ describe('connection', () => {
6464
await handle({ payload }, res, next)
6565
expect(next).not.toHaveBeenCalled()
6666

67-
const [ token ] = res.write.mock.calls[0]
67+
const [token] = res.write.mock.calls[0]
6868
const result = JWT.decode(token)
6969

7070
expect(result).not.toBe(null)

β€Ž__tests__/data.test.jsβ€Ž

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { JWE, JWS, JWK } = require('@panva/jose')
1+
const { JWE, JWS, JWK } = require('jose')
22
const data = require('../lib/data')
33
const { generateKey, toPublicKey } = require('../lib/crypto')
44
const { verify } = require('../lib/jwt')
@@ -58,7 +58,7 @@ describe('data', () => {
5858
await write(connectionId, { domain, area, data: payload })
5959

6060
expect(tokens.createWriteDataToken).toHaveBeenCalledWith(
61-
connectionId, [ { domain, area, data: expect.any(Object) } ]
61+
connectionId, [{ domain, area, data: expect.any(Object) }]
6262
)
6363
})
6464
it('creates a token with the correct arguments without domain', async () => {
@@ -109,22 +109,22 @@ describe('data', () => {
109109
tokens.send.mockResolvedValue('read.response.token')
110110
verify.mockImplementation(() => ({
111111
payload: {
112-
paths: [ { domain, area, data: createJWE(data) } ]
112+
paths: [{ domain, area, data: createJWE(data) }]
113113
}
114114
}))
115115
})
116116
it('creates a token with the correct arguments', async () => {
117117
await read(connectionId, { domain, area })
118118

119119
expect(tokens.createReadDataToken).toHaveBeenCalledWith(
120-
connectionId, [ { domain, area } ]
120+
connectionId, [{ domain, area }]
121121
)
122122
})
123123
it('creates a token with the correct arguments without domain', async () => {
124124
await read(connectionId, { area })
125125

126126
expect(tokens.createReadDataToken).toHaveBeenCalledWith(
127-
connectionId, [ { domain: config.clientId, area } ]
127+
connectionId, [{ domain: config.clientId, area }]
128128
)
129129
})
130130
it('creates a token with the correct arguments with multiple paths', async () => {
@@ -166,22 +166,22 @@ describe('data', () => {
166166
tokens.send.mockResolvedValue('read.response.token')
167167
verify.mockImplementation(() => ({
168168
payload: {
169-
paths: [ { domain, area } ]
169+
paths: [{ domain, area }]
170170
}
171171
}))
172172
})
173173
it('creates a token with the correct arguments', async () => {
174174
await read(connectionId, { domain, area })
175175

176176
expect(tokens.createReadDataToken).toHaveBeenCalledWith(
177-
connectionId, [ { domain, area } ]
177+
connectionId, [{ domain, area }]
178178
)
179179
})
180180
it('creates a token with the correct arguments without domain', async () => {
181181
await read(connectionId, { area })
182182

183183
expect(tokens.createReadDataToken).toHaveBeenCalledWith(
184-
connectionId, [ { domain: config.clientId, area } ]
184+
connectionId, [{ domain: config.clientId, area }]
185185
)
186186
})
187187
it('posts to operator', async () => {
@@ -215,14 +215,14 @@ describe('data', () => {
215215
await auth(accessToken).write({ domain, area, data: payload })
216216

217217
expect(tokens.createWriteDataToken).toHaveBeenCalledWith(
218-
connectionId, [ { domain, area, data: expect.any(Object) } ]
218+
connectionId, [{ domain, area, data: expect.any(Object) }]
219219
)
220220
})
221221
it('creates a token with the correct arguments without domain', async () => {
222222
await auth(accessToken).write({ area, data: payload })
223223

224224
expect(tokens.createWriteDataToken).toHaveBeenCalledWith(
225-
connectionId, [ { domain: config.clientId, area, data: expect.any(Object) } ]
225+
connectionId, [{ domain: config.clientId, area, data: expect.any(Object) }]
226226
)
227227
})
228228
it('posts to operator', async () => {
@@ -254,9 +254,11 @@ describe('data', () => {
254254
case 'access.token':
255255
return { payload: { sub: connectionId } }
256256
case 'read.response.token':
257-
return { payload: {
258-
paths: [ { domain, area, data: createJWE(data) } ]
259-
} }
257+
return {
258+
payload: {
259+
paths: [{ domain, area, data: createJWE(data) }]
260+
}
261+
}
260262
default:
261263
throw new Error('Unmocked token')
262264
}
@@ -271,14 +273,14 @@ describe('data', () => {
271273
await auth(accessToken).read({ domain, area })
272274

273275
expect(tokens.createReadDataToken).toHaveBeenCalledWith(
274-
connectionId, [ { domain, area } ]
276+
connectionId, [{ domain, area }]
275277
)
276278
})
277279
it('creates a token with the correct arguments without domain', async () => {
278280
await auth(accessToken).read({ area })
279281

280282
expect(tokens.createReadDataToken).toHaveBeenCalledWith(
281-
connectionId, [ { domain: config.clientId, area } ]
283+
connectionId, [{ domain: config.clientId, area }]
282284
)
283285
})
284286
it('posts to operator', async () => {
@@ -313,7 +315,7 @@ describe('data', () => {
313315
case 'access.token':
314316
return { payload: { sub: connectionId } }
315317
case 'read.response.token':
316-
return { payload: { paths: [ { domain, area } ] } }
318+
return { payload: { paths: [{ domain, area }] } }
317319
default:
318320
throw new Error('Unmocked token')
319321
}
@@ -328,14 +330,14 @@ describe('data', () => {
328330
await auth(accessToken).read({ domain, area })
329331

330332
expect(tokens.createReadDataToken).toHaveBeenCalledWith(
331-
connectionId, [ { domain, area } ]
333+
connectionId, [{ domain, area }]
332334
)
333335
})
334336
it('creates a token with the correct arguments without domain', async () => {
335337
await auth(accessToken).read({ area })
336338

337339
expect(tokens.createReadDataToken).toHaveBeenCalledWith(
338-
connectionId, [ { domain: config.clientId, area } ]
340+
connectionId, [{ domain: config.clientId, area }]
339341
)
340342
})
341343
it('posts to operator', async () => {

β€Ž__tests__/keyProvider.test.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('KeyProvider', () => {
9494
it('returns a jwks with client_key', async () => {
9595
const result = await keyProvider.jwksKeyList()
9696
expect(result).toEqual({
97-
keys: [ toPublicKey(clientKey) ]
97+
keys: [toPublicKey(clientKey)]
9898
})
9999
})
100100
})

β€Ž__tests__/permissions.test.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('permissions', () => {
3030
it('creates an encryption key for READ', async () => {
3131
const jwk = { kid: 'foo', kty: 'RSA', use: 'enc', e: 'AQAB', n: 'a-large-number' }
3232
keyProvider.generateTemporaryKey.mockResolvedValue(jwk)
33-
shorthandPermissions = [ { area: 'education', types: ['READ'], purpose: 'stuff' } ]
33+
shorthandPermissions = [{ area: 'education', types: ['READ'], purpose: 'stuff' }]
3434

3535
const result = await createPermissions(shorthandPermissions, clientId, keyProvider)
3636

β€Ž__tests__/tokens.test.jsβ€Ž

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { JWT, JWS, JWE, JWK } = require('@panva/jose')
1+
const { JWT, JWS, JWE, JWK } = require('jose')
22
const { schemas } = require('@egendata/messaging')
33
const axios = require('axios')
44
const createClient = require('../lib/client')
@@ -215,7 +215,7 @@ describe('tokens', () => {
215215
})
216216
it('creates a valid jwt', async () => {
217217
const authReq = await client.tokens
218-
.createWriteDataToken(connectionId, [ { domain, area, data: jwe } ])
218+
.createWriteDataToken(connectionId, [{ domain, area, data: jwe }])
219219

220220
const payload = JWT.decode(authReq)
221221
expect(payload).not.toBe(null)
@@ -225,23 +225,23 @@ describe('tokens', () => {
225225
})
226226
it('creates header with correct kid', async () => {
227227
const authReq = await client.tokens
228-
.createWriteDataToken(connectionId, [ { domain, area, data: jwe } ])
228+
.createWriteDataToken(connectionId, [{ domain, area, data: jwe }])
229229

230230
const { header } = JWT.decode(authReq, { complete: true })
231231

232232
expect(header.kid).toEqual('https://mycv.work/jwks/client_key')
233233
})
234234
it('creates header with correct type', async () => {
235235
const authReq = await client.tokens
236-
.createWriteDataToken(connectionId, [ { domain, area, data: jwe } ])
236+
.createWriteDataToken(connectionId, [{ domain, area, data: jwe }])
237237

238238
const { type } = JWT.decode(authReq)
239239

240240
expect(type).toEqual('DATA_WRITE')
241241
})
242242
it('creates the correct jwt claimsSet', async () => {
243243
const authReq = await client.tokens
244-
.createWriteDataToken(connectionId, [ { domain, area, data: jwe } ])
244+
.createWriteDataToken(connectionId, [{ domain, area, data: jwe }])
245245

246246
const payload = JWT.decode(authReq)
247247

@@ -261,7 +261,7 @@ describe('tokens', () => {
261261
})
262262
it('creates a valid jwt', async () => {
263263
const authReq = await client.tokens
264-
.createReadDataToken(connectionId, [ { domain, area } ])
264+
.createReadDataToken(connectionId, [{ domain, area }])
265265

266266
const payload = JWT.decode(authReq)
267267
expect(payload).not.toBe(null)
@@ -271,29 +271,29 @@ describe('tokens', () => {
271271
})
272272
it('creates header with correct kid', async () => {
273273
const authReq = await client.tokens
274-
.createReadDataToken(connectionId, [ { domain, area } ])
274+
.createReadDataToken(connectionId, [{ domain, area }])
275275

276276
const { header } = JWT.decode(authReq, { complete: true })
277277

278278
expect(header.kid).toEqual('https://mycv.work/jwks/client_key')
279279
})
280280
it('creates header with correct type', async () => {
281281
const authReq = await client.tokens
282-
.createReadDataToken(connectionId, [ { domain, area } ])
282+
.createReadDataToken(connectionId, [{ domain, area }])
283283

284284
const { type } = JWT.decode(authReq)
285285

286286
expect(type).toEqual('DATA_READ_REQUEST')
287287
})
288288
it('creates the correct jwt claimsSet', async () => {
289289
const authReq = await client.tokens
290-
.createReadDataToken(connectionId, [ { domain, area } ])
290+
.createReadDataToken(connectionId, [{ domain, area }])
291291

292292
const payload = JWT.decode(authReq)
293293

294294
expect(payload.aud).toBe('https://smoothoperator.work')
295295
expect(payload.iss).toBe('https://mycv.work')
296-
expect(payload.paths).toEqual([ { domain, area } ])
296+
expect(payload.paths).toEqual([{ domain, area }])
297297
})
298298
})
299299
describe('#createAccessToken', () => {

β€Žlib/connection.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const connectionEventHandler = (client) => async ({ payload }, res, next) => {
3636
client.keyValueStore.save(`${CONNECTION_PREFIX}${sub}`, JSON.stringify(connection))
3737

3838
if (permissions && permissions.approved) {
39-
for (let permission of permissions.approved) {
39+
for (const permission of permissions.approved) {
4040
if (permission.type === 'READ') {
4141
await client.keyProvider.makeKeyPermanent(permission.kid)
4242
} else if (permission.type === 'WRITE') {

β€Žlib/data.jsβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { JWS, JWE, JWK } = require('@panva/jose')
1+
const { JWS, JWE, JWK } = require('jose')
22
const { verify } = require('./jwt')
33
const {
44
emitter,
@@ -48,7 +48,7 @@ const read = (config, keyProvider, tokens) => async (connectionId, ...paths) =>
4848

4949
// Iterate through the paths
5050
const result = []
51-
for (let { domain, area, data } of response.paths) {
51+
for (const { domain, area, data } of response.paths) {
5252
try {
5353
let decryptedData
5454
if (data) {
@@ -116,7 +116,7 @@ const write = (config, keyProvider, tokens) => async (connectionId, ...paths) =>
116116

117117
const permissionJWKS = await keyProvider.getWriteKeys(domain, area)
118118
const writeKeys = permissionJWKS.keys.map((key) => JWK.asKey(key))
119-
for (let key of writeKeys) {
119+
for (const key of writeKeys) {
120120
encryptor.recipient(key, { kid: key.kid })
121121
}
122122

β€Žlib/index.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { createMemoryStore } = require('./memoryStore')
22

33
module.exports = {
44
create: require('./client'),
5+
events: require('./events'),
56
utils: {
67
createMemoryStore
78
}

β€Žlib/jwt.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { token } = require('@egendata/messaging')
2-
const { JWT, JWK } = require('@panva/jose')
2+
const { JWT, JWK } = require('jose')
33
const {
44
emitter,
55

0 commit comments

Comments
Β (0)