rpc: unify acl branch and user

Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-02-11 19:07:12 +00:00
parent 2213ab3ed0
commit 7d3884eb9a
6 changed files with 82 additions and 57 deletions

View File

@@ -1,42 +1,41 @@
const ACL = require('../acl')
const home = require('../home')
async function getACLHandler (publicKey, req) {
const { repoName, userId } = await this.parseACLRequest(publicKey, req)
return Buffer.from(JSON.stringify(ACL.getACL(repoName)))
const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req)
const repoACL = ACL.getACL(repoName)
return JSON.stringify(repoACL)
}
async function addACLHandler (publicKey, req) {
const { repoName, userId, acl } = await this.parseACLRequest(publicKey, req)
const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req)
// TODO
const aclData = JSON.parse(acl)
ACL.setACL(repoName, aclData)
return Buffer.from('ACL updated')
const { protectedBranches } = ACL.getACL(repoName)
return JSON.stringify({ protectedBranches })
}
async function delACLHandler (publicKey, req) {
const { repoName, userId, acl } = await this.parseACLRequest(publicKey, req)
const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req)
}
async function chgACLHandler (publicKey, req) {
const { repoName, userId, acl } = await this.parseACLRequest(publicKey, req)
const { protectedBranches } = ACL.getACL(repoName)
return JSON.stringify({ protectedBranches })
}
async function parseACLRequest(publicKey, req) {
if (!req) throw new Error('Request is empty')
const request = JSON.parse(req.toString())
const userId = await this.authenticate(publicKey, request)
const isOwner = ACL.getOwnder(repoName).includes(userId)
if (!isOwner) throw new Error('You are not allowed to access this repo')
const repoName = request.body.url?.split('/')?.pop()
// TODO: check if repo exists
if (!home.isInitialized(repoName)) throw new Error('Repo does not exist')
const isOwner = ACL.getOwners(repoName).includes(userId)
if (!isOwner) throw new Error('You are not allowed to access this repo')
return {
repoName,
userId,
// FIXME
acl: request.body.acl,
}
}
@@ -45,5 +44,7 @@ module.exports = {
getACLHandler,
addACLHandler,
delACLHandler,
chgACLHandler,
}

View File

@@ -1,35 +0,0 @@
const ACL = require('../acl')
async function getBPRHandler (publicKey, req) {
}
async function addBPRHandler (publicKey, req) {
}
async function delBPRHandler (publicKey, req) {
}
async function parseBPRRequest(publicKey, req) {
if (!req) throw new Error('Request is empty')
const request = JSON.parse(req.toString())
const userId = await this.authenticate(publicKey, request)
const isOwner = ACL.getOwnder(repoName).includes(userId)
if (!isOwner) throw new Error('You are not allowed to access this repo')
const repoName = request.body.url?.split('/')?.pop()
// TODO: check if repo exists
return {
repoName,
userId,
// FIXME
acl: request.body.acl,
}
}
module.exports = {
getBPRHandler,
addBPRHandler,
delBPRHandler,
}

View File

View File

@@ -0,0 +1,61 @@
const ProtomuxRPC = require('protomux-rpc')
const Hyperswarm = require('hyperswarm')
const crypto = require('hypercore-crypto')
const home = require('../home')
const auth = require('../auth')
async function list (url) {
const matches = url.match(/pear:\/\/([a-f0-9]{64})/)
if (!matches || matches.length < 2) {
console.error('Invalid URL')
process.exit(1)
}
const targetKey = matches[1]
console.log('Connecting to:', targetKey)
const swarmOpts = {}
if (process.env.GIT_PEAR_AUTH === 'native') {
swarmOpts.keyPair = home.getKeyPair()
}
const swarm = new Hyperswarm(swarmOpts)
swarm.join(crypto.discoveryKey(Buffer.from(targetKey, 'hex')), { server: false })
swarm.on('connection', async (socket) => {
const rpc = new ProtomuxRPC(socket)
let payload = { body: { url, method: 'get-repos' } }
if (!process.env.GIT_PEAR_AUTH) {
console.debug('Retreiving data using un-authenticated access')
} else {
console.debug('Retreiving data using authenticated access')
}
if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
payload.header = await auth.getToken(payload.body)
}
const reposRes = await rpc.request('get-repos', Buffer.from(JSON.stringify(payload)))
const repositories = JSON.parse(reposRes.toString())
if (!repositories) {
console.error('Failed to retrieve repositories')
process.exit(1)
}
paylod = { body: { url, method: 'get-bpr' } }
if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
payload.header = await auth.getToken(payload.body)
}
const bpr = await rpc.request('get-bpr', Buffer.from(JSON.stringify(payload)))
console.log('BPR:', JSON.parse(bpr.toString()))
process.exit(0)
})
}
module.exports = {
list,
}

View File

@@ -1,5 +1,7 @@
const listRemote = require('./list-remote')
const bpr = require('./bpr-remote')
module.exports = {
listRemote,
bpr,
}

View File

@@ -42,10 +42,6 @@ module.exports = class RPC {
rpc.respond('add-acl', async req => await acl.addACLHandler.bind(this)(socket.remotePublicKey, req))
rpc.respond('chg-acl', async req => await acl.chgCLHandler.bind(this)(socket.remotePublicKey, req))
rpc.respond('del-acl', async req => await acl.delACLHandler.bind(this)(socket.remotePublicKey, req))
/* -- BRANCH HANDLERS -- */
rpc.respond('get-bpr', async req => await bpr.getBPRHandler.bind(this)(socket.remotePublicKey, req))
rpc.respond('add-bpr', async req => await bpr.addBPRHandler.bind(this)(socket.remotePublicKey, req))
rpc.respond('del-bpr', async req => await bpr.delBPRHandler.bind(this)(socket.remotePublicKey, req))
}
async authenticate (publicKey, request) {