/
signature-form.js
123 lines (102 loc) · 5.23 KB
/
signature-form.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
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
// -----------------------------------------------------------------------------------------------------
// This file contains logic for calling the Web PKi component. It is only an example, feel free to alter
// it to meet your application's needs.
// -----------------------------------------------------------------------------------------------------
var signatureForm = (function() {
// Auxiliary global variables.
var token = null;
var formElement = null;
var selectElement = null;
// Create an instance of the LacunaWebPKi object.
var pki = new LacunaWebPKI(_webPkiLicense);
// -------------------------------------------------------------------------------------------------
// Initializes the signature form.
// -------------------------------------------------------------------------------------------------
function init(args) {
token = args.token;
formElement = args.form;
selectElement = args.certificateSelect;
// Wireup of button clicks.
args.signButton.click(sign);
args.refreshButton.click(refresh);
// Block the UI while we get things ready.
$.blockUI();
// Call the init() method on the LacunaWebPKI object, passing a callback for when the component
// is ready to be used and another to be called when an error occurs on any of the subsequen
// operations. For more information, see:
// http://docs.lacunasoftware.com/en-us/articles/web-pki/get-started.html
// http://webpki.lacunasoftware.com/Help/classes/LacunaWebPKI.html#method_init
pki.init({
ready: loadCertificates, // As soon as the component is ready we'll load the certificates.
defaultError: onWebPkiError, // Generic error callback.
restPkiUrl: _restPkiEndpoint
});
}
// -------------------------------------------------------------------------------------------------
// Function called when the user clicks the "Refresh" button.
// -------------------------------------------------------------------------------------------------
function refresh() {
// Block the UI while we load the certificates.
$.blockUI();
// Invoke the loading of the certificates.
loadCertificates();
}
// -------------------------------------------------------------------------------------------------
// Function that loads the certificates, wither on startup or when the user clicks the "Refresh"
// button. At this point, the UI is already blocked.
// -------------------------------------------------------------------------------------------------
function loadCertificates() {
// Call the listCertificates() method to list the user' certificates. For more information see:
// http://webpki.lacunasoftware.com/Help/classes/LacunaWebPKI.html#method_listCertificates
pki.listCertificates({
// The ID of the <select> element to be populated with the certificates.
selectId: selectElement.attr('id'),
// Function that will be called to get the text that should be displayed for each option.
selectOptionFormatter: function(cert) {
var s = cert.subjectName + ' (issued by ' + cert.issuerName + ')';
if (new Date() > cert.validityEnd) {
s = '[EXPIRED] ' + s;
}
return s;
}
}).success(function () {
// Once the certificates have been listed, unblock the UI.
$.unblockUI();
});
}
// -------------------------------------------------------------------------------------------------
// Function called when the user clicks the "Sign" button.
// -------------------------------------------------------------------------------------------------
function sign() {
// Block the UI while we perform the signature.
$.blockUI();
// Get the thumbprint of the selected certificate.
var selectedCertThumbprint = selectElement.val();
// Call signWithRestPki() on the Web PKI component passing the token received from REST PKI
// and the certificate selected by the user.
pki.signWithRestPki({
token: token,
thumbprint: selectedCertThumbprint
}).success(function () {
// Once the operation is completed, we submit the form.
formElement.submit();
});
}
// -------------------------------------------------------------------------------------------------
// Function called if an error occurs on the Web PKI component.
// -------------------------------------------------------------------------------------------------
function onWebPkiError(message, error, origin) {
// Unblock the UI.
$.unblockUI();
// Log the error to the browser console (for debugging purposes).
if (console) {
console.log('An error has occurred on the signature browser component: ' + message, error);
}
// Show the message to the user. You might want to substitute the alert below with a more
// user-friendly UI component to show the error.
alert(message);
}
return {
init: init
};
})();