This repository was archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice-token-authentication.spec.ts
More file actions
135 lines (120 loc) · 3.21 KB
/
Copy pathdevice-token-authentication.spec.ts
File metadata and controls
135 lines (120 loc) · 3.21 KB
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
import { spawn } from 'child_process'
import { randomUUID } from 'node:crypto'
import os from 'os'
import { apiClient, tokenAuthorization } from './api-client.js'
import { describe, it, before } from 'node:test'
import assert from 'node:assert/strict'
const endpoint = process.env.API_HOST
const apiKeyClient = apiClient({
endpoint,
authorizationToken: process.env.API_KEY as string,
})
void describe('authenticate using device keys', () => {
let privateKey: string
let publicKey: string
let deviceId: string
let bulkOpsRequestId: string
before(async () => {
// Generate a globally uniqe device ID
deviceId = randomUUID()
// Generate a key for the device
privateKey = await new Promise<string>((resolve, reject) => {
const openssl = spawn('openssl', [
'ecparam',
'-name',
'prime256v1',
'-genkey',
])
const res: string[] = []
const err: string[] = []
openssl.stdout.on('data', (data) => {
res.push(data)
})
openssl.stderr.on('data', (data) => {
err.push(data)
})
openssl.on('close', (code) => {
if (code !== 0) {
return reject(err.join(os.EOL))
}
return resolve(res.join(os.EOL))
})
})
publicKey = await new Promise<string>((resolve, reject) => {
const openssl = spawn('openssl', ['ec', '-pubout', '-outform', 'pem'])
openssl.stdin.write(privateKey)
console.log(privateKey)
const res: string[] = []
const err: string[] = []
openssl.stdout.on('data', (data) => {
console.log(Buffer.from(data).toString())
res.push(data)
})
openssl.stderr.on('data', (data) => {
err.push(data)
})
openssl.on('close', (code) => {
if (code !== 0) {
return reject(err.join(os.EOL))
}
return resolve(res.join(os.EOL))
})
})
})
void it('should register a new device key', async () => {
const { bulkOpsRequestId: rid } = JSON.parse(
(
await apiKeyClient.postBinary({
resource: 'devices/public-keys',
payload: `${deviceId},"${publicKey}"`,
})
).toString('utf-8'),
)
bulkOpsRequestId = rid
assert.notEqual(bulkOpsRequestId, undefined)
})
void it('should process the request', async () => {
const getStatus = async () =>
apiKeyClient
.getJSON({
resource: `bulk-ops-requests/${bulkOpsRequestId}`,
})
.then(({ status }) => status)
const status = await new Promise((resolve, reject) => {
let t: NodeJS.Timeout | undefined = undefined
const i = setInterval(async () => {
const status = await getStatus()
if (status !== 'PENDING') {
clearInterval(i)
if (t !== undefined) clearTimeout(t)
resolve(status)
}
}, 1000)
t = setTimeout(() => {
clearInterval(i)
reject(new Error(`Timeout`))
}, 30000)
})
assert.equal(status, 'SUCCEEDED')
})
void it('should accept the device-key based JWT', async () => {
const { getJSON } = apiClient({
endpoint,
authorizationToken: tokenAuthorization({
tokenKey: privateKey,
tokenPayload: {
sub: deviceId,
},
}),
})
const res = await getJSON<{ host: string; path: string }>({
resource: 'location/pgps',
payload: {
predictionCount: 6,
predictionIntervalMinutes: 120,
},
})
assert.notEqual(res.host, undefined)
assert.notEqual(res.path, undefined)
})
})