mobileconfig-generator/js/main.js

133 lines
4.4 KiB
JavaScript
Raw Normal View History

2020-06-22 17:24:44 +02:00
"use strict";
2020-06-22 21:28:22 +02:00
const data = {
payload: {
EmailAccountType: "",
EmailAddress: "",
EmailAccountDescription: "",
IncomingMailServerAuthentication: "",
IncomingMailServerHostName: "",
2020-06-22 21:35:20 +02:00
IncomingMailServerPortNumber: 993,
2020-06-22 21:28:22 +02:00
IncomingMailServerUsername: "",
2020-06-22 21:35:20 +02:00
IncomingMailServerUseSSL: false,
2020-06-22 21:28:22 +02:00
OutgoingMailServerAuthentication: "",
OutgoingMailServerHostName: "",
2020-06-22 21:35:20 +02:00
OutgoingMailServerPortNumber: 465,
2020-06-22 21:28:22 +02:00
OutgoingMailServerUsername: "",
2020-06-22 21:35:20 +02:00
OutgoingMailServerUseSSL: false,
OutgoingPasswordSameAsIncomingPassword: false,
2020-06-22 21:28:22 +02:00
PayloadDescription: "",
PayloadDisplayName: "",
PayloadIdentifier: "",
PayloadType: "com.apple.mail.managed",
PayloadUUID: generateUUID,
PayloadVersion: 1,
},
payloadPlaceholders: {
EmailAddress: "john@example.com",
EmailAccountDescription: "Example email account",
2020-06-22 17:24:44 +02:00
2020-06-22 21:28:22 +02:00
IncomingMailServerHostName: "imap.example.com",
IncomingMailServerPortNumber: 993,
IncomingMailServerUsername: "john@example.com",
2020-06-22 17:24:44 +02:00
2020-06-22 21:28:22 +02:00
OutgoingMailServerHostName: "smtp.example.com",
OutgoingMailServerPortNumber: 465,
OutgoingMailServerUsername: "john@example.com",
PayloadDescription: "Email configuration profile",
PayloadDisplayName: "Display name",
PayloadIdentifier: "com.apple.dns.reverse",
},
container: {
PayloadDescription: "",
PayloadDisplayName: "",
PayloadIdentifier: "",
PayloadType: "Configuration",
PayloadUUID: generateUUID,
PayloadVersion: 1,
},
containerPlaceholders: {
PayloadDescription: "Email configuration profile",
PayloadDisplayName: "Display name",
PayloadIdentifier: "com.apple.dns.reverse",
},
generated: false,
2020-06-22 17:24:44 +02:00
}
function generateUUID() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
)
}
function convertKeyValueToXml(xmlDoc, key, value) {
2020-06-22 21:28:22 +02:00
const keyElement = xmlDoc.createElement("key");
2020-06-22 17:24:44 +02:00
keyElement.innerHTML = key;
let valueElementTag = null;
if (typeof value === "function") {
value = value();
}
if (typeof value === "number") {
valueElementTag = "integer";
}
else if (typeof value === "string") {
valueElementTag = "string";
}
else if (typeof value === "boolean") {
valueElementTag = String(value);
}
if (valueElementTag === null) {
throw Error("Value type not recognized")
}
2020-06-22 21:28:22 +02:00
const valueElement = xmlDoc.createElement(valueElementTag);
2020-06-22 17:24:44 +02:00
if (typeof value === "number" || typeof value === "string"){
valueElement.innerHTML = value;
}
return [keyElement, valueElement];
}
2020-06-22 21:28:22 +02:00
const app = new Vue({
el: '#app',
data: data,
computed: {
generatedProfile: function () {
const template = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" +
2020-06-22 21:35:20 +02:00
"<plist version='1.0'><dict><key>PayloadContent</key><array><dict></dict></array></dict></plist>"
2020-06-22 21:28:22 +02:00
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(template, "application/xml");
const dict = xmlDoc.getElementsByTagName("dict")[0];
2020-06-22 21:35:20 +02:00
for (const [key, value] of Object.entries(this.container)) {
const [keyElement, valueElement] = convertKeyValueToXml(xmlDoc, key, value);
dict.appendChild(keyElement);
dict.appendChild(valueElement);
}
2020-06-22 21:28:22 +02:00
2020-06-22 21:35:20 +02:00
const contentDict = xmlDoc.getElementsByTagName("dict")[1];
2020-06-22 21:28:22 +02:00
for (const [key, value] of Object.entries(this.payload)) {
const [keyElement, valueElement] = convertKeyValueToXml(xmlDoc, key, value);
contentDict.appendChild(keyElement);
contentDict.appendChild(valueElement);
}
const serializer = new XMLSerializer();
return serializer.serializeToString(xmlDoc);
},
generatedProfileLink: function () {
const xmlString = this.generatedProfile;
return 'data:application/xml;charset=utf-8,' + encodeURIComponent(xmlString);
},
downloadFileName: function() {
return `${this.payload.EmailAddress}.mobileconfig`;
},
}
})
2020-06-22 17:24:44 +02:00