Skip to content

Commit

Permalink
Merge pull request #231 from matrix-org/hs/encryption-pan
Browse files Browse the repository at this point in the history
Add support for encryption via pantalaimon
  • Loading branch information
Half-Shot authored Sep 23, 2020
2 parents edbdc9a + f14a434 commit 9f799ca
Show file tree
Hide file tree
Showing 17 changed files with 6,733 additions and 155 deletions.
1 change: 1 addition & 0 deletions changelog.d/231.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for bridging encrypted events via [matrix-org/pantalaimon](https://github.com/matrix-org/pantalaimon).
3 changes: 3 additions & 0 deletions examples/encryption/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*.db
/lib
/*registration.yaml
4,546 changes: 4,546 additions & 0 deletions examples/encryption/package-lock.json

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions examples/encryption/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "matrix-bridge-encryption-example",
"private": "true",
"version": "1.0.0",
"description": "",
"main": "lib/index.js",
"scripts": {
"start": "node ./lib/index.js",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Matrix.org",
"license": "Apache-2.0",
"dependencies": {
"@types/node": "^14",
"@types/request": "^2.48.5",
"matrix-appservice-bridge": "file:../.."
},
"devDependencies": {
"typescript": "^4.0.2"
}
}
96 changes: 96 additions & 0 deletions examples/encryption/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Usage:
// node index.js -r -u "http://localhost:9000" # remember to add the registration!
// node index.js -p 9000
import { Cli, Bridge, AppServiceRegistration, ClientEncryptionSession, ClientEncryptionStore, Logging} from 'matrix-appservice-bridge';

Logging.configure({
console: "debug",
});
const log = Logging.get("index");

const encMap = new Map<string, ClientEncryptionSession>();
const encryptionStore: ClientEncryptionStore = {
async getStoredSession(userId: string) {
return encMap.get(userId) || null;
},
async setStoredSession(session: ClientEncryptionSession) {
log.info("Set session", session.userId, session.deviceId);
encMap.set(session.userId, session);
}
};

new Cli({
registrationPath: "enc-registration.yaml",
generateRegistration: function (reg, callback) {
reg.setId(AppServiceRegistration.generateToken());
reg.setHomeserverToken(AppServiceRegistration.generateToken());
reg.setAppServiceToken(AppServiceRegistration.generateToken());
reg.setSenderLocalpart("encbot");
reg.addRegexPattern("users", "@enc_.*", true);
callback(reg);
},
run: function (port, config) {
let bridge: Bridge;
bridge = new Bridge({
homeserverUrl: "http://localhost:8008",
domain: "halfyxps",
registration: "enc-registration.yaml",
bridgeEncryption: {
homeserverUrl: "http://localhost:8009",
store: encryptionStore,
},
controller: {
onUserQuery: function (queriedUser) {
return {}; // auto-provision users with no additonal data
},

onEvent: async function (request, context) {
const event = request.getData();
const bot = bridge.getBot();
const intent = bridge.getIntentFromLocalpart(`enc_${context.senders.matrix.localpart}`);
console.log(event, bot.getUserId());
if (event.type === "m.room.member" &&
event.content.membership === "invite" &&
event.state_key === "@encbot:halfyxps") {
console.log("Joining the room!");
try {
await intent.join(event.room_id);
console.log("Joined the room!");
} catch (ex) {
console.log("Err joining room:", ex);
}
return;
}

if (event.type === "m.room.encrypted") {
await intent.sendText(event.room_id, "Not encrypted!");
return;
}

if (event.type !== "m.room.message" || !event.content) {
return;
}

await intent.sendText(event.room_id, event.content.body as string);
}
}
});
log.info("Matrix-side listening on port %s", port);
bridge.run(port, config);
}
}).run();
23 changes: 23 additions & 0 deletions examples/encryption/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "./lib",
"sourceMap": true,
"moduleResolution": "Node",
"esModuleInterop": true,
},
"include": [
"src/**/*"
],
"types": [
"node",
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
Loading

0 comments on commit 9f799ca

Please sign in to comment.