| 
									
										
										
										
											2015-12-29 18:32:37 +00:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var scmp = require('scmp'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function middleware(opts) { | 
					
						
							|  |  |  |   var key = opts.key; | 
					
						
							|  |  |  |   var val = opts.value; | 
					
						
							|  |  |  |   var vhost = opts.vhost; | 
					
						
							| 
									
										
										
										
											2015-12-30 06:35:21 +00:00
										 |  |  |   var pathnamePrefix = opts.loopbackPrefix; | 
					
						
							|  |  |  |   var defaultHostname = opts.loopbackHostname; | 
					
						
							| 
									
										
										
										
											2015-12-29 18:32:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-30 06:35:21 +00:00
										 |  |  |   if (!defaultHostname) { | 
					
						
							|  |  |  |     defaultHostname = opts.loopbackHostname = 'loopback.daplie.invalid'; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (!pathnamePrefix) { | 
					
						
							|  |  |  |     pathnamePrefix = opts.loopbackPrefix = '/.well-known/com.daplie.loopback/'; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-12-29 18:32:37 +00:00
										 |  |  |   if (!key) { | 
					
						
							| 
									
										
										
										
											2015-12-30 06:35:21 +00:00
										 |  |  |     key = opts.key = require('crypto').randomBytes(8).toString('hex'); | 
					
						
							| 
									
										
										
										
											2015-12-29 18:32:37 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (!val) { | 
					
						
							| 
									
										
										
										
											2015-12-30 06:35:21 +00:00
										 |  |  |     val = opts.value = require('crypto').randomBytes(16).toString('hex'); | 
					
						
							| 
									
										
										
										
											2015-12-29 18:32:37 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (!vhost && vhost !== false) { | 
					
						
							|  |  |  |     vhost = defaultHostname; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if ('/' !== pathnamePrefix[pathnamePrefix.length - 1]) { | 
					
						
							|  |  |  |     pathnamePrefix += '/'; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return function (req, res, next) { | 
					
						
							|  |  |  |     var hostname = (req.hostname || req.headers.host || '').toLowerCase(); | 
					
						
							|  |  |  |     var urlpath = (req.pathname || req.url); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (vhost !== false && vhost !== hostname) { | 
					
						
							|  |  |  |       if (opts.debug) { | 
					
						
							|  |  |  |         console.log("[HP] Host '" + hostname + "' failed to match '" + vhost + "'"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       next(); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (0 !== urlpath.indexOf(pathnamePrefix)) { | 
					
						
							|  |  |  |       if (opts.debug) { | 
					
						
							|  |  |  |         console.log("[HP] Pathname '" + urlpath + "'" | 
					
						
							| 
									
										
										
										
											2015-12-30 08:46:22 +00:00
										 |  |  |           + " failed to match prefix '" + pathnamePrefix + ": " | 
					
						
							|  |  |  |           + urlpath.indexOf(pathnamePrefix) | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-12-29 18:32:37 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       next(); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-30 08:46:22 +00:00
										 |  |  |     if (!scmp(key, urlpath.substr(pathnamePrefix.length))) { | 
					
						
							| 
									
										
										
										
											2015-12-29 18:32:37 +00:00
										 |  |  |       if (opts.debug) { | 
					
						
							| 
									
										
										
										
											2015-12-30 08:46:22 +00:00
										 |  |  |         console.log("[HP] key '" + urlpath.substr(pathnamePrefix.length) + "'" | 
					
						
							|  |  |  |           + " failed to match '" + key + "': " | 
					
						
							|  |  |  |           + scmp(key, urlpath.substr(pathnamePrefix.length)) | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-12-29 18:32:37 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       next(); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'text/plain'); | 
					
						
							|  |  |  |     res.end(val); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = middleware; |