blob: 49ad050baceba272c0b812289121a15da4c33ac1 (
plain)
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
|
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Success! All the File APIs are supported.
} else {
alert('Please use a web browser that supports HTML5 file APIs.')
}
function formatCredentialTextForHeader (credentialText) {
// Replace any CR/LF pairs with a newline character.
credentialText = credentialText.replace(/\r\n/g, '\n')
// Add line endings for C-language variable declaration.
credentialText = credentialText.replace(/\n/g, '\\n" \\\n "')
// Remove '\n"' from the last line of the declaration and add a semicolon.
credentialText = credentialText.slice(0, -9) + '"\n'
return credentialText
}
function generateCertificateConfigurationHeader () {
var pemCertificateText = ''
var pemPrivateKeyText = ''
var filename = 'aws_iot_demo_profile.h'
var outputText = ''
var readerCertificate = new FileReader()
var readerPrivateKey = new FileReader()
// Start certificate read
readerCertificate.readAsText(pemInputFileCertificate.files[0])
// Define a handler to create appropriate client certificate file text.
readerCertificate.onload = function (e) {
pemCertificateText = e.target.result
// Add C-language variable declaration plus EOL formatting.
pemCertificateText = ' "' + formatCredentialTextForHeader(pemCertificateText)
// Because this is async, read next file inline.
readerPrivateKey.readAsText(pemInputFilePrivateKey.files[0])
}
// Define a handler to create appropriate private key file text.
readerPrivateKey.onload = function (e) {
pemPrivateKeyText = e.target.result
// Add C-language variable declaration plus EOL formatting.
pemPrivateKeyText = ' "' + formatCredentialTextForHeader(pemPrivateKeyText)
outputText = awsIotProfileTemplate
outputText = outputText.replace('<IOTEndpoint>', '"' + document.getElementById('AWSEndpoint').value + '"')
outputText = outputText.replace('<IOTThingName>', '"' + document.getElementById('thingName').value + '"')
outputText = outputText.replace('<ClientCertificatePEM>', pemCertificateText)
outputText = outputText.replace('<ClientPrivateKeyPEM>', pemPrivateKeyText)
// Because this is async, handle download generation inline.
var downloadBlob = new Blob([outputText], { type: 'text/plain' })
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(downloadBlob, filename)
} else {
var downloadLink = document.createElement('a')
downloadLink.href = window.URL.createObjectURL(downloadBlob)
downloadLink.download = filename
document.body.appendChild(downloadLink)
downloadLink.click()
document.body.removeChild(downloadLink)
}
}
}
|