blob: 7fa42a1017602cb7a54633d5b2f4cc7a0661336e [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', [
13 '$scope', 'APIUtils', '$q', 'Constants', 'toastService',
14 function($scope, APIUtils, $q, Constants, toastService) {
15 $scope.loading = false;
16 $scope.certificates = [];
17 $scope.availableCertificateTypes = [];
18 $scope.addCertificateModal = false;
19 $scope.newCertificate = {};
20 $scope.submitted = false;
21
22 $scope.loadCertificates = function() {
23 $scope.certificates = [];
24 $scope.availableCertificateTypes = Constants.CERTIFICATE_TYPES;
25 $scope.loading = true;
26 // Use Certificate Service to get the locations of all the certificates,
27 // then add a promise for fetching each certificate
28 APIUtils.getCertificateLocations().then(
29 function(data) {
30 var promises = [];
31 var locations = data.Links.Certificates;
32 for (var i in locations) {
33 var location = locations[i];
34 promises.push(getCertificatePromise(location['@odata.id']));
35 }
36 $q.all(promises)
37 .catch(function(error) {
38 toastService.error('Failed to load certificates.');
39 console.log(JSON.stringify(error));
40 })
41 .finally(function() {
42 $scope.loading = false;
43 });
44 },
45 function(error) {
46 $scope.loading = false;
47 $scope.availableCertificateTypes = [];
48 toastService.error('Failed to load certificates.');
49 console.log(JSON.stringify(error));
50 });
51 };
52
53 $scope.uploadCertificate = function() {
54 if ($scope.newCertificate.file.name.split('.').pop() !== 'pem') {
55 toastService.error('Certificate must be a .pem file.');
56 return;
57 }
58 $scope.addCertificateModal = false;
59 APIUtils
60 .addNewCertificate(
61 $scope.newCertificate.file, $scope.newCertificate.selectedType)
62 .then(
63 function(data) {
64 toastService.success(
65 $scope.newCertificate.selectedType.Description +
66 ' was uploaded.');
67 $scope.newCertificate = {};
68 $scope.loadCertificates();
69 },
70 function(error) {
71 toastService.error(
72 $scope.newCertificate.selectedType.Description +
73 ' failed upload.');
74 console.log(JSON.stringify(error));
75 });
76 };
77
78 var getCertificatePromise = function(url) {
79 var promise = APIUtils.getCertificate(url).then(function(data) {
80 var certificate = data;
81 isExpiring(certificate);
82 updateAvailableTypes(certificate);
83 $scope.certificates.push(certificate);
84 });
85 return promise;
86 };
87
88 var isExpiring = function(certificate) {
89 // if ValidNotAfter is less than or equal to 30 days from today
90 // (2592000000), isExpiring. If less than or equal to 0, is expired.
91 var difference = certificate.ValidNotAfter - new Date();
92 if (difference <= 0) {
93 certificate.isExpired = true;
94 } else if (difference <= 2592000000) {
95 certificate.isExpiring = true;
96 } else {
97 certificate.isExpired = false;
98 certificate.isExpiring = false;
99 }
100 };
101
102 var updateAvailableTypes = function(certificate) {
103 // TODO: at this time only one of each type of certificate is allowed.
104 // When this changes, this will need to be updated.
105 // Removes certificate type from available types to be added.
106 $scope.availableCertificateTypes =
107 $scope.availableCertificateTypes.filter(function(type) {
108 return type.Description !== certificate.Description;
109 });
110 };
111
112 $scope.getDays = function(endDate) {
113 // finds number of days until certificate expiration
114 var ms = endDate - new Date();
115 return Math.floor(ms / (24 * 60 * 60 * 1000));
116 };
117
118 $scope.loadCertificates();
119 }
120 ]);
121})(angular);