blob: 589035c34f2754160d35924ce6da90e94da9774e [file] [log] [blame]
beccabroek309b5da2018-11-07 12:22:31 -06001/**
2 * Controller for Certificate Management
3 *
4 * @module app/configuration
5 * @exports certificateController
6 * @name certificateController
7 */
8
9window.angular && (function(angular) {
10 'use strict';
11
12 angular.module('app.configuration').controller('certificateController', [
miramurali235e8785d2019-06-10 15:09:27 -050013 '$scope', 'APIUtils', '$q', 'Constants', 'toastService', '$timeout',
14 function($scope, APIUtils, $q, Constants, toastService, $timeout) {
beccabroek309b5da2018-11-07 12:22:31 -060015 $scope.loading = false;
16 $scope.certificates = [];
17 $scope.availableCertificateTypes = [];
miramurali235e8785d2019-06-10 15:09:27 -050018 $scope.allCertificateTypes = Constants.CERTIFICATE_TYPES;
beccabroek309b5da2018-11-07 12:22:31 -060019 $scope.addCertificateModal = false;
miramurali235e8785d2019-06-10 15:09:27 -050020 $scope.addCSRModal = false;
beccabroek309b5da2018-11-07 12:22:31 -060021 $scope.newCertificate = {};
miramurali235e8785d2019-06-10 15:09:27 -050022 $scope.newCSR = {};
beccabroek309b5da2018-11-07 12:22:31 -060023 $scope.submitted = false;
miramurali235e8785d2019-06-10 15:09:27 -050024 $scope.csrSubmitted = false;
25 $scope.csrCode = '';
26 $scope.displayCSRCode = false;
27 $scope.keyBitLength = Constants.CERTIFICATE.KEY_BIT_LENGTH;
28 $scope.keyPairAlgorithm = Constants.CERTIFICATE.KEY_PAIR_ALGORITHM;
29 $scope.keyCurveId = Constants.CERTIFICATE.KEY_CURVE_ID;
30 $scope.countryList = Constants.COUNTRIES;
31
beccabroek309b5da2018-11-07 12:22:31 -060032
33 $scope.loadCertificates = function() {
34 $scope.certificates = [];
35 $scope.availableCertificateTypes = Constants.CERTIFICATE_TYPES;
36 $scope.loading = true;
37 // Use Certificate Service to get the locations of all the certificates,
38 // then add a promise for fetching each certificate
39 APIUtils.getCertificateLocations().then(
40 function(data) {
41 var promises = [];
42 var locations = data.Links.Certificates;
43 for (var i in locations) {
44 var location = locations[i];
45 promises.push(getCertificatePromise(location['@odata.id']));
46 }
47 $q.all(promises)
48 .catch(function(error) {
49 toastService.error('Failed to load certificates.');
50 console.log(JSON.stringify(error));
51 })
52 .finally(function() {
53 $scope.loading = false;
54 });
55 },
56 function(error) {
57 $scope.loading = false;
58 $scope.availableCertificateTypes = [];
59 toastService.error('Failed to load certificates.');
60 console.log(JSON.stringify(error));
61 });
62 };
63
64 $scope.uploadCertificate = function() {
65 if ($scope.newCertificate.file.name.split('.').pop() !== 'pem') {
66 toastService.error('Certificate must be a .pem file.');
67 return;
68 }
69 $scope.addCertificateModal = false;
70 APIUtils
71 .addNewCertificate(
72 $scope.newCertificate.file, $scope.newCertificate.selectedType)
73 .then(
74 function(data) {
75 toastService.success(
76 $scope.newCertificate.selectedType.Description +
77 ' was uploaded.');
78 $scope.newCertificate = {};
79 $scope.loadCertificates();
80 },
81 function(error) {
82 toastService.error(
83 $scope.newCertificate.selectedType.Description +
84 ' failed upload.');
85 console.log(JSON.stringify(error));
86 });
87 };
88
89 var getCertificatePromise = function(url) {
90 var promise = APIUtils.getCertificate(url).then(function(data) {
91 var certificate = data;
92 isExpiring(certificate);
93 updateAvailableTypes(certificate);
94 $scope.certificates.push(certificate);
95 });
96 return promise;
97 };
98
99 var isExpiring = function(certificate) {
100 // if ValidNotAfter is less than or equal to 30 days from today
101 // (2592000000), isExpiring. If less than or equal to 0, is expired.
102 var difference = certificate.ValidNotAfter - new Date();
103 if (difference <= 0) {
104 certificate.isExpired = true;
105 } else if (difference <= 2592000000) {
106 certificate.isExpiring = true;
107 } else {
108 certificate.isExpired = false;
109 certificate.isExpiring = false;
110 }
111 };
112
miramurali235e8785d2019-06-10 15:09:27 -0500113 // add optional name
114 $scope.names = [];
115 $scope.addOptionalRow = function() {
116 $scope.names.push({Value: ''})
117 };
118
119 // remove optional name row
120 $scope.deleteOptionalRow = function(index) {
121 $scope.names.splice(index, 1);
122 if ($scope.names.length == 0) {
123 $scope.names = [];
124 }
125 };
126
127
128 // create a CSR object to send to the backend
129 $scope.getCSRCode = function() {
130 var addCSR = {};
131 let alternativeNames = $scope.names.map(name => name.Value);
132
133 // if user provided a first alternative name then push to alternative
134 // names array
135 $scope.newCSR.firstAlternativeName ?
136 alternativeNames.push($scope.newCSR.firstAlternativeName) :
137 $scope.newCSR.firstAlternativeName = '';
138
139
140 addCSR.CertificateCollection = {
141 '@odata.id': $scope.newCSR.certificateCollection.location
142 };
143 addCSR.CommonName = $scope.newCSR.commonName;
144 addCSR.ContactPerson = $scope.newCSR.contactPerson || '';
145 addCSR.City = $scope.newCSR.city;
146 addCSR.AlternativeNames = alternativeNames || [];
147 addCSR.ChallengePassword = $scope.newCSR.challengePassword || '';
148 addCSR.Email = $scope.newCSR.emailAddress || '';
149 addCSR.Country = $scope.newCSR.countryCode.code;
150 addCSR.Organization = $scope.newCSR.organization;
151 addCSR.OrganizationalUnit = $scope.newCSR.companyUnit;
152 addCSR.KeyCurveId = $scope.newCSR.keyCurveId || '';
153 addCSR.KeyBitLength = $scope.newCSR.keyBitLength
154 addCSR.KeyPairAlgorithm = $scope.newCSR.keyPairAlgorithm || '';
155 addCSR.State = $scope.newCSR.state;
156
157 APIUtils.createCSRCertificate(addCSR).then(
158 function(data) {
159 $scope.displayCSRCode = true;
160 $scope.csrCode = data;
161 },
162 function(error) {
163 $scope.addCSRModal = false;
164 toastService.error('Unable to generate CSR. Try again.');
165 console.log(JSON.stringify(error));
166 })
167 };
168
169 // resetting the modal when user clicks cancel/closes the
170 // modal
171 $scope.resetCSRModal = function() {
172 $scope.addCSRModal = false;
173 $scope.displayCSRCode = false;
174 $scope.newCSR.certificateCollection = $scope.selectOption;
175 $scope.newCSR.commonName = '';
176 $scope.newCSR.contactPerson = '';
177 $scope.newCSR.city = '';
178 $scope.names = [];
179 $scope.newCSR.challengePassword = '';
180 $scope.newCSR.emailAddress = '';
181 $scope.newCSR.countryCode = '';
182 $scope.newCSR.keyCurveId = '';
183 $scope.newCSR.firstAlternativeName = '';
184 $scope.newCSR.keyBitLength = $scope.selectOption;
185 $scope.newCSR.keyPairAlgorithm = $scope.selectOption;
186 $scope.newCSR.organization = '';
187 $scope.newCSR.companyUnit = '';
188 $scope.newCSR.state = '';
189 };
190
191 // copies the CSR code
192 $scope.copySuccess = function(event) {
193 $scope.copied = true;
194 $timeout(function() {
195 $scope.copied = false;
196 }, 5000);
197 };
198 $scope.copyFailed = function(err) {
199 console.log(JSON.stringify(err));
200 };
201
202
beccabroek309b5da2018-11-07 12:22:31 -0600203 var updateAvailableTypes = function(certificate) {
204 // TODO: at this time only one of each type of certificate is allowed.
205 // When this changes, this will need to be updated.
206 // Removes certificate type from available types to be added.
207 $scope.availableCertificateTypes =
208 $scope.availableCertificateTypes.filter(function(type) {
209 return type.Description !== certificate.Description;
210 });
211 };
212
213 $scope.getDays = function(endDate) {
214 // finds number of days until certificate expiration
215 var ms = endDate - new Date();
216 return Math.floor(ms / (24 * 60 * 60 * 1000));
217 };
218
219 $scope.loadCertificates();
220 }
221 ]);
222})(angular);