Files: b6540cc83c887c7a955b7a6e3e2280c017951e30 / types.js
1984 bytesRaw
1 | var ref = require('ssb-ref') |
2 | var caps = {invite: require('./cap')} |
3 | var ssbKeys = require('ssb-keys') |
4 | var createIsBase64 = require('is-canonical-base64') |
5 | |
6 | function isObject (o) { |
7 | return o && 'object' === typeof o |
8 | } |
9 | |
10 | // signatures have a type (eg `.ed25519`) at the end, |
11 | // but not gonna check it right here. |
12 | var signature_rx = createIsBase64('', '\\.sig\\.\\w+') |
13 | var box_rx = createIsBase64() |
14 | |
15 | function isSignature(b) { |
16 | return signature_rx.test(b) |
17 | } |
18 | |
19 | function isMaybeBase64(b) { |
20 | return b === undefined || box_rx.test(b) |
21 | } |
22 | |
23 | exports.isInvite = function (msg, caps) { |
24 | if(!isObject(caps)) throw new Error('caps must be provided') |
25 | //return true |
26 | return isObject(msg) && isObject(msg.content) && ( |
27 | 'user-invite' === msg.content.type && |
28 | ref.isFeed(msg.content.host) && |
29 | ref.isFeed(msg.content.invite) && |
30 | isMaybeBase64(msg.content.reveal) && |
31 | isMaybeBase64(msg.content.public) && |
32 | // signature must be valid !!! |
33 | ssbKeys.verifyObj(msg.content.invite, caps.userInvite, msg.content) |
34 | ) |
35 | } |
36 | |
37 | exports.isAccept = function (msg) { |
38 | return isObject(msg) && isObject(msg.content) && ( |
39 | 'user-invite/accept' === msg.content.type && |
40 | msg.content.id == msg.author && |
41 | ref.isMsg(msg.content.receipt) && |
42 | isMaybeBase64(msg.content.key) && |
43 | // can't verify this without having the invite message. |
44 | // (that's intentional, forces implementers not to cut corners, |
45 | // but to check that the receipt is correct) |
46 | isSignature(msg.content.signature) |
47 | ) |
48 | } |
49 | |
50 | exports.isConfirm = function (msg) { |
51 | return isObject(msg) && isObject(msg.content) && ( |
52 | 'user-invite/confirm' === msg.content.type && |
53 | exports.isAccept(msg.content.embed) && |
54 | //second pointer back to receipt, so that links can find it |
55 | //(since it unfortunately does not handle links nested deeper |
56 | //inside objects. when we look up the message, |
57 | //confirm that content.embed.content.receipt is the same) |
58 | msg.content.embed.content.receipt === msg.content.receipt |
59 | ) |
60 | } |
61 | |
62 | |
63 | |
64 |
Built with git-ssb-web