| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  | import * as plugins from './smartacme.plugins'; | 
					
						
							|  |  |  | import { Cert } from './smartacme.classes.cert'; | 
					
						
							| 
									
										
										
										
											2019-01-09 00:01:01 +01:00
										 |  |  | import { SmartAcme } from './smartacme.classes.smartacme'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import * as interfaces from './interfaces'; | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class CertManager { | 
					
						
							|  |  |  |   // =========
 | 
					
						
							|  |  |  |   // STATIC
 | 
					
						
							|  |  |  |   // =========
 | 
					
						
							|  |  |  |   public static activeDB: plugins.smartdata.SmartdataDb; | 
					
						
							| 
									
										
										
										
											2019-02-06 09:47:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  |   // =========
 | 
					
						
							|  |  |  |   // INSTANCE
 | 
					
						
							|  |  |  |   // =========
 | 
					
						
							|  |  |  |   private mongoDescriptor: plugins.smartdata.IMongoDescriptor; | 
					
						
							|  |  |  |   public smartdataDb: plugins.smartdata.SmartdataDb; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 20:13:06 +00:00
										 |  |  |   public interestMap: plugins.lik.InterestMap<string, Cert>; | 
					
						
							| 
									
										
										
										
											2019-01-09 00:01:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-06 09:47:33 +01:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |     smartAcmeArg: SmartAcme, | 
					
						
							|  |  |  |     optionsArg: { | 
					
						
							|  |  |  |       mongoDescriptor: plugins.smartdata.IMongoDescriptor; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   ) { | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  |     this.mongoDescriptor = optionsArg.mongoDescriptor; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-06 09:47:33 +01:00
										 |  |  |   public async init() { | 
					
						
							| 
									
										
										
										
											2019-01-09 00:01:01 +01:00
										 |  |  |     // Smartdata DB
 | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  |     this.smartdataDb = new plugins.smartdata.SmartdataDb(this.mongoDescriptor); | 
					
						
							|  |  |  |     await this.smartdataDb.init(); | 
					
						
							|  |  |  |     CertManager.activeDB = this.smartdataDb; | 
					
						
							| 
									
										
										
										
											2019-01-09 00:01:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Pending Map
 | 
					
						
							| 
									
										
										
										
											2020-02-21 10:48:08 +00:00
										 |  |  |     this.interestMap = new plugins.lik.InterestMap(certName => certName); | 
					
						
							| 
									
										
										
										
											2019-01-13 21:40:40 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * retrieves a certificate | 
					
						
							|  |  |  |    * @returns the Cert class or null | 
					
						
							| 
									
										
										
										
											2020-02-10 20:13:06 +00:00
										 |  |  |    * @param certDomainNameArg the domain Name to retrieve the vcertificate for | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-02-10 20:13:06 +00:00
										 |  |  |   public async retrieveCertificate(certDomainNameArg: string): Promise<Cert> { | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  |     const existingCertificate: Cert = await Cert.getInstance({ | 
					
						
							| 
									
										
										
										
											2020-02-10 20:13:06 +00:00
										 |  |  |       domainName: certDomainNameArg | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-06 09:47:33 +01:00
										 |  |  |     if (existingCertificate) { | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  |       return existingCertificate; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2019-01-15 23:39:31 +01:00
										 |  |  |    * stores the certificate | 
					
						
							| 
									
										
										
										
											2019-02-06 09:47:33 +01:00
										 |  |  |    * @param optionsArg | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-02-10 20:13:06 +00:00
										 |  |  |   public async storeCertificate(optionsArg: plugins.tsclass.network.ICert) { | 
					
						
							| 
									
										
										
										
											2019-01-12 13:44:18 +01:00
										 |  |  |     const cert = new Cert(optionsArg); | 
					
						
							| 
									
										
										
										
											2019-01-13 19:40:32 +01:00
										 |  |  |     await cert.save(); | 
					
						
							| 
									
										
										
										
											2020-02-10 20:13:06 +00:00
										 |  |  |     const interest = this.interestMap.findInterest(cert.domainName); | 
					
						
							|  |  |  |     if (interest) { | 
					
						
							|  |  |  |       interest.fullfillInterest(cert); | 
					
						
							|  |  |  |       interest.markLost(); | 
					
						
							| 
									
										
										
										
											2019-01-09 00:01:01 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 20:13:06 +00:00
										 |  |  |   public async deleteCertificate(certDomainNameArg: string) { | 
					
						
							|  |  |  |     const cert: Cert = await Cert.getInstance({ | 
					
						
							|  |  |  |       domainName: certDomainNameArg | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await cert.delete(); | 
					
						
							| 
									
										
										
										
											2019-02-06 09:47:33 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-01-08 20:45:35 +01:00
										 |  |  | } |