-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAzDpsClient.js
45 lines (42 loc) · 1.45 KB
/
AzDpsClient.js
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
// const REGISTRATION_TOPIC = '$dps/registrations/res/#'
// const REGISTER_TOPIC = '$dps/registrations/PUT/iotdps-register/?$rid='
/**
*
* @param {String} key
* @param {String} msg
* @returns {Promise<string>}
*/
export const createHmac = async (key, msg) => {
const keyBytes = Uint8Array.from(window.atob(key), c => c.charCodeAt(0))
const msgBytes = Uint8Array.from(msg, c => c.charCodeAt(0))
const cryptoKey = await window.crypto.subtle.importKey(
'raw', keyBytes, { name: 'HMAC', hash: 'SHA-256' },
true, ['sign']
)
const signature = await window.crypto.subtle.sign('HMAC', cryptoKey, msgBytes)
return window.btoa(String.fromCharCode(...new Uint8Array(signature)))
}
export class AzDpsClient {
constructor (scopeId, deviceId, deviceKey, modelId) {
this.host = 'global.azure-devices-provisioning.net'
this.scopeId = scopeId
this.deviceId = deviceId
this.deviceKey = deviceKey
this.modelId = modelId
}
async registerDevice () {
const endpoint = 'https://dps-proxy.azurewebsites.net/register'
const url = `${endpoint}?scopeId=${this.scopeId}&deviceId=${this.deviceId}&deviceKey=${encodeURIComponent(this.deviceKey)}&modelId=${this.modelId}`
console.log(url)
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Content-Encoding': 'utf-8'
}
})
const resp = await response.json()
console.log(resp)
return resp
}
}