| 
									
										
										
										
											2015-12-16 02:51:38 +00:00
										 |  |  | /*! | 
					
						
							|  |  |  |  * letiny-core | 
					
						
							|  |  |  |  * Copyright(c) 2015 AJ ONeal <aj@daplie.com> https://daplie.com
 | 
					
						
							|  |  |  |  * Apache-2.0 OR MIT (and hence also MPL 2.0) | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports.create = function (deps) { | 
					
						
							|  |  |  |   var request = deps.request; | 
					
						
							|  |  |  |   var knownUrls = deps.LeCore.knownEndpoints; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function getAcmeUrls(acmeDiscoveryUrl, cb) { | 
					
						
							| 
									
										
										
										
											2015-12-16 09:48:34 +00:00
										 |  |  |     if ('string' !== typeof acmeDiscoveryUrl) { | 
					
						
							|  |  |  |       cb(new Error("getAcmeUrls: acmeDiscoveryUrl must be a string")); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-16 02:51:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // TODO check response header on request for cache time
 | 
					
						
							|  |  |  |     return request({ | 
					
						
							|  |  |  |       url: acmeDiscoveryUrl | 
					
						
							| 
									
										
										
										
											2015-12-16 12:57:26 +00:00
										 |  |  |     , encoding: 'utf8' | 
					
						
							| 
									
										
										
										
											2015-12-16 02:51:38 +00:00
										 |  |  |     }, function (err, resp) { | 
					
						
							|  |  |  |       if (err) { | 
					
						
							|  |  |  |         cb(err); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var data = resp.body; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if ('string' === typeof data) { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |           data = JSON.parse(data); | 
					
						
							|  |  |  |         } catch(e) { | 
					
						
							|  |  |  |           cb(e); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (4 !== Object.keys(data).length) { | 
					
						
							|  |  |  |         console.warn("This Let's Encrypt / ACME server has been updated with urls that this client doesn't understand"); | 
					
						
							|  |  |  |         console.warn(data); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!knownUrls.every(function (url) { | 
					
						
							|  |  |  |         return data[url]; | 
					
						
							|  |  |  |       })) { | 
					
						
							|  |  |  |         console.warn("This Let's Encrypt / ACME server is missing urls that this client may need."); | 
					
						
							|  |  |  |         console.warn(data); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 02:57:17 +00:00
										 |  |  |       cb(null, { | 
					
						
							| 
									
										
										
										
											2015-12-16 02:51:38 +00:00
										 |  |  |         newAuthz: data['new-authz'] | 
					
						
							|  |  |  |       , newCert: data['new-cert'] | 
					
						
							|  |  |  |       , newReg: data['new-reg'] | 
					
						
							|  |  |  |       , revokeCert: data['revoke-cert'] | 
					
						
							| 
									
										
										
										
											2015-12-16 02:57:17 +00:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-12-16 02:51:38 +00:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return getAcmeUrls; | 
					
						
							|  |  |  | }; |