Files: d19c73f6a5c9563914be6761839a3ba08ba10394 / lib / acme-challenge.js
1397 bytesRaw
1 | var pull = require('pull-stream') |
2 | var asyncmemo = require('asyncmemo') |
3 | var lru = require('hashlru') |
4 | |
5 | function getChallenge(sbot, token, cb) { |
6 | /* TODO: index this on disk */ |
7 | pull( |
8 | sbot.messagesByType('acme-challenges-http-01'), |
9 | pull.map(function (msg) { |
10 | return msg.value.content.challenges |
11 | }), |
12 | pull.flatten(), |
13 | pull.filter(function (challenge) { |
14 | return challenge.token === token |
15 | }), |
16 | pull.collect(function (err, msgs) { |
17 | cb(err, msgs && msgs[0]) |
18 | }) |
19 | ) |
20 | } |
21 | |
22 | function respond(res, code, str) { |
23 | res.writeHead(code) |
24 | res.end(str) |
25 | } |
26 | |
27 | function respondError(res, code, err) { |
28 | return respond(res, code, JSON.stringify(err, 0, 2)) |
29 | } |
30 | |
31 | module.exports = function (sbot) { |
32 | var getChallengeCached = asyncmemo({cache: lru(10)}, getChallenge, sbot) |
33 | |
34 | function serveChallenge(req, res, token) { |
35 | getChallengeCached(token, function (err, challenge) { |
36 | if (err) return respondError(res, 500, err) |
37 | if (!challenge) return respond(res, 404, 'Challenge not found') |
38 | /* TODO (maybe): |
39 | * validate challenge based on domain, msg author, or something */ |
40 | respond(res, 200, challenge.keyAuthorization) |
41 | }) |
42 | } |
43 | |
44 | return function (req, res, next) { |
45 | var m = /^\/\.well-known\/acme-challenge\/([^?]*)/.exec(req.url) |
46 | if (m) return serveChallenge(req, res, m[1]) |
47 | if (next) return next() |
48 | respond(res, 404, 'Not found') |
49 | } |
50 | } |
51 |
Built with git-ssb-web