-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
38 lines (36 loc) · 1.14 KB
/
index.js
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
var querystr = require('querystring')
var address = require('ssb-address')
var hyperquest = require('hyperquest')
var concat = require('concat-stream')
var ACCESS_PATH = '/access.json'
var AUTH_PATH = '/auth.html'
module.exports = {
getToken: function(addr, cb) {
addr = address(addr)
var req = hyperquest.get(addr.domain + ACCESS_PATH)
req.setHeader('Content-Type', 'application/json')
req.on('response', function(res) {
if (res.statusCode < 200 || res.statusCode >= 400)
return cb(new Error(res.statusCode + ' ' + res.statusMessage))
res.pipe(concat(function(body) {
try {
var json = JSON.parse(body)
cb(null, json)
} catch (e) {
cb(new Error('Failed to parse json response'))
}
}))
})
req.on('error', cb)
},
deauth: function(addr, cb) {
addr = address(addr)
var req = hyperquest.delete(address(addr).domain + AUTH_PATH)
req.on('response', function(res) {
if (res.statusCode < 200 || res.statusCode >= 400)
return cb(new Error(res.statusCode + ' ' + res.statusMessage))
return cb()
})
req.on('error', cb)
}
}