invites are issued by pubkey, not account, for now

This commit is contained in:
Andre Staltz 2024-01-16 15:12:47 +02:00
parent 85737b4898
commit e931f4ebbd
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
4 changed files with 36 additions and 28 deletions

View File

@ -25,16 +25,16 @@ const p = require('promisify-tuple')
* @typedef {`tunnel-connect/${string}/${string}`} TunnelConnectCommandStr
* @typedef {{
* type: 'promise.follow',
* issuerID: string,
* issuer: ['account' | 'pubkey', string],
* token: string,
* }} PromiseFollowCommand
* @typedef {`promise.follow/account.${string}/${string}`} PromiseFollowCommandStr
* @typedef {`promise.follow/${string}/${'account' | 'pubkey'}.${string}`} PromiseFollowCommandStr
* @typedef {{
* type: 'promise.account-add',
* issuerID: string,
* issuer: ['account' | 'pubkey', string],
* token: string,
* }} PromiseAccountAddCommand
* @typedef {`promise.account-add/account.${string}/${string}`} PromiseAccountAddCommandStr
* @typedef {`promise.account-add/${string}/${'account' | 'pubkey'}.${string}`} PromiseAccountAddCommandStr
* @typedef {| JoinCommand
* | FollowCommand
* | TunnelConnectCommand
@ -152,24 +152,28 @@ function parseTunnelConnectCommand(pieces, uri) {
* @returns {PromiseFollowCommand}
*/
function parsePromiseFollowCommand(pieces, uri) {
const [label, issuerAndType, token] = pieces
if (!issuerAndType) {
// prettier-ignore
throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.follow issuer`)
}
const [label, token, issuerAndType] = pieces
if (!token) {
// prettier-ignore
throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.follow token`)
}
if (!issuerAndType) {
// prettier-ignore
throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.follow issuer`)
}
pieces.shift()
pieces.shift()
pieces.shift()
const [issuerType, issuerID] = issuerAndType.split('.')
if (issuerType !== 'account') {
if (issuerType !== 'pubkey') {
// prettier-ignore
throw new Error(`Invalid URI "${uri}" for invite.parse, invalid promise.follow issuer type "${issuerType}"`)
}
return { type: 'promise.follow', issuerID, token }
return {
type: 'promise.follow',
issuer: [issuerType, issuerID],
token,
}
}
/**
@ -178,24 +182,28 @@ function parsePromiseFollowCommand(pieces, uri) {
* @returns {PromiseAccountAddCommand}
*/
function parsePromiseAccountAddCommand(pieces, uri) {
const [label, issuerAndType, token] = pieces
if (!issuerAndType) {
// prettier-ignore
throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.account-add issuer`)
}
const [label, token, issuerAndType] = pieces
if (!token) {
// prettier-ignore
throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.account-add token`)
}
if (!issuerAndType) {
// prettier-ignore
throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.account-add issuer`)
}
pieces.shift()
pieces.shift()
pieces.shift()
const [issuerType, issuerID] = issuerAndType.split('.')
if (issuerType !== 'account') {
if (issuerType !== 'pubkey') {
// prettier-ignore
throw new Error(`Invalid URI "${uri}" for invite.parse, invalid promise.account-add issuer type "${issuerType}"`)
}
return { type: 'promise.account-add', issuerID, token }
return {
type: 'promise.account-add',
issuer: [issuerType, issuerID],
token,
}
}
/**
@ -372,7 +380,7 @@ function initInvite(peer, config) {
})
if (err2) return cb(err2)
/** @type {PromiseFollowCommandStr} */
const promiseCommand = `promise.follow/account.${opts.id}/${token}`
const promiseCommand = `promise.follow/${token}/pubkey.${peer.shse.pubkey}`
// Create follow command
/** @type {FollowCommandStr} */
@ -421,7 +429,7 @@ function initInvite(peer, config) {
const [err3, token] = await p(peer.promise.create)(promise)
if (err3) return cb(err3)
/** @type {PromiseAccountAddCommandStr} */
const promiseCommand = `promise.account-add/account.${opts.id}/${token}`
const promiseCommand = `promise.account-add/${token}/pubkey.${peer.shse.pubkey}`
// Create tunnel-connect command
/** @type {TunnelConnectCommandStr} */

View File

@ -89,11 +89,11 @@ test('createForFriend()', async (t) => {
})
assert.equal(
uri,
`ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/follow/MOCK_ID/promise.follow/account.MOCK_ID/MOCK_PROMISE`
`ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/follow/MOCK_ID/promise.follow/MOCK_PROMISE/pubkey.${keypair.public}`
)
assert.equal(
url,
`https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ffollow%2FMOCK_ID%2Fpromise.follow%2Faccount.MOCK_ID%2FMOCK_PROMISE`
`https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ffollow%2FMOCK_ID%2Fpromise.follow%2FMOCK_PROMISE%2Fpubkey.${keypair.public}`
)
assert.ok(connectCalled)

View File

@ -87,11 +87,11 @@ test('createForMyself()', async (t) => {
})
assert.equal(
uri,
`ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/tunnel-connect/HUB_PUBKEY/${local.shse.pubkey}/promise.account-add/account.MOCK_ID/MOCK_PROMISE`
`ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/tunnel-connect/HUB_PUBKEY/${local.shse.pubkey}/promise.account-add/MOCK_PROMISE/pubkey.${keypair.public}`
)
assert.equal(
url,
`https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ftunnel-connect%2FHUB_PUBKEY%2F${local.shse.pubkey}%2Fpromise.account-add%2Faccount.MOCK_ID%2FMOCK_PROMISE`
`https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ftunnel-connect%2FHUB_PUBKEY%2F${local.shse.pubkey}%2Fpromise.account-add%2FMOCK_PROMISE%2Fpubkey.${keypair.public}`
)
assert.ok(connectCalled)

View File

@ -16,7 +16,7 @@ test('parse() error cases', (t) => {
test('parse() good friend invite', (t) => {
const commands = plugin.parse(
'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/follow/ALICE/promise.follow/account.ALICE/ALICE_TOKEN'
'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/follow/ALICE/promise.follow/ALICE_TOKEN/pubkey.ALICE'
)
assert.deepEqual(commands, [
{
@ -29,7 +29,7 @@ test('parse() good friend invite', (t) => {
},
{
type: 'promise.follow',
issuerID: 'ALICE',
issuer: ['pubkey', 'ALICE'],
token: 'ALICE_TOKEN',
},
])
@ -37,7 +37,7 @@ test('parse() good friend invite', (t) => {
test('parse() good myself invite', (t) => {
const commands = plugin.parse(
'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/tunnel-connect/HUB_PUBKEY/OLD_PUBKEY/promise.account-add/account.ACCOUNT_ID/OLD_TOKEN'
'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/tunnel-connect/HUB_PUBKEY/OLD_PUBKEY/promise.account-add/OLD_TOKEN/pubkey.PUBKEY'
)
assert.deepEqual(commands, [
{
@ -50,7 +50,7 @@ test('parse() good myself invite', (t) => {
},
{
type: 'promise.account-add',
issuerID: 'ACCOUNT_ID',
issuer: ['pubkey', 'PUBKEY'],
token: 'OLD_TOKEN',
},
])