git ssb

0+

Dominic / ssb-peer-invites



Commit 040389641134bf4080d6df213d990ebbb8a3a667

update everything to pass and use caps

Dominic Tarr committed on 12/16/2018, 12:34:41 AM
Parent: c42bd4ae81847e219235cbdc6c86571ff49fc410

Files changed

cap.jschanged
index.jschanged
types.jschanged
valid.jschanged
cap.jsView
@@ -5,6 +5,6 @@
55 'string' == typeof s ? new Buffer(s, 'utf8') : s
66 )
77 }
88
9-module.exports = hash("user-invites:DEVELOPMENT")
9 +module.exports = hash("user-invites:DEVELOPMENT") //XXX DON'T publish without fixing this!
1010
index.jsView
@@ -48,22 +48,25 @@
4848 exports.init = function (sbot, config) {
4949 var init = false
5050 var layer = sbot.friends.createLayer('user-invites')
5151
52 + var caps = config.caps || {}
53 + caps.userInvite = caps.userInvite || require('./cap')
54 +
5255 function reduce (acc, data, _seq) {
5356 if(!acc) acc = {invited: {}, invites:{}, accepts: {}, hosts: {}}
5457 var msg = data.value
5558 var invite, accept
56- if(types.isInvite(msg)) {
59 + if(types.isInvite(msg, caps)) {
5760 //TODO: validate that this is a msg we understand!
5861 invite = msg
5962 accept = acc.accepts[data.key]
6063 }
61- else if(types.isAccept(msg)) {
64 + else if(types.isAccept(msg, caps)) {
6265 accept = msg
6366 invite = acc.invites[accept.content.receipt]
6467 }
65- else if(types.isConfirm(msg)) {
68 + else if(types.isConfirm(msg, caps)) {
6669 //TODO: just for when we are the guest, but we need to make sure at least one confirm exists.
6770 accept = msg.content.embed
6871 invite = acc.invites[accept.content.receipt]
6972 }
@@ -71,9 +74,9 @@
7174 if(invite && accept) {
7275 if(invite === true)
7376 return acc
7477 var invite_id = accept.content.receipt
75- try { I.verifyAccept(accept, invite) }
78 + try { I.verifyAccept(accept, invite, caps) }
7679 catch (err) { return acc }
7780 //fall through from not throwing
7881
7982 //delete matched invites, but _only_ if they are VALID. (returned in the catch if invalid)
@@ -210,9 +213,9 @@
210213 if(confirm) return cb(null, confirm)
211214
212215 sbot.get(invite_id, function (err, invite) {
213216 try {
214- I.verifyAccept(accept, invite)
217 + I.verifyAccept(accept, invite, caps)
215218 } catch (err) {
216219 return cb(err)
217220 }
218221 //there is a little race condition here, if accept is called again
@@ -275,9 +278,9 @@
275278 getNearbyPubs(opts, function (err, near) {
276279 var seed = crypto.randomBytes(32)
277280 sbot.identities.publishAs({
278281 id: opts.id || sbot.id,
279- content: I.createInvite(seed, opts.id || sbot.id, opts.reveal, opts.private)
282 + content: I.createInvite(seed, opts.id || sbot.id, opts.reveal, opts.private, caps)
280283 }, function (err, data) {
281284 cb(null, {
282285 seed: seed,
283286 invite: data.key,
@@ -293,9 +296,9 @@
293296 pubs.forEach(function (addr) {
294297 n++
295298 ssbClient(keys, {
296299 remote: addr,
297- caps: require('ssb-config').caps,
300 + caps: caps,
298301 manifest: {
299302 userInvites: {
300303 getInvite: 'async',
301304 confirm: 'async'
@@ -321,9 +324,8 @@
321324 next(msg)
322325 else {
323326 var pubs = invite.pubs
324327 var keys = ssbKeys.generate(null, invite.seed)
325- console.log("CF", invite)
326328 connectFirst(keys, pubs, function (err, rpc) {
327329 if(err) return cb(err)
328330 rpc.userInvites.getInvite(invite.invite, function (err, msg) {
329331 if(err) return cb(err)
@@ -338,14 +340,13 @@
338340 return cb(new Error(
339341 'incorrect invite was returned! expected:'+invite.invite+', but got:'+inviteId
340342 ))
341343 var opened
342- try { opened = I.verifyInvitePrivate(msg, invite.seed) }
344 + try { opened = I.verifyInvitePrivate(msg, invite.seed, caps) }
343345 catch (err) { return cb(err) }
344346 //UPDATE REDUCE STATE.
345347 // this is a wee bit naughty, because if you rebuild the index it might not have this invite
346348 // (until you replicate it, but when you do the value won't change)
347- console.log("CURR REDUCE STATE", state.value)
348349 state.set(reduce(state.value, {key: invite_id, value:msg}, invites.since.value))
349350 cb(null, msg, opened)
350351 }
351352 })
@@ -365,9 +366,9 @@
365366 else {
366367 invites.openInvite(invite, function (err, invite_msg, opened) {
367368 sbot.identities.publishAs({
368369 id: id,
369- content: I.createAccept(invite_msg, invite.seed, id)
370 + content: I.createAccept(invite_msg, invite.seed, id, caps)
370371 }, function (err, accept) {
371372 if(err) cb(err)
372373 else {
373374 state.set(reduce(state.value, accept, invites.since.value))
@@ -395,4 +396,5 @@
395396 }
396397 return invites
397398 }
398399
400 +
types.jsView
@@ -19,18 +19,19 @@
1919 function isMaybeBase64(b) {
2020 return b === undefined || box_rx.test(b)
2121 }
2222
23-exports.isInvite = function (msg) {
23 +exports.isInvite = function (msg, caps) {
24 + if(!isObject(caps)) throw new Error('caps must be provided')
2425 //return true
2526 return isObject(msg) && isObject(msg.content) && (
2627 'user-invite' === msg.content.type &&
2728 ref.isFeed(msg.content.host) &&
2829 ref.isFeed(msg.content.invite) &&
2930 isMaybeBase64(msg.content.reveal) &&
3031 isMaybeBase64(msg.content.public) &&
3132 // signature must be valid !!!
32- ssbKeys.verifyObj(msg.content.invite, caps.invite, msg.content)
33 + ssbKeys.verifyObj(msg.content.invite, caps.userInvite, msg.content)
3334 )
3435 }
3536
3637 exports.isAccept = function (msg) {
@@ -54,8 +55,4 @@
5455 }
5556
5657
5758
58-
59-
60-
61-
valid.jsView
@@ -8,37 +8,46 @@
88 err.code = 'user-invites:'+c
99 return err
1010 }
1111
12-exports.createInvite = function (seed, host, reveal, private) {
12 +function isObject (o) {
13 + return o && 'object' === typeof o
14 +}
15 +
16 +exports.createInvite = function (seed, host, reveal, private, caps) {
17 + if(!isObject(caps)) throw new Error('caps *must* be provided')
18 +
1319 var keys = ssbKeys.generate(null, seed) //K
1420 if(keys.id === host)
1521 throw code(new Error('do not create invite with own public key'), 'user-invites:no-own-goal')
16- return ssbKeys.signObj(keys, invite_key, {
22 + return ssbKeys.signObj(keys, caps.userInvite, {
1723 type: 'user-invite',
1824 invite: keys.id,
1925 host: host, //sign our own key, to prove we created K
2026 reveal: reveal ? u.box(reveal, u.hash(u.hash(seed))) : undefined,
2127 private: private ? u.box(private, u.hash(seed)) : undefined
2228 })
2329 }
2430
25-exports.verifyInvitePublic = function (msg) {
31 +exports.verifyInvitePublic = function (msg, caps) {
32 + if(!isObject(caps)) throw new Error('caps *must* be provided')
33 +
2634 if(msg.content.host != msg.author)
2735 throw code(new Error('host did not match author'), 'host-must-match-author')
2836
29- if(!ssbKeys.verifyObj(msg.content.invite, invite_key, msg.content))
37 + if(!ssbKeys.verifyObj(msg.content.invite, caps.userInvite, msg.content))
3038 throw code(new Error('invalid invite signature'), 'invite-signature-failed')
3139
3240 //an ordinary message so doesn't use special hmac_key, unless configed to.
33- //TODO: import caps from config!!!
34- if(!ssbKeys.verifyObj(msg.author, msg))
41 + if(!ssbKeys.verifyObj(msg.author, caps.sign, msg))
3542 throw code(new Error('invalid host signature'), 'host-signature-failed')
3643 return true
3744 }
3845
39-exports.verifyInvitePrivate = function (msg, seed) {
40- exports.verifyInvitePublic(msg)
46 +exports.verifyInvitePrivate = function (msg, seed, caps) {
47 + if(!isObject(caps)) throw new Error('caps *must* be provided')
48 +
49 + exports.verifyInvitePublic(msg, caps)
4150 if(msg.content.reveal) {
4251 var reveal = u.unbox(msg.content.reveal, u.hash(u.hash(seed)))
4352 if(!reveal) throw code(new Error('could not decrypt reveal field'), 'decrypt-reveal-failed')
4453 }
@@ -49,35 +58,40 @@
4958
5059 return {reveal: reveal, private: private}
5160 }
5261
53-exports.createAccept = function (msg, seed, id) {
54- exports.verifyInvitePrivate(msg, seed)
62 +exports.createAccept = function (msg, seed, id, caps) {
63 + if(!isObject(caps)) throw new Error('caps *must* be provided')
64 +
65 + exports.verifyInvitePrivate(msg, seed, caps)
5566 var keys = ssbKeys.generate(null, seed) //K
5667 if(keys.id != msg.content.invite)
5768 throw code(new Error('seed does not match invite'), 'seed-must-match-invite')
5869 var inviteId = '%'+ssbKeys.hash(JSON.stringify(msg, null, 2))
59- return ssbKeys.signObj(keys, invite_key, {
70 + return ssbKeys.signObj(keys, caps.userInvite, {
6071 type: 'user-invite/accept',
6172 receipt: inviteId,
6273 id: id,
6374 key: msg.content.reveal ? u.hash(u.hash(seed)).toString('base64') : undefined
6475 })
6576 }
6677
67-exports.verifyAcceptOnly = function (accept) {
78 +exports.verifyAcceptOnly = function (accept, caps) {
79 + if(!isObject(caps)) throw new Error('caps *must* be provided')
6880 if(accept.content.type !== 'user-invite/accept')
6981 throw code(new Error('accept must be type: "user-invite/accept", was:'+JSON.stringify(accept.content.type)), 'accept-message-type')
7082 if(!isMsg(accept.content.receipt))
7183 throw code(new Error('accept must reference invite message id'), 'accept-reference-invite')
72- if(!ssbKeys.verifyObj(accept.content.id, accept))
84 + //verify signed as ordinary message.
85 + if(!ssbKeys.verifyObj(accept.content.id, caps.sign, accept))
7386 throw code(new Error('acceptance must be signed by claimed key'), 'accept-signature-failed')
7487 }
7588
76-exports.verifyAccept = function (accept, invite) {
89 +exports.verifyAccept = function (accept, invite, caps) {
90 + if(!isObject(caps)) throw new Error('caps *must* be provided')
7791 if(!invite) throw new Error('invite must be provided')
7892
79- exports.verifyAcceptOnly(accept)
93 + exports.verifyAcceptOnly(accept, caps)
8094
8195 if(invite.content.type !== 'user-invite')
8296 throw code(new Error('accept must be type: invite, was:'+accept.content.type), 'user-invites:invite-message-type')
8397
@@ -95,10 +109,11 @@
95109 reveal = u.unbox(invite.content.reveal, new Buffer(accept.content.key, 'base64'))
96110 if(!reveal) throw code(new Error('accept did not correctly reveal invite'), 'decrypt-accept-reveal-failed')
97111 }
98112
99- if(!ssbKeys.verifyObj(invite.content.invite, invite_key, accept.content))
113 + if(!ssbKeys.verifyObj(invite.content.invite, caps.userInvite, accept.content))
100114 throw code(new Error('did not verify invite-acceptance contents'), 'accept-invite-signature-failed')
101115 //an ordinary message, so does not use hmac_key
102116 return reveal || true
103117 }
104118
119 +

Built with git-ssb-web