index.js
3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
var fs = require('fs');
var path = require('path');
var myDefaults = {
//webrootPath: [ '~', 'letsencrypt', 'var', 'lib' ].join(path.sep)
webrootPath: path.join(require('os').tmpdir(), 'acme-challenge')
, loopbackTimeout: 5 * 1000
, debug: false
};
var Challenge = module.exports;
Challenge.create = function (options) {
var results = {};
Object.keys(Challenge).forEach(function (key) {
results[key] = Challenge[key];
});
results.create = undefined;
Object.keys(myDefaults).forEach(function (key) {
if ('undefined' === typeof options[key]) {
options[key] = myDefaults[key];
}
});
results._options = options;
results.getOptions = function () {
return results._options;
};
return results;
};
//
// NOTE: the "args" here in `set()` are NOT accessible to `get()` and `remove()`
// They are provided so that you can store them in an implementation-specific way
// if you need access to them.
//
Challenge.set = function (args, domain, challengePath, keyAuthorization, done) {
var mkdirp = require('@root/mkdirp');
keyAuthorization = String(keyAuthorization);
mkdirp(args.webrootPath, function (err) {
if (err) {
done(err);
return;
}
fs.writeFile(path.join(args.webrootPath, challengePath), keyAuthorization, 'utf8', function (err) {
done(err);
});
});
};
//
// NOTE: the "defaults" here are still merged and templated, just like "args" would be,
// but if you specifically need "args" you must retrieve them from some storage mechanism
// based on domain and key
//
Challenge.get = function (defaults, domain, key, done) {
fs.readFile(path.join(defaults.webrootPath, key), 'utf8', done);
};
Challenge.remove = function (defaults, domain, key, done) {
fs.unlink(path.join(defaults.webrootPath, key), done);
};
Challenge.loopback = function (defaults, domain, key, done) {
var hostname = domain + (defaults.loopbackPort ? ':' + defaults.loopbackPort : '');
var urlstr = 'http://' + hostname + '/.well-known/acme-challenge/' + key;
require('http').get(urlstr, function (res) {
if (200 !== res.statusCode) {
done(new Error("local loopback failed with statusCode " + res.statusCode));
return;
}
var chunks = [];
res.on('data', function (chunk) {
chunks.push(chunk);
});
res.on('end', function () {
var str = Buffer.concat(chunks).toString('utf8').trim();
done(null, str);
});
}).setTimeout(defaults.loopbackTimeout, function () {
done(new Error("loopback timeout, could not reach server"));
}).on('error', function (err) {
done(err);
});
};
Challenge.test = function (args, domain, challenge, keyAuthorization, done) {
var me = this;
var key = keyAuthorization || challenge;
me.set(args, domain, challenge, key, function (err) {
if (err) { done(err); return; }
myDefaults.loopbackPort = args.loopbackPort;
myDefaults.webrootPath = args.webrootPath;
me.loopback(args, domain, challenge, function (err, _key) {
if (err) { done(err); return; }
if (key !== _key) {
err = new Error("keyAuthorization [original] '" + key + "'"
+ " did not match [result] '" + _key + "'");
return;
}
me.remove(myDefaults, domain, challenge, function (_err) {
if (_err) { done(_err); return; }
done(err);
});
});
});
};