Guest User

Untitled

a guest
Mar 22nd, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. <script src="common/js/enrollment-2.0.4.min.js"></script>
  2. <script type="text/javascript">
  3. const button = document.getElementById('register');
  4. const keytype = document.getElementById('keytype');
  5. const username_f = document.getElementById('username');
  6. const email_f = document.getElementById('email');
  7. const name_f = document.getElementById('name');
  8. const externalWebId_f = document.getElementById('externalWebId');
  9. const password = document.getElementById('password');
  10. const certificate_f = document.getElementById('certificatePublic');
  11.  
  12. function formatPEM(pemString) {
  13. /// <summary>Format string in order to have each line with length equal to 63</summary>
  14. /// <param name="pemString" type="String">String to format</param>
  15.  
  16. const stringLength = pemString.length;
  17. let resultString = "";
  18.  
  19. for(let i = 0, count = 0; i < stringLength; i++, count++) {
  20. if(count > 63) {
  21. resultString = `${resultString}\r\n`;
  22. count = 0;
  23. }
  24. resultString = `${resultString}${pemString[i]}`;
  25. }
  26. return resultString;
  27. }
  28.  
  29. button.addEventListener('click', function(event) {
  30. // TODO: take this out so normal validation still happens
  31. event.preventDefault();
  32.  
  33. // First check the account name, with an AJAX POST to webidfor API endpoint
  34. let data = {
  35. username: username_f.value,
  36. email: email_f.value,
  37. name: name_f.value,
  38. externalWebId: externalWebId_f.value
  39. };
  40.  
  41. let formdata = {
  42. method: 'POST',
  43. // TODO: add credentials: 'include' here, as per login/tls approach (not sure what this does exactly!)
  44. headers: new Headers({ 'Content-Type': 'application/json' }),
  45. body: JSON.stringify(data),
  46. };
  47. fetch('/api/accounts/webidfor', formdata).then(function (response) {
  48. return response.text();
  49. }).then(function (webid) {
  50.  
  51. // DEBUG
  52. console.log("webid: "+webid);
  53.  
  54. // 2. generate a pair of keys for this webid
  55. let options = { type: keytype.value.toUpperCase() };
  56.  
  57. enrollment.keygen.keygen(options).then(function (keypair) {
  58. // DEBUG
  59. console.log("keypair: "+keypair);
  60. //let pubkey = keypair.publicKey;
  61. //return crypto.subtle.exportKey("jwk", pubkey);
  62.  
  63. // 3. Create and self-sign this certificate with the keys
  64. enrollment.cert.generateAndSign(keypair, webid).then((cert_data) => {
  65. // DEBUG
  66. console.log(cert_data);
  67.  
  68. // 4. Package it all up in a .p12 and send it back to the user for Download/Install
  69.  
  70. let certificateBuffer = cert_data.certificate.toSchema(true).toBER(false);
  71. let certificateString = String.fromCharCode.apply(null, new Uint8Array(certificateBuffer));
  72.  
  73. let resultString = "-----BEGIN CERTIFICATE-----\r\n";
  74. resultString = `${resultString}${formatPEM(window.btoa(certificateString))}`;
  75. resultString = `${resultString}\r\n-----END CERTIFICATE-----\r\n`;
  76.  
  77. // fill this value in to a textarea for submitting with the form at the end..
  78. certificate_f.innerHTML = resultString;
  79.  
  80. return enrollment.pkcs12.create({
  81. password: password.value,
  82. certificateChain: [resultString],
  83. key: cert_data.keypair,
  84. });
  85. }).then((encodedP12) => {
  86. // DEBUG
  87. console.log(encodedP12);
  88. // Now send the final .p12 file back to the user
  89. const pkcs12AsBlob = new Blob([encodedP12], { type: "application/x-pkcs12" });
  90. const downloadLink = document.createElement("a");
  91. // TODO: give it a proper name
  92. downloadLink.download = "pkijs_pkcs12.p12";
  93. downloadLink.innerHTML = "Download P12";
  94.  
  95. downloadLink.href = window.URL.createObjectURL(pkcs12AsBlob);
  96. downloadLink.onclick = function (event) { document.body.removeChild(event.target); }
  97. downloadLink.style.display = "none";
  98. document.body.appendChild(downloadLink);
  99.  
  100. downloadLink.click();
  101. });
  102. });
  103. });
  104. });
  105. </script>
Add Comment
Please, Sign In to add comment