mirror of https://codeberg.org/pzp/pzp-hub.git
fix members.add()
This commit is contained in:
parent
80f2a3afb0
commit
9998885daf
|
@ -1,9 +1,7 @@
|
|||
const Crypto = require('node:crypto')
|
||||
const Path = require('node:path')
|
||||
const AtomicFileRW = require('atomic-file-rw')
|
||||
const Base58 = require('bs58')
|
||||
|
||||
class Tokens {
|
||||
class Members {
|
||||
static #filePath
|
||||
|
||||
/**
|
||||
|
@ -34,38 +32,36 @@ class Tokens {
|
|||
}
|
||||
const json = typeof buf === 'string' ? buf : buf.toString('utf-8')
|
||||
const arr = JSON.parse(json)
|
||||
for (const token of arr) {
|
||||
this.#set.add(token)
|
||||
for (const pubkey of arr) {
|
||||
this.#set.add(pubkey)
|
||||
}
|
||||
this.#loaded = true
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} token
|
||||
* @param {string} pubkey
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static has(token) {
|
||||
static has(pubkey) {
|
||||
if (!this.#loaded) {
|
||||
throw new Error('Members not loaded yet, cannot call has()')
|
||||
}
|
||||
return this.#set.has(token)
|
||||
return this.#set.has(pubkey)
|
||||
}
|
||||
|
||||
static create() {
|
||||
/**
|
||||
* @param {string} pubkey
|
||||
*/
|
||||
static add(pubkey) {
|
||||
if (!this.#loaded) {
|
||||
throw new Error('Members not loaded yet, cannot call create()')
|
||||
}
|
||||
let token
|
||||
do {
|
||||
token = Base58.encode(Crypto.randomBytes(32))
|
||||
} while (this.#set.has(token))
|
||||
this.#set.add(token)
|
||||
this.#set.add(pubkey)
|
||||
this.#save((err, _) => {
|
||||
if (err) console.warn('Problem saving members file:', err)
|
||||
})
|
||||
return token
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Tokens
|
||||
module.exports = Members
|
||||
|
|
|
@ -45,13 +45,15 @@ module.exports = {
|
|||
const [clientMetadata, cb] = args
|
||||
const {pubkey, extra} = clientMetadata
|
||||
if (Members.has(pubkey)) {
|
||||
debug('authorized member %s to connect', pubkey)
|
||||
cb(null, true)
|
||||
} else if (extra && Tokens.has(extra)) {
|
||||
debug('authorized NEW member %s to connect', pubkey)
|
||||
Tokens.delete(extra)
|
||||
Members.add(pubkey)
|
||||
cb(null, true)
|
||||
} else {
|
||||
debug('prevented stranger %s from connecting to us', pubkey)
|
||||
debug('denied stranger %s from connecting', pubkey)
|
||||
cb(new Error('client is a stranger'))
|
||||
}
|
||||
})
|
||||
|
|
|
@ -32,7 +32,7 @@ class Tokens {
|
|||
}
|
||||
return
|
||||
}
|
||||
const json = typeof buf === 'string' ? buf : Buffer.toString(buf, 'utf-8')
|
||||
const json = typeof buf === 'string' ? buf : buf.toString('utf-8')
|
||||
const arr = JSON.parse(json)
|
||||
for (const token of arr) {
|
||||
this.#set.add(token)
|
||||
|
|
Loading…
Reference in New Issue