-
Notifications
You must be signed in to change notification settings - Fork 46
/
pades-signature.js
153 lines (125 loc) · 6.05 KB
/
pades-signature.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
app.controller('padesSignatureController', ['$scope', '$http', '$routeParams', 'blockUI', 'util', function ($scope, $http, $routeParams, blockUI, util) {
$scope.certificates = [];
$scope.selectedCertificate = null;
$scope.userfile = null;
// Create an instance of the LacunaWebPKI "object"
var pki = new LacunaWebPKI(_webPkiLicense);
// -------------------------------------------------------------------------------------------------
// Function that initializes the Web PKI component
// -------------------------------------------------------------------------------------------------
var init = function () {
// Block the UI while we get things ready
blockUI.start();
// Retrive parameter "userfile"
$scope.userfile = $routeParams.userfile;
// 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 occurrs
// on any of the subsequent operations. For more information, see:
// https://webpki.lacunasoftware.com/#/Documentation#coding-the-first-lines
// http://webpki.lacunasoftware.com/Help/classes/LacunaWebPKI.html#method_init
pki.init({
ready: loadCertificates,
defaultError: onWebPkiError, // generic error callback
restPkiUrl: _restPkiEndpoint, // URL of the Rest PKI instance to be used
angularScope: $scope // Pass Angularjs scope for WebPKI
});
};
// -------------------------------------------------------------------------------------------------
// Function called when the user clicks the "Refresh" button
// -------------------------------------------------------------------------------------------------
$scope.refresh = function () {
blockUI.start();
loadCertificates();
};
// -------------------------------------------------------------------------------------------------
// Function that loads the certificates, either on startup or when the user
// clicks the "Refresh" button. At this point, the UI is already blocked.
// -------------------------------------------------------------------------------------------------
var loadCertificates = function () {
// Call the listCertificates() method to list the user's certificates. For more information see
// http://webpki.lacunasoftware.com/Help/classes/LacunaWebPKI.html#method_listCertificates
pki.listCertificates({
// specify that expired certificates should be ignored
//filter: pki.filters.isWithinValidity,
// in order to list only certificates within validity period and having a CPF (ICP-Brasil), use this instead:
//filter: pki.filters.all(pki.filters.hasPkiBrazilCpf, pki.filters.isWithinValidity),
}).success(function (certificates) {
// Remember the selected certificate (see below)
var originalSelected = ($scope.selectedCertificate || {}).thumbprint;
// Set available certificates on scope
$scope.certificates = certificates;
// Recover previous selection
angular.forEach(certificates, function (c) {
if (c.thumbprint === originalSelected) {
$scope.selectedCertificate = c;
}
});
// once the certificates have been listed, unblock the UI
blockUI.stop();
});
};
$scope.getCertificateDisplayName = function (cert) {
return cert.subjectName + ' (expires on ' + cert.validityEnd.toDateString() + ', issued by ' + cert.issuerName + ')';
};
// -------------------------------------------------------------------------------------------------
// Function called when the user clicks the "Sign" button
// -------------------------------------------------------------------------------------------------
$scope.sign = function () {
if ($scope.selectedCertificate == null) {
util.showMessage('Message', 'Please select a certificate');
return;
}
blockUI.start();
if ($scope.userfile) {
$http.post('Api/PadesSignature?userfile=' + $scope.userfile).then(function (response) {
onSignatureStartCompleted(response.data);
}, util.handleServerError);
} else {
$http.post('Api/PadesSignature').then(function (response) {
onSignatureStartCompleted(response.data);
}, util.handleServerError);
}
};
// -------------------------------------------------------------------------------------------------
// Function called once the server replies with the token for the signature process
// -------------------------------------------------------------------------------------------------
var onSignatureStartCompleted = function (token) {
pki.signWithRestPki({
thumbprint: $scope.selectedCertificate.thumbprint,
token: token
}).success(function () {
onSignCompleted(token);
});
};
// -------------------------------------------------------------------------------------------------
// Function called once the signature is completed
// -------------------------------------------------------------------------------------------------
var onSignCompleted = function (token) {
$http.post('Api/PadesSignature/' + token).then(function (response) {
blockUI.stop();
util.showMessage('Signature completed successfully!', 'Click OK to see details').result.then(function () {
var results = {
cosignUrl: 'pades-signature',
signedfile: response.data.filename,
certificate: response.data.certificate
};
util.showSignatureResults(results);
});
}, util.handleServerError);
};
// -------------------------------------------------------------------------------------------------
// Function called if an error occurs on the Web PKI component
// -------------------------------------------------------------------------------------------------
var onWebPkiError = function (message, error, origin) {
// Unblock the UI
blockUI.stop();
// 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
util.showMessage('Error', message);
};
init();
}]);