Skip to content

Commit 9f799ca

Browse files
authored
Merge pull request #231 from matrix-org/hs/encryption-pan
Add support for encryption via pantalaimon
2 parents edbdc9a + f14a434 commit 9f799ca

File tree

17 files changed

+6733
-155
lines changed

17 files changed

+6733
-155
lines changed

changelog.d/231.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for bridging encrypted events via [matrix-org/pantalaimon](https://github.com/matrix-org/pantalaimon).

examples/encryption/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*.db
2+
/lib
3+
/*registration.yaml

examples/encryption/package-lock.json

Lines changed: 4546 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/encryption/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "matrix-bridge-encryption-example",
3+
"private": "true",
4+
"version": "1.0.0",
5+
"description": "",
6+
"main": "lib/index.js",
7+
"scripts": {
8+
"start": "node ./lib/index.js",
9+
"build": "tsc",
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"author": "Matrix.org",
13+
"license": "Apache-2.0",
14+
"dependencies": {
15+
"@types/node": "^14",
16+
"@types/request": "^2.48.5",
17+
"matrix-appservice-bridge": "file:../.."
18+
},
19+
"devDependencies": {
20+
"typescript": "^4.0.2"
21+
}
22+
}

examples/encryption/src/index.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright 2020 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
// Usage:
17+
// node index.js -r -u "http://localhost:9000" # remember to add the registration!
18+
// node index.js -p 9000
19+
import { Cli, Bridge, AppServiceRegistration, ClientEncryptionSession, ClientEncryptionStore, Logging} from 'matrix-appservice-bridge';
20+
21+
Logging.configure({
22+
console: "debug",
23+
});
24+
const log = Logging.get("index");
25+
26+
const encMap = new Map<string, ClientEncryptionSession>();
27+
const encryptionStore: ClientEncryptionStore = {
28+
async getStoredSession(userId: string) {
29+
return encMap.get(userId) || null;
30+
},
31+
async setStoredSession(session: ClientEncryptionSession) {
32+
log.info("Set session", session.userId, session.deviceId);
33+
encMap.set(session.userId, session);
34+
}
35+
};
36+
37+
new Cli({
38+
registrationPath: "enc-registration.yaml",
39+
generateRegistration: function (reg, callback) {
40+
reg.setId(AppServiceRegistration.generateToken());
41+
reg.setHomeserverToken(AppServiceRegistration.generateToken());
42+
reg.setAppServiceToken(AppServiceRegistration.generateToken());
43+
reg.setSenderLocalpart("encbot");
44+
reg.addRegexPattern("users", "@enc_.*", true);
45+
callback(reg);
46+
},
47+
run: function (port, config) {
48+
let bridge: Bridge;
49+
bridge = new Bridge({
50+
homeserverUrl: "http://localhost:8008",
51+
domain: "halfyxps",
52+
registration: "enc-registration.yaml",
53+
bridgeEncryption: {
54+
homeserverUrl: "http://localhost:8009",
55+
store: encryptionStore,
56+
},
57+
controller: {
58+
onUserQuery: function (queriedUser) {
59+
return {}; // auto-provision users with no additonal data
60+
},
61+
62+
onEvent: async function (request, context) {
63+
const event = request.getData();
64+
const bot = bridge.getBot();
65+
const intent = bridge.getIntentFromLocalpart(`enc_${context.senders.matrix.localpart}`);
66+
console.log(event, bot.getUserId());
67+
if (event.type === "m.room.member" &&
68+
event.content.membership === "invite" &&
69+
event.state_key === "@encbot:halfyxps") {
70+
console.log("Joining the room!");
71+
try {
72+
await intent.join(event.room_id);
73+
console.log("Joined the room!");
74+
} catch (ex) {
75+
console.log("Err joining room:", ex);
76+
}
77+
return;
78+
}
79+
80+
if (event.type === "m.room.encrypted") {
81+
await intent.sendText(event.room_id, "Not encrypted!");
82+
return;
83+
}
84+
85+
if (event.type !== "m.room.message" || !event.content) {
86+
return;
87+
}
88+
89+
await intent.sendText(event.room_id, event.content.body as string);
90+
}
91+
}
92+
});
93+
log.info("Matrix-side listening on port %s", port);
94+
bridge.run(port, config);
95+
}
96+
}).run();

examples/encryption/tsconfig.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "commonjs",
5+
"noImplicitAny": true,
6+
"removeComments": true,
7+
"preserveConstEnums": true,
8+
"outDir": "./lib",
9+
"sourceMap": true,
10+
"moduleResolution": "Node",
11+
"esModuleInterop": true,
12+
},
13+
"include": [
14+
"src/**/*"
15+
],
16+
"types": [
17+
"node",
18+
],
19+
"exclude": [
20+
"node_modules",
21+
"**/*.spec.ts"
22+
]
23+
}

0 commit comments

Comments
 (0)