Fix routing/proxying of AP, works locally

RSA keys get seeded but not for tests
This commit is contained in:
Robert Schäfer 2019-03-26 01:01:30 +01:00
parent 17fdbf4389
commit 46c0d0659a
9 changed files with 39 additions and 39 deletions

View File

@ -4,7 +4,7 @@
"description": "GraphQL Backend for Human Connection",
"main": "src/index.js",
"config": {
"no_auth": "cross-env GRAPHQL_URI=http://localhost:4001 GRAPHQL_PORT=4001 PERMISSIONS=disabled"
"no_auth": "cross-env GRAPHQL_URI=http://localhost:4001 GRAPHQL_PORT=4001 DISABLED_MIDDLEWARES=permissions"
},
"scripts": {
"build": "babel src/ -d dist/ --copy-files",
@ -15,7 +15,7 @@
"test": "nyc --reporter=text-lcov yarn test:jest",
"test:cypress": "run-p --race test:before:*",
"test:before:server": "cross-env CLIENT_URI=http://localhost:4123 GRAPHQL_URI=http://localhost:4123 GRAPHQL_PORT=4123 babel-node src/ 2> /dev/null",
"test:before:seeder": "cross-env GRAPHQL_URI=http://localhost:4001 GRAPHQL_PORT=4001 PERMISSIONS=disabled babel-node src/ 2> /dev/null",
"test:before:seeder": "cross-env GRAPHQL_URI=http://localhost:4001 GRAPHQL_PORT=4001 DISABLED_MIDDLEWARES=permissions,activityPub babel-node src/ 2> /dev/null",
"test:jest:cmd": "wait-on tcp:4001 tcp:4123 && jest --forceExit --detectOpenHandles --runInBand",
"test:cucumber:cmd": "wait-on tcp:4001 tcp:4123 && cucumber-js --require-module @babel/register --exit test/",
"test:jest:cmd:debug": "wait-on tcp:4001 tcp:4123 && node --inspect-brk ./node_modules/.bin/jest -i --forceExit --detectOpenHandles --runInBand",

View File

@ -22,21 +22,19 @@ let activityPub = null
export { activityPub }
export default class ActivityPub {
constructor (host, uri) {
this.host = host
this.dataSource = new NitroDataSource(uri)
constructor (activity_pub_endpoint_uri, internal_graphql_uri) {
this.endpoint = activity_pub_endpoint_uri
this.dataSource = new NitroDataSource(internal_graphql_uri)
this.collections = new Collections(this.dataSource)
}
static init (server) {
if (!activityPub) {
dotenv.config()
const url = new URL(process.env.CLIENT_URI)
activityPub = new ActivityPub(url.host || 'localhost:4000', url.origin)
activityPub = new ActivityPub(process.env.CLIENT_URI || 'http://localhost:3000', process.env.GRAPHQL_URI || 'http://localhost:4000')
// integrate into running graphql express server
server.express.set('ap', activityPub)
server.express.set('port', url.port)
server.express.use(router)
console.log('-> ActivityPub middleware added to the graphql express server')
} else {

View File

@ -11,14 +11,14 @@ export function createNoteObject (text, name, id, published) {
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': `https://${activityPub.host}/activitypub/users/${name}/status/${createUuid}`,
'id': `${activityPub.endpoint}/activitypub/users/${name}/status/${createUuid}`,
'type': 'Create',
'actor': `https://${activityPub.host}/activitypub/users/${name}`,
'actor': `${activityPub.endpoint}/activitypub/users/${name}`,
'object': {
'id': `https://${activityPub.host}/activitypub/users/${name}/status/${id}`,
'id': `${activityPub.endpoint}/activitypub/users/${name}/status/${id}`,
'type': 'Note',
'published': published,
'attributedTo': `https://${activityPub.host}/activitypub/users/${name}`,
'attributedTo': `${activityPub.endpoint}/activitypub/users/${name}`,
'content': text,
'to': 'https://www.w3.org/ns/activitystreams#Public'
}
@ -64,8 +64,8 @@ export async function getActorId (name) {
export function sendAcceptActivity (theBody, name, targetDomain, url) {
as.accept()
.id(`https://${activityPub.host}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
.actor(`https://${activityPub.host}/activitypub/users/${name}`)
.id(`${activityPub.endpoint}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
.actor(`${activityPub.endpoint}/activitypub/users/${name}`)
.object(theBody)
.prettyWrite((err, doc) => {
if (!err) {
@ -79,8 +79,8 @@ export function sendAcceptActivity (theBody, name, targetDomain, url) {
export function sendRejectActivity (theBody, name, targetDomain, url) {
as.reject()
.id(`https://${activityPub.host}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
.actor(`https://${activityPub.host}/activitypub/users/${name}`)
.id(`${activityPub.endpoint}/activitypub/users/${name}/status/` + crypto.randomBytes(16).toString('hex'))
.actor(`${activityPub.endpoint}/activitypub/users/${name}`)
.object(theBody)
.prettyWrite((err, doc) => {
if (!err) {

View File

@ -6,34 +6,35 @@ export function createActor (name, pubkey) {
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1'
],
'id': `https://${activityPub.host}/activitypub/users/${name}`,
'id': `${activityPub.endpoint}/activitypub/users/${name}`,
'type': 'Person',
'preferredUsername': `${name}`,
'name': `${name}`,
'following': `https://${activityPub.host}/activitypub/users/${name}/following`,
'followers': `https://${activityPub.host}/activitypub/users/${name}/followers`,
'inbox': `https://${activityPub.host}/activitypub/users/${name}/inbox`,
'outbox': `https://${activityPub.host}/activitypub/users/${name}/outbox`,
'url': `https://${activityPub.host}/activitypub/@${name}`,
'following': `${activityPub.endpoint}/activitypub/users/${name}/following`,
'followers': `${activityPub.endpoint}/activitypub/users/${name}/followers`,
'inbox': `${activityPub.endpoint}/activitypub/users/${name}/inbox`,
'outbox': `${activityPub.endpoint}/activitypub/users/${name}/outbox`,
'url': `${activityPub.endpoint}/activitypub/@${name}`,
'endpoints': {
'sharedInbox': `https://${activityPub.host}/activitypub/inbox`
'sharedInbox': `${activityPub.endpoint}/activitypub/inbox`
},
'publicKey': {
'id': `https://${activityPub.host}/activitypub/users/${name}#main-key`,
'owner': `https://${activityPub.host}/activitypub/users/${name}`,
'id': `${activityPub.endpoint}/activitypub/users/${name}#main-key`,
'owner': `${activityPub.endpoint}/activitypub/users/${name}`,
'publicKeyPem': pubkey
}
}
}
export function createWebFinger (name) {
const { host } = new URL(activityPub.endpoint)
return {
'subject': `acct:${name}@${activityPub.host}`,
'subject': `acct:${name}@${host}`,
'links': [
{
'rel': 'self',
'type': 'application/activity+json',
'href': `https://${activityPub.host}/activitypub/users/${name}`
'href': `${activityPub.endpoint}/activitypub/users/${name}`
}
]
}

View File

@ -5,10 +5,10 @@ const debug = require('debug')('ea:utils:collections')
export function createOrderedCollection (name, collectionName) {
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': `https://${activityPub.host}/activitypub/users/${name}/${collectionName}`,
'id': `${activityPub.endpoint}/activitypub/users/${name}/${collectionName}`,
'summary': `${name}s ${collectionName} collection`,
'type': 'OrderedCollection',
'first': `https://${activityPub.host}/activitypub/users/${name}/${collectionName}?page=true`,
'first': `${activityPub.endpoint}/activitypub/users/${name}/${collectionName}?page=true`,
'totalItems': 0
}
}
@ -16,11 +16,11 @@ export function createOrderedCollection (name, collectionName) {
export function createOrderedCollectionPage (name, collectionName) {
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': `https://${activityPub.host}/activitypub/users/${name}/${collectionName}?page=true`,
'id': `${activityPub.endpoint}/activitypub/users/${name}/${collectionName}?page=true`,
'summary': `${name}s ${collectionName} collection`,
'type': 'OrderedCollectionPage',
'totalItems': 0,
'partOf': `https://${activityPub.host}/activitypub/users/${name}/${collectionName}`,
'partOf': `${activityPub.endpoint}/activitypub/users/${name}/${collectionName}`,
'orderedItems': []
}
}

View File

@ -20,8 +20,8 @@ export function extractIdFromActivityId (uri) {
return splitted[splitted.indexOf('status') + 1]
}
export function constructIdFromName (name, fromDomain = activityPub.host) {
return `http://${fromDomain}/activitypub/users/${name}`
export function constructIdFromName (name, fromDomain = activityPub.endpoint) {
return `${fromDomain}/activitypub/users/${name}`
}
export function extractDomainFromUrl (url) {
@ -76,7 +76,7 @@ export function signAndSend (activity, fromName, targetDomain, url) {
'Host': targetDomain,
'Date': date,
'Signature': createSignature({ privateKey,
keyId: `http://${activityPub.host}/activitypub/users/${fromName}#main-key`,
keyId: `${activityPub.endpoint}/activitypub/users/${fromName}#main-key`,
url,
headers: {
'Host': targetDomain,

View File

@ -25,9 +25,11 @@ export default schema => {
// add permisions middleware at the first position (unless we're seeding)
// NOTE: DO NOT SET THE PERMISSION FLAT YOUR SELF
if (process.env.PERMISSIONS !== 'disabled' && process.env.NODE_ENV !== 'production') {
middleware.unshift(activityPubMiddleware)
middleware.unshift(permissionsMiddleware.generate(schema))
if (process.env.NODE_ENV !== 'production') {
const DISABLED_MIDDLEWARES = process.env.DISABLED_MIDDLEWARES || ''
const disabled = DISABLED_MIDDLEWARES.split(',')
if (!disabled.includes('activityPub')) middleware.unshift(activityPubMiddleware)
if (!disabled.includes('permissions')) middleware.unshift(permissionsMiddleware.generate(schema))
}
return middleware
}

View File

@ -4,7 +4,7 @@ Feature: Webfinger discovery
In order to follow the actor
Background:
Given our own server runs at "http://localhost:4100"
Given our own server runs at "http://localhost:4123"
And we have the following users in our database:
| Slug |
| peter-lustiger |

View File

@ -133,7 +133,6 @@ module.exports = {
'/activitypub': {
// make this configurable (nuxt-dotenv)
target: process.env.GRAPHQL_URI || 'http://localhost:4000',
pathRewrite: { '^/activitypub': '' },
toProxy: true, // cloudflare needs that
headers: {
Accept: 'application/json',