| 
									
										
										
										
											2015-12-16 12:42:04 +00:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 12:51:14 +00:00
										 |  |  | module.exports.create = function (defaults) { | 
					
						
							| 
									
										
										
										
											2015-12-16 12:42:04 +00:00
										 |  |  |   var fs = require('fs'); | 
					
						
							|  |  |  |   var path = require('path'); | 
					
						
							|  |  |  |   var mkdirp = require('mkdirp'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 12:51:14 +00:00
										 |  |  |   var handlers = { | 
					
						
							| 
									
										
										
										
											2015-12-16 12:42:04 +00:00
										 |  |  |     //
 | 
					
						
							|  |  |  |     // set,get,remove challenges
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     _challenges: {} | 
					
						
							|  |  |  |   , setChallenge: function (args, key, value, cb) { | 
					
						
							| 
									
										
										
										
											2015-12-19 20:50:02 +00:00
										 |  |  |       var challengePath = path.join(defaults.webrootPath, '.well-known', 'acme-challenge'); | 
					
						
							|  |  |  |       mkdirp(challengePath, function (err) { | 
					
						
							| 
									
										
										
										
											2015-12-16 12:42:04 +00:00
										 |  |  |         if (err) { | 
					
						
							| 
									
										
										
										
											2015-12-19 20:50:02 +00:00
										 |  |  |           console.error("Could not create --webroot-path '" + challengePath + "':", err.code); | 
					
						
							| 
									
										
										
										
											2015-12-16 12:42:04 +00:00
										 |  |  |           console.error("Try checking the permissions, maybe?"); | 
					
						
							|  |  |  |           cb(err); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-19 20:50:02 +00:00
										 |  |  |         var keyfile = path.join(challengePath, key); | 
					
						
							| 
									
										
										
										
											2015-12-16 12:42:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         fs.writeFile(keyfile, value, 'utf8', function (err) { | 
					
						
							|  |  |  |           if (err) { | 
					
						
							|  |  |  |             console.error("Could not write '" + keyfile + "':", err.code); | 
					
						
							|  |  |  |             cb(err); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           cb(null); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   // handled as file read by web server
 | 
					
						
							|  |  |  |   // , getChallenge: function (args, key, cb) {}
 | 
					
						
							|  |  |  |   , removeChallenge: function (args, key, cb) { | 
					
						
							| 
									
										
										
										
											2015-12-19 20:50:02 +00:00
										 |  |  |       var keyfile = path.join(defaults.webrootPath, '.well-known', 'acme-challenge', key); | 
					
						
							| 
									
										
										
										
											2015-12-16 12:42:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       fs.unlink(keyfile, function (err) { | 
					
						
							|  |  |  |         if (err) { | 
					
						
							|  |  |  |           console.error("Could not unlink '" + keyfile + "':", err.code); | 
					
						
							|  |  |  |           cb(err); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cb(null); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2015-12-16 12:51:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return handlers; | 
					
						
							| 
									
										
										
										
											2015-12-16 12:42:04 +00:00
										 |  |  | }; |