git ssb

30+

cel / git-ssb-web



Commit 55018e457a311cc1c0ebcb5e9c4285a582bee8ba

Serve ACME challenges

cel committed on 7/26/2017, 3:51:13 AM
Parent: 2ec4c75b9f6a1dec550137036f6caeba13597fe4

Files changed

index.jschanged
lib/acme-challenge.jsadded
index.jsView
@@ -133,8 +133,10 @@
133133 if (webConfig.computeIssueCounts !== false) {
134134 this.indexCache = require('./lib/index-cache')(ssb)
135135 }
136136
137 + this.serveAcmeChallenge = require('./lib/acme-challenge')(ssb)
138 +
137139 var addr = parseAddr(config.listenAddr, {
138140 host: webConfig.host || 'localhost',
139141 port: webConfig.port || 7718
140142 })
@@ -203,8 +205,12 @@
203205 }
204206
205207 function G_onRequest(req, res) {
206208 this.log('info', req.method, req.url)
209 +
210 + if (req.url.startsWith('/.well-known/acme-challenge'))
211 + return this.serveAcmeChallenge(req, res)
212 +
207213 req._u = url.parse(req.url, true)
208214 var locale = req._u.query.locale ||
209215 (/locale=([^;]*)/.exec(req.headers.cookie) || [])[1]
210216 var reqLocales = req.headers['accept-language']
lib/acme-challenge.jsView
@@ -1,0 +1,50 @@
1 +var pull = require('pull-stream')
2 +var asyncmemo = require('asyncmemo')
3 +var lru = require('lrucache')
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 +}

Built with git-ssb-web