Compare commits
220 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0dc9c27651 | |||
| f6f54c35a3 | |||
| 786590eadc | |||
| c9174188ba | |||
| 64fb79e0be | |||
| 088ff5d0aa | |||
| 99e58b0297 | |||
| f8a1e98de1 | |||
| 5487cf2070 | |||
| e998be3a9b | |||
| d70767ef3a | |||
| fbb355c5c9 | |||
| 20bc8071fc | |||
| 0438c6c51c | |||
| b39abba41e | |||
| fe2bdd027e | |||
| 320ab050fe | |||
| 1816d7aa4c | |||
| 41b763f331 | |||
| 36db57615d | |||
| 8f7ed1dc15 | |||
| 83a8a0cf21 | |||
| 9a0de545b8 | |||
| 049b769f68 | |||
| 1fc2ab7f7d | |||
| f2c5b2bd49 | |||
| f31f88ce31 | |||
| d35f5152a9 | |||
| d8e19db8bf | |||
| 376e56d5fd | |||
| 72f856eca4 | |||
| dbab75eae7 | |||
| 7457da80e9 | |||
| 443e01d38c | |||
| 880438c5c1 | |||
| 5423d3ca61 | |||
| 3f448df1d3 | |||
| a626b44bbe | |||
| 4c6e2fca91 | |||
| ab4d9ae4bc | |||
| fb3d075da2 | |||
| 657e48de7e | |||
| 1b63cb1406 | |||
| 01f0dd4498 | |||
| f59650d8a6 | |||
| 0e444fd925 | |||
| 9b8b57d186 | |||
| ca6a52727c | |||
| 3dfde6bf6a | |||
| 780394b051 | |||
| 6942e3467b | |||
| 70eb8a7300 | |||
| 15a8c23cd0 | |||
| 49f0e368d0 | |||
| 590608a215 | |||
| 202fec2a35 | |||
| 817bfa35e5 | |||
| 110c9800f0 | |||
| 1a6dc973bb | |||
| 44dd674dab | |||
| 4a3ce640d7 | |||
| df6ebf83b4 | |||
| e5dcc5a407 | |||
| 1ee8abb0e6 | |||
| dd40435425 | |||
| 74cb57c761 | |||
| 86123f28f7 | |||
| f97ab32e7c | |||
| b0e2544e4b | |||
| 0d59963b53 | |||
| c669aafedb | |||
| 2a2a40af7a | |||
| 1df12d1677 | |||
| 14a2d7e860 | |||
| 3f2c05664f | |||
| 9b05d1d68e | |||
| 772d668389 | |||
| 03360a663e | |||
| e1e9f690c9 | |||
| 934e81d16c | |||
| 88bb31d3e6 | |||
| 33f5894547 | |||
| fa46d2bef8 | |||
| 65f8556ee9 | |||
| ebe174fbef | |||
| eaaeedbb37 | |||
| bf45c176a7 | |||
| 87a8e4c216 | |||
| 30cc7d4f0f | |||
| 4a47867e49 | |||
| 5fced642fa | |||
| 9fb559307b | |||
| 96c8c2b9c3 | |||
| 145cdf6985 | |||
| 5910fd95ff | |||
| c0dbf2df7f | |||
| cfaadef669 | |||
| eeffe208ec | |||
| 358f13500b | |||
| 016f16954a | |||
| 9dc61faa6f | |||
| 2173ab3437 | |||
| c1543545d2 | |||
| 5da936d96a | |||
| 0dead73837 | |||
| 66a6dd1f0c | |||
| 8a8109272a | |||
| 7ea30c449e | |||
| a6e4096773 | |||
| c1e2d646b6 | |||
| 710ac6847d | |||
| f0267eae36 | |||
| 1632ee3537 | |||
| a16cdb948c | |||
| c4ae27dae6 | |||
| 053bc49738 | |||
| 3a1de9fbdc | |||
| efcaadd0b4 | |||
| 0170cb066d | |||
| 6bba5ca25a | |||
| edcdeb31ea | |||
| 1286007b2e | |||
| 9faab093f7 | |||
| 64bf145e4b | |||
| 733008cfc4 | |||
| bab4582139 | |||
| fddf2843b4 | |||
| f8d83f8273 | |||
| cfeaf188ed | |||
| 58ad1ecbfe | |||
| 463538178d | |||
| 14907065d7 | |||
| ce2059a4b9 | |||
| 2bfc157e64 | |||
| fda7a2cf13 | |||
| e69de8c26f | |||
| f404c80714 | |||
| 92ca2386ea | |||
| 59b25d6837 | |||
| a6f7936311 | |||
| e2b680c223 | |||
| bdaf2e3b4f | |||
| 2190022e64 | |||
| e000e2b9fd | |||
| 7392b4de17 | |||
| 79b0a5fada | |||
| aee9442e52 | |||
| d5000820fd | |||
| 569d5d1fce | |||
| 9d91d197e4 | |||
| 5b767ae948 | |||
| 6ea8003df2 | |||
| e0c90ec9e3 | |||
| 7ad5021147 | |||
| fd73c3fb3a | |||
| e3dbf7cc41 | |||
| 18749c580e | |||
| 5b8b0a8aa3 | |||
| b1924d4db6 | |||
| 1b877118ef | |||
| 682a5daf1c | |||
| fcbfaac1fd | |||
| 3787b6f1c7 | |||
| 6e08835496 | |||
| 191695da5a | |||
| 2215087f96 | |||
| 32234ee7fc | |||
| aa37f697bf | |||
| 49448fafaa | |||
| 057303d57c | |||
| ccc85d98e2 | |||
| 7fb807919c | |||
| bd8f8ef28d | |||
| 3901a381cc | |||
| 12f6e51ef6 | |||
| aa8454e30a | |||
| 6b70230e0d | |||
| 5e0ba9971c | |||
| fa577c9475 | |||
| 11a958b8ca | |||
| 6952db6762 | |||
| 51898cffe8 | |||
| d8337d703d | |||
| adac0c353c | |||
| 04fca16420 | |||
| ca89b6e7a8 | |||
| ac1173c628 | |||
| 0a0ae111f6 | |||
| 71a6e015f4 | |||
| e8bbb8a1cc | |||
| 04764998cb | |||
| 5262d716e4 | |||
| 7addacba38 | |||
| 8f8c9c8ec0 | |||
| 3a9832a8c6 | |||
| 4a40c10d4c | |||
| 58f8ca7d66 | |||
| 4d950fec66 | |||
| b4f68f4fc6 | |||
| ac742aad70 | |||
| 53d225a1d1 | |||
| 549b0f9313 | |||
| 2ce106382a | |||
| b44f43e5db | |||
| 2321b9a04e | |||
| 3bd518cf7f | |||
| c57109c2f3 | |||
| 522640edd9 | |||
| 5fc0629201 | |||
| 26edd7431a | |||
| fd58957b06 | |||
| 4116d89d5f | |||
| cc192efe45 | |||
| feef1a35b9 | |||
| 55a2f46604 | |||
| ed8b303400 | |||
| 4acd06eaba | |||
| 91df096698 | |||
| e8fd0498a7 | |||
| 7973b99f50 |
@@ -1,15 +1,21 @@
|
||||
{
|
||||
"presets": ["es2015", "es2016"],
|
||||
"plugins": [
|
||||
"transform-class-properties",
|
||||
|
||||
// this transforms async functions into generator functions, which
|
||||
// are then made to use the regenerator module by babel's
|
||||
// transform-regnerator plugin (which is enabled by es2015).
|
||||
"transform-async-to-bluebird",
|
||||
|
||||
// This makes sure that the regenerator runtime is available to
|
||||
// the transpiled code.
|
||||
"transform-runtime",
|
||||
"presets": [
|
||||
["@babel/preset-env", {
|
||||
"targets": {
|
||||
"browsers": [
|
||||
"last 2 versions"
|
||||
],
|
||||
"node": 12
|
||||
},
|
||||
"modules": "commonjs"
|
||||
}],
|
||||
"@babel/preset-typescript"
|
||||
],
|
||||
"plugins": [
|
||||
"@babel/plugin-proposal-numeric-separator",
|
||||
"@babel/plugin-proposal-class-properties",
|
||||
"@babel/plugin-proposal-object-rest-spread",
|
||||
"@babel/plugin-syntax-dynamic-import",
|
||||
"@babel/plugin-transform-runtime"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ steps:
|
||||
- docker#v3.0.1:
|
||||
image: "node:10"
|
||||
|
||||
- label: ":karma: Tests"
|
||||
- label: ":jest: Tests"
|
||||
command:
|
||||
- "yarn install"
|
||||
- "yarn test"
|
||||
|
||||
@@ -12,10 +12,12 @@ module.exports = {
|
||||
// babel's transform-runtime converts references to ES6 globals such as
|
||||
// Promise and Map to core-js polyfills, so we can use ES6 globals.
|
||||
es6: true,
|
||||
jest: true,
|
||||
},
|
||||
extends: ["eslint:recommended", "google"],
|
||||
plugins: [
|
||||
"babel",
|
||||
"jest",
|
||||
],
|
||||
rules: {
|
||||
// rules we've always adhered to or now do
|
||||
|
||||
+103
@@ -1,3 +1,106 @@
|
||||
Changes in [3.0.0](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v3.0.0) (2020-01-13)
|
||||
================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v3.0.0-rc.1...v3.0.0)
|
||||
|
||||
* No changes from rc.1
|
||||
|
||||
Changes in [3.0.0-rc.1](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v3.0.0-rc.1) (2020-01-06)
|
||||
==========================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v2.4.6...v3.0.0-rc.1)
|
||||
|
||||
BREAKING CHANGES
|
||||
================
|
||||
* matrix-js-sdk no longer uses bluebird promises, so promises returned
|
||||
by the js-sdk no longer support the done() method. Code that calls
|
||||
done() on promises returned by the js-sdk will break and will need
|
||||
to be updated to remove the done() call.
|
||||
|
||||
All Changes
|
||||
===========
|
||||
* Make displayName disambiguation more fuzzy especially against RTL/LTR
|
||||
content
|
||||
[\#1141](https://github.com/matrix-org/matrix-js-sdk/pull/1141)
|
||||
* stop trying to resend event if we get M_TOO_LARGE
|
||||
[\#1129](https://github.com/matrix-org/matrix-js-sdk/pull/1129)
|
||||
* Fix creating a key backup with cross signing diabled
|
||||
[\#1139](https://github.com/matrix-org/matrix-js-sdk/pull/1139)
|
||||
* Use checkDeviceTrust with key backup
|
||||
[\#1138](https://github.com/matrix-org/matrix-js-sdk/pull/1138)
|
||||
* Add support for passthrough SSSS secrets
|
||||
[\#1128](https://github.com/matrix-org/matrix-js-sdk/pull/1128)
|
||||
* Add support for key backups using secret storage
|
||||
[\#1118](https://github.com/matrix-org/matrix-js-sdk/pull/1118)
|
||||
* Remove unused user verification event
|
||||
[\#1117](https://github.com/matrix-org/matrix-js-sdk/pull/1117)
|
||||
* Fix check for private keys
|
||||
[\#1116](https://github.com/matrix-org/matrix-js-sdk/pull/1116)
|
||||
* Restore watching mode for `start:watch`
|
||||
[\#1115](https://github.com/matrix-org/matrix-js-sdk/pull/1115)
|
||||
* Add secret storage bootstrap flow
|
||||
[\#1079](https://github.com/matrix-org/matrix-js-sdk/pull/1079)
|
||||
* Part 1 of many: Upgrade to babel@7 and TypeScript
|
||||
[\#1112](https://github.com/matrix-org/matrix-js-sdk/pull/1112)
|
||||
* Remove Bluebird: phase 2.5
|
||||
[\#1100](https://github.com/matrix-org/matrix-js-sdk/pull/1100)
|
||||
* Remove Bluebird: phase 3
|
||||
[\#1088](https://github.com/matrix-org/matrix-js-sdk/pull/1088)
|
||||
* ignore m.key.verification.done messages when we don't expect any more
|
||||
messages
|
||||
[\#1104](https://github.com/matrix-org/matrix-js-sdk/pull/1104)
|
||||
* dont cancel on remote echo of own .request event
|
||||
[\#1111](https://github.com/matrix-org/matrix-js-sdk/pull/1111)
|
||||
* Refactor verification request code
|
||||
[\#1109](https://github.com/matrix-org/matrix-js-sdk/pull/1109)
|
||||
* Fix device list's cross-signing storage path
|
||||
[\#1105](https://github.com/matrix-org/matrix-js-sdk/pull/1105)
|
||||
* yarn upgrade
|
||||
[\#1103](https://github.com/matrix-org/matrix-js-sdk/pull/1103)
|
||||
|
||||
Changes in [2.4.6](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v2.4.6) (2019-12-09)
|
||||
================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v2.4.6-rc.1...v2.4.6)
|
||||
|
||||
* No changes since rc.1
|
||||
|
||||
Changes in [2.4.6-rc.1](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v2.4.6-rc.1) (2019-12-04)
|
||||
==========================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v2.4.5...v2.4.6-rc.1)
|
||||
|
||||
* Update alias handling
|
||||
[\#1102](https://github.com/matrix-org/matrix-js-sdk/pull/1102)
|
||||
* increase timeout on flush to fix failing unit test
|
||||
[\#1096](https://github.com/matrix-org/matrix-js-sdk/pull/1096)
|
||||
* Disable broken cross-signing test
|
||||
[\#1095](https://github.com/matrix-org/matrix-js-sdk/pull/1095)
|
||||
* Fix a couple SAS tests
|
||||
[\#1094](https://github.com/matrix-org/matrix-js-sdk/pull/1094)
|
||||
* Fix Olm unwedging test
|
||||
[\#1093](https://github.com/matrix-org/matrix-js-sdk/pull/1093)
|
||||
* Fix empty string handling in push notifications
|
||||
[\#1089](https://github.com/matrix-org/matrix-js-sdk/pull/1089)
|
||||
* expand e2ee logging to better debug UISIs
|
||||
[\#1090](https://github.com/matrix-org/matrix-js-sdk/pull/1090)
|
||||
* Remove Bluebird: phase 2
|
||||
[\#1087](https://github.com/matrix-org/matrix-js-sdk/pull/1087)
|
||||
* Relax identity server discovery checks to FAIL_PROMPT
|
||||
[\#1062](https://github.com/matrix-org/matrix-js-sdk/pull/1062)
|
||||
* Fix incorrect return value of MatrixClient.prototype.uploadKeys
|
||||
[\#1061](https://github.com/matrix-org/matrix-js-sdk/pull/1061)
|
||||
* Fix calls in e2e rooms
|
||||
[\#1086](https://github.com/matrix-org/matrix-js-sdk/pull/1086)
|
||||
* Monitor verification request over DM as well
|
||||
[\#1085](https://github.com/matrix-org/matrix-js-sdk/pull/1085)
|
||||
* Remove 'check' npm script
|
||||
[\#1084](https://github.com/matrix-org/matrix-js-sdk/pull/1084)
|
||||
* Always process call events in batches
|
||||
[\#1083](https://github.com/matrix-org/matrix-js-sdk/pull/1083)
|
||||
* Fix ringing chirp on loading
|
||||
[\#1082](https://github.com/matrix-org/matrix-js-sdk/pull/1082)
|
||||
* Remove *most* bluebird specific things
|
||||
[\#1081](https://github.com/matrix-org/matrix-js-sdk/pull/1081)
|
||||
* Switch to Jest
|
||||
[\#1080](https://github.com/matrix-org/matrix-js-sdk/pull/1080)
|
||||
|
||||
Changes in [2.4.5](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v2.4.5) (2019-11-27)
|
||||
================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v2.4.4...v2.4.5)
|
||||
|
||||
@@ -161,7 +161,7 @@ which will be fulfilled in the future.
|
||||
The typical usage is something like:
|
||||
|
||||
```javascript
|
||||
matrixClient.someMethod(arg1, arg2).done(function(result) {
|
||||
matrixClient.someMethod(arg1, arg2).then(function(result) {
|
||||
...
|
||||
});
|
||||
```
|
||||
@@ -206,7 +206,7 @@ core functionality of the SDK. These examples assume the SDK is setup like this:
|
||||
```javascript
|
||||
matrixClient.on("RoomMember.membership", function(event, member) {
|
||||
if (member.membership === "invite" && member.userId === myUserId) {
|
||||
matrixClient.joinRoom(member.roomId).done(function() {
|
||||
matrixClient.joinRoom(member.roomId).then(function() {
|
||||
console.log("Auto-joined %s", member.roomId);
|
||||
});
|
||||
}
|
||||
@@ -297,7 +297,7 @@ End-to-end encryption support
|
||||
=============================
|
||||
|
||||
The SDK supports end-to-end encryption via the Olm and Megolm protocols, using
|
||||
[libolm](https://gitlab.matrix.org/matrix-org/olm). It is left up to the
|
||||
[libolm](https://gitlab.matrix.org/matrix-org/olm). It is left up to the
|
||||
application to make libolm available, via the ``Olm`` global.
|
||||
|
||||
It is also necessry to call ``matrixClient.initCrypto()`` after creating a new
|
||||
@@ -319,7 +319,7 @@ To provide the Olm library in a browser application:
|
||||
|
||||
* download the transpiled libolm (from https://packages.matrix.org/npm/olm/).
|
||||
* load ``olm.js`` as a ``<script>`` *before* ``browser-matrix.js``.
|
||||
|
||||
|
||||
To provide the Olm library in a node.js application:
|
||||
|
||||
* ``yarn add https://packages.matrix.org/npm/olm/olm-3.1.4.tgz``
|
||||
|
||||
@@ -56,7 +56,7 @@ rl.on('line', function(line) {
|
||||
}
|
||||
}
|
||||
if (notSentEvent) {
|
||||
matrixClient.resendEvent(notSentEvent, viewingRoom).done(function() {
|
||||
matrixClient.resendEvent(notSentEvent, viewingRoom).then(function() {
|
||||
printMessages();
|
||||
rl.prompt();
|
||||
}, function(err) {
|
||||
@@ -70,7 +70,7 @@ rl.on('line', function(line) {
|
||||
}
|
||||
else if (line.indexOf("/more ") === 0) {
|
||||
var amount = parseInt(line.split(" ")[1]) || 20;
|
||||
matrixClient.scrollback(viewingRoom, amount).done(function(room) {
|
||||
matrixClient.scrollback(viewingRoom, amount).then(function(room) {
|
||||
printMessages();
|
||||
rl.prompt();
|
||||
}, function(err) {
|
||||
@@ -79,7 +79,7 @@ rl.on('line', function(line) {
|
||||
}
|
||||
else if (line.indexOf("/invite ") === 0) {
|
||||
var userId = line.split(" ")[1].trim();
|
||||
matrixClient.invite(viewingRoom.roomId, userId).done(function() {
|
||||
matrixClient.invite(viewingRoom.roomId, userId).then(function() {
|
||||
printMessages();
|
||||
rl.prompt();
|
||||
}, function(err) {
|
||||
@@ -92,7 +92,7 @@ rl.on('line', function(line) {
|
||||
matrixClient.uploadContent({
|
||||
stream: stream,
|
||||
name: filename
|
||||
}).done(function(url) {
|
||||
}).then(function(url) {
|
||||
var content = {
|
||||
msgtype: "m.file",
|
||||
body: filename,
|
||||
@@ -116,7 +116,7 @@ rl.on('line', function(line) {
|
||||
viewingRoom = roomList[roomIndex];
|
||||
if (viewingRoom.getMember(myUserId).membership === "invite") {
|
||||
// join the room first
|
||||
matrixClient.joinRoom(viewingRoom.roomId).done(function(room) {
|
||||
matrixClient.joinRoom(viewingRoom.roomId).then(function(room) {
|
||||
setRoomList();
|
||||
viewingRoom = room;
|
||||
printMessages();
|
||||
@@ -128,7 +128,7 @@ rl.on('line', function(line) {
|
||||
else {
|
||||
printMessages();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
rl.prompt();
|
||||
});
|
||||
@@ -281,8 +281,8 @@ function printMemberList(room) {
|
||||
member.membership + new Array(10 - member.membership.length).join(" ")
|
||||
);
|
||||
print(
|
||||
"%s"+fmt(" :: ")+"%s"+fmt(" (")+"%s"+fmt(")"),
|
||||
membershipWithPadding, member.name,
|
||||
"%s"+fmt(" :: ")+"%s"+fmt(" (")+"%s"+fmt(")"),
|
||||
membershipWithPadding, member.name,
|
||||
(member.userId === myUserId ? "Me" : member.userId),
|
||||
fmt
|
||||
);
|
||||
@@ -295,7 +295,7 @@ function printRoomInfo(room) {
|
||||
var sendHeader = " Sender ";
|
||||
// pad content to 100
|
||||
var restCount = (
|
||||
100 - "Content".length - " | ".length - " | ".length -
|
||||
100 - "Content".length - " | ".length - " | ".length -
|
||||
eTypeHeader.length - sendHeader.length
|
||||
);
|
||||
var padSide = new Array(Math.floor(restCount/2)).join(" ");
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"tags": {
|
||||
"allowUnknownTags": true
|
||||
},
|
||||
"plugins": [
|
||||
"node_modules/better-docs/category",
|
||||
"node_modules/better-docs/typescript"
|
||||
],
|
||||
"source": {
|
||||
"include": [
|
||||
"src"
|
||||
],
|
||||
"includePattern": ".(ts|js)$"
|
||||
},
|
||||
"opts": {
|
||||
"encoding": "utf8",
|
||||
"destination": ".jsdoc",
|
||||
"readme": "README.md",
|
||||
"recurse": true,
|
||||
"verbose": true,
|
||||
"template": "node_modules/better-docs"
|
||||
}
|
||||
}
|
||||
+24
-23
@@ -1,20 +1,17 @@
|
||||
{
|
||||
"name": "matrix-js-sdk",
|
||||
"version": "2.4.5",
|
||||
"version": "3.0.0",
|
||||
"description": "Matrix Client-Server SDK for Javascript",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test:build": "babel -s -d specbuild spec",
|
||||
"test:run": "istanbul cover --report text --report cobertura --config .istanbul.yml -i \"lib/**/*.js\" node_modules/mocha/bin/_mocha -- --recursive specbuild --colors",
|
||||
"test:watch": "mocha --watch --compilers js:babel-core/register --recursive spec --colors",
|
||||
"test": "yarn test:build && yarn test:run",
|
||||
"check": "yarn test:build && _mocha --recursive specbuild --colors",
|
||||
"gendoc": "babel --no-babelrc --plugins transform-class-properties -d .jsdocbuild src && jsdoc -r .jsdocbuild -P package.json -R README.md -d .jsdoc",
|
||||
"test:watch": "jest spec/ --coverage --testEnvironment node --watch",
|
||||
"test": "jest spec/ --coverage --testEnvironment node",
|
||||
"gendoc": "jsdoc -c jsdoc.json -P package.json",
|
||||
"start": "yarn start:init && yarn start:watch",
|
||||
"start:watch": "babel -s -w --skip-initial-build -d lib src",
|
||||
"start:init": "babel -s -d lib src",
|
||||
"start:watch": "babel src -s -w --skip-initial-build -d lib --verbose --extensions \".ts,.js\"",
|
||||
"start:init": "babel src -s -d lib --verbose --extensions \".ts,.js\"",
|
||||
"clean": "rimraf lib dist",
|
||||
"build": "babel -s -d lib src && rimraf dist && mkdir dist && browserify -d browser-index.js | exorcist dist/browser-matrix.js.map > dist/browser-matrix.js && terser -c -m -o dist/browser-matrix.min.js --source-map \"content='dist/browser-matrix.js.map'\" dist/browser-matrix.js",
|
||||
"build": "babel src -s -d lib --verbose --extensions \".ts,.js\" && rimraf dist && mkdir dist && browserify -d browser-index.js | exorcist dist/browser-matrix.js.map > dist/browser-matrix.js && terser -c -m -o dist/browser-matrix.min.js --source-map \"content='dist/browser-matrix.js.map'\" dist/browser-matrix.js",
|
||||
"dist": "yarn build",
|
||||
"watch": "watchify -d browser-index.js -o 'exorcist dist/browser-matrix.js.map > dist/browser-matrix.js' -v",
|
||||
"lint": "eslint --max-warnings 93 src spec",
|
||||
@@ -52,8 +49,6 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"another-json": "^0.2.0",
|
||||
"babel-runtime": "^6.26.0",
|
||||
"bluebird": "3.5.5",
|
||||
"browser-request": "^0.3.3",
|
||||
"bs58": "^4.0.1",
|
||||
"content-type": "^1.0.2",
|
||||
@@ -63,30 +58,36 @@
|
||||
"unhomoglyph": "^1.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.18.0",
|
||||
"babel-eslint": "^10.0.1",
|
||||
"babel-plugin-transform-async-to-bluebird": "^1.1.1",
|
||||
"babel-plugin-transform-class-properties": "^6.24.1",
|
||||
"babel-plugin-transform-runtime": "^6.23.0",
|
||||
"babel-preset-es2015": "^6.18.0",
|
||||
"babel-preset-es2016": "^6.24.1",
|
||||
"@babel/cli": "^7.7.5",
|
||||
"@babel/core": "^7.7.5",
|
||||
"@babel/plugin-proposal-class-properties": "^7.7.4",
|
||||
"@babel/plugin-proposal-numeric-separator": "^7.7.4",
|
||||
"@babel/plugin-proposal-object-rest-spread": "^7.7.4",
|
||||
"@babel/plugin-syntax-dynamic-import": "^7.7.4",
|
||||
"@babel/plugin-transform-runtime": "^7.7.6",
|
||||
"@babel/preset-env": "^7.7.6",
|
||||
"@babel/preset-typescript": "^7.7.4",
|
||||
"@babel/register": "^7.7.4",
|
||||
"@babel/runtime": "^7.7.6",
|
||||
"babel-eslint": "^10.0.3",
|
||||
"babel-jest": "^24.9.0",
|
||||
"better-docs": "^1.4.7",
|
||||
"browserify": "^16.2.3",
|
||||
"browserify-shim": "^3.8.13",
|
||||
"eslint": "^5.12.0",
|
||||
"eslint-config-google": "^0.7.1",
|
||||
"eslint-plugin-babel": "^5.3.0",
|
||||
"eslint-plugin-jest": "^23.0.4",
|
||||
"exorcist": "^1.0.1",
|
||||
"expect": "^1.20.2",
|
||||
"istanbul": "^0.4.5",
|
||||
"jest": "^24.9.0",
|
||||
"jsdoc": "^3.5.5",
|
||||
"lolex": "^1.5.2",
|
||||
"matrix-mock-request": "^1.2.3",
|
||||
"mocha": "^6.2.1",
|
||||
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz",
|
||||
"rimraf": "^3.0.0",
|
||||
"source-map-support": "^0.5.13",
|
||||
"sourceify": "^1.0.0",
|
||||
"terser": "^4.3.8",
|
||||
"typescript": "^3.7.3",
|
||||
"watchify": "^3.11.1"
|
||||
},
|
||||
"browserify": {
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
mocha: true,
|
||||
},
|
||||
}
|
||||
+2
-4
@@ -24,10 +24,8 @@ import './olm-loader';
|
||||
import sdk from '..';
|
||||
import testUtils from './test-utils';
|
||||
import MockHttpBackend from 'matrix-mock-request';
|
||||
import expect from 'expect';
|
||||
import Promise from 'bluebird';
|
||||
import LocalStorageCryptoStore from '../lib/crypto/store/localStorage-crypto-store';
|
||||
import logger from '../src/logger';
|
||||
import logger from '../lib/logger';
|
||||
|
||||
/**
|
||||
* Wrapper for a MockStorageApi, MockHttpBackend and MatrixClient
|
||||
@@ -159,7 +157,7 @@ TestClient.prototype.awaitOneTimeKeyUpload = function() {
|
||||
.respond(200, (path, content) => {
|
||||
expect(content.device_keys).toBe(undefined);
|
||||
expect(content.one_time_keys).toBeTruthy();
|
||||
expect(content.one_time_keys).toNotEqual({});
|
||||
expect(content.one_time_keys).not.toEqual({});
|
||||
logger.log('%s: received %i one-time keys', this,
|
||||
Object.keys(content.one_time_keys).length);
|
||||
this.oneTimeKeys = content.one_time_keys;
|
||||
|
||||
@@ -15,12 +15,9 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import expect from 'expect';
|
||||
import Promise from 'bluebird';
|
||||
|
||||
import TestClient from '../TestClient';
|
||||
import testUtils from '../test-utils';
|
||||
import logger from '../../src/logger';
|
||||
import logger from '../../lib/logger';
|
||||
|
||||
const ROOM_ID = "!room:id";
|
||||
|
||||
@@ -88,8 +85,6 @@ describe("DeviceList management:", function() {
|
||||
}
|
||||
|
||||
beforeEach(async function() {
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
|
||||
// we create our own sessionStoreBackend so that we can use it for
|
||||
// another TestClient.
|
||||
sessionStoreBackend = new testUtils.MockStorageApi();
|
||||
|
||||
@@ -30,13 +30,11 @@ import 'source-map-support/register';
|
||||
// load olm before the sdk if possible
|
||||
import '../olm-loader';
|
||||
|
||||
import expect from 'expect';
|
||||
const sdk = require("../..");
|
||||
import Promise from 'bluebird';
|
||||
const utils = require("../../lib/utils");
|
||||
const testUtils = require("../test-utils");
|
||||
const TestClient = require('../TestClient').default;
|
||||
import logger from '../../src/logger';
|
||||
import logger from '../../lib/logger';
|
||||
|
||||
let aliTestClient;
|
||||
const roomId = "!room:localhost";
|
||||
@@ -56,7 +54,7 @@ function bobUploadsDeviceKeys() {
|
||||
bobTestClient.client.uploadKeys(),
|
||||
bobTestClient.httpBackend.flush(),
|
||||
]).then(() => {
|
||||
expect(Object.keys(bobTestClient.deviceKeys).length).toNotEqual(0);
|
||||
expect(Object.keys(bobTestClient.deviceKeys).length).not.toEqual(0);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -204,7 +202,7 @@ function aliSendsFirstMessage() {
|
||||
expectAliQueryKeys()
|
||||
.then(expectAliClaimKeys)
|
||||
.then(expectAliSendMessageRequest),
|
||||
]).spread(function(_, ciphertext) {
|
||||
]).then(function([_, ciphertext]) {
|
||||
return ciphertext;
|
||||
});
|
||||
}
|
||||
@@ -219,7 +217,7 @@ function aliSendsMessage() {
|
||||
return Promise.all([
|
||||
sendMessage(aliTestClient.client),
|
||||
expectAliSendMessageRequest(),
|
||||
]).spread(function(_, ciphertext) {
|
||||
]).then(function([_, ciphertext]) {
|
||||
return ciphertext;
|
||||
});
|
||||
}
|
||||
@@ -235,7 +233,7 @@ function bobSendsReplyMessage() {
|
||||
sendMessage(bobTestClient.client),
|
||||
expectBobQueryKeys()
|
||||
.then(expectBobSendMessageRequest),
|
||||
]).spread(function(_, ciphertext) {
|
||||
]).then(function([_, ciphertext]) {
|
||||
return ciphertext;
|
||||
});
|
||||
}
|
||||
@@ -280,16 +278,17 @@ function sendMessage(client) {
|
||||
|
||||
function expectSendMessageRequest(httpBackend) {
|
||||
const path = "/send/m.room.encrypted/";
|
||||
const deferred = Promise.defer();
|
||||
httpBackend.when("PUT", path).respond(200, function(path, content) {
|
||||
deferred.resolve(content);
|
||||
return {
|
||||
event_id: "asdfgh",
|
||||
};
|
||||
const prom = new Promise((resolve) => {
|
||||
httpBackend.when("PUT", path).respond(200, function(path, content) {
|
||||
resolve(content);
|
||||
return {
|
||||
event_id: "asdfgh",
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
// it can take a while to process the key query
|
||||
return httpBackend.flush(path, 1).then(() => deferred.promise);
|
||||
return httpBackend.flush(path, 1).then(() => prom);
|
||||
}
|
||||
|
||||
function aliRecvMessage() {
|
||||
@@ -406,8 +405,6 @@ describe("MatrixClient crypto", function() {
|
||||
}
|
||||
|
||||
beforeEach(async function() {
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
|
||||
aliTestClient = new TestClient(aliUserId, aliDeviceId, aliAccessToken);
|
||||
await aliTestClient.client.initCrypto();
|
||||
|
||||
@@ -430,15 +427,14 @@ describe("MatrixClient crypto", function() {
|
||||
.then(bobUploadsDeviceKeys);
|
||||
});
|
||||
|
||||
it("Ali downloads Bobs device keys", function(done) {
|
||||
Promise.resolve()
|
||||
it("Ali downloads Bobs device keys", function() {
|
||||
return Promise.resolve()
|
||||
.then(bobUploadsDeviceKeys)
|
||||
.then(aliDownloadsKeys)
|
||||
.nodeify(done);
|
||||
.then(aliDownloadsKeys);
|
||||
});
|
||||
|
||||
it("Ali gets keys with an invalid signature", function(done) {
|
||||
Promise.resolve()
|
||||
it("Ali gets keys with an invalid signature", function() {
|
||||
return Promise.resolve()
|
||||
.then(bobUploadsDeviceKeys)
|
||||
.then(function() {
|
||||
// tamper bob's keys
|
||||
@@ -455,11 +451,10 @@ describe("MatrixClient crypto", function() {
|
||||
}).then((devices) => {
|
||||
// should get an empty list
|
||||
expect(devices).toEqual([]);
|
||||
})
|
||||
.nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
it("Ali gets keys with an incorrect userId", function(done) {
|
||||
it("Ali gets keys with an incorrect userId", function() {
|
||||
const eveUserId = "@eve:localhost";
|
||||
|
||||
const bobDeviceKeys = {
|
||||
@@ -488,7 +483,7 @@ describe("MatrixClient crypto", function() {
|
||||
return {device_keys: result};
|
||||
});
|
||||
|
||||
Promise.all([
|
||||
return Promise.all([
|
||||
aliTestClient.client.downloadKeys([bobUserId, eveUserId]),
|
||||
aliTestClient.httpBackend.flush("/keys/query", 1),
|
||||
]).then(function() {
|
||||
@@ -496,14 +491,14 @@ describe("MatrixClient crypto", function() {
|
||||
aliTestClient.client.getStoredDevicesForUser(bobUserId),
|
||||
aliTestClient.client.getStoredDevicesForUser(eveUserId),
|
||||
]);
|
||||
}).spread((bobDevices, eveDevices) => {
|
||||
}).then(([bobDevices, eveDevices]) => {
|
||||
// should get an empty list
|
||||
expect(bobDevices).toEqual([]);
|
||||
expect(eveDevices).toEqual([]);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
it("Ali gets keys with an incorrect deviceId", function(done) {
|
||||
it("Ali gets keys with an incorrect deviceId", function() {
|
||||
const bobDeviceKeys = {
|
||||
algorithms: ['m.olm.v1.curve25519-aes-sha2', 'm.megolm.v1.aes-sha2'],
|
||||
device_id: 'bad_device',
|
||||
@@ -530,7 +525,7 @@ describe("MatrixClient crypto", function() {
|
||||
return {device_keys: result};
|
||||
});
|
||||
|
||||
Promise.all([
|
||||
return Promise.all([
|
||||
aliTestClient.client.downloadKeys([bobUserId]),
|
||||
aliTestClient.httpBackend.flush("/keys/query", 1),
|
||||
]).then(function() {
|
||||
@@ -538,7 +533,7 @@ describe("MatrixClient crypto", function() {
|
||||
}).then((devices) => {
|
||||
// should get an empty list
|
||||
expect(devices).toEqual([]);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -548,19 +543,18 @@ describe("MatrixClient crypto", function() {
|
||||
.then(() => bobTestClient.awaitOneTimeKeyUpload())
|
||||
.then((keys) => {
|
||||
expect(Object.keys(keys).length).toEqual(5);
|
||||
expect(Object.keys(bobTestClient.deviceKeys).length).toNotEqual(0);
|
||||
expect(Object.keys(bobTestClient.deviceKeys).length).not.toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
it("Ali sends a message", function(done) {
|
||||
it("Ali sends a message", function() {
|
||||
aliTestClient.expectKeyQuery({device_keys: {[aliUserId]: {}}});
|
||||
Promise.resolve()
|
||||
return Promise.resolve()
|
||||
.then(() => aliTestClient.start())
|
||||
.then(() => bobTestClient.start())
|
||||
.then(() => firstSync(aliTestClient))
|
||||
.then(aliEnablesEncryption)
|
||||
.then(aliSendsFirstMessage)
|
||||
.nodeify(done);
|
||||
.then(aliSendsFirstMessage);
|
||||
});
|
||||
|
||||
it("Bob receives a message", function() {
|
||||
@@ -628,9 +622,9 @@ describe("MatrixClient crypto", function() {
|
||||
});
|
||||
});
|
||||
|
||||
it("Ali blocks Bob's device", function(done) {
|
||||
it("Ali blocks Bob's device", function() {
|
||||
aliTestClient.expectKeyQuery({device_keys: {[aliUserId]: {}}});
|
||||
Promise.resolve()
|
||||
return Promise.resolve()
|
||||
.then(() => aliTestClient.start())
|
||||
.then(() => bobTestClient.start())
|
||||
.then(() => firstSync(aliTestClient))
|
||||
@@ -645,12 +639,12 @@ describe("MatrixClient crypto", function() {
|
||||
expect(sentContent.ciphertext).toEqual({});
|
||||
});
|
||||
return Promise.all([p1, p2]);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
it("Bob receives two pre-key messages", function(done) {
|
||||
it("Bob receives two pre-key messages", function() {
|
||||
aliTestClient.expectKeyQuery({device_keys: {[aliUserId]: {}}});
|
||||
Promise.resolve()
|
||||
return Promise.resolve()
|
||||
.then(() => aliTestClient.start())
|
||||
.then(() => bobTestClient.start())
|
||||
.then(() => firstSync(aliTestClient))
|
||||
@@ -658,8 +652,7 @@ describe("MatrixClient crypto", function() {
|
||||
.then(aliSendsFirstMessage)
|
||||
.then(bobRecvMessage)
|
||||
.then(aliSendsMessage)
|
||||
.then(bobRecvMessage)
|
||||
.nodeify(done);
|
||||
.then(bobRecvMessage);
|
||||
});
|
||||
|
||||
it("Bob replies to the message", function() {
|
||||
@@ -753,9 +746,9 @@ describe("MatrixClient crypto", function() {
|
||||
.then(() => httpBackend.when("POST", "/keys/upload")
|
||||
.respond(200, (path, content) => {
|
||||
expect(content.one_time_keys).toBeTruthy();
|
||||
expect(content.one_time_keys).toNotEqual({});
|
||||
expect(content.one_time_keys).not.toEqual({});
|
||||
expect(Object.keys(content.one_time_keys).length)
|
||||
.toBeGreaterThanOrEqualTo(1);
|
||||
.toBeGreaterThanOrEqual(1);
|
||||
logger.log('received %i one-time keys',
|
||||
Object.keys(content.one_time_keys).length);
|
||||
// cancel futher calls by telling the client
|
||||
|
||||
@@ -4,9 +4,6 @@ const sdk = require("../..");
|
||||
const HttpBackend = require("matrix-mock-request");
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
import Promise from 'bluebird';
|
||||
|
||||
describe("MatrixClient events", function() {
|
||||
const baseUrl = "http://localhost.or.something";
|
||||
let client;
|
||||
@@ -15,7 +12,6 @@ describe("MatrixClient events", function() {
|
||||
const selfAccessToken = "aseukfgwef";
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
httpBackend = new HttpBackend();
|
||||
sdk.request(httpBackend.requestFn);
|
||||
client = sdk.createClient({
|
||||
@@ -164,7 +160,7 @@ describe("MatrixClient events", function() {
|
||||
});
|
||||
client.startClient();
|
||||
|
||||
httpBackend.flushAllExpected().done(function() {
|
||||
httpBackend.flushAllExpected().then(function() {
|
||||
expect(fired).toBe(true, "User.presence didn't fire.");
|
||||
done();
|
||||
});
|
||||
@@ -219,7 +215,7 @@ describe("MatrixClient events", function() {
|
||||
client.on("RoomState.events", function(event, state) {
|
||||
eventsInvokeCount++;
|
||||
const index = roomStateEventTypes.indexOf(event.getType());
|
||||
expect(index).toNotEqual(
|
||||
expect(index).not.toEqual(
|
||||
-1, "Unexpected room state event type: " + event.getType(),
|
||||
);
|
||||
if (index >= 0) {
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
"use strict";
|
||||
import 'source-map-support/register';
|
||||
import Promise from 'bluebird';
|
||||
const sdk = require("../..");
|
||||
const HttpBackend = require("matrix-mock-request");
|
||||
const utils = require("../test-utils");
|
||||
const EventTimeline = sdk.EventTimeline;
|
||||
import logger from '../../src/logger';
|
||||
import logger from '../../lib/logger';
|
||||
|
||||
const baseUrl = "http://localhost.or.something";
|
||||
const userId = "@alice:localhost";
|
||||
@@ -83,18 +82,19 @@ function startClient(httpBackend, client) {
|
||||
client.startClient();
|
||||
|
||||
// set up a promise which will resolve once the client is initialised
|
||||
const deferred = Promise.defer();
|
||||
client.on("sync", function(state) {
|
||||
logger.log("sync", state);
|
||||
if (state != "SYNCING") {
|
||||
return;
|
||||
}
|
||||
deferred.resolve();
|
||||
const prom = new Promise((resolve) => {
|
||||
client.on("sync", function(state) {
|
||||
logger.log("sync", state);
|
||||
if (state != "SYNCING") {
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.all([
|
||||
httpBackend.flushAllExpected(),
|
||||
deferred.promise,
|
||||
prom,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -103,7 +103,6 @@ describe("getEventTimeline support", function() {
|
||||
let client;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
httpBackend = new HttpBackend();
|
||||
sdk.request(httpBackend.requestFn);
|
||||
});
|
||||
@@ -115,21 +114,20 @@ describe("getEventTimeline support", function() {
|
||||
return httpBackend.stop();
|
||||
});
|
||||
|
||||
it("timeline support must be enabled to work", function(done) {
|
||||
it("timeline support must be enabled to work", function() {
|
||||
client = sdk.createClient({
|
||||
baseUrl: baseUrl,
|
||||
userId: userId,
|
||||
accessToken: accessToken,
|
||||
});
|
||||
|
||||
startClient(httpBackend, client,
|
||||
).then(function() {
|
||||
return startClient(httpBackend, client).then(function() {
|
||||
const room = client.getRoom(roomId);
|
||||
const timelineSet = room.getTimelineSets()[0];
|
||||
expect(function() {
|
||||
client.getEventTimeline(timelineSet, "event");
|
||||
}).toThrow();
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
it("timeline support works when enabled", function() {
|
||||
@@ -145,13 +143,13 @@ describe("getEventTimeline support", function() {
|
||||
const timelineSet = room.getTimelineSets()[0];
|
||||
expect(function() {
|
||||
client.getEventTimeline(timelineSet, "event");
|
||||
}).toNotThrow();
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it("scrollback should be able to scroll back to before a gappy /sync",
|
||||
function(done) {
|
||||
function() {
|
||||
// need a client with timelineSupport disabled to make this work
|
||||
client = sdk.createClient({
|
||||
baseUrl: baseUrl,
|
||||
@@ -160,8 +158,7 @@ describe("getEventTimeline support", function() {
|
||||
});
|
||||
let room;
|
||||
|
||||
startClient(httpBackend, client,
|
||||
).then(function() {
|
||||
return startClient(httpBackend, client).then(function() {
|
||||
room = client.getRoom(roomId);
|
||||
|
||||
httpBackend.when("GET", "/sync").respond(200, {
|
||||
@@ -217,18 +214,15 @@ describe("getEventTimeline support", function() {
|
||||
expect(room.timeline[0].event).toEqual(EVENTS[0]);
|
||||
expect(room.timeline[1].event).toEqual(EVENTS[1]);
|
||||
expect(room.oldState.paginationToken).toEqual("pagin_end");
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("MatrixClient event timelines", function() {
|
||||
let client = null;
|
||||
let httpBackend = null;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
httpBackend = new HttpBackend();
|
||||
sdk.request(httpBackend.requestFn);
|
||||
|
||||
@@ -349,25 +343,25 @@ describe("MatrixClient event timelines", function() {
|
||||
};
|
||||
});
|
||||
|
||||
const deferred = Promise.defer();
|
||||
client.on("sync", function() {
|
||||
client.getEventTimeline(timelineSet, EVENTS[2].event_id,
|
||||
).then(function(tl) {
|
||||
expect(tl.getEvents().length).toEqual(4);
|
||||
expect(tl.getEvents()[0].event).toEqual(EVENTS[1]);
|
||||
expect(tl.getEvents()[1].event).toEqual(EVENTS[2]);
|
||||
expect(tl.getEvents()[3].event).toEqual(EVENTS[3]);
|
||||
expect(tl.getPaginationToken(EventTimeline.BACKWARDS))
|
||||
.toEqual("start_token");
|
||||
// expect(tl.getPaginationToken(EventTimeline.FORWARDS))
|
||||
// .toEqual("s_5_4");
|
||||
}).done(() => deferred.resolve(),
|
||||
(e) => deferred.reject(e));
|
||||
const prom = new Promise((resolve, reject) => {
|
||||
client.on("sync", function() {
|
||||
client.getEventTimeline(timelineSet, EVENTS[2].event_id,
|
||||
).then(function(tl) {
|
||||
expect(tl.getEvents().length).toEqual(4);
|
||||
expect(tl.getEvents()[0].event).toEqual(EVENTS[1]);
|
||||
expect(tl.getEvents()[1].event).toEqual(EVENTS[2]);
|
||||
expect(tl.getEvents()[3].event).toEqual(EVENTS[3]);
|
||||
expect(tl.getPaginationToken(EventTimeline.BACKWARDS))
|
||||
.toEqual("start_token");
|
||||
// expect(tl.getPaginationToken(EventTimeline.FORWARDS))
|
||||
// .toEqual("s_5_4");
|
||||
}).then(resolve, reject);
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.all([
|
||||
httpBackend.flushAllExpected(),
|
||||
deferred.promise,
|
||||
prom,
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -697,7 +691,7 @@ describe("MatrixClient event timelines", function() {
|
||||
});
|
||||
|
||||
|
||||
it("should handle gappy syncs after redactions", function(done) {
|
||||
it("should handle gappy syncs after redactions", function() {
|
||||
// https://github.com/vector-im/vector-web/issues/1389
|
||||
|
||||
// a state event, followed by a redaction thereof
|
||||
@@ -729,7 +723,7 @@ describe("MatrixClient event timelines", function() {
|
||||
};
|
||||
httpBackend.when("GET", "/sync").respond(200, syncData);
|
||||
|
||||
Promise.all([
|
||||
return Promise.all([
|
||||
httpBackend.flushAllExpected(),
|
||||
utils.syncPromise(client),
|
||||
]).then(function() {
|
||||
@@ -765,6 +759,6 @@ describe("MatrixClient event timelines", function() {
|
||||
const room = client.getRoom(roomId);
|
||||
const tl = room.getLiveTimeline();
|
||||
expect(tl.getEvents().length).toEqual(1);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,8 +9,6 @@ const Filter = publicGlobals.Filter;
|
||||
const utils = require("../test-utils");
|
||||
const MockStorageApi = require("../MockStorageApi");
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("MatrixClient", function() {
|
||||
const baseUrl = "http://localhost.or.something";
|
||||
let client = null;
|
||||
@@ -21,7 +19,6 @@ describe("MatrixClient", function() {
|
||||
const accessToken = "aseukfgwef";
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
httpBackend = new HttpBackend();
|
||||
store = new MemoryStore();
|
||||
|
||||
@@ -46,7 +43,7 @@ describe("MatrixClient", function() {
|
||||
|
||||
describe("uploadContent", function() {
|
||||
const buf = new Buffer('hello world');
|
||||
it("should upload the file", function(done) {
|
||||
it("should upload the file", function() {
|
||||
httpBackend.when(
|
||||
"POST", "/_matrix/media/r0/upload",
|
||||
).check(function(req) {
|
||||
@@ -74,25 +71,26 @@ describe("MatrixClient", function() {
|
||||
expect(uploads[0].promise).toBe(prom);
|
||||
expect(uploads[0].loaded).toEqual(0);
|
||||
|
||||
prom.then(function(response) {
|
||||
const prom2 = prom.then(function(response) {
|
||||
// for backwards compatibility, we return the raw JSON
|
||||
expect(response).toEqual("content");
|
||||
|
||||
const uploads = client.getCurrentUploads();
|
||||
expect(uploads.length).toEqual(0);
|
||||
}).nodeify(done);
|
||||
});
|
||||
|
||||
httpBackend.flush();
|
||||
return prom2;
|
||||
});
|
||||
|
||||
it("should parse the response if rawResponse=false", function(done) {
|
||||
it("should parse the response if rawResponse=false", function() {
|
||||
httpBackend.when(
|
||||
"POST", "/_matrix/media/r0/upload",
|
||||
).check(function(req) {
|
||||
expect(req.opts.json).toBeFalsy();
|
||||
}).respond(200, { "content_uri": "uri" });
|
||||
|
||||
client.uploadContent({
|
||||
const prom = client.uploadContent({
|
||||
stream: buf,
|
||||
name: "hi.txt",
|
||||
type: "text/plain",
|
||||
@@ -100,12 +98,13 @@ describe("MatrixClient", function() {
|
||||
rawResponse: false,
|
||||
}).then(function(response) {
|
||||
expect(response.content_uri).toEqual("uri");
|
||||
}).nodeify(done);
|
||||
});
|
||||
|
||||
httpBackend.flush();
|
||||
return prom;
|
||||
});
|
||||
|
||||
it("should parse errors into a MatrixError", function(done) {
|
||||
it("should parse errors into a MatrixError", function() {
|
||||
httpBackend.when(
|
||||
"POST", "/_matrix/media/r0/upload",
|
||||
).check(function(req) {
|
||||
@@ -116,7 +115,7 @@ describe("MatrixClient", function() {
|
||||
"error": "broken",
|
||||
});
|
||||
|
||||
client.uploadContent({
|
||||
const prom = client.uploadContent({
|
||||
stream: buf,
|
||||
name: "hi.txt",
|
||||
type: "text/plain",
|
||||
@@ -126,12 +125,13 @@ describe("MatrixClient", function() {
|
||||
expect(error.httpStatus).toEqual(400);
|
||||
expect(error.errcode).toEqual("M_SNAFU");
|
||||
expect(error.message).toEqual("broken");
|
||||
}).nodeify(done);
|
||||
});
|
||||
|
||||
httpBackend.flush();
|
||||
return prom;
|
||||
});
|
||||
|
||||
it("should return a promise which can be cancelled", function(done) {
|
||||
it("should return a promise which can be cancelled", function() {
|
||||
const prom = client.uploadContent({
|
||||
stream: buf,
|
||||
name: "hi.txt",
|
||||
@@ -143,17 +143,18 @@ describe("MatrixClient", function() {
|
||||
expect(uploads[0].promise).toBe(prom);
|
||||
expect(uploads[0].loaded).toEqual(0);
|
||||
|
||||
prom.then(function(response) {
|
||||
const prom2 = prom.then(function(response) {
|
||||
throw Error("request not aborted");
|
||||
}, function(error) {
|
||||
expect(error).toEqual("aborted");
|
||||
|
||||
const uploads = client.getCurrentUploads();
|
||||
expect(uploads.length).toEqual(0);
|
||||
}).nodeify(done);
|
||||
});
|
||||
|
||||
const r = client.cancelUpload(prom);
|
||||
expect(r).toBe(true);
|
||||
return prom2;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -180,7 +181,7 @@ describe("MatrixClient", function() {
|
||||
event_format: "client",
|
||||
});
|
||||
store.storeFilter(filter);
|
||||
client.getFilter(userId, filterId, true).done(function(gotFilter) {
|
||||
client.getFilter(userId, filterId, true).then(function(gotFilter) {
|
||||
expect(gotFilter).toEqual(filter);
|
||||
done();
|
||||
});
|
||||
@@ -201,7 +202,7 @@ describe("MatrixClient", function() {
|
||||
event_format: "client",
|
||||
});
|
||||
store.storeFilter(storeFilter);
|
||||
client.getFilter(userId, filterId, false).done(function(gotFilter) {
|
||||
client.getFilter(userId, filterId, false).then(function(gotFilter) {
|
||||
expect(gotFilter.getDefinition()).toEqual(httpFilterDefinition);
|
||||
done();
|
||||
});
|
||||
@@ -219,7 +220,7 @@ describe("MatrixClient", function() {
|
||||
httpBackend.when(
|
||||
"GET", "/user/" + encodeURIComponent(userId) + "/filter/" + filterId,
|
||||
).respond(200, httpFilterDefinition);
|
||||
client.getFilter(userId, filterId, true).done(function(gotFilter) {
|
||||
client.getFilter(userId, filterId, true).then(function(gotFilter) {
|
||||
expect(gotFilter.getDefinition()).toEqual(httpFilterDefinition);
|
||||
expect(store.getFilter(userId, filterId)).toBeTruthy();
|
||||
done();
|
||||
@@ -247,7 +248,7 @@ describe("MatrixClient", function() {
|
||||
filter_id: filterId,
|
||||
});
|
||||
|
||||
client.createFilter(filterDefinition).done(function(gotFilter) {
|
||||
client.createFilter(filterDefinition).then(function(gotFilter) {
|
||||
expect(gotFilter.getDefinition()).toEqual(filterDefinition);
|
||||
expect(store.getFilter(userId, filterId)).toEqual(gotFilter);
|
||||
done();
|
||||
@@ -294,7 +295,7 @@ describe("MatrixClient", function() {
|
||||
});
|
||||
}).respond(200, response);
|
||||
|
||||
httpBackend.flush().done(function() {
|
||||
httpBackend.flush().then(function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -310,7 +311,7 @@ describe("MatrixClient", function() {
|
||||
return client.initCrypto();
|
||||
});
|
||||
|
||||
it("should do an HTTP request and then store the keys", function(done) {
|
||||
it("should do an HTTP request and then store the keys", function() {
|
||||
const ed25519key = "7wG2lzAqbjcyEkOP7O4gU7ItYcn+chKzh5sT/5r2l78";
|
||||
// ed25519key = client.getDeviceEd25519Key();
|
||||
const borisKeys = {
|
||||
@@ -372,7 +373,7 @@ describe("MatrixClient", function() {
|
||||
},
|
||||
});
|
||||
|
||||
client.downloadKeys(["boris", "chaz"]).then(function(res) {
|
||||
const prom = client.downloadKeys(["boris", "chaz"]).then(function(res) {
|
||||
assertObjectContains(res.boris.dev1, {
|
||||
verified: 0, // DeviceVerification.UNVERIFIED
|
||||
keys: { "ed25519:dev1": ed25519key },
|
||||
@@ -386,26 +387,26 @@ describe("MatrixClient", function() {
|
||||
algorithms: ["2"],
|
||||
unsigned: { "ghi": "def" },
|
||||
});
|
||||
}).nodeify(done);
|
||||
});
|
||||
|
||||
httpBackend.flush();
|
||||
return prom;
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteDevice", function() {
|
||||
const auth = {a: 1};
|
||||
it("should pass through an auth dict", function(done) {
|
||||
it("should pass through an auth dict", function() {
|
||||
httpBackend.when(
|
||||
"DELETE", "/_matrix/client/r0/devices/my_device",
|
||||
).check(function(req) {
|
||||
expect(req.data).toEqual({auth: auth});
|
||||
}).respond(200);
|
||||
|
||||
client.deleteDevice(
|
||||
"my_device", auth,
|
||||
).nodeify(done);
|
||||
const prom = client.deleteDevice("my_device", auth);
|
||||
|
||||
httpBackend.flush();
|
||||
return prom;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,9 +5,6 @@ const MatrixClient = sdk.MatrixClient;
|
||||
const HttpBackend = require("matrix-mock-request");
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
import Promise from 'bluebird';
|
||||
|
||||
describe("MatrixClient opts", function() {
|
||||
const baseUrl = "http://localhost.or.something";
|
||||
let client = null;
|
||||
@@ -58,7 +55,6 @@ describe("MatrixClient opts", function() {
|
||||
};
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
httpBackend = new HttpBackend();
|
||||
});
|
||||
|
||||
@@ -88,7 +84,7 @@ describe("MatrixClient opts", function() {
|
||||
httpBackend.when("PUT", "/txn1").respond(200, {
|
||||
event_id: eventId,
|
||||
});
|
||||
client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
|
||||
client.sendTextMessage("!foo:bar", "a body", "txn1").then(function(res) {
|
||||
expect(res.event_id).toEqual(eventId);
|
||||
done();
|
||||
});
|
||||
@@ -101,7 +97,7 @@ describe("MatrixClient opts", function() {
|
||||
"m.room.create",
|
||||
];
|
||||
client.on("event", function(event) {
|
||||
expect(expectedEventTypes.indexOf(event.getType())).toNotEqual(
|
||||
expect(expectedEventTypes.indexOf(event.getType())).not.toEqual(
|
||||
-1, "Recv unexpected event type: " + event.getType(),
|
||||
);
|
||||
expectedEventTypes.splice(
|
||||
@@ -141,7 +137,7 @@ describe("MatrixClient opts", function() {
|
||||
errcode: "M_SOMETHING",
|
||||
error: "Ruh roh",
|
||||
});
|
||||
client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
|
||||
client.sendTextMessage("!foo:bar", "a body", "txn1").then(function(res) {
|
||||
expect(false).toBe(true, "sendTextMessage resolved but shouldn't");
|
||||
}, function(err) {
|
||||
expect(err.errcode).toEqual("M_SOMETHING");
|
||||
@@ -159,16 +155,16 @@ describe("MatrixClient opts", function() {
|
||||
});
|
||||
let sentA = false;
|
||||
let sentB = false;
|
||||
client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
|
||||
client.sendTextMessage("!foo:bar", "a body", "txn1").then(function(res) {
|
||||
sentA = true;
|
||||
expect(sentB).toBe(true);
|
||||
});
|
||||
client.sendTextMessage("!foo:bar", "b body", "txn2").done(function(res) {
|
||||
client.sendTextMessage("!foo:bar", "b body", "txn2").then(function(res) {
|
||||
sentB = true;
|
||||
expect(sentA).toBe(false);
|
||||
});
|
||||
httpBackend.flush("/txn2", 1).done(function() {
|
||||
httpBackend.flush("/txn1", 1).done(function() {
|
||||
httpBackend.flush("/txn2", 1).then(function() {
|
||||
httpBackend.flush("/txn1", 1).then(function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -178,7 +174,7 @@ describe("MatrixClient opts", function() {
|
||||
httpBackend.when("PUT", "/txn1").respond(200, {
|
||||
event_id: "foo",
|
||||
});
|
||||
client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
|
||||
client.sendTextMessage("!foo:bar", "a body", "txn1").then(function(res) {
|
||||
expect(res.event_id).toEqual("foo");
|
||||
done();
|
||||
});
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
"use strict";
|
||||
import 'source-map-support/register';
|
||||
import Promise from 'bluebird';
|
||||
|
||||
const sdk = require("../..");
|
||||
const HttpBackend = require("matrix-mock-request");
|
||||
const utils = require("../test-utils");
|
||||
const EventStatus = sdk.EventStatus;
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("MatrixClient retrying", function() {
|
||||
const baseUrl = "http://localhost.or.something";
|
||||
let client = null;
|
||||
@@ -20,7 +16,6 @@ describe("MatrixClient retrying", function() {
|
||||
let room;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
httpBackend = new HttpBackend();
|
||||
sdk.request(httpBackend.requestFn);
|
||||
scheduler = new sdk.MatrixScheduler();
|
||||
|
||||
@@ -5,9 +5,6 @@ const EventStatus = sdk.EventStatus;
|
||||
const HttpBackend = require("matrix-mock-request");
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import Promise from 'bluebird';
|
||||
import expect from 'expect';
|
||||
|
||||
describe("MatrixClient room timelines", function() {
|
||||
const baseUrl = "http://localhost.or.something";
|
||||
let client = null;
|
||||
@@ -103,8 +100,7 @@ describe("MatrixClient room timelines", function() {
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(function(done) {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
beforeEach(function() {
|
||||
httpBackend = new HttpBackend();
|
||||
sdk.request(httpBackend.requestFn);
|
||||
client = sdk.createClient({
|
||||
@@ -122,9 +118,9 @@ describe("MatrixClient room timelines", function() {
|
||||
return NEXT_SYNC_DATA;
|
||||
});
|
||||
client.startClient();
|
||||
httpBackend.flush("/pushrules").then(function() {
|
||||
return httpBackend.flush("/pushrules").then(function() {
|
||||
return httpBackend.flush("/filter");
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
@@ -153,7 +149,7 @@ describe("MatrixClient room timelines", function() {
|
||||
expect(member.userId).toEqual(userId);
|
||||
expect(member.name).toEqual(userName);
|
||||
|
||||
httpBackend.flush("/sync", 1).done(function() {
|
||||
httpBackend.flush("/sync", 1).then(function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -179,10 +175,10 @@ describe("MatrixClient room timelines", function() {
|
||||
return;
|
||||
}
|
||||
const room = client.getRoom(roomId);
|
||||
client.sendTextMessage(roomId, "I am a fish", "txn1").done(
|
||||
client.sendTextMessage(roomId, "I am a fish", "txn1").then(
|
||||
function() {
|
||||
expect(room.timeline[1].getId()).toEqual(eventId);
|
||||
httpBackend.flush("/sync", 1).done(function() {
|
||||
httpBackend.flush("/sync", 1).then(function() {
|
||||
expect(room.timeline[1].getId()).toEqual(eventId);
|
||||
done();
|
||||
});
|
||||
@@ -212,10 +208,10 @@ describe("MatrixClient room timelines", function() {
|
||||
}
|
||||
const room = client.getRoom(roomId);
|
||||
const promise = client.sendTextMessage(roomId, "I am a fish", "txn1");
|
||||
httpBackend.flush("/sync", 1).done(function() {
|
||||
httpBackend.flush("/sync", 1).then(function() {
|
||||
expect(room.timeline.length).toEqual(2);
|
||||
httpBackend.flush("/txn1", 1);
|
||||
promise.done(function() {
|
||||
promise.then(function() {
|
||||
expect(room.timeline.length).toEqual(2);
|
||||
expect(room.timeline[1].getId()).toEqual(eventId);
|
||||
done();
|
||||
@@ -250,7 +246,7 @@ describe("MatrixClient room timelines", function() {
|
||||
const room = client.getRoom(roomId);
|
||||
expect(room.timeline.length).toEqual(1);
|
||||
|
||||
client.scrollback(room).done(function() {
|
||||
client.scrollback(room).then(function() {
|
||||
expect(room.timeline.length).toEqual(1);
|
||||
expect(room.oldState.paginationToken).toBe(null);
|
||||
|
||||
@@ -314,7 +310,7 @@ describe("MatrixClient room timelines", function() {
|
||||
// sync response
|
||||
expect(room.timeline.length).toEqual(1);
|
||||
|
||||
client.scrollback(room).done(function() {
|
||||
client.scrollback(room).then(function() {
|
||||
expect(room.timeline.length).toEqual(5);
|
||||
const joinMsg = room.timeline[0];
|
||||
expect(joinMsg.sender.name).toEqual("Old Alice");
|
||||
@@ -352,7 +348,7 @@ describe("MatrixClient room timelines", function() {
|
||||
const room = client.getRoom(roomId);
|
||||
expect(room.timeline.length).toEqual(1);
|
||||
|
||||
client.scrollback(room).done(function() {
|
||||
client.scrollback(room).then(function() {
|
||||
expect(room.timeline.length).toEqual(3);
|
||||
expect(room.timeline[0].event).toEqual(sbEvents[1]);
|
||||
expect(room.timeline[1].event).toEqual(sbEvents[0]);
|
||||
@@ -383,11 +379,11 @@ describe("MatrixClient room timelines", function() {
|
||||
const room = client.getRoom(roomId);
|
||||
expect(room.oldState.paginationToken).toBeTruthy();
|
||||
|
||||
client.scrollback(room, 1).done(function() {
|
||||
client.scrollback(room, 1).then(function() {
|
||||
expect(room.oldState.paginationToken).toEqual(sbEndTok);
|
||||
});
|
||||
|
||||
httpBackend.flush("/messages", 1).done(function() {
|
||||
httpBackend.flush("/messages", 1).then(function() {
|
||||
// still have a sync to flush
|
||||
httpBackend.flush("/sync", 1).then(() => {
|
||||
done();
|
||||
|
||||
@@ -6,9 +6,6 @@ const utils = require("../test-utils");
|
||||
const MatrixEvent = sdk.MatrixEvent;
|
||||
const EventTimeline = sdk.EventTimeline;
|
||||
|
||||
import expect from 'expect';
|
||||
import Promise from 'bluebird';
|
||||
|
||||
describe("MatrixClient syncing", function() {
|
||||
const baseUrl = "http://localhost.or.something";
|
||||
let client = null;
|
||||
@@ -23,7 +20,6 @@ describe("MatrixClient syncing", function() {
|
||||
const roomTwo = "!bar:localhost";
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
httpBackend = new HttpBackend();
|
||||
sdk.request(httpBackend.requestFn);
|
||||
client = sdk.createClient({
|
||||
@@ -53,7 +49,7 @@ describe("MatrixClient syncing", function() {
|
||||
|
||||
client.startClient();
|
||||
|
||||
httpBackend.flushAllExpected().done(function() {
|
||||
httpBackend.flushAllExpected().then(function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -67,7 +63,7 @@ describe("MatrixClient syncing", function() {
|
||||
|
||||
client.startClient();
|
||||
|
||||
httpBackend.flushAllExpected().done(function() {
|
||||
httpBackend.flushAllExpected().then(function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -528,7 +524,7 @@ describe("MatrixClient syncing", function() {
|
||||
awaitSyncEvent(),
|
||||
]).then(function() {
|
||||
const room = client.getRoom(roomTwo);
|
||||
expect(room).toExist();
|
||||
expect(room).toBeDefined();
|
||||
const tok = room.getLiveTimeline()
|
||||
.getPaginationToken(EventTimeline.BACKWARDS);
|
||||
expect(tok).toEqual("roomtwotok");
|
||||
@@ -693,12 +689,12 @@ describe("MatrixClient syncing", function() {
|
||||
include_leave: true }});
|
||||
}).respond(200, { filter_id: "another_id" });
|
||||
|
||||
const defer = Promise.defer();
|
||||
|
||||
httpBackend.when("GET", "/sync").check(function(req) {
|
||||
expect(req.queryParams.filter).toEqual("another_id");
|
||||
defer.resolve();
|
||||
}).respond(200, {});
|
||||
const prom = new Promise((resolve) => {
|
||||
httpBackend.when("GET", "/sync").check(function(req) {
|
||||
expect(req.queryParams.filter).toEqual("another_id");
|
||||
resolve();
|
||||
}).respond(200, {});
|
||||
});
|
||||
|
||||
client.syncLeftRooms();
|
||||
|
||||
@@ -709,7 +705,7 @@ describe("MatrixClient syncing", function() {
|
||||
// flush the syncs
|
||||
return httpBackend.flushAllExpected();
|
||||
}),
|
||||
defer.promise,
|
||||
prom,
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
@@ -17,13 +17,11 @@ limitations under the License.
|
||||
"use strict";
|
||||
|
||||
const anotherjson = require('another-json');
|
||||
import Promise from 'bluebird';
|
||||
import expect from 'expect';
|
||||
|
||||
const utils = require('../../lib/utils');
|
||||
const testUtils = require('../test-utils');
|
||||
const TestClient = require('../TestClient').default;
|
||||
import logger from '../../src/logger';
|
||||
import logger from '../../lib/logger';
|
||||
|
||||
const ROOM_ID = "!room:id";
|
||||
|
||||
@@ -283,8 +281,6 @@ describe("megolm", function() {
|
||||
}
|
||||
|
||||
beforeEach(async function() {
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
|
||||
aliceTestClient = new TestClient(
|
||||
"@alice:localhost", "xzcvb", "akjgkrgjs",
|
||||
);
|
||||
@@ -713,7 +709,7 @@ describe("megolm", function() {
|
||||
'PUT', '/send/',
|
||||
).respond(200, function(path, content) {
|
||||
logger.log('/send:', content);
|
||||
expect(content.session_id).toNotEqual(megolmSessionId);
|
||||
expect(content.session_id).not.toEqual(megolmSessionId);
|
||||
return {
|
||||
event_id: '$event_id',
|
||||
};
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import logger from '../src/logger';
|
||||
import logger from '../lib/logger';
|
||||
|
||||
// try to load the olm library.
|
||||
try {
|
||||
|
||||
+11
-26
@@ -1,11 +1,8 @@
|
||||
"use strict";
|
||||
import expect from 'expect';
|
||||
import Promise from 'bluebird';
|
||||
|
||||
// load olm before the sdk if possible
|
||||
import './olm-loader';
|
||||
|
||||
import logger from '../src/logger';
|
||||
import logger from '../lib/logger';
|
||||
import sdk from '..';
|
||||
const MatrixEvent = sdk.MatrixEvent;
|
||||
|
||||
@@ -41,18 +38,6 @@ module.exports.syncPromise = function(client, count) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Perform common actions before each test case, e.g. printing the test case
|
||||
* name to stdout.
|
||||
* @param {Mocha.Context} context The test context
|
||||
*/
|
||||
module.exports.beforeEach = function(context) {
|
||||
const desc = context.currentTest.fullTitle();
|
||||
|
||||
logger.log(desc);
|
||||
logger.log(new Array(1 + desc.length).join("="));
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a spy for an object and automatically spy its methods.
|
||||
* @param {*} constr The class constructor (used with 'new')
|
||||
@@ -71,7 +56,7 @@ module.exports.mock = function(constr, name) {
|
||||
for (const key in constr.prototype) { // eslint-disable-line guard-for-in
|
||||
try {
|
||||
if (constr.prototype[key] instanceof Function) {
|
||||
result[key] = expect.createSpy();
|
||||
result[key] = jest.fn();
|
||||
}
|
||||
} catch (ex) {
|
||||
// Direct access to some non-function fields of DOM prototypes may
|
||||
@@ -269,7 +254,7 @@ HttpResponse.prototype.request = function HttpResponse(
|
||||
if (!next) { // no more things to return
|
||||
if (method === "GET" && path === "/sync" && this.ignoreUnhandledSync) {
|
||||
logger.log("MatrixClient[UT] Ignoring.");
|
||||
return Promise.defer().promise;
|
||||
return new Promise(() => {});
|
||||
}
|
||||
if (this.pendingLookup) {
|
||||
if (this.pendingLookup.method === method
|
||||
@@ -284,7 +269,7 @@ HttpResponse.prototype.request = function HttpResponse(
|
||||
);
|
||||
}
|
||||
this.pendingLookup = {
|
||||
promise: Promise.defer().promise,
|
||||
promise: new Promise(() => {}),
|
||||
method: method,
|
||||
path: path,
|
||||
};
|
||||
@@ -321,10 +306,10 @@ HttpResponse.prototype.request = function HttpResponse(
|
||||
} else if (method === "GET" && path === "/sync" && this.ignoreUnhandledSync) {
|
||||
logger.log("MatrixClient[UT] Ignoring.");
|
||||
this.httpLookups.unshift(next);
|
||||
return Promise.defer().promise;
|
||||
return new Promise(() => {});
|
||||
}
|
||||
expect(true).toBe(false, "Expected different request. " + logLine);
|
||||
return Promise.defer().promise;
|
||||
return new Promise(() => {});
|
||||
};
|
||||
|
||||
HttpResponse.KEEP_ALIVE_PATH = "/_matrix/client/versions";
|
||||
@@ -377,9 +362,9 @@ module.exports.setHttpResponses = function setHttpResponses(
|
||||
client._http = [
|
||||
"authedRequest", "authedRequestWithPrefix", "getContentUri",
|
||||
"request", "requestWithPrefix", "uploadContent",
|
||||
].reduce((r, k) => {r[k] = expect.createSpy(); return r;}, {});
|
||||
client._http.authedRequest.andCall(httpReq);
|
||||
client._http.authedRequestWithPrefix.andCall(httpReq);
|
||||
client._http.requestWithPrefix.andCall(httpReq);
|
||||
client._http.request.andCall(httpReq);
|
||||
].reduce((r, k) => {r[k] = jest.fn(); return r;}, {});
|
||||
client._http.authedRequest.mockImplementation(httpReq);
|
||||
client._http.authedRequestWithPrefix.mockImplementation(httpReq);
|
||||
client._http.requestWithPrefix.mockImplementation(httpReq);
|
||||
client._http.request.mockImplementation(httpReq);
|
||||
};
|
||||
|
||||
@@ -16,13 +16,10 @@ limitations under the License.
|
||||
"use strict";
|
||||
|
||||
import 'source-map-support/register';
|
||||
import Promise from 'bluebird';
|
||||
const sdk = require("../..");
|
||||
const utils = require("../test-utils");
|
||||
|
||||
const AutoDiscovery = sdk.AutoDiscovery;
|
||||
|
||||
import expect from 'expect';
|
||||
import MockHttpBackend from "matrix-mock-request";
|
||||
|
||||
|
||||
@@ -30,7 +27,6 @@ describe("AutoDiscovery", function() {
|
||||
let httpBackend = null;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
httpBackend = new MockHttpBackend();
|
||||
sdk.request(httpBackend.requestFn);
|
||||
});
|
||||
|
||||
@@ -1,17 +1,10 @@
|
||||
"use strict";
|
||||
import 'source-map-support/register';
|
||||
const ContentRepo = require("../../lib/content-repo");
|
||||
const testUtils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("ContentRepo", function() {
|
||||
const baseUrl = "https://my.home.server";
|
||||
|
||||
beforeEach(function() {
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
});
|
||||
|
||||
describe("getHttpUriForMxc", function() {
|
||||
it("should do nothing to HTTP URLs when allowing direct links", function() {
|
||||
const httpUrl = "http://example.com/image.jpeg";
|
||||
|
||||
+41
-65
@@ -3,7 +3,6 @@ import 'source-map-support/register';
|
||||
import '../olm-loader';
|
||||
|
||||
import Crypto from '../../lib/crypto';
|
||||
import expect from 'expect';
|
||||
|
||||
import WebStorageSessionStore from '../../lib/store/session/webstorage';
|
||||
import MemoryCryptoStore from '../../lib/crypto/store/memory-crypto-store.js';
|
||||
@@ -12,7 +11,7 @@ import TestClient from '../TestClient';
|
||||
import {MatrixEvent} from '../../lib/models/event';
|
||||
import Room from '../../lib/models/room';
|
||||
import olmlib from '../../lib/crypto/olmlib';
|
||||
import lolex from 'lolex';
|
||||
import {sleep} from "../../src/utils";
|
||||
|
||||
const EventEmitter = require("events").EventEmitter;
|
||||
|
||||
@@ -25,8 +24,8 @@ describe("Crypto", function() {
|
||||
return;
|
||||
}
|
||||
|
||||
beforeEach(function(done) {
|
||||
Olm.init().then(done);
|
||||
beforeAll(function() {
|
||||
return Olm.init();
|
||||
});
|
||||
|
||||
it("Crypto exposes the correct olm library version", function() {
|
||||
@@ -76,9 +75,9 @@ describe("Crypto", function() {
|
||||
});
|
||||
|
||||
mockBaseApis = {
|
||||
sendToDevice: expect.createSpy(),
|
||||
getKeyBackupVersion: expect.createSpy(),
|
||||
isGuest: expect.createSpy(),
|
||||
sendToDevice: jest.fn(),
|
||||
getKeyBackupVersion: jest.fn(),
|
||||
isGuest: jest.fn(),
|
||||
};
|
||||
mockRoomList = {};
|
||||
|
||||
@@ -110,15 +109,16 @@ describe("Crypto", function() {
|
||||
});
|
||||
|
||||
fakeEmitter.emit('toDeviceEvent', {
|
||||
getType: expect.createSpy().andReturn('m.room.message'),
|
||||
getContent: expect.createSpy().andReturn({
|
||||
getId: jest.fn().mockReturnValue("$wedged"),
|
||||
getType: jest.fn().mockReturnValue('m.room.message'),
|
||||
getContent: jest.fn().mockReturnValue({
|
||||
msgtype: 'm.bad.encrypted',
|
||||
}),
|
||||
getWireContent: expect.createSpy().andReturn({
|
||||
getWireContent: jest.fn().mockReturnValue({
|
||||
algorithm: 'm.olm.v1.curve25519-aes-sha2',
|
||||
sender_key: 'this is a key',
|
||||
}),
|
||||
getSender: expect.createSpy().andReturn('@bob:home.server'),
|
||||
getSender: jest.fn().mockReturnValue('@bob:home.server'),
|
||||
});
|
||||
|
||||
await prom;
|
||||
@@ -245,7 +245,7 @@ describe("Crypto", function() {
|
||||
await bobDecryptor.onRoomKeyEvent(ksEvent);
|
||||
await eventPromise;
|
||||
expect(events[0].getContent().msgtype).toBe("m.bad.encrypted");
|
||||
expect(events[1].getContent().msgtype).toNotBe("m.bad.encrypted");
|
||||
expect(events[1].getContent().msgtype).not.toBe("m.bad.encrypted");
|
||||
|
||||
const cryptoStore = bobClient._cryptoStore;
|
||||
const eventContent = events[0].getWireContent();
|
||||
@@ -260,7 +260,7 @@ describe("Crypto", function() {
|
||||
// the room key request should still be there, since we haven't
|
||||
// decrypted everything
|
||||
expect(await cryptoStore.getOutgoingRoomKeyRequest(roomKeyRequestBody))
|
||||
.toExist();
|
||||
.toBeDefined();
|
||||
|
||||
// keyshare the session key starting at the first message, so
|
||||
// that it can now be decrypted
|
||||
@@ -268,10 +268,11 @@ describe("Crypto", function() {
|
||||
ksEvent = await keyshareEventForEvent(events[0], 0);
|
||||
await bobDecryptor.onRoomKeyEvent(ksEvent);
|
||||
await eventPromise;
|
||||
expect(events[0].getContent().msgtype).toNotBe("m.bad.encrypted");
|
||||
// the room key request should be gone since we've now decypted everything
|
||||
expect(events[0].getContent().msgtype).not.toBe("m.bad.encrypted");
|
||||
await sleep(1);
|
||||
// the room key request should be gone since we've now decrypted everything
|
||||
expect(await cryptoStore.getOutgoingRoomKeyRequest(roomKeyRequestBody))
|
||||
.toNotExist();
|
||||
.toBeFalsy();
|
||||
},
|
||||
);
|
||||
|
||||
@@ -296,10 +297,12 @@ describe("Crypto", function() {
|
||||
sender_key: "senderkey",
|
||||
};
|
||||
expect(await cryptoStore.getOutgoingRoomKeyRequest(roomKeyRequestBody))
|
||||
.toExist();
|
||||
.toBeDefined();
|
||||
});
|
||||
|
||||
it("uses a new txnid for re-requesting keys", async function() {
|
||||
jest.useFakeTimers();
|
||||
|
||||
const event = new MatrixEvent({
|
||||
sender: "@bob:example.com",
|
||||
room_id: "!someroom",
|
||||
@@ -309,58 +312,31 @@ describe("Crypto", function() {
|
||||
sender_key: "senderkey",
|
||||
},
|
||||
});
|
||||
/* return a promise and a function. When the function is called,
|
||||
* the promise will be resolved.
|
||||
*/
|
||||
function awaitFunctionCall() {
|
||||
let func;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
func = function(...args) {
|
||||
resolve(args);
|
||||
return new Promise((resolve, reject) => {
|
||||
// give us some time to process the result before
|
||||
// continuing
|
||||
global.setTimeout(resolve, 1);
|
||||
});
|
||||
};
|
||||
});
|
||||
return {func, promise};
|
||||
}
|
||||
|
||||
// replace Alice's sendToDevice function with a mock
|
||||
aliceClient.sendToDevice = jest.fn().mockResolvedValue(undefined);
|
||||
aliceClient.startClient();
|
||||
|
||||
const clock = lolex.install();
|
||||
// make a room key request, and record the transaction ID for the
|
||||
// sendToDevice call
|
||||
await aliceClient.cancelAndResendEventRoomKeyRequest(event);
|
||||
jest.runAllTimers();
|
||||
await Promise.resolve();
|
||||
expect(aliceClient.sendToDevice).toBeCalledTimes(1);
|
||||
const txnId = aliceClient.sendToDevice.mock.calls[0][2];
|
||||
|
||||
try {
|
||||
let promise;
|
||||
// make a room key request, and record the transaction ID for the
|
||||
// sendToDevice call
|
||||
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
|
||||
await aliceClient.cancelAndResendEventRoomKeyRequest(event);
|
||||
clock.runToLast();
|
||||
let args = await promise;
|
||||
const txnId = args[2];
|
||||
clock.runToLast();
|
||||
// give the room key request manager time to update the state
|
||||
// of the request
|
||||
await Promise.resolve();
|
||||
|
||||
// give the room key request manager time to update the state
|
||||
// of the request
|
||||
await Promise.resolve();
|
||||
|
||||
// cancel and resend the room key request
|
||||
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
|
||||
await aliceClient.cancelAndResendEventRoomKeyRequest(event);
|
||||
clock.runToLast();
|
||||
// the first call to sendToDevice will be the cancellation
|
||||
args = await promise;
|
||||
// the second call to sendToDevice will be the key request
|
||||
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
|
||||
clock.runToLast();
|
||||
args = await promise;
|
||||
clock.runToLast();
|
||||
expect(args[2]).toNotBe(txnId);
|
||||
} finally {
|
||||
clock.uninstall();
|
||||
}
|
||||
// cancel and resend the room key request
|
||||
await aliceClient.cancelAndResendEventRoomKeyRequest(event);
|
||||
jest.runAllTimers();
|
||||
await Promise.resolve();
|
||||
// cancelAndResend will call sendToDevice twice:
|
||||
// the first call to sendToDevice will be the cancellation
|
||||
// the second call to sendToDevice will be the key request
|
||||
expect(aliceClient.sendToDevice).toBeCalledTimes(3);
|
||||
expect(aliceClient.sendToDevice.mock.calls[2][2]).not.toBe(txnId);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,12 +17,8 @@ limitations under the License.
|
||||
|
||||
import DeviceList from '../../../lib/crypto/DeviceList';
|
||||
import MemoryCryptoStore from '../../../lib/crypto/store/memory-crypto-store.js';
|
||||
import testUtils from '../../test-utils';
|
||||
import utils from '../../../lib/utils';
|
||||
import logger from '../../../src/logger';
|
||||
|
||||
import expect from 'expect';
|
||||
import Promise from 'bluebird';
|
||||
import logger from '../../../lib/logger';
|
||||
|
||||
const signedDeviceList = {
|
||||
"failures": {},
|
||||
@@ -60,11 +56,9 @@ describe('DeviceList', function() {
|
||||
let deviceLists = [];
|
||||
|
||||
beforeEach(function() {
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
|
||||
deviceLists = [];
|
||||
|
||||
downloadSpy = expect.createSpy();
|
||||
downloadSpy = jest.fn();
|
||||
cryptoStore = new MemoryCryptoStore();
|
||||
});
|
||||
|
||||
@@ -91,8 +85,8 @@ describe('DeviceList', function() {
|
||||
|
||||
dl.startTrackingDeviceList('@test1:sw1v.org');
|
||||
|
||||
const queryDefer1 = Promise.defer();
|
||||
downloadSpy.andReturn(queryDefer1.promise);
|
||||
const queryDefer1 = utils.defer();
|
||||
downloadSpy.mockReturnValue(queryDefer1.promise);
|
||||
|
||||
const prom1 = dl.refreshOutdatedDeviceLists();
|
||||
expect(downloadSpy).toHaveBeenCalledWith(['@test1:sw1v.org'], {});
|
||||
@@ -110,16 +104,16 @@ describe('DeviceList', function() {
|
||||
|
||||
dl.startTrackingDeviceList('@test1:sw1v.org');
|
||||
|
||||
const queryDefer1 = Promise.defer();
|
||||
downloadSpy.andReturn(queryDefer1.promise);
|
||||
const queryDefer1 = utils.defer();
|
||||
downloadSpy.mockReturnValue(queryDefer1.promise);
|
||||
|
||||
const prom1 = dl.refreshOutdatedDeviceLists();
|
||||
expect(downloadSpy).toHaveBeenCalledWith(['@test1:sw1v.org'], {});
|
||||
downloadSpy.reset();
|
||||
downloadSpy.mockReset();
|
||||
|
||||
// outdated notif arrives while the request is in flight.
|
||||
const queryDefer2 = Promise.defer();
|
||||
downloadSpy.andReturn(queryDefer2.promise);
|
||||
const queryDefer2 = utils.defer();
|
||||
downloadSpy.mockReturnValue(queryDefer2.promise);
|
||||
|
||||
dl.invalidateUserDeviceList('@test1:sw1v.org');
|
||||
dl.refreshOutdatedDeviceLists();
|
||||
@@ -136,10 +130,10 @@ describe('DeviceList', function() {
|
||||
// uh-oh; user restarts before second request completes. The new instance
|
||||
// should know we never got a complete device list.
|
||||
logger.log("Creating new devicelist to simulate app reload");
|
||||
downloadSpy.reset();
|
||||
downloadSpy.mockReset();
|
||||
const dl2 = createTestDeviceList();
|
||||
const queryDefer3 = Promise.defer();
|
||||
downloadSpy.andReturn(queryDefer3.promise);
|
||||
const queryDefer3 = utils.defer();
|
||||
downloadSpy.mockReturnValue(queryDefer3.promise);
|
||||
|
||||
const prom3 = dl2.refreshOutdatedDeviceLists();
|
||||
expect(downloadSpy).toHaveBeenCalledWith(['@test1:sw1v.org'], {});
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import '../../../olm-loader';
|
||||
|
||||
import expect from 'expect';
|
||||
import Promise from 'bluebird';
|
||||
|
||||
import sdk from '../../../..';
|
||||
import algorithms from '../../../../lib/crypto/algorithms';
|
||||
import MemoryCryptoStore from '../../../../lib/crypto/store/memory-crypto-store.js';
|
||||
@@ -10,7 +7,7 @@ import MockStorageApi from '../../../MockStorageApi';
|
||||
import testUtils from '../../../test-utils';
|
||||
import OlmDevice from '../../../../lib/crypto/OlmDevice';
|
||||
import Crypto from '../../../../lib/crypto';
|
||||
import logger from '../../../../src/logger';
|
||||
import logger from '../../../../lib/logger';
|
||||
|
||||
const MatrixEvent = sdk.MatrixEvent;
|
||||
const MegolmDecryption = algorithms.DECRYPTION_CLASSES['m.megolm.v1.aes-sha2'];
|
||||
@@ -26,16 +23,16 @@ describe("MegolmDecryption", function() {
|
||||
return;
|
||||
}
|
||||
|
||||
beforeAll(function() {
|
||||
return Olm.init();
|
||||
});
|
||||
|
||||
let megolmDecryption;
|
||||
let mockOlmLib;
|
||||
let mockCrypto;
|
||||
let mockBaseApis;
|
||||
|
||||
beforeEach(async function() {
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
|
||||
await Olm.init();
|
||||
|
||||
mockCrypto = testUtils.mock(Crypto, 'Crypto');
|
||||
mockBaseApis = {};
|
||||
|
||||
@@ -55,9 +52,9 @@ describe("MegolmDecryption", function() {
|
||||
|
||||
// we stub out the olm encryption bits
|
||||
mockOlmLib = {};
|
||||
mockOlmLib.ensureOlmSessionsForDevices = expect.createSpy();
|
||||
mockOlmLib.ensureOlmSessionsForDevices = jest.fn();
|
||||
mockOlmLib.encryptMessageForDevice =
|
||||
expect.createSpy().andReturn(Promise.resolve());
|
||||
jest.fn().mockResolvedValue(undefined);
|
||||
megolmDecryption.olmlib = mockOlmLib;
|
||||
});
|
||||
|
||||
@@ -135,22 +132,22 @@ describe("MegolmDecryption", function() {
|
||||
|
||||
// set up some pre-conditions for the share call
|
||||
const deviceInfo = {};
|
||||
mockCrypto.getStoredDevice.andReturn(deviceInfo);
|
||||
mockCrypto.getStoredDevice.mockReturnValue(deviceInfo);
|
||||
|
||||
mockOlmLib.ensureOlmSessionsForDevices.andReturn(
|
||||
Promise.resolve({'@alice:foo': {'alidevice': {
|
||||
mockOlmLib.ensureOlmSessionsForDevices.mockResolvedValue({
|
||||
'@alice:foo': {'alidevice': {
|
||||
sessionId: 'alisession',
|
||||
}}}),
|
||||
);
|
||||
}},
|
||||
});
|
||||
|
||||
const awaitEncryptForDevice = new Promise((res, rej) => {
|
||||
mockOlmLib.encryptMessageForDevice.andCall(() => {
|
||||
mockOlmLib.encryptMessageForDevice.mockImplementation(() => {
|
||||
res();
|
||||
return Promise.resolve();
|
||||
});
|
||||
});
|
||||
|
||||
mockBaseApis.sendToDevice = expect.createSpy();
|
||||
mockBaseApis.sendToDevice = jest.fn();
|
||||
|
||||
// do the share
|
||||
megolmDecryption.shareKeysWithDevice(keyRequest);
|
||||
@@ -160,21 +157,20 @@ describe("MegolmDecryption", function() {
|
||||
}).then(() => {
|
||||
// check that it called encryptMessageForDevice with
|
||||
// appropriate args.
|
||||
expect(mockOlmLib.encryptMessageForDevice.calls.length)
|
||||
.toEqual(1);
|
||||
expect(mockOlmLib.encryptMessageForDevice).toBeCalledTimes(1);
|
||||
|
||||
const call = mockOlmLib.encryptMessageForDevice.calls[0];
|
||||
const payload = call.arguments[6];
|
||||
const call = mockOlmLib.encryptMessageForDevice.mock.calls[0];
|
||||
const payload = call[6];
|
||||
|
||||
expect(payload.type).toEqual("m.forwarded_room_key");
|
||||
expect(payload.content).toInclude({
|
||||
expect(payload.content).toMatchObject({
|
||||
sender_key: "SENDER_CURVE25519",
|
||||
sender_claimed_ed25519_key: "SENDER_ED25519",
|
||||
session_id: groupSession.session_id(),
|
||||
chain_index: 0,
|
||||
forwarding_curve25519_key_chain: [],
|
||||
});
|
||||
expect(payload.content.session_key).toExist();
|
||||
expect(payload.content.session_key).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -201,13 +197,12 @@ describe("MegolmDecryption", function() {
|
||||
origin_server_ts: 1507753886000,
|
||||
});
|
||||
|
||||
const successHandler = expect.createSpy();
|
||||
const failureHandler = expect.createSpy()
|
||||
.andCall((err) => {
|
||||
expect(err.toString()).toMatch(
|
||||
/Duplicate message index, possible replay attack/,
|
||||
);
|
||||
});
|
||||
const successHandler = jest.fn();
|
||||
const failureHandler = jest.fn((err) => {
|
||||
expect(err.toString()).toMatch(
|
||||
/Duplicate message index, possible replay attack/,
|
||||
);
|
||||
});
|
||||
|
||||
return megolmDecryption.decryptEvent(event1).then((res) => {
|
||||
const event2 = new MatrixEvent({
|
||||
@@ -228,7 +223,7 @@ describe("MegolmDecryption", function() {
|
||||
successHandler,
|
||||
failureHandler,
|
||||
).then(() => {
|
||||
expect(successHandler).toNotHaveBeenCalled();
|
||||
expect(successHandler).not.toHaveBeenCalled();
|
||||
expect(failureHandler).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -266,10 +261,10 @@ describe("MegolmDecryption", function() {
|
||||
const cryptoStore = new MemoryCryptoStore(mockStorage);
|
||||
|
||||
const olmDevice = new OlmDevice(cryptoStore);
|
||||
olmDevice.verifySignature = expect.createSpy();
|
||||
olmDevice.verifySignature = jest.fn();
|
||||
await olmDevice.init();
|
||||
|
||||
mockBaseApis.claimOneTimeKeys = expect.createSpy().andReturn(Promise.resolve({
|
||||
mockBaseApis.claimOneTimeKeys = jest.fn().mockReturnValue(Promise.resolve({
|
||||
one_time_keys: {
|
||||
'@alice:home.server': {
|
||||
aliceDevice: {
|
||||
@@ -285,18 +280,18 @@ describe("MegolmDecryption", function() {
|
||||
},
|
||||
},
|
||||
}));
|
||||
mockBaseApis.sendToDevice = expect.createSpy().andReturn(Promise.resolve());
|
||||
mockBaseApis.sendToDevice = jest.fn().mockResolvedValue(undefined);
|
||||
|
||||
mockCrypto.downloadKeys.andReturn(Promise.resolve({
|
||||
mockCrypto.downloadKeys.mockReturnValue(Promise.resolve({
|
||||
'@alice:home.server': {
|
||||
aliceDevice: {
|
||||
deviceId: 'aliceDevice',
|
||||
isBlocked: expect.createSpy().andReturn(false),
|
||||
isUnverified: expect.createSpy().andReturn(false),
|
||||
getIdentityKey: expect.createSpy().andReturn(
|
||||
isBlocked: jest.fn().mockReturnValue(false),
|
||||
isUnverified: jest.fn().mockReturnValue(false),
|
||||
getIdentityKey: jest.fn().mockReturnValue(
|
||||
'YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWE',
|
||||
),
|
||||
getFingerprint: expect.createSpy().andReturn(''),
|
||||
getFingerprint: jest.fn().mockReturnValue(''),
|
||||
},
|
||||
},
|
||||
}));
|
||||
@@ -312,10 +307,10 @@ describe("MegolmDecryption", function() {
|
||||
},
|
||||
});
|
||||
const mockRoom = {
|
||||
getEncryptionTargetMembers: expect.createSpy().andReturn(
|
||||
getEncryptionTargetMembers: jest.fn().mockReturnValue(
|
||||
[{userId: "@alice:home.server"}],
|
||||
),
|
||||
getBlacklistUnverifiedDevices: expect.createSpy().andReturn(false),
|
||||
getBlacklistUnverifiedDevices: jest.fn().mockReturnValue(false),
|
||||
};
|
||||
const ct1 = await megolmEncryption.encryptMessage(mockRoom, "a.fake.type", {
|
||||
body: "Some text",
|
||||
@@ -323,25 +318,25 @@ describe("MegolmDecryption", function() {
|
||||
expect(mockRoom.getEncryptionTargetMembers).toHaveBeenCalled();
|
||||
|
||||
// this should have claimed a key for alice as it's starting a new session
|
||||
expect(mockBaseApis.claimOneTimeKeys).toHaveBeenCalled(
|
||||
expect(mockBaseApis.claimOneTimeKeys).toHaveBeenCalledWith(
|
||||
[['@alice:home.server', 'aliceDevice']], 'signed_curve25519',
|
||||
);
|
||||
expect(mockCrypto.downloadKeys).toHaveBeenCalledWith(
|
||||
['@alice:home.server'], false,
|
||||
);
|
||||
expect(mockBaseApis.sendToDevice).toHaveBeenCalled();
|
||||
expect(mockBaseApis.claimOneTimeKeys).toHaveBeenCalled(
|
||||
expect(mockBaseApis.claimOneTimeKeys).toHaveBeenCalledWith(
|
||||
[['@alice:home.server', 'aliceDevice']], 'signed_curve25519',
|
||||
);
|
||||
|
||||
mockBaseApis.claimOneTimeKeys.reset();
|
||||
mockBaseApis.claimOneTimeKeys.mockReset();
|
||||
|
||||
const ct2 = await megolmEncryption.encryptMessage(mockRoom, "a.fake.type", {
|
||||
body: "Some more text",
|
||||
});
|
||||
|
||||
// this should *not* have claimed a key as it should be using the same session
|
||||
expect(mockBaseApis.claimOneTimeKeys).toNotHaveBeenCalled();
|
||||
expect(mockBaseApis.claimOneTimeKeys).not.toHaveBeenCalled();
|
||||
|
||||
// likewise they should show the same session ID
|
||||
expect(ct2.session_id).toEqual(ct1.session_id);
|
||||
|
||||
@@ -16,11 +16,9 @@ limitations under the License.
|
||||
|
||||
import '../../../olm-loader';
|
||||
|
||||
import expect from 'expect';
|
||||
import MemoryCryptoStore from '../../../../lib/crypto/store/memory-crypto-store.js';
|
||||
import MockStorageApi from '../../../MockStorageApi';
|
||||
import testUtils from '../../../test-utils';
|
||||
import logger from '../../../../src/logger';
|
||||
import logger from '../../../../lib/logger';
|
||||
|
||||
import OlmDevice from '../../../../lib/crypto/OlmDevice';
|
||||
import olmlib from '../../../../lib/crypto/olmlib';
|
||||
@@ -50,14 +48,14 @@ describe("OlmDecryption", function() {
|
||||
return;
|
||||
}
|
||||
|
||||
beforeAll(function() {
|
||||
return global.Olm.init();
|
||||
});
|
||||
|
||||
let aliceOlmDevice;
|
||||
let bobOlmDevice;
|
||||
|
||||
beforeEach(async function() {
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
|
||||
await global.Olm.init();
|
||||
|
||||
aliceOlmDevice = makeOlmDevice();
|
||||
bobOlmDevice = makeOlmDevice();
|
||||
await aliceOlmDevice.init();
|
||||
|
||||
@@ -16,9 +16,6 @@ limitations under the License.
|
||||
|
||||
import '../../olm-loader';
|
||||
|
||||
import expect from 'expect';
|
||||
import Promise from 'bluebird';
|
||||
|
||||
import sdk from '../../..';
|
||||
import algorithms from '../../../lib/crypto/algorithms';
|
||||
import WebStorageSessionStore from '../../../lib/store/session/webstorage';
|
||||
@@ -28,7 +25,7 @@ import testUtils from '../../test-utils';
|
||||
|
||||
import OlmDevice from '../../../lib/crypto/OlmDevice';
|
||||
import Crypto from '../../../lib/crypto';
|
||||
import logger from '../../../src/logger';
|
||||
import logger from '../../../lib/logger';
|
||||
import olmlib from '../../../lib/crypto/olmlib';
|
||||
|
||||
const Olm = global.Olm;
|
||||
@@ -98,16 +95,16 @@ function makeTestClient(sessionStore, cryptoStore) {
|
||||
const scheduler = [
|
||||
"getQueueForEvent", "queueEvent", "removeEventFromQueue",
|
||||
"setProcessFunction",
|
||||
].reduce((r, k) => {r[k] = expect.createSpy(); return r;}, {});
|
||||
].reduce((r, k) => {r[k] = jest.fn(); return r;}, {});
|
||||
const store = [
|
||||
"getRoom", "getRooms", "getUser", "getSyncToken", "scrollback",
|
||||
"save", "wantsSave", "setSyncToken", "storeEvents", "storeRoom",
|
||||
"storeUser", "getFilterIdByName", "setFilterIdByName", "getFilter",
|
||||
"storeFilter", "getSyncAccumulator", "startup", "deleteAllData",
|
||||
].reduce((r, k) => {r[k] = expect.createSpy(); return r;}, {});
|
||||
store.getSavedSync = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
store.getSavedSyncToken = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
store.setSyncData = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
].reduce((r, k) => {r[k] = jest.fn(); return r;}, {});
|
||||
store.getSavedSync = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
store.getSavedSyncToken = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
store.setSyncData = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
return new MatrixClient({
|
||||
baseUrl: "https://my.home.server",
|
||||
idBaseUrl: "https://identity.server",
|
||||
@@ -129,6 +126,10 @@ describe("MegolmBackup", function() {
|
||||
return;
|
||||
}
|
||||
|
||||
beforeAll(function() {
|
||||
return Olm.init();
|
||||
});
|
||||
|
||||
let olmDevice;
|
||||
let mockOlmLib;
|
||||
let mockCrypto;
|
||||
@@ -137,9 +138,6 @@ describe("MegolmBackup", function() {
|
||||
let cryptoStore;
|
||||
let megolmDecryption;
|
||||
beforeEach(async function() {
|
||||
await Olm.init();
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
|
||||
mockCrypto = testUtils.mock(Crypto, 'Crypto');
|
||||
mockCrypto.backupKey = new Olm.PkEncryption();
|
||||
mockCrypto.backupKey.set_recipient_key(
|
||||
@@ -155,9 +153,9 @@ describe("MegolmBackup", function() {
|
||||
|
||||
// we stub out the olm encryption bits
|
||||
mockOlmLib = {};
|
||||
mockOlmLib.ensureOlmSessionsForDevices = expect.createSpy();
|
||||
mockOlmLib.ensureOlmSessionsForDevices = jest.fn();
|
||||
mockOlmLib.encryptMessageForDevice =
|
||||
expect.createSpy().andReturn(Promise.resolve());
|
||||
jest.fn().mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
describe("backup", function() {
|
||||
@@ -218,7 +216,7 @@ describe("MegolmBackup", function() {
|
||||
};
|
||||
mockCrypto.cancelRoomKeyRequest = function() {};
|
||||
|
||||
mockCrypto.backupGroupSession = expect.createSpy();
|
||||
mockCrypto.backupGroupSession = jest.fn();
|
||||
|
||||
return event.attemptDecryption(mockCrypto).then(() => {
|
||||
return megolmDecryption.onRoomKeyEvent(event);
|
||||
@@ -279,7 +277,7 @@ describe("MegolmBackup", function() {
|
||||
callback, method, path, queryParams, data, opts,
|
||||
) {
|
||||
++numCalls;
|
||||
expect(numCalls).toBeLessThanOrEqualTo(1);
|
||||
expect(numCalls).toBeLessThanOrEqual(1);
|
||||
if (numCalls >= 2) {
|
||||
// exit out of retry loop if there's something wrong
|
||||
reject(new Error("authedRequest called too many timmes"));
|
||||
@@ -288,8 +286,8 @@ describe("MegolmBackup", function() {
|
||||
expect(method).toBe("PUT");
|
||||
expect(path).toBe("/room_keys/keys");
|
||||
expect(queryParams.version).toBe(1);
|
||||
expect(data.rooms[ROOM_ID].sessions).toExist();
|
||||
expect(data.rooms[ROOM_ID].sessions).toIncludeKey(
|
||||
expect(data.rooms[ROOM_ID].sessions).toBeDefined();
|
||||
expect(data.rooms[ROOM_ID].sessions).toHaveProperty(
|
||||
groupSession.session_id(),
|
||||
);
|
||||
resolve();
|
||||
@@ -338,12 +336,12 @@ describe("MegolmBackup", function() {
|
||||
});
|
||||
await client.resetCrossSigningKeys();
|
||||
let numCalls = 0;
|
||||
await new Promise(async (resolve, reject) => {
|
||||
await new Promise((resolve, reject) => {
|
||||
client._http.authedRequest = function(
|
||||
callback, method, path, queryParams, data, opts,
|
||||
) {
|
||||
++numCalls;
|
||||
expect(numCalls).toBeLessThanOrEqualTo(1);
|
||||
expect(numCalls).toBeLessThanOrEqual(1);
|
||||
if (numCalls >= 2) {
|
||||
// exit out of retry loop if there's something wrong
|
||||
reject(new Error("authedRequest called too many timmes"));
|
||||
@@ -363,7 +361,7 @@ describe("MegolmBackup", function() {
|
||||
resolve();
|
||||
return Promise.resolve({});
|
||||
};
|
||||
await client.createKeyBackupVersion({
|
||||
client.createKeyBackupVersion({
|
||||
algorithm: "m.megolm_backup.v1",
|
||||
auth_data: {
|
||||
public_key: "hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmo",
|
||||
@@ -382,16 +380,16 @@ describe("MegolmBackup", function() {
|
||||
const scheduler = [
|
||||
"getQueueForEvent", "queueEvent", "removeEventFromQueue",
|
||||
"setProcessFunction",
|
||||
].reduce((r, k) => {r[k] = expect.createSpy(); return r;}, {});
|
||||
].reduce((r, k) => {r[k] = jest.fn(); return r;}, {});
|
||||
const store = [
|
||||
"getRoom", "getRooms", "getUser", "getSyncToken", "scrollback",
|
||||
"save", "wantsSave", "setSyncToken", "storeEvents", "storeRoom",
|
||||
"storeUser", "getFilterIdByName", "setFilterIdByName", "getFilter",
|
||||
"storeFilter", "getSyncAccumulator", "startup", "deleteAllData",
|
||||
].reduce((r, k) => {r[k] = expect.createSpy(); return r;}, {});
|
||||
store.getSavedSync = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
store.getSavedSyncToken = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
store.setSyncData = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
].reduce((r, k) => {r[k] = jest.fn(); return r;}, {});
|
||||
store.getSavedSync = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
store.getSavedSyncToken = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
store.setSyncData = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
const client = new MatrixClient({
|
||||
baseUrl: "https://my.home.server",
|
||||
idBaseUrl: "https://identity.server",
|
||||
@@ -449,7 +447,7 @@ describe("MegolmBackup", function() {
|
||||
callback, method, path, queryParams, data, opts,
|
||||
) {
|
||||
++numCalls;
|
||||
expect(numCalls).toBeLessThanOrEqualTo(2);
|
||||
expect(numCalls).toBeLessThanOrEqual(2);
|
||||
if (numCalls >= 3) {
|
||||
// exit out of retry loop if there's something wrong
|
||||
reject(new Error("authedRequest called too many timmes"));
|
||||
@@ -458,8 +456,8 @@ describe("MegolmBackup", function() {
|
||||
expect(method).toBe("PUT");
|
||||
expect(path).toBe("/room_keys/keys");
|
||||
expect(queryParams.version).toBe(1);
|
||||
expect(data.rooms[ROOM_ID].sessions).toExist();
|
||||
expect(data.rooms[ROOM_ID].sessions).toIncludeKey(
|
||||
expect(data.rooms[ROOM_ID].sessions).toBeDefined();
|
||||
expect(data.rooms[ROOM_ID].sessions).toHaveProperty(
|
||||
groupSession.session_id(),
|
||||
);
|
||||
if (numCalls > 1) {
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
|
||||
import '../../olm-loader';
|
||||
|
||||
import expect from 'expect';
|
||||
import anotherjson from 'another-json';
|
||||
|
||||
import olmlib from '../../../lib/crypto/olmlib';
|
||||
@@ -56,21 +55,20 @@ describe("Cross Signing", function() {
|
||||
return;
|
||||
}
|
||||
|
||||
beforeEach(async function() {
|
||||
await global.Olm.init();
|
||||
beforeAll(function() {
|
||||
return global.Olm.init();
|
||||
});
|
||||
|
||||
it("should sign the master key with the device key", async function() {
|
||||
const alice = await makeTestClient(
|
||||
{userId: "@alice:example.com", deviceId: "Osborne2"},
|
||||
);
|
||||
alice.uploadDeviceSigningKeys = expect.createSpy()
|
||||
.andCall(async (auth, keys) => {
|
||||
await olmlib.verifySignature(
|
||||
alice._crypto._olmDevice, keys.master_key, "@alice:example.com",
|
||||
"Osborne2", alice._crypto._olmDevice.deviceEd25519Key,
|
||||
);
|
||||
});
|
||||
alice.uploadDeviceSigningKeys = jest.fn(async (auth, keys) => {
|
||||
await olmlib.verifySignature(
|
||||
alice._crypto._olmDevice, keys.master_key, "@alice:example.com",
|
||||
"Osborne2", alice._crypto._olmDevice.deviceEd25519Key,
|
||||
);
|
||||
});
|
||||
alice.uploadKeySignatures = async () => {};
|
||||
// set Alice's cross-signing key
|
||||
await alice.resetCrossSigningKeys();
|
||||
@@ -146,7 +144,7 @@ describe("Cross Signing", function() {
|
||||
});
|
||||
|
||||
const uploadSigsPromise = new Promise((resolve, reject) => {
|
||||
alice.uploadKeySignatures = expect.createSpy().andCall(async (content) => {
|
||||
alice.uploadKeySignatures = jest.fn(async (content) => {
|
||||
await olmlib.verifySignature(
|
||||
alice._crypto._olmDevice,
|
||||
content["@alice:example.com"][
|
||||
@@ -732,7 +730,7 @@ describe("Cross Signing", function() {
|
||||
{
|
||||
cryptoCallbacks: {
|
||||
shouldUpgradeDeviceVerifications: (verifs) => {
|
||||
expect(verifs.users["@bob:example.com"]).toExist();
|
||||
expect(verifs.users["@bob:example.com"]).toBeDefined();
|
||||
upgradeResolveFunc();
|
||||
return ["@bob:example.com"];
|
||||
},
|
||||
@@ -791,6 +789,9 @@ describe("Cross Signing", function() {
|
||||
upgradeResolveFunc = resolve;
|
||||
});
|
||||
alice._crypto._deviceList.emit("userCrossSigningUpdated", "@bob:example.com");
|
||||
await new Promise((resolve) => {
|
||||
alice._crypto.on("userTrustStatusChanged", resolve);
|
||||
});
|
||||
await upgradePromise;
|
||||
|
||||
const bobTrust3 = alice.checkUserTrust("@bob:example.com");
|
||||
|
||||
@@ -16,7 +16,6 @@ limitations under the License.
|
||||
|
||||
import '../../olm-loader';
|
||||
|
||||
import expect from 'expect';
|
||||
import { MatrixEvent } from '../../../lib/models/event';
|
||||
import { SECRET_STORAGE_ALGORITHM_V1 } from '../../../lib/crypto/SecretStorage';
|
||||
|
||||
@@ -41,8 +40,8 @@ describe("Secrets", function() {
|
||||
return;
|
||||
}
|
||||
|
||||
beforeEach(async function() {
|
||||
await global.Olm.init();
|
||||
beforeAll(function() {
|
||||
return global.Olm.init();
|
||||
});
|
||||
|
||||
it("should store and retrieve a secret", async function() {
|
||||
@@ -62,7 +61,7 @@ describe("Secrets", function() {
|
||||
},
|
||||
};
|
||||
|
||||
const getKey = expect.createSpy().andCall(e => {
|
||||
const getKey = jest.fn(e => {
|
||||
expect(Object.keys(e.keys)).toEqual(["abc"]);
|
||||
return ['abc', privkey];
|
||||
});
|
||||
@@ -164,7 +163,7 @@ describe("Secrets", function() {
|
||||
};
|
||||
alice.resetCrossSigningKeys();
|
||||
|
||||
const newKeyId = await alice.addSecretKey(
|
||||
const newKeyId = await alice.addSecretStorageKey(
|
||||
SECRET_STORAGE_ALGORITHM_V1,
|
||||
);
|
||||
// we don't await on this because it waits for the event to come down the sync
|
||||
@@ -245,4 +244,90 @@ describe("Secrets", function() {
|
||||
|
||||
expect(secret).toBe("bar");
|
||||
});
|
||||
|
||||
it("bootstraps when no storage or cross-signing keys locally", async function() {
|
||||
const bob = await makeTestClient(
|
||||
{
|
||||
userId: "@bob:example.com",
|
||||
deviceId: "bob1",
|
||||
},
|
||||
);
|
||||
bob.uploadDeviceSigningKeys = async () => {};
|
||||
bob.uploadKeySignatures = async () => {};
|
||||
bob.setAccountData = async function(eventType, contents, callback) {
|
||||
const event = new MatrixEvent({
|
||||
type: eventType,
|
||||
content: contents,
|
||||
});
|
||||
this.store.storeAccountDataEvents([
|
||||
event,
|
||||
]);
|
||||
this.emit("accountData", event);
|
||||
};
|
||||
|
||||
await bob.bootstrapSecretStorage();
|
||||
|
||||
const crossSigning = bob._crypto._crossSigningInfo;
|
||||
const secretStorage = bob._crypto._secretStorage;
|
||||
|
||||
expect(crossSigning.getId()).toBeTruthy();
|
||||
expect(crossSigning.isStoredInSecretStorage(secretStorage)).toBeTruthy();
|
||||
expect(secretStorage.hasKey()).toBeTruthy();
|
||||
});
|
||||
|
||||
it("bootstraps when cross-signing keys in secret storage", async function() {
|
||||
const decryption = new global.Olm.PkDecryption();
|
||||
const storagePublicKey = decryption.generate_key();
|
||||
const storagePrivateKey = decryption.get_private_key();
|
||||
|
||||
const bob = await makeTestClient(
|
||||
{
|
||||
userId: "@bob:example.com",
|
||||
deviceId: "bob1",
|
||||
},
|
||||
{
|
||||
cryptoCallbacks: {
|
||||
getSecretStorageKey: request => {
|
||||
const defaultKeyId = bob.getDefaultSecretStorageKeyId();
|
||||
expect(Object.keys(request.keys)).toEqual([defaultKeyId]);
|
||||
return [defaultKeyId, storagePrivateKey];
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
bob.uploadDeviceSigningKeys = async () => {};
|
||||
bob.uploadKeySignatures = async () => {};
|
||||
bob.setAccountData = async function(eventType, contents, callback) {
|
||||
const event = new MatrixEvent({
|
||||
type: eventType,
|
||||
content: contents,
|
||||
});
|
||||
this.store.storeAccountDataEvents([
|
||||
event,
|
||||
]);
|
||||
this.emit("accountData", event);
|
||||
};
|
||||
bob._crypto.checkKeyBackup = async () => {};
|
||||
|
||||
const crossSigning = bob._crypto._crossSigningInfo;
|
||||
const secretStorage = bob._crypto._secretStorage;
|
||||
|
||||
// Set up cross-signing keys from scratch with specific storage key
|
||||
await bob.bootstrapSecretStorage({
|
||||
createSecretStorageKey: async () => ({ pubkey: storagePublicKey }),
|
||||
});
|
||||
|
||||
// Clear local cross-signing keys and read from secret storage
|
||||
bob._crypto._deviceList.storeCrossSigningForUser(
|
||||
"@bob:example.com",
|
||||
crossSigning.toStorage(),
|
||||
);
|
||||
crossSigning.keys = {};
|
||||
await bob.bootstrapSecretStorage();
|
||||
|
||||
expect(crossSigning.getId()).toBeTruthy();
|
||||
expect(crossSigning.isStoredInSecretStorage(secretStorage)).toBeTruthy();
|
||||
expect(secretStorage.hasKey()).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ 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.
|
||||
*/
|
||||
import logger from '../../../../src/logger';
|
||||
import logger from '../../../../lib/logger';
|
||||
|
||||
try {
|
||||
global.Olm = require('olm');
|
||||
@@ -21,7 +21,6 @@ try {
|
||||
logger.warn("unable to run device verification tests: libolm not available");
|
||||
}
|
||||
|
||||
import expect from 'expect';
|
||||
import DeviceInfo from '../../../../lib/crypto/deviceinfo';
|
||||
|
||||
import {ShowQRCode, ScanQRCode} from '../../../../lib/crypto/verification/QRCode';
|
||||
@@ -34,20 +33,23 @@ describe("QR code verification", function() {
|
||||
return;
|
||||
}
|
||||
|
||||
beforeEach(async function() {
|
||||
await Olm.init();
|
||||
beforeAll(function() {
|
||||
return Olm.init();
|
||||
});
|
||||
|
||||
describe("showing", function() {
|
||||
it("should emit an event to show a QR code", async function() {
|
||||
const qrCode = new ShowQRCode({
|
||||
const channel = {
|
||||
send: jest.fn(),
|
||||
};
|
||||
const qrCode = new ShowQRCode(channel, {
|
||||
getUserId: () => "@alice:example.com",
|
||||
deviceId: "ABCDEFG",
|
||||
getDeviceEd25519Key: function() {
|
||||
return "device+ed25519+key";
|
||||
},
|
||||
});
|
||||
const spy = expect.createSpy().andCall((e) => {
|
||||
const spy = jest.fn((e) => {
|
||||
qrCode.done();
|
||||
});
|
||||
qrCode.on("show_qr_code", spy);
|
||||
@@ -77,10 +79,13 @@ describe("QR code verification", function() {
|
||||
"ABCDEFG",
|
||||
);
|
||||
const client = {
|
||||
getStoredDevice: expect.createSpy().andReturn(device),
|
||||
setDeviceVerified: expect.createSpy(),
|
||||
getStoredDevice: jest.fn().mockReturnValue(device),
|
||||
setDeviceVerified: jest.fn(),
|
||||
};
|
||||
const qrCode = new ScanQRCode(client);
|
||||
const channel = {
|
||||
send: jest.fn(),
|
||||
};
|
||||
const qrCode = new ScanQRCode(channel, client);
|
||||
qrCode.on("confirm_user_id", ({userId, confirm}) => {
|
||||
if (userId === "@alice:example.com") {
|
||||
confirm();
|
||||
@@ -100,18 +105,22 @@ describe("QR code verification", function() {
|
||||
|
||||
it("should error when the user ID doesn't match", async function() {
|
||||
const client = {
|
||||
getStoredDevice: expect.createSpy(),
|
||||
setDeviceVerified: expect.createSpy(),
|
||||
getStoredDevice: jest.fn(),
|
||||
setDeviceVerified: jest.fn(),
|
||||
};
|
||||
const qrCode = new ScanQRCode(client, "@bob:example.com", "ABCDEFG");
|
||||
const channel = {
|
||||
send: jest.fn(),
|
||||
};
|
||||
const qrCode = new ScanQRCode(channel, client, "@bob:example.com", "ABCDEFG");
|
||||
qrCode.on("scan", ({done}) => {
|
||||
done(QR_CODE_URL);
|
||||
});
|
||||
const spy = expect.createSpy();
|
||||
const spy = jest.fn();
|
||||
await qrCode.verify().catch(spy);
|
||||
expect(spy).toHaveBeenCalled();
|
||||
expect(client.getStoredDevice).toNotHaveBeenCalled();
|
||||
expect(client.setDeviceVerified).toNotHaveBeenCalled();
|
||||
expect(channel.send).toHaveBeenCalled();
|
||||
expect(client.getStoredDevice).not.toHaveBeenCalled();
|
||||
expect(client.setDeviceVerified).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should error if the key doesn't match", async function() {
|
||||
@@ -129,18 +138,23 @@ describe("QR code verification", function() {
|
||||
"ABCDEFG",
|
||||
);
|
||||
const client = {
|
||||
getStoredDevice: expect.createSpy().andReturn(device),
|
||||
setDeviceVerified: expect.createSpy(),
|
||||
getStoredDevice: jest.fn().mockReturnValue(device),
|
||||
setDeviceVerified: jest.fn(),
|
||||
};
|
||||
const qrCode = new ScanQRCode(client, "@alice:example.com", "ABCDEFG");
|
||||
const channel = {
|
||||
send: jest.fn(),
|
||||
};
|
||||
const qrCode = new ScanQRCode(
|
||||
channel, client, "@alice:example.com", "ABCDEFG");
|
||||
qrCode.on("scan", ({done}) => {
|
||||
done(QR_CODE_URL);
|
||||
});
|
||||
const spy = expect.createSpy();
|
||||
const spy = jest.fn();
|
||||
await qrCode.verify().catch(spy);
|
||||
expect(spy).toHaveBeenCalled();
|
||||
expect(channel.send).toHaveBeenCalled();
|
||||
expect(client.getStoredDevice).toHaveBeenCalled();
|
||||
expect(client.setDeviceVerified).toNotHaveBeenCalled();
|
||||
expect(client.setDeviceVerified).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ 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.
|
||||
*/
|
||||
import logger from '../../../../src/logger';
|
||||
import logger from '../../../../lib/logger';
|
||||
|
||||
try {
|
||||
global.Olm = require('olm');
|
||||
@@ -21,8 +21,6 @@ try {
|
||||
logger.warn("unable to run device verification tests: libolm not available");
|
||||
}
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
import {verificationMethods} from '../../../../lib/crypto';
|
||||
|
||||
import SAS from '../../../../lib/crypto/verification/SAS';
|
||||
@@ -37,8 +35,8 @@ describe("verification request", function() {
|
||||
return;
|
||||
}
|
||||
|
||||
beforeEach(async function() {
|
||||
await Olm.init();
|
||||
beforeAll(function() {
|
||||
return Olm.init();
|
||||
});
|
||||
|
||||
it("should request and accept a verification", async function() {
|
||||
@@ -74,7 +72,7 @@ describe("verification request", function() {
|
||||
bobVerifier._endTimer();
|
||||
});
|
||||
const aliceVerifier = await alice.client.requestVerification("@bob:example.com");
|
||||
expect(aliceVerifier).toBeAn(SAS);
|
||||
expect(aliceVerifier).toBeInstanceOf(SAS);
|
||||
|
||||
// XXX: Private function access (but it's a test, so we're okay)
|
||||
aliceVerifier._endTimer();
|
||||
|
||||
@@ -13,7 +13,7 @@ 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.
|
||||
*/
|
||||
import logger from '../../../../src/logger';
|
||||
import logger from '../../../../lib/logger';
|
||||
|
||||
try {
|
||||
global.Olm = require('olm');
|
||||
@@ -21,7 +21,6 @@ try {
|
||||
logger.warn("unable to run device verification tests: libolm not available");
|
||||
}
|
||||
|
||||
import expect from 'expect';
|
||||
import olmlib from '../../../../lib/crypto/olmlib';
|
||||
|
||||
import sdk from '../../../..';
|
||||
@@ -46,8 +45,8 @@ describe("SAS verification", function() {
|
||||
return;
|
||||
}
|
||||
|
||||
beforeEach(async function() {
|
||||
await Olm.init();
|
||||
beforeAll(function() {
|
||||
return Olm.init();
|
||||
});
|
||||
|
||||
it("should error on an unexpected event", async function() {
|
||||
@@ -57,16 +56,15 @@ describe("SAS verification", function() {
|
||||
type: "es.inquisition",
|
||||
content: {},
|
||||
}));
|
||||
const spy = expect.createSpy();
|
||||
await sas.verify()
|
||||
.catch(spy);
|
||||
const spy = jest.fn();
|
||||
await sas.verify().catch(spy);
|
||||
expect(spy).toHaveBeenCalled();
|
||||
|
||||
// Cancel the SAS for cleanup (we started a verification, so abort)
|
||||
sas.cancel();
|
||||
});
|
||||
|
||||
describe("verification", function() {
|
||||
describe("verification", () => {
|
||||
let alice;
|
||||
let bob;
|
||||
let aliceSasEvent;
|
||||
@@ -74,7 +72,7 @@ describe("SAS verification", function() {
|
||||
let aliceVerifier;
|
||||
let bobPromise;
|
||||
|
||||
beforeEach(async function() {
|
||||
beforeEach(async () => {
|
||||
[alice, bob] = await makeTestClients(
|
||||
[
|
||||
{userId: "@alice:example.com", deviceId: "Osborne2"},
|
||||
@@ -115,14 +113,14 @@ describe("SAS verification", function() {
|
||||
alice.client._crypto._deviceList.storeDevicesForUser(
|
||||
"@bob:example.com", BOB_DEVICES,
|
||||
);
|
||||
alice.downloadKeys = () => {
|
||||
alice.client.downloadKeys = () => {
|
||||
return Promise.resolve();
|
||||
};
|
||||
|
||||
bob.client._crypto._deviceList.storeDevicesForUser(
|
||||
"@alice:example.com", ALICE_DEVICES,
|
||||
);
|
||||
bob.downloadKeys = () => {
|
||||
bob.client.downloadKeys = () => {
|
||||
return Promise.resolve();
|
||||
};
|
||||
|
||||
@@ -171,16 +169,22 @@ describe("SAS verification", function() {
|
||||
}
|
||||
});
|
||||
});
|
||||
afterEach(async () => {
|
||||
await Promise.all([
|
||||
alice.stop(),
|
||||
bob.stop(),
|
||||
]);
|
||||
});
|
||||
|
||||
it("should verify a key", async function() {
|
||||
it("should verify a key", async () => {
|
||||
let macMethod;
|
||||
const origSendToDevice = alice.client.sendToDevice;
|
||||
const origSendToDevice = bob.client.sendToDevice.bind(bob.client);
|
||||
bob.client.sendToDevice = function(type, map) {
|
||||
if (type === "m.key.verification.accept") {
|
||||
macMethod = map[alice.client.getUserId()][alice.client.deviceId]
|
||||
.message_authentication_code;
|
||||
}
|
||||
return origSendToDevice.call(this, type, map);
|
||||
return origSendToDevice(type, map);
|
||||
};
|
||||
|
||||
alice.httpBackend.when('POST', '/keys/query').respond(200, {
|
||||
@@ -215,12 +219,12 @@ describe("SAS verification", function() {
|
||||
expect(aliceDevice.isVerified()).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should be able to verify using the old MAC", async function() {
|
||||
it("should be able to verify using the old MAC", async () => {
|
||||
// pretend that Alice can only understand the old (incorrect) MAC,
|
||||
// and make sure that she can still verify with Bob
|
||||
let macMethod;
|
||||
const origSendToDevice = alice.client.sendToDevice;
|
||||
alice.client.sendToDevice = function(type, map) {
|
||||
const aliceOrigSendToDevice = alice.client.sendToDevice.bind(alice.client);
|
||||
alice.client.sendToDevice = (type, map) => {
|
||||
if (type === "m.key.verification.start") {
|
||||
// Note: this modifies not only the message that Bob
|
||||
// receives, but also the copy of the message that Alice
|
||||
@@ -230,14 +234,15 @@ describe("SAS verification", function() {
|
||||
map[bob.client.getUserId()][bob.client.deviceId]
|
||||
.message_authentication_codes = ['hmac-sha256'];
|
||||
}
|
||||
return origSendToDevice.call(this, type, map);
|
||||
return aliceOrigSendToDevice(type, map);
|
||||
};
|
||||
bob.client.sendToDevice = function(type, map) {
|
||||
const bobOrigSendToDevice = bob.client.sendToDevice.bind(bob.client);
|
||||
bob.client.sendToDevice = (type, map) => {
|
||||
if (type === "m.key.verification.accept") {
|
||||
macMethod = map[alice.client.getUserId()][alice.client.deviceId]
|
||||
.message_authentication_code;
|
||||
}
|
||||
return origSendToDevice.call(this, type, map);
|
||||
return bobOrigSendToDevice(type, map);
|
||||
};
|
||||
|
||||
alice.httpBackend.when('POST', '/keys/query').respond(200, {
|
||||
@@ -270,7 +275,7 @@ describe("SAS verification", function() {
|
||||
expect(aliceDevice.isVerified()).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should verify a cross-signing key", async function() {
|
||||
it("should verify a cross-signing key", async () => {
|
||||
alice.httpBackend.when('POST', '/keys/device_signing/upload').respond(
|
||||
200, {},
|
||||
);
|
||||
@@ -289,33 +294,17 @@ describe("SAS verification", function() {
|
||||
},
|
||||
);
|
||||
|
||||
alice.httpBackend.when('POST', '/keys/query').respond(200, {
|
||||
failures: {},
|
||||
device_keys: {
|
||||
"@bob:example.com": BOB_DEVICES,
|
||||
},
|
||||
});
|
||||
bob.httpBackend.when('POST', '/keys/query').respond(200, {
|
||||
failures: {},
|
||||
device_keys: {
|
||||
"@alice:example.com": ALICE_DEVICES,
|
||||
},
|
||||
});
|
||||
|
||||
const verifyProm = Promise.all([
|
||||
aliceVerifier.verify(),
|
||||
bobPromise.then((verifier) => {
|
||||
bob.httpBackend.when(
|
||||
'POST', '/keys/signatures/upload',
|
||||
).respond(200, {});
|
||||
bob.httpBackend.flush(undefined, 2);
|
||||
bob.httpBackend.flush(undefined, 1, 2000);
|
||||
return verifier.verify();
|
||||
}),
|
||||
]);
|
||||
|
||||
await alice.httpBackend.flush(undefined, 1);
|
||||
console.log("alice reqs flushed");
|
||||
|
||||
await verifyProm;
|
||||
|
||||
const bobDeviceTrust = alice.client.checkDeviceTrust(
|
||||
@@ -346,11 +335,11 @@ describe("SAS verification", function() {
|
||||
verificationMethods: [verificationMethods.SAS],
|
||||
},
|
||||
);
|
||||
alice.client.setDeviceVerified = expect.createSpy();
|
||||
alice.client.setDeviceVerified = jest.fn();
|
||||
alice.client.downloadKeys = () => {
|
||||
return Promise.resolve();
|
||||
};
|
||||
bob.client.setDeviceVerified = expect.createSpy();
|
||||
bob.client.setDeviceVerified = jest.fn();
|
||||
bob.client.downloadKeys = () => {
|
||||
return Promise.resolve();
|
||||
};
|
||||
@@ -368,8 +357,8 @@ describe("SAS verification", function() {
|
||||
verificationMethods.SAS, bob.client.getUserId(), bob.client.deviceId,
|
||||
);
|
||||
|
||||
const aliceSpy = expect.createSpy();
|
||||
const bobSpy = expect.createSpy();
|
||||
const aliceSpy = jest.fn();
|
||||
const bobSpy = jest.fn();
|
||||
await Promise.all([
|
||||
aliceVerifier.verify().catch(aliceSpy),
|
||||
bobPromise.then((verifier) => verifier.verify()).catch(bobSpy),
|
||||
@@ -377,9 +366,9 @@ describe("SAS verification", function() {
|
||||
expect(aliceSpy).toHaveBeenCalled();
|
||||
expect(bobSpy).toHaveBeenCalled();
|
||||
expect(alice.client.setDeviceVerified)
|
||||
.toNotHaveBeenCalled();
|
||||
.not.toHaveBeenCalled();
|
||||
expect(bob.client.setDeviceVerified)
|
||||
.toNotHaveBeenCalled();
|
||||
.not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe("verification in DM", function() {
|
||||
@@ -401,7 +390,7 @@ describe("SAS verification", function() {
|
||||
},
|
||||
);
|
||||
|
||||
alice.client.setDeviceVerified = expect.createSpy();
|
||||
alice.client.setDeviceVerified = jest.fn();
|
||||
alice.client.getDeviceEd25519Key = () => {
|
||||
return "alice+base64+ed25519+key";
|
||||
};
|
||||
@@ -419,7 +408,7 @@ describe("SAS verification", function() {
|
||||
return Promise.resolve();
|
||||
};
|
||||
|
||||
bob.client.setDeviceVerified = expect.createSpy();
|
||||
bob.client.setDeviceVerified = jest.fn();
|
||||
bob.client.getStoredDevice = () => {
|
||||
return DeviceInfo.fromStorage(
|
||||
{
|
||||
@@ -441,32 +430,26 @@ describe("SAS verification", function() {
|
||||
bobSasEvent = null;
|
||||
|
||||
bobPromise = new Promise((resolve, reject) => {
|
||||
bob.client.on("event", async (event) => {
|
||||
const content = event.getContent();
|
||||
if (event.getType() === "m.room.message"
|
||||
&& content.msgtype === "m.key.verification.request") {
|
||||
expect(content.methods).toInclude(SAS.NAME);
|
||||
expect(content.to).toBe(bob.client.getUserId());
|
||||
const verifier = bob.client.acceptVerificationDM(event, SAS.NAME);
|
||||
verifier.on("show_sas", (e) => {
|
||||
if (!e.sas.emoji || !e.sas.decimal) {
|
||||
e.cancel();
|
||||
} else if (!aliceSasEvent) {
|
||||
bobSasEvent = e;
|
||||
} else {
|
||||
try {
|
||||
expect(e.sas).toEqual(aliceSasEvent.sas);
|
||||
e.confirm();
|
||||
aliceSasEvent.confirm();
|
||||
} catch (error) {
|
||||
e.mismatch();
|
||||
aliceSasEvent.mismatch();
|
||||
}
|
||||
bob.client.on("crypto.verification.request", async (request) => {
|
||||
const verifier = request.beginKeyVerification(SAS.NAME);
|
||||
verifier.on("show_sas", (e) => {
|
||||
if (!e.sas.emoji || !e.sas.decimal) {
|
||||
e.cancel();
|
||||
} else if (!aliceSasEvent) {
|
||||
bobSasEvent = e;
|
||||
} else {
|
||||
try {
|
||||
expect(e.sas).toEqual(aliceSasEvent.sas);
|
||||
e.confirm();
|
||||
aliceSasEvent.confirm();
|
||||
} catch (error) {
|
||||
e.mismatch();
|
||||
aliceSasEvent.mismatch();
|
||||
}
|
||||
});
|
||||
await verifier.verify();
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
});
|
||||
await verifier.verify();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -490,6 +473,12 @@ describe("SAS verification", function() {
|
||||
}
|
||||
});
|
||||
});
|
||||
afterEach(async function() {
|
||||
await Promise.all([
|
||||
alice.stop(),
|
||||
bob.stop(),
|
||||
]);
|
||||
});
|
||||
|
||||
it("should verify a key", async function() {
|
||||
await Promise.all([
|
||||
|
||||
@@ -60,7 +60,7 @@ export async function makeTestClients(userInfos, options) {
|
||||
});
|
||||
for (const tc of clients) {
|
||||
setTimeout(
|
||||
() => tc.client.emit("event", event),
|
||||
() => tc.client.emit("Room.timeline", event),
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -9,8 +9,6 @@ function mockRoomStates(timeline) {
|
||||
timeline._endState = utils.mock(sdk.RoomState, "endState");
|
||||
}
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("EventTimeline", function() {
|
||||
const roomId = "!foo:bar";
|
||||
const userA = "@alice:bar";
|
||||
@@ -18,8 +16,6 @@ describe("EventTimeline", function() {
|
||||
let timeline;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
|
||||
// XXX: this is a horrid hack; should use sinon or something instead to mock
|
||||
const timelineSet = { room: { roomId: roomId }};
|
||||
timelineSet.room.getUnfilteredTimelineSet = function() {
|
||||
@@ -78,7 +74,7 @@ describe("EventTimeline", function() {
|
||||
|
||||
expect(function() {
|
||||
timeline.initialiseState(state);
|
||||
}).toNotThrow();
|
||||
}).not.toThrow();
|
||||
timeline.addEvent(event, false);
|
||||
expect(function() {
|
||||
timeline.initialiseState(state);
|
||||
@@ -121,7 +117,7 @@ describe("EventTimeline", function() {
|
||||
const next = {b: "b"};
|
||||
expect(function() {
|
||||
timeline.setNeighbouringTimeline(prev, EventTimeline.BACKWARDS);
|
||||
}).toNotThrow();
|
||||
}).not.toThrow();
|
||||
expect(timeline.getNeighbouringTimeline(EventTimeline.BACKWARDS))
|
||||
.toBe(prev);
|
||||
expect(function() {
|
||||
@@ -130,7 +126,7 @@ describe("EventTimeline", function() {
|
||||
|
||||
expect(function() {
|
||||
timeline.setNeighbouringTimeline(next, EventTimeline.FORWARDS);
|
||||
}).toNotThrow();
|
||||
}).not.toThrow();
|
||||
expect(timeline.getNeighbouringTimeline(EventTimeline.FORWARDS))
|
||||
.toBe(next);
|
||||
expect(function() {
|
||||
@@ -187,14 +183,14 @@ describe("EventTimeline", function() {
|
||||
name: "Old Alice",
|
||||
};
|
||||
timeline.getState(EventTimeline.FORWARDS).getSentinelMember
|
||||
.andCall(function(uid) {
|
||||
.mockImplementation(function(uid) {
|
||||
if (uid === userA) {
|
||||
return sentinel;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
timeline.getState(EventTimeline.BACKWARDS).getSentinelMember
|
||||
.andCall(function(uid) {
|
||||
.mockImplementation(function(uid) {
|
||||
if (uid === userA) {
|
||||
return oldSentinel;
|
||||
}
|
||||
@@ -229,14 +225,14 @@ describe("EventTimeline", function() {
|
||||
name: "Old Alice",
|
||||
};
|
||||
timeline.getState(EventTimeline.FORWARDS).getSentinelMember
|
||||
.andCall(function(uid) {
|
||||
.mockImplementation(function(uid) {
|
||||
if (uid === userA) {
|
||||
return sentinel;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
timeline.getState(EventTimeline.BACKWARDS).getSentinelMember
|
||||
.andCall(function(uid) {
|
||||
.mockImplementation(function(uid) {
|
||||
if (uid === userA) {
|
||||
return oldSentinel;
|
||||
}
|
||||
@@ -281,7 +277,7 @@ describe("EventTimeline", function() {
|
||||
expect(events[1].forwardLooking).toBe(true);
|
||||
|
||||
expect(timeline.getState(EventTimeline.BACKWARDS).setStateEvents).
|
||||
toNotHaveBeenCalled();
|
||||
not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
@@ -311,7 +307,7 @@ describe("EventTimeline", function() {
|
||||
expect(events[1].forwardLooking).toBe(false);
|
||||
|
||||
expect(timeline.getState(EventTimeline.FORWARDS).setStateEvents).
|
||||
toNotHaveBeenCalled();
|
||||
not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+4
-10
@@ -17,17 +17,9 @@ limitations under the License.
|
||||
import sdk from '../..';
|
||||
const MatrixEvent = sdk.MatrixEvent;
|
||||
|
||||
import testUtils from '../test-utils';
|
||||
|
||||
import expect from 'expect';
|
||||
import Promise from 'bluebird';
|
||||
import logger from '../../src/logger';
|
||||
import logger from '../../lib/logger';
|
||||
|
||||
describe("MatrixEvent", () => {
|
||||
beforeEach(function() {
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
});
|
||||
|
||||
describe(".attemptDecryption", () => {
|
||||
let encryptedEvent;
|
||||
|
||||
@@ -45,6 +37,7 @@ describe("MatrixEvent", () => {
|
||||
let callCount = 0;
|
||||
|
||||
let prom2;
|
||||
let prom2Fulfilled = false;
|
||||
|
||||
const crypto = {
|
||||
decryptEvent: function() {
|
||||
@@ -54,12 +47,13 @@ describe("MatrixEvent", () => {
|
||||
// schedule a second decryption attempt while
|
||||
// the first one is still running.
|
||||
prom2 = encryptedEvent.attemptDecryption(crypto);
|
||||
prom2.then(() => prom2Fulfilled = true);
|
||||
|
||||
const error = new Error("nope");
|
||||
error.name = 'DecryptionError';
|
||||
return Promise.reject(error);
|
||||
} else {
|
||||
expect(prom2.isFulfilled()).toBe(
|
||||
expect(prom2Fulfilled).toBe(
|
||||
false, 'second attemptDecryption resolved too soon');
|
||||
|
||||
return Promise.resolve({
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
import 'source-map-support/register';
|
||||
const sdk = require("../..");
|
||||
const Filter = sdk.Filter;
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("Filter", function() {
|
||||
const filterId = "f1lt3ring15g00d4ursoul";
|
||||
@@ -12,7 +9,6 @@ describe("Filter", function() {
|
||||
let filter;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
filter = new Filter(userId);
|
||||
});
|
||||
|
||||
|
||||
@@ -16,15 +16,12 @@ limitations under the License.
|
||||
"use strict";
|
||||
|
||||
import 'source-map-support/register';
|
||||
import Promise from 'bluebird';
|
||||
const sdk = require("../..");
|
||||
const utils = require("../test-utils");
|
||||
|
||||
const InteractiveAuth = sdk.InteractiveAuth;
|
||||
const MatrixError = sdk.MatrixError;
|
||||
|
||||
import expect from 'expect';
|
||||
import logger from '../../src/logger';
|
||||
import logger from '../../lib/logger';
|
||||
|
||||
// Trivial client object to test interactive auth
|
||||
// (we do not need TestClient here)
|
||||
@@ -35,13 +32,9 @@ class FakeClient {
|
||||
}
|
||||
|
||||
describe("InteractiveAuth", function() {
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
});
|
||||
|
||||
it("should start an auth stage and complete it", function(done) {
|
||||
const doRequest = expect.createSpy();
|
||||
const stateUpdated = expect.createSpy();
|
||||
it("should start an auth stage and complete it", function() {
|
||||
const doRequest = jest.fn();
|
||||
const stateUpdated = jest.fn();
|
||||
|
||||
const ia = new InteractiveAuth({
|
||||
matrixClient: new FakeClient(),
|
||||
@@ -64,7 +57,7 @@ describe("InteractiveAuth", function() {
|
||||
});
|
||||
|
||||
// first we expect a call here
|
||||
stateUpdated.andCall(function(stage) {
|
||||
stateUpdated.mockImplementation(function(stage) {
|
||||
logger.log('aaaa');
|
||||
expect(stage).toEqual("logintype");
|
||||
ia.submitAuthDict({
|
||||
@@ -75,7 +68,7 @@ describe("InteractiveAuth", function() {
|
||||
|
||||
// .. which should trigger a call here
|
||||
const requestRes = {"a": "b"};
|
||||
doRequest.andCall(function(authData) {
|
||||
doRequest.mockImplementation(function(authData) {
|
||||
logger.log('cccc');
|
||||
expect(authData).toEqual({
|
||||
session: "sessionId",
|
||||
@@ -85,16 +78,16 @@ describe("InteractiveAuth", function() {
|
||||
return Promise.resolve(requestRes);
|
||||
});
|
||||
|
||||
ia.attemptAuth().then(function(res) {
|
||||
return ia.attemptAuth().then(function(res) {
|
||||
expect(res).toBe(requestRes);
|
||||
expect(doRequest.calls.length).toEqual(1);
|
||||
expect(stateUpdated.calls.length).toEqual(1);
|
||||
}).nodeify(done);
|
||||
expect(doRequest).toBeCalledTimes(1);
|
||||
expect(stateUpdated).toBeCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
it("should make a request if no authdata is provided", function(done) {
|
||||
const doRequest = expect.createSpy();
|
||||
const stateUpdated = expect.createSpy();
|
||||
it("should make a request if no authdata is provided", function() {
|
||||
const doRequest = jest.fn();
|
||||
const stateUpdated = jest.fn();
|
||||
|
||||
const ia = new InteractiveAuth({
|
||||
matrixClient: new FakeClient(),
|
||||
@@ -106,7 +99,7 @@ describe("InteractiveAuth", function() {
|
||||
expect(ia.getStageParams("logintype")).toBe(undefined);
|
||||
|
||||
// first we expect a call to doRequest
|
||||
doRequest.andCall(function(authData) {
|
||||
doRequest.mockImplementation(function(authData) {
|
||||
logger.log("request1", authData);
|
||||
expect(authData).toEqual({});
|
||||
const err = new MatrixError({
|
||||
@@ -124,7 +117,7 @@ describe("InteractiveAuth", function() {
|
||||
|
||||
// .. which should be followed by a call to stateUpdated
|
||||
const requestRes = {"a": "b"};
|
||||
stateUpdated.andCall(function(stage) {
|
||||
stateUpdated.mockImplementation(function(stage) {
|
||||
expect(stage).toEqual("logintype");
|
||||
expect(ia.getSessionId()).toEqual("sessionId");
|
||||
expect(ia.getStageParams("logintype")).toEqual({
|
||||
@@ -132,7 +125,7 @@ describe("InteractiveAuth", function() {
|
||||
});
|
||||
|
||||
// submitAuthDict should trigger another call to doRequest
|
||||
doRequest.andCall(function(authData) {
|
||||
doRequest.mockImplementation(function(authData) {
|
||||
logger.log("request2", authData);
|
||||
expect(authData).toEqual({
|
||||
session: "sessionId",
|
||||
@@ -148,10 +141,10 @@ describe("InteractiveAuth", function() {
|
||||
});
|
||||
});
|
||||
|
||||
ia.attemptAuth().then(function(res) {
|
||||
return ia.attemptAuth().then(function(res) {
|
||||
expect(res).toBe(requestRes);
|
||||
expect(doRequest.calls.length).toEqual(2);
|
||||
expect(stateUpdated.calls.length).toEqual(1);
|
||||
}).nodeify(done);
|
||||
expect(doRequest).toBeCalledTimes(2);
|
||||
expect(stateUpdated).toBeCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import expect from 'expect';
|
||||
import TestClient from '../TestClient';
|
||||
|
||||
describe('Login request', function() {
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
"use strict";
|
||||
import 'source-map-support/register';
|
||||
import Promise from 'bluebird';
|
||||
const sdk = require("../..");
|
||||
const MatrixClient = sdk.MatrixClient;
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
import lolex from 'lolex';
|
||||
import logger from '../../src/logger';
|
||||
import logger from '../../lib/logger';
|
||||
|
||||
jest.useFakeTimers();
|
||||
|
||||
describe("MatrixClient", function() {
|
||||
const userId = "@alice:bar";
|
||||
@@ -16,7 +14,6 @@ describe("MatrixClient", function() {
|
||||
let client;
|
||||
let store;
|
||||
let scheduler;
|
||||
let clock;
|
||||
|
||||
const KEEP_ALIVE_PATH = "/_matrix/client/versions";
|
||||
|
||||
@@ -85,7 +82,7 @@ describe("MatrixClient", function() {
|
||||
);
|
||||
}
|
||||
pendingLookup = {
|
||||
promise: Promise.defer().promise,
|
||||
promise: new Promise(() => {}),
|
||||
method: method,
|
||||
path: path,
|
||||
};
|
||||
@@ -121,28 +118,26 @@ describe("MatrixClient", function() {
|
||||
return Promise.resolve(next.data);
|
||||
}
|
||||
expect(true).toBe(false, "Expected different request. " + logLine);
|
||||
return Promise.defer().promise;
|
||||
return new Promise(() => {});
|
||||
}
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
clock = lolex.install();
|
||||
scheduler = [
|
||||
"getQueueForEvent", "queueEvent", "removeEventFromQueue",
|
||||
"setProcessFunction",
|
||||
].reduce((r, k) => { r[k] = expect.createSpy(); return r; }, {});
|
||||
].reduce((r, k) => { r[k] = jest.fn(); return r; }, {});
|
||||
store = [
|
||||
"getRoom", "getRooms", "getUser", "getSyncToken", "scrollback",
|
||||
"save", "wantsSave", "setSyncToken", "storeEvents", "storeRoom", "storeUser",
|
||||
"getFilterIdByName", "setFilterIdByName", "getFilter", "storeFilter",
|
||||
"getSyncAccumulator", "startup", "deleteAllData",
|
||||
].reduce((r, k) => { r[k] = expect.createSpy(); return r; }, {});
|
||||
store.getSavedSync = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
store.getSavedSyncToken = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
store.setSyncData = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
store.getClientOptions = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
store.storeClientOptions = expect.createSpy().andReturn(Promise.resolve(null));
|
||||
store.isNewlyCreated = expect.createSpy().andReturn(Promise.resolve(true));
|
||||
].reduce((r, k) => { r[k] = jest.fn(); return r; }, {});
|
||||
store.getSavedSync = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
store.getSavedSyncToken = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
store.setSyncData = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
store.getClientOptions = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
store.storeClientOptions = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||
store.isNewlyCreated = jest.fn().mockReturnValue(Promise.resolve(true));
|
||||
client = new MatrixClient({
|
||||
baseUrl: "https://my.home.server",
|
||||
idBaseUrl: identityServerUrl,
|
||||
@@ -155,9 +150,9 @@ describe("MatrixClient", function() {
|
||||
// FIXME: We shouldn't be yanking _http like this.
|
||||
client._http = [
|
||||
"authedRequest", "getContentUri", "request", "uploadContent",
|
||||
].reduce((r, k) => { r[k] = expect.createSpy(); return r; }, {});
|
||||
client._http.authedRequest.andCall(httpReq);
|
||||
client._http.request.andCall(httpReq);
|
||||
].reduce((r, k) => { r[k] = jest.fn(); return r; }, {});
|
||||
client._http.authedRequest.mockImplementation(httpReq);
|
||||
client._http.request.mockImplementation(httpReq);
|
||||
|
||||
// set reasonable working defaults
|
||||
acceptKeepalives = true;
|
||||
@@ -169,14 +164,13 @@ describe("MatrixClient", function() {
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
clock.uninstall();
|
||||
// need to re-stub the requests with NOPs because there are no guarantees
|
||||
// clients from previous tests will be GC'd before the next test. This
|
||||
// means they may call /events and then fail an expect() which will fail
|
||||
// a DIFFERENT test (pollution between tests!) - we return unresolved
|
||||
// promises to stop the client from continuing to run.
|
||||
client._http.authedRequest.andCall(function() {
|
||||
return Promise.defer().promise;
|
||||
client._http.authedRequest.mockImplementation(function() {
|
||||
return new Promise(() => {});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -185,10 +179,10 @@ describe("MatrixClient", function() {
|
||||
httpLookups.push(PUSH_RULES_RESPONSE);
|
||||
httpLookups.push(SYNC_RESPONSE);
|
||||
const filterId = "ehfewf";
|
||||
store.getFilterIdByName.andReturn(filterId);
|
||||
store.getFilterIdByName.mockReturnValue(filterId);
|
||||
const filter = new sdk.Filter(0, filterId);
|
||||
filter.setDefinition({"room": {"timeline": {"limit": 8}}});
|
||||
store.getFilter.andReturn(filter);
|
||||
store.getFilter.mockReturnValue(filter);
|
||||
const syncPromise = new Promise((resolve, reject) => {
|
||||
client.on("sync", function syncListener(state) {
|
||||
if (state === "SYNCING") {
|
||||
@@ -249,7 +243,7 @@ describe("MatrixClient", function() {
|
||||
},
|
||||
});
|
||||
httpLookups.push(FILTER_RESPONSE);
|
||||
store.getFilterIdByName.andReturn(invalidFilterId);
|
||||
store.getFilterIdByName.mockReturnValue(invalidFilterId);
|
||||
|
||||
const filterName = getFilterName(client.credentials.userId);
|
||||
client.store.setFilterIdByName(filterName, invalidFilterId);
|
||||
@@ -281,7 +275,7 @@ describe("MatrixClient", function() {
|
||||
if (state === "ERROR" && httpLookups.length > 0) {
|
||||
expect(httpLookups.length).toEqual(2);
|
||||
expect(client.retryImmediately()).toBe(true);
|
||||
clock.tick(1);
|
||||
jest.advanceTimersByTime(1);
|
||||
} else if (state === "PREPARED" && httpLookups.length === 0) {
|
||||
client.removeListener("sync", syncListener);
|
||||
done();
|
||||
@@ -307,9 +301,9 @@ describe("MatrixClient", function() {
|
||||
expect(client.retryImmediately()).toBe(
|
||||
true, "retryImmediately returned false",
|
||||
);
|
||||
clock.tick(1);
|
||||
jest.advanceTimersByTime(1);
|
||||
} else if (state === "RECONNECTING" && httpLookups.length > 0) {
|
||||
clock.tick(10000);
|
||||
jest.advanceTimersByTime(10000);
|
||||
} else if (state === "SYNCING" && httpLookups.length === 0) {
|
||||
client.removeListener("sync", syncListener);
|
||||
done();
|
||||
@@ -331,7 +325,7 @@ describe("MatrixClient", function() {
|
||||
if (state === "ERROR" && httpLookups.length > 0) {
|
||||
expect(httpLookups.length).toEqual(3);
|
||||
expect(client.retryImmediately()).toBe(true);
|
||||
clock.tick(1);
|
||||
jest.advanceTimersByTime(1);
|
||||
} else if (state === "PREPARED" && httpLookups.length === 0) {
|
||||
client.removeListener("sync", syncListener);
|
||||
done();
|
||||
@@ -362,7 +356,7 @@ describe("MatrixClient", function() {
|
||||
done();
|
||||
}
|
||||
// standard retry time is 5 to 10 seconds
|
||||
clock.tick(10000);
|
||||
jest.advanceTimersByTime(10000);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,6 @@ import 'source-map-support/register';
|
||||
const PushProcessor = require("../../lib/pushprocessor");
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe('NotificationService', function() {
|
||||
const testUserId = "@ali:matrix.org";
|
||||
const testDisplayName = "Alice M";
|
||||
|
||||
@@ -1,53 +1,46 @@
|
||||
"use strict";
|
||||
|
||||
import 'source-map-support/register';
|
||||
const callbacks = require("../../lib/realtime-callbacks");
|
||||
const testUtils = require("../test-utils.js");
|
||||
const callbacks = require("../../src/realtime-callbacks");
|
||||
|
||||
import expect from 'expect';
|
||||
import lolex from 'lolex';
|
||||
let wallTime = 1234567890;
|
||||
jest.useFakeTimers();
|
||||
|
||||
describe("realtime-callbacks", function() {
|
||||
let clock;
|
||||
|
||||
function tick(millis) {
|
||||
clock.tick(millis);
|
||||
wallTime += millis;
|
||||
jest.advanceTimersByTime(millis);
|
||||
}
|
||||
|
||||
beforeEach(function() {
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
clock = lolex.install();
|
||||
const fakeDate = clock.Date;
|
||||
callbacks.setNow(fakeDate.now.bind(fakeDate));
|
||||
callbacks.setNow(() => wallTime);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
callbacks.setNow();
|
||||
clock.uninstall();
|
||||
});
|
||||
|
||||
describe("setTimeout", function() {
|
||||
it("should call the callback after the timeout", function() {
|
||||
const callback = expect.createSpy();
|
||||
const callback = jest.fn();
|
||||
callbacks.setTimeout(callback, 100);
|
||||
|
||||
expect(callback).toNotHaveBeenCalled();
|
||||
expect(callback).not.toHaveBeenCalled();
|
||||
tick(100);
|
||||
expect(callback).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
it("should default to a zero timeout", function() {
|
||||
const callback = expect.createSpy();
|
||||
const callback = jest.fn();
|
||||
callbacks.setTimeout(callback);
|
||||
|
||||
expect(callback).toNotHaveBeenCalled();
|
||||
expect(callback).not.toHaveBeenCalled();
|
||||
tick(0);
|
||||
expect(callback).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should pass any parameters to the callback", function() {
|
||||
const callback = expect.createSpy();
|
||||
const callback = jest.fn();
|
||||
callbacks.setTimeout(callback, 0, "a", "b", "c");
|
||||
tick(0);
|
||||
expect(callback).toHaveBeenCalledWith("a", "b", "c");
|
||||
@@ -66,10 +59,10 @@ describe("realtime-callbacks", function() {
|
||||
});
|
||||
|
||||
it("should handle timeouts of several seconds", function() {
|
||||
const callback = expect.createSpy();
|
||||
const callback = jest.fn();
|
||||
callbacks.setTimeout(callback, 2000);
|
||||
|
||||
expect(callback).toNotHaveBeenCalled();
|
||||
expect(callback).not.toHaveBeenCalled();
|
||||
for (let i = 0; i < 4; i++) {
|
||||
tick(500);
|
||||
}
|
||||
@@ -77,24 +70,24 @@ describe("realtime-callbacks", function() {
|
||||
});
|
||||
|
||||
it("should call multiple callbacks in the right order", function() {
|
||||
const callback1 = expect.createSpy();
|
||||
const callback2 = expect.createSpy();
|
||||
const callback3 = expect.createSpy();
|
||||
const callback1 = jest.fn();
|
||||
const callback2 = jest.fn();
|
||||
const callback3 = jest.fn();
|
||||
callbacks.setTimeout(callback2, 200);
|
||||
callbacks.setTimeout(callback1, 100);
|
||||
callbacks.setTimeout(callback3, 300);
|
||||
|
||||
expect(callback1).toNotHaveBeenCalled();
|
||||
expect(callback2).toNotHaveBeenCalled();
|
||||
expect(callback3).toNotHaveBeenCalled();
|
||||
expect(callback1).not.toHaveBeenCalled();
|
||||
expect(callback2).not.toHaveBeenCalled();
|
||||
expect(callback3).not.toHaveBeenCalled();
|
||||
tick(100);
|
||||
expect(callback1).toHaveBeenCalled();
|
||||
expect(callback2).toNotHaveBeenCalled();
|
||||
expect(callback3).toNotHaveBeenCalled();
|
||||
expect(callback2).not.toHaveBeenCalled();
|
||||
expect(callback3).not.toHaveBeenCalled();
|
||||
tick(100);
|
||||
expect(callback1).toHaveBeenCalled();
|
||||
expect(callback2).toHaveBeenCalled();
|
||||
expect(callback3).toNotHaveBeenCalled();
|
||||
expect(callback3).not.toHaveBeenCalled();
|
||||
tick(100);
|
||||
expect(callback1).toHaveBeenCalled();
|
||||
expect(callback2).toHaveBeenCalled();
|
||||
@@ -102,35 +95,34 @@ describe("realtime-callbacks", function() {
|
||||
});
|
||||
|
||||
it("should treat -ve timeouts the same as a zero timeout", function() {
|
||||
const callback1 = expect.createSpy();
|
||||
const callback2 = expect.createSpy();
|
||||
const callback1 = jest.fn();
|
||||
const callback2 = jest.fn();
|
||||
|
||||
// check that cb1 is called before cb2
|
||||
callback1.andCall(function() {
|
||||
expect(callback2).toNotHaveBeenCalled();
|
||||
callback1.mockImplementation(function() {
|
||||
expect(callback2).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
callbacks.setTimeout(callback1);
|
||||
callbacks.setTimeout(callback2, -100);
|
||||
|
||||
expect(callback1).toNotHaveBeenCalled();
|
||||
expect(callback2).toNotHaveBeenCalled();
|
||||
expect(callback1).not.toHaveBeenCalled();
|
||||
expect(callback2).not.toHaveBeenCalled();
|
||||
tick(0);
|
||||
expect(callback1).toHaveBeenCalled();
|
||||
expect(callback2).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not get confused by chained calls", function() {
|
||||
const callback2 = expect.createSpy();
|
||||
const callback1 = expect.createSpy();
|
||||
callback1.andCall(function() {
|
||||
const callback2 = jest.fn();
|
||||
const callback1 = jest.fn(function() {
|
||||
callbacks.setTimeout(callback2, 0);
|
||||
expect(callback2).toNotHaveBeenCalled();
|
||||
expect(callback2).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
callbacks.setTimeout(callback1);
|
||||
expect(callback1).toNotHaveBeenCalled();
|
||||
expect(callback2).toNotHaveBeenCalled();
|
||||
expect(callback1).not.toHaveBeenCalled();
|
||||
expect(callback2).not.toHaveBeenCalled();
|
||||
tick(0);
|
||||
expect(callback1).toHaveBeenCalled();
|
||||
// the fake timer won't actually run callbacks registered during
|
||||
@@ -140,16 +132,15 @@ describe("realtime-callbacks", function() {
|
||||
});
|
||||
|
||||
it("should be immune to exceptions", function() {
|
||||
const callback1 = expect.createSpy();
|
||||
callback1.andCall(function() {
|
||||
const callback1 = jest.fn(function() {
|
||||
throw new Error("prepare to die");
|
||||
});
|
||||
const callback2 = expect.createSpy();
|
||||
const callback2 = jest.fn();
|
||||
callbacks.setTimeout(callback1, 0);
|
||||
callbacks.setTimeout(callback2, 0);
|
||||
|
||||
expect(callback1).toNotHaveBeenCalled();
|
||||
expect(callback2).toNotHaveBeenCalled();
|
||||
expect(callback1).not.toHaveBeenCalled();
|
||||
expect(callback2).not.toHaveBeenCalled();
|
||||
tick(0);
|
||||
expect(callback1).toHaveBeenCalled();
|
||||
expect(callback2).toHaveBeenCalled();
|
||||
@@ -158,16 +149,16 @@ describe("realtime-callbacks", function() {
|
||||
|
||||
describe("cancelTimeout", function() {
|
||||
it("should cancel a pending timeout", function() {
|
||||
const callback = expect.createSpy();
|
||||
const callback = jest.fn();
|
||||
const k = callbacks.setTimeout(callback);
|
||||
callbacks.clearTimeout(k);
|
||||
tick(0);
|
||||
expect(callback).toNotHaveBeenCalled();
|
||||
expect(callback).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not affect sooner timeouts", function() {
|
||||
const callback1 = expect.createSpy();
|
||||
const callback2 = expect.createSpy();
|
||||
const callback1 = jest.fn();
|
||||
const callback2 = jest.fn();
|
||||
|
||||
callbacks.setTimeout(callback1, 100);
|
||||
const k = callbacks.setTimeout(callback2, 200);
|
||||
@@ -175,10 +166,10 @@ describe("realtime-callbacks", function() {
|
||||
|
||||
tick(100);
|
||||
expect(callback1).toHaveBeenCalled();
|
||||
expect(callback2).toNotHaveBeenCalled();
|
||||
expect(callback2).not.toHaveBeenCalled();
|
||||
|
||||
tick(150);
|
||||
expect(callback2).toNotHaveBeenCalled();
|
||||
expect(callback2).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,8 +4,6 @@ const sdk = require("../..");
|
||||
const RoomMember = sdk.RoomMember;
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("RoomMember", function() {
|
||||
const roomId = "!foo:bar";
|
||||
const userA = "@alice:bar";
|
||||
@@ -14,7 +12,6 @@ describe("RoomMember", function() {
|
||||
let member;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
member = new RoomMember(roomId, userA);
|
||||
});
|
||||
|
||||
@@ -36,7 +33,7 @@ describe("RoomMember", function() {
|
||||
const url = member.getAvatarUrl(hsUrl);
|
||||
// we don't care about how the mxc->http conversion is done, other
|
||||
// than it contains the mxc body.
|
||||
expect(url.indexOf("flibble/wibble")).toNotEqual(-1);
|
||||
expect(url.indexOf("flibble/wibble")).not.toEqual(-1);
|
||||
});
|
||||
|
||||
it("should return an identicon HTTP URL if allowDefault was set and there " +
|
||||
@@ -255,9 +252,9 @@ describe("RoomMember", function() {
|
||||
member.setMembershipEvent(joinEvent);
|
||||
expect(member.name).toEqual("Alice"); // prefer displayname
|
||||
member.setMembershipEvent(joinEvent, roomState);
|
||||
expect(member.name).toNotEqual("Alice"); // it should disambig.
|
||||
expect(member.name).not.toEqual("Alice"); // it should disambig.
|
||||
// user_id should be there somewhere
|
||||
expect(member.name.indexOf(userA)).toNotEqual(-1);
|
||||
expect(member.name.indexOf(userA)).not.toEqual(-1);
|
||||
});
|
||||
|
||||
it("should emit 'RoomMember.membership' if the membership changes", function() {
|
||||
@@ -328,9 +325,9 @@ describe("RoomMember", function() {
|
||||
};
|
||||
expect(member.name).toEqual(userA); // default = user_id
|
||||
member.setMembershipEvent(joinEvent, roomState);
|
||||
expect(member.name).toNotEqual("Alíce"); // it should disambig.
|
||||
expect(member.name).not.toEqual("Alíce"); // it should disambig.
|
||||
// user_id should be there somewhere
|
||||
expect(member.name.indexOf(userA)).toNotEqual(-1);
|
||||
expect(member.name.indexOf(userA)).not.toEqual(-1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,8 +5,6 @@ const RoomState = sdk.RoomState;
|
||||
const RoomMember = sdk.RoomMember;
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("RoomState", function() {
|
||||
const roomId = "!foo:bar";
|
||||
const userA = "@alice:bar";
|
||||
@@ -17,7 +15,6 @@ describe("RoomState", function() {
|
||||
let state;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
state = new RoomState(roomId);
|
||||
state.setStateEvents([
|
||||
utils.mkMembership({ // userA joined
|
||||
@@ -49,8 +46,8 @@ describe("RoomState", function() {
|
||||
const members = state.getMembers();
|
||||
expect(members.length).toEqual(2);
|
||||
// ordering unimportant
|
||||
expect([userA, userB].indexOf(members[0].userId)).toNotEqual(-1);
|
||||
expect([userA, userB].indexOf(members[1].userId)).toNotEqual(-1);
|
||||
expect([userA, userB].indexOf(members[0].userId)).not.toEqual(-1);
|
||||
expect([userA, userB].indexOf(members[1].userId)).not.toEqual(-1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -120,8 +117,8 @@ describe("RoomState", function() {
|
||||
const events = state.getStateEvents("m.room.member");
|
||||
expect(events.length).toEqual(2);
|
||||
// ordering unimportant
|
||||
expect([userA, userB].indexOf(events[0].getStateKey())).toNotEqual(-1);
|
||||
expect([userA, userB].indexOf(events[1].getStateKey())).toNotEqual(-1);
|
||||
expect([userA, userB].indexOf(events[0].getStateKey())).not.toEqual(-1);
|
||||
expect([userA, userB].indexOf(events[1].getStateKey())).not.toEqual(-1);
|
||||
});
|
||||
|
||||
it("should return a single MatrixEvent if a state_key was specified",
|
||||
@@ -258,7 +255,7 @@ describe("RoomState", function() {
|
||||
});
|
||||
state.setStateEvents([memberEvent]);
|
||||
|
||||
expect(state.members[userA].setMembershipEvent).toNotHaveBeenCalled();
|
||||
expect(state.members[userA].setMembershipEvent).not.toHaveBeenCalled();
|
||||
expect(state.members[userB].setMembershipEvent).toHaveBeenCalledWith(
|
||||
memberEvent, state,
|
||||
);
|
||||
@@ -306,7 +303,7 @@ describe("RoomState", function() {
|
||||
state.markOutOfBandMembersStarted();
|
||||
state.setOutOfBandMembers([oobMemberEvent]);
|
||||
const memberA = state.getMember(userA);
|
||||
expect(memberA.events.member.getId()).toNotEqual(oobMemberEvent.getId());
|
||||
expect(memberA.events.member.getId()).not.toEqual(oobMemberEvent.getId());
|
||||
expect(memberA.isOutOfBand()).toEqual(false);
|
||||
});
|
||||
|
||||
|
||||
+32
-33
@@ -8,8 +8,6 @@ const EventStatus = sdk.EventStatus;
|
||||
const EventTimeline = sdk.EventTimeline;
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("Room", function() {
|
||||
const roomId = "!foo:bar";
|
||||
const userA = "@alice:bar";
|
||||
@@ -19,7 +17,6 @@ describe("Room", function() {
|
||||
let room;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
room = new Room(roomId);
|
||||
// mock RoomStates
|
||||
room.oldState = room.getLiveTimeline()._startState =
|
||||
@@ -32,7 +29,7 @@ describe("Room", function() {
|
||||
const hsUrl = "https://my.home.server";
|
||||
|
||||
it("should return the URL from m.room.avatar preferentially", function() {
|
||||
room.currentState.getStateEvents.andCall(function(type, key) {
|
||||
room.currentState.getStateEvents.mockImplementation(function(type, key) {
|
||||
if (type === "m.room.avatar" && key === "") {
|
||||
return utils.mkEvent({
|
||||
event: true,
|
||||
@@ -49,7 +46,7 @@ describe("Room", function() {
|
||||
const url = room.getAvatarUrl(hsUrl);
|
||||
// we don't care about how the mxc->http conversion is done, other
|
||||
// than it contains the mxc body.
|
||||
expect(url.indexOf("flibble/wibble")).toNotEqual(-1);
|
||||
expect(url.indexOf("flibble/wibble")).not.toEqual(-1);
|
||||
});
|
||||
|
||||
it("should return an identicon HTTP URL if allowDefault was set and there " +
|
||||
@@ -67,13 +64,13 @@ describe("Room", function() {
|
||||
|
||||
describe("getMember", function() {
|
||||
beforeEach(function() {
|
||||
room.currentState.getMember.andCall(function(userId) {
|
||||
room.currentState.getMember.mockImplementation(function(userId) {
|
||||
return {
|
||||
"@alice:bar": {
|
||||
userId: userA,
|
||||
roomId: roomId,
|
||||
},
|
||||
}[userId];
|
||||
}[userId] || null;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -82,7 +79,7 @@ describe("Room", function() {
|
||||
});
|
||||
|
||||
it("should return the member from current state", function() {
|
||||
expect(room.getMember(userA)).toNotEqual(null);
|
||||
expect(room.getMember(userA)).not.toEqual(null);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -174,7 +171,7 @@ describe("Room", function() {
|
||||
);
|
||||
expect(events[0].forwardLooking).toBe(true);
|
||||
expect(events[1].forwardLooking).toBe(true);
|
||||
expect(room.oldState.setStateEvents).toNotHaveBeenCalled();
|
||||
expect(room.oldState.setStateEvents).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should synthesize read receipts for the senders of events", function() {
|
||||
@@ -183,7 +180,7 @@ describe("Room", function() {
|
||||
membership: "join",
|
||||
name: "Alice",
|
||||
};
|
||||
room.currentState.getSentinelMember.andCall(function(uid) {
|
||||
room.currentState.getSentinelMember.mockImplementation(function(uid) {
|
||||
if (uid === userA) {
|
||||
return sentinel;
|
||||
}
|
||||
@@ -292,13 +289,13 @@ describe("Room", function() {
|
||||
membership: "join",
|
||||
name: "Old Alice",
|
||||
};
|
||||
room.currentState.getSentinelMember.andCall(function(uid) {
|
||||
room.currentState.getSentinelMember.mockImplementation(function(uid) {
|
||||
if (uid === userA) {
|
||||
return sentinel;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
room.oldState.getSentinelMember.andCall(function(uid) {
|
||||
room.oldState.getSentinelMember.mockImplementation(function(uid) {
|
||||
if (uid === userA) {
|
||||
return oldSentinel;
|
||||
}
|
||||
@@ -331,13 +328,13 @@ describe("Room", function() {
|
||||
membership: "join",
|
||||
name: "Old Alice",
|
||||
};
|
||||
room.currentState.getSentinelMember.andCall(function(uid) {
|
||||
room.currentState.getSentinelMember.mockImplementation(function(uid) {
|
||||
if (uid === userA) {
|
||||
return sentinel;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
room.oldState.getSentinelMember.andCall(function(uid) {
|
||||
room.oldState.getSentinelMember.mockImplementation(function(uid) {
|
||||
if (uid === userA) {
|
||||
return oldSentinel;
|
||||
}
|
||||
@@ -379,7 +376,7 @@ describe("Room", function() {
|
||||
);
|
||||
expect(events[0].forwardLooking).toBe(false);
|
||||
expect(events[1].forwardLooking).toBe(false);
|
||||
expect(room.currentState.setStateEvents).toNotHaveBeenCalled();
|
||||
expect(room.currentState.setStateEvents).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -545,7 +542,7 @@ describe("Room", function() {
|
||||
|
||||
describe("getJoinedMembers", function() {
|
||||
it("should return members whose membership is 'join'", function() {
|
||||
room.currentState.getMembers.andCall(function() {
|
||||
room.currentState.getMembers.mockImplementation(function() {
|
||||
return [
|
||||
{ userId: "@alice:bar", membership: "join" },
|
||||
{ userId: "@bob:bar", membership: "invite" },
|
||||
@@ -558,7 +555,7 @@ describe("Room", function() {
|
||||
});
|
||||
|
||||
it("should return an empty list if no membership is 'join'", function() {
|
||||
room.currentState.getMembers.andCall(function() {
|
||||
room.currentState.getMembers.mockImplementation(function() {
|
||||
return [
|
||||
{ userId: "@bob:bar", membership: "invite" },
|
||||
];
|
||||
@@ -571,7 +568,7 @@ describe("Room", function() {
|
||||
describe("hasMembershipState", function() {
|
||||
it("should return true for a matching userId and membership",
|
||||
function() {
|
||||
room.currentState.getMember.andCall(function(userId) {
|
||||
room.currentState.getMember.mockImplementation(function(userId) {
|
||||
return {
|
||||
"@alice:bar": { userId: "@alice:bar", membership: "join" },
|
||||
"@bob:bar": { userId: "@bob:bar", membership: "invite" },
|
||||
@@ -582,7 +579,7 @@ describe("Room", function() {
|
||||
|
||||
it("should return false if match membership but no match userId",
|
||||
function() {
|
||||
room.currentState.getMember.andCall(function(userId) {
|
||||
room.currentState.getMember.mockImplementation(function(userId) {
|
||||
return {
|
||||
"@alice:bar": { userId: "@alice:bar", membership: "join" },
|
||||
}[userId];
|
||||
@@ -592,7 +589,7 @@ describe("Room", function() {
|
||||
|
||||
it("should return false if match userId but no match membership",
|
||||
function() {
|
||||
room.currentState.getMember.andCall(function(userId) {
|
||||
room.currentState.getMember.mockImplementation(function(userId) {
|
||||
return {
|
||||
"@alice:bar": { userId: "@alice:bar", membership: "join" },
|
||||
}[userId];
|
||||
@@ -602,7 +599,7 @@ describe("Room", function() {
|
||||
|
||||
it("should return false if no match membership or userId",
|
||||
function() {
|
||||
room.currentState.getMember.andCall(function(userId) {
|
||||
room.currentState.getMember.mockImplementation(function(userId) {
|
||||
return {
|
||||
"@alice:bar": { userId: "@alice:bar", membership: "join" },
|
||||
}[userId];
|
||||
@@ -626,7 +623,9 @@ describe("Room", function() {
|
||||
};
|
||||
const setAliases = function(aliases, stateKey) {
|
||||
if (!stateKey) {
|
||||
stateKey = "flibble";
|
||||
stateKey = aliases.length
|
||||
? aliases[0].split(':').splice(1).join(':') // domain+port
|
||||
: 'fibble';
|
||||
}
|
||||
room.addLiveEvents([utils.mkEvent({
|
||||
type: "m.room.aliases", room: roomId, skey: stateKey, content: {
|
||||
@@ -814,8 +813,8 @@ describe("Room", function() {
|
||||
addMember(userC);
|
||||
room.recalculate();
|
||||
const name = room.name;
|
||||
expect(name.indexOf(userB)).toNotEqual(-1, name);
|
||||
expect(name.indexOf(userC)).toNotEqual(-1, name);
|
||||
expect(name.indexOf(userB)).not.toEqual(-1, name);
|
||||
expect(name.indexOf(userC)).not.toEqual(-1, name);
|
||||
});
|
||||
|
||||
it("should return the names of members in a public (public join_rules)" +
|
||||
@@ -827,8 +826,8 @@ describe("Room", function() {
|
||||
addMember(userC);
|
||||
room.recalculate();
|
||||
const name = room.name;
|
||||
expect(name.indexOf(userB)).toNotEqual(-1, name);
|
||||
expect(name.indexOf(userC)).toNotEqual(-1, name);
|
||||
expect(name.indexOf(userB)).not.toEqual(-1, name);
|
||||
expect(name.indexOf(userC)).not.toEqual(-1, name);
|
||||
});
|
||||
|
||||
it("should show the other user's name for public (public join_rules)" +
|
||||
@@ -839,7 +838,7 @@ describe("Room", function() {
|
||||
addMember(userB);
|
||||
room.recalculate();
|
||||
const name = room.name;
|
||||
expect(name.indexOf(userB)).toNotEqual(-1, name);
|
||||
expect(name.indexOf(userB)).not.toEqual(-1, name);
|
||||
});
|
||||
|
||||
it("should show the other user's name for private " +
|
||||
@@ -850,7 +849,7 @@ describe("Room", function() {
|
||||
addMember(userB);
|
||||
room.recalculate();
|
||||
const name = room.name;
|
||||
expect(name.indexOf(userB)).toNotEqual(-1, name);
|
||||
expect(name.indexOf(userB)).not.toEqual(-1, name);
|
||||
});
|
||||
|
||||
it("should show the other user's name for private" +
|
||||
@@ -860,14 +859,14 @@ describe("Room", function() {
|
||||
addMember(userB);
|
||||
room.recalculate();
|
||||
const name = room.name;
|
||||
expect(name.indexOf(userB)).toNotEqual(-1, name);
|
||||
expect(name.indexOf(userB)).not.toEqual(-1, name);
|
||||
});
|
||||
|
||||
it("should show the room alias if one exists for private " +
|
||||
"(invite join_rules) rooms if a room name doesn't exist.", function() {
|
||||
const alias = "#room_alias:here";
|
||||
setJoinRule("invite");
|
||||
setAliases([alias, "#another:one"]);
|
||||
setAliases([alias, "#another:here"]);
|
||||
room.recalculate();
|
||||
const name = room.name;
|
||||
expect(name).toEqual(alias);
|
||||
@@ -877,7 +876,7 @@ describe("Room", function() {
|
||||
"(public join_rules) rooms if a room name doesn't exist.", function() {
|
||||
const alias = "#room_alias:here";
|
||||
setJoinRule("public");
|
||||
setAliases([alias, "#another:one"]);
|
||||
setAliases([alias, "#another:here"]);
|
||||
room.recalculate();
|
||||
const name = room.name;
|
||||
expect(name).toEqual(alias);
|
||||
@@ -1004,7 +1003,7 @@ describe("Room", function() {
|
||||
|
||||
it("should emit an event when a receipt is added",
|
||||
function() {
|
||||
const listener = expect.createSpy();
|
||||
const listener = jest.fn();
|
||||
room.on("Room.receipt", listener);
|
||||
|
||||
const ts = 13787898424;
|
||||
@@ -1175,7 +1174,7 @@ describe("Room", function() {
|
||||
it("should emit Room.tags event when new tags are " +
|
||||
"received on the event stream",
|
||||
function() {
|
||||
const listener = expect.createSpy();
|
||||
const listener = jest.fn();
|
||||
room.on("Room.tags", listener);
|
||||
|
||||
const tags = { "m.foo": { "order": 0.5 } };
|
||||
|
||||
+18
-26
@@ -2,21 +2,19 @@
|
||||
/* eslint new-cap: "off" */
|
||||
|
||||
import 'source-map-support/register';
|
||||
import Promise from 'bluebird';
|
||||
import {defer} from '../../src/utils';
|
||||
const sdk = require("../..");
|
||||
const MatrixScheduler = sdk.MatrixScheduler;
|
||||
const MatrixError = sdk.MatrixError;
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
import lolex from 'lolex';
|
||||
jest.useFakeTimers();
|
||||
|
||||
describe("MatrixScheduler", function() {
|
||||
let clock;
|
||||
let scheduler;
|
||||
let retryFn;
|
||||
let queueFn;
|
||||
let defer;
|
||||
let deferred;
|
||||
const roomId = "!foo:bar";
|
||||
const eventA = utils.mkMessage({
|
||||
user: "@alice:bar", room: roomId, event: true,
|
||||
@@ -26,8 +24,6 @@ describe("MatrixScheduler", function() {
|
||||
});
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
clock = lolex.install();
|
||||
scheduler = new MatrixScheduler(function(ev, attempts, err) {
|
||||
if (retryFn) {
|
||||
return retryFn(ev, attempts, err);
|
||||
@@ -41,11 +37,7 @@ describe("MatrixScheduler", function() {
|
||||
});
|
||||
retryFn = null;
|
||||
queueFn = null;
|
||||
defer = Promise.defer();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
clock.uninstall();
|
||||
deferred = defer();
|
||||
});
|
||||
|
||||
it("should process events in a queue in a FIFO manner", async function() {
|
||||
@@ -55,8 +47,8 @@ describe("MatrixScheduler", function() {
|
||||
queueFn = function() {
|
||||
return "one_big_queue";
|
||||
};
|
||||
const deferA = Promise.defer();
|
||||
const deferB = Promise.defer();
|
||||
const deferA = defer();
|
||||
const deferB = defer();
|
||||
let yieldedA = false;
|
||||
scheduler.setProcessFunction(function(event) {
|
||||
if (yieldedA) {
|
||||
@@ -82,7 +74,7 @@ describe("MatrixScheduler", function() {
|
||||
it("should invoke the retryFn on failure and wait the amount of time specified",
|
||||
async function() {
|
||||
const waitTimeMs = 1500;
|
||||
const retryDefer = Promise.defer();
|
||||
const retryDefer = defer();
|
||||
retryFn = function() {
|
||||
retryDefer.resolve();
|
||||
return waitTimeMs;
|
||||
@@ -96,9 +88,9 @@ describe("MatrixScheduler", function() {
|
||||
procCount += 1;
|
||||
if (procCount === 1) {
|
||||
expect(ev).toEqual(eventA);
|
||||
return defer.promise;
|
||||
return deferred.promise;
|
||||
} else if (procCount === 2) {
|
||||
// don't care about this defer
|
||||
// don't care about this deferred
|
||||
return new Promise();
|
||||
}
|
||||
expect(procCount).toBeLessThan(3);
|
||||
@@ -109,10 +101,10 @@ describe("MatrixScheduler", function() {
|
||||
// wait just long enough before it does
|
||||
await Promise.resolve();
|
||||
expect(procCount).toEqual(1);
|
||||
defer.reject({});
|
||||
deferred.reject({});
|
||||
await retryDefer.promise;
|
||||
expect(procCount).toEqual(1);
|
||||
clock.tick(waitTimeMs);
|
||||
jest.advanceTimersByTime(waitTimeMs);
|
||||
await Promise.resolve();
|
||||
expect(procCount).toEqual(2);
|
||||
});
|
||||
@@ -129,8 +121,8 @@ describe("MatrixScheduler", function() {
|
||||
return "yep";
|
||||
};
|
||||
|
||||
const deferA = Promise.defer();
|
||||
const deferB = Promise.defer();
|
||||
const deferA = defer();
|
||||
const deferB = defer();
|
||||
let procCount = 0;
|
||||
scheduler.setProcessFunction(function(ev) {
|
||||
procCount += 1;
|
||||
@@ -185,14 +177,14 @@ describe("MatrixScheduler", function() {
|
||||
const expectOrder = [
|
||||
eventA.getId(), eventB.getId(), eventD.getId(),
|
||||
];
|
||||
const deferA = Promise.defer();
|
||||
const deferA = defer();
|
||||
scheduler.setProcessFunction(function(event) {
|
||||
const id = expectOrder.shift();
|
||||
expect(id).toEqual(event.getId());
|
||||
if (expectOrder.length === 0) {
|
||||
done();
|
||||
}
|
||||
return id === eventA.getId() ? deferA.promise : defer.promise;
|
||||
return id === eventA.getId() ? deferA.promise : deferred.promise;
|
||||
});
|
||||
scheduler.queueEvent(eventA);
|
||||
scheduler.queueEvent(eventB);
|
||||
@@ -203,7 +195,7 @@ describe("MatrixScheduler", function() {
|
||||
setTimeout(function() {
|
||||
deferA.resolve({});
|
||||
}, 1000);
|
||||
clock.tick(1000);
|
||||
jest.advanceTimersByTime(1000);
|
||||
});
|
||||
|
||||
describe("queueEvent", function() {
|
||||
@@ -306,7 +298,7 @@ describe("MatrixScheduler", function() {
|
||||
scheduler.setProcessFunction(function(ev) {
|
||||
procCount += 1;
|
||||
expect(ev).toEqual(eventA);
|
||||
return defer.promise;
|
||||
return deferred.promise;
|
||||
});
|
||||
// as queueing doesn't start processing synchronously anymore (see commit bbdb5ac)
|
||||
// wait just long enough before it does
|
||||
@@ -322,7 +314,7 @@ describe("MatrixScheduler", function() {
|
||||
let procCount = 0;
|
||||
scheduler.setProcessFunction(function(ev) {
|
||||
procCount += 1;
|
||||
return defer.promise;
|
||||
return deferred.promise;
|
||||
});
|
||||
expect(procCount).toEqual(0);
|
||||
});
|
||||
|
||||
@@ -16,9 +16,7 @@ limitations under the License.
|
||||
|
||||
"use strict";
|
||||
import 'source-map-support/register';
|
||||
import utils from "../test-utils";
|
||||
import sdk from "../..";
|
||||
import expect from 'expect';
|
||||
|
||||
const SyncAccumulator = sdk.SyncAccumulator;
|
||||
|
||||
@@ -26,7 +24,6 @@ describe("SyncAccumulator", function() {
|
||||
let sa;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
sa = new SyncAccumulator({
|
||||
maxTimelineEntries: 10,
|
||||
});
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
"use strict";
|
||||
import 'source-map-support/register';
|
||||
import Promise from 'bluebird';
|
||||
const sdk = require("../..");
|
||||
const EventTimeline = sdk.EventTimeline;
|
||||
const TimelineWindow = sdk.TimelineWindow;
|
||||
const TimelineIndex = require("../../lib/timeline-window").TimelineIndex;
|
||||
|
||||
const utils = require("../test-utils");
|
||||
import expect from 'expect';
|
||||
|
||||
const ROOM_ID = "roomId";
|
||||
const USER_ID = "userId";
|
||||
@@ -67,10 +65,6 @@ function createLinkedTimelines() {
|
||||
|
||||
|
||||
describe("TimelineIndex", function() {
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
});
|
||||
|
||||
describe("minIndex", function() {
|
||||
it("should return the min index relative to BaseIndex", function() {
|
||||
const timelineIndex = new TimelineIndex(createTimeline(), 0);
|
||||
@@ -153,7 +147,7 @@ describe("TimelineWindow", function() {
|
||||
let timelineSet;
|
||||
let client;
|
||||
function createWindow(timeline, opts) {
|
||||
timelineSet = {};
|
||||
timelineSet = {getTimelineForEvent: () => null};
|
||||
client = {};
|
||||
client.getEventTimeline = function(timelineSet0, eventId0) {
|
||||
expect(timelineSet0).toBe(timelineSet);
|
||||
@@ -163,12 +157,8 @@ describe("TimelineWindow", function() {
|
||||
return new TimelineWindow(client, timelineSet, opts);
|
||||
}
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
});
|
||||
|
||||
describe("load", function() {
|
||||
it("should initialise from the live timeline", function(done) {
|
||||
it("should initialise from the live timeline", function() {
|
||||
const liveTimeline = createTimeline();
|
||||
const room = {};
|
||||
room.getLiveTimeline = function() {
|
||||
@@ -176,17 +166,17 @@ describe("TimelineWindow", function() {
|
||||
};
|
||||
|
||||
const timelineWindow = new TimelineWindow(undefined, room);
|
||||
timelineWindow.load(undefined, 2).then(function() {
|
||||
return timelineWindow.load(undefined, 2).then(function() {
|
||||
const expectedEvents = liveTimeline.getEvents().slice(1);
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
it("should initialise from a specific event", function(done) {
|
||||
it("should initialise from a specific event", function() {
|
||||
const timeline = createTimeline();
|
||||
const eventId = timeline.getEvents()[1].getId();
|
||||
|
||||
const timelineSet = {};
|
||||
const timelineSet = {getTimelineForEvent: () => null};
|
||||
const client = {};
|
||||
client.getEventTimeline = function(timelineSet0, eventId0) {
|
||||
expect(timelineSet0).toBe(timelineSet);
|
||||
@@ -195,21 +185,20 @@ describe("TimelineWindow", function() {
|
||||
};
|
||||
|
||||
const timelineWindow = new TimelineWindow(client, timelineSet);
|
||||
timelineWindow.load(eventId, 3).then(function() {
|
||||
return timelineWindow.load(eventId, 3).then(function() {
|
||||
const expectedEvents = timeline.getEvents();
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
it("canPaginate should return false until load has returned",
|
||||
function(done) {
|
||||
it("canPaginate should return false until load has returned", function() {
|
||||
const timeline = createTimeline();
|
||||
timeline.setPaginationToken("toktok1", EventTimeline.BACKWARDS);
|
||||
timeline.setPaginationToken("toktok2", EventTimeline.FORWARDS);
|
||||
|
||||
const eventId = timeline.getEvents()[1].getId();
|
||||
|
||||
const timelineSet = {};
|
||||
const timelineSet = {getTimelineForEvent: () => null};
|
||||
const client = {};
|
||||
|
||||
const timelineWindow = new TimelineWindow(client, timelineSet);
|
||||
@@ -222,25 +211,24 @@ describe("TimelineWindow", function() {
|
||||
return Promise.resolve(timeline);
|
||||
};
|
||||
|
||||
timelineWindow.load(eventId, 3).then(function() {
|
||||
return timelineWindow.load(eventId, 3).then(function() {
|
||||
const expectedEvents = timeline.getEvents();
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
expect(timelineWindow.canPaginate(EventTimeline.BACKWARDS))
|
||||
.toBe(true);
|
||||
expect(timelineWindow.canPaginate(EventTimeline.FORWARDS))
|
||||
.toBe(true);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("pagination", function() {
|
||||
it("should be able to advance across the initial timeline",
|
||||
function(done) {
|
||||
it("should be able to advance across the initial timeline", function() {
|
||||
const timeline = createTimeline();
|
||||
const eventId = timeline.getEvents()[1].getId();
|
||||
const timelineWindow = createWindow(timeline);
|
||||
|
||||
timelineWindow.load(eventId, 1).then(function() {
|
||||
return timelineWindow.load(eventId, 1).then(function() {
|
||||
const expectedEvents = [timeline.getEvents()[1]];
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
|
||||
@@ -277,15 +265,15 @@ describe("TimelineWindow", function() {
|
||||
return timelineWindow.paginate(EventTimeline.BACKWARDS, 2);
|
||||
}).then(function(success) {
|
||||
expect(success).toBe(false);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
it("should advance into next timeline", function(done) {
|
||||
it("should advance into next timeline", function() {
|
||||
const tls = createLinkedTimelines();
|
||||
const eventId = tls[0].getEvents()[1].getId();
|
||||
const timelineWindow = createWindow(tls[0], {windowLimit: 5});
|
||||
|
||||
timelineWindow.load(eventId, 3).then(function() {
|
||||
return timelineWindow.load(eventId, 3).then(function() {
|
||||
const expectedEvents = tls[0].getEvents();
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
|
||||
@@ -322,15 +310,15 @@ describe("TimelineWindow", function() {
|
||||
return timelineWindow.paginate(EventTimeline.FORWARDS, 2);
|
||||
}).then(function(success) {
|
||||
expect(success).toBe(false);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
it("should retreat into previous timeline", function(done) {
|
||||
it("should retreat into previous timeline", function() {
|
||||
const tls = createLinkedTimelines();
|
||||
const eventId = tls[1].getEvents()[1].getId();
|
||||
const timelineWindow = createWindow(tls[1], {windowLimit: 5});
|
||||
|
||||
timelineWindow.load(eventId, 3).then(function() {
|
||||
return timelineWindow.load(eventId, 3).then(function() {
|
||||
const expectedEvents = tls[1].getEvents();
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
|
||||
@@ -367,10 +355,10 @@ describe("TimelineWindow", function() {
|
||||
return timelineWindow.paginate(EventTimeline.BACKWARDS, 2);
|
||||
}).then(function(success) {
|
||||
expect(success).toBe(false);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
it("should make forward pagination requests", function(done) {
|
||||
it("should make forward pagination requests", function() {
|
||||
const timeline = createTimeline();
|
||||
timeline.setPaginationToken("toktok", EventTimeline.FORWARDS);
|
||||
|
||||
@@ -386,7 +374,7 @@ describe("TimelineWindow", function() {
|
||||
return Promise.resolve(true);
|
||||
};
|
||||
|
||||
timelineWindow.load(eventId, 3).then(function() {
|
||||
return timelineWindow.load(eventId, 3).then(function() {
|
||||
const expectedEvents = timeline.getEvents();
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
|
||||
@@ -399,11 +387,11 @@ describe("TimelineWindow", function() {
|
||||
expect(success).toBe(true);
|
||||
const expectedEvents = timeline.getEvents().slice(0, 5);
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it("should make backward pagination requests", function(done) {
|
||||
it("should make backward pagination requests", function() {
|
||||
const timeline = createTimeline();
|
||||
timeline.setPaginationToken("toktok", EventTimeline.BACKWARDS);
|
||||
|
||||
@@ -419,7 +407,7 @@ describe("TimelineWindow", function() {
|
||||
return Promise.resolve(true);
|
||||
};
|
||||
|
||||
timelineWindow.load(eventId, 3).then(function() {
|
||||
return timelineWindow.load(eventId, 3).then(function() {
|
||||
const expectedEvents = timeline.getEvents();
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
|
||||
@@ -432,11 +420,10 @@ describe("TimelineWindow", function() {
|
||||
expect(success).toBe(true);
|
||||
const expectedEvents = timeline.getEvents().slice(1, 6);
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
|
||||
it("should limit the number of unsuccessful pagination requests",
|
||||
function(done) {
|
||||
it("should limit the number of unsuccessful pagination requests", function() {
|
||||
const timeline = createTimeline();
|
||||
timeline.setPaginationToken("toktok", EventTimeline.FORWARDS);
|
||||
|
||||
@@ -452,7 +439,7 @@ describe("TimelineWindow", function() {
|
||||
return Promise.resolve(true);
|
||||
};
|
||||
|
||||
timelineWindow.load(eventId, 3).then(function() {
|
||||
return timelineWindow.load(eventId, 3).then(function() {
|
||||
const expectedEvents = timeline.getEvents();
|
||||
expect(timelineWindow.getEvents()).toEqual(expectedEvents);
|
||||
|
||||
@@ -471,7 +458,7 @@ describe("TimelineWindow", function() {
|
||||
.toBe(false);
|
||||
expect(timelineWindow.canPaginate(EventTimeline.FORWARDS))
|
||||
.toBe(true);
|
||||
}).nodeify(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,14 +4,11 @@ const sdk = require("../..");
|
||||
const User = sdk.User;
|
||||
const utils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("User", function() {
|
||||
const userId = "@alice:bar";
|
||||
let user;
|
||||
|
||||
beforeEach(function() {
|
||||
utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
user = new User(userId);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,15 +1,8 @@
|
||||
"use strict";
|
||||
import 'source-map-support/register';
|
||||
const utils = require("../../lib/utils");
|
||||
const testUtils = require("../test-utils");
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
describe("utils", function() {
|
||||
beforeEach(function() {
|
||||
testUtils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
|
||||
});
|
||||
|
||||
describe("encodeParams", function() {
|
||||
it("should url encode and concat with &s", function() {
|
||||
const params = {
|
||||
@@ -135,7 +128,7 @@ describe("utils", function() {
|
||||
utils.checkObjectHasKeys({
|
||||
foo: "bar",
|
||||
}, ["foo"]);
|
||||
}).toNotThrow();
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -152,7 +145,7 @@ describe("utils", function() {
|
||||
utils.checkObjectHasNoAdditionalKeys({
|
||||
foo: "bar",
|
||||
}, ["foo"]);
|
||||
}).toNotThrow();
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
|
||||
/** @module auto-discovery */
|
||||
|
||||
import Promise from 'bluebird';
|
||||
import logger from './logger';
|
||||
import { URL as NodeURL } from "url";
|
||||
|
||||
|
||||
+320
-170
@@ -19,13 +19,13 @@ limitations under the License.
|
||||
"use strict";
|
||||
|
||||
const PushProcessor = require('./pushprocessor');
|
||||
import {sleep} from './utils';
|
||||
|
||||
/**
|
||||
* This is an internal module. See {@link MatrixClient} for the public class.
|
||||
* @module client
|
||||
*/
|
||||
const EventEmitter = require("events").EventEmitter;
|
||||
import Promise from 'bluebird';
|
||||
const url = require('url');
|
||||
|
||||
const httpApi = require("./http-api");
|
||||
@@ -50,14 +50,10 @@ import logger from './logger';
|
||||
|
||||
import Crypto from './crypto';
|
||||
import { isCryptoAvailable } from './crypto';
|
||||
import { encodeRecoveryKey, decodeRecoveryKey } from './crypto/recoverykey';
|
||||
import { keyFromPassphrase, keyFromAuthData } from './crypto/key_passphrase';
|
||||
import { decodeRecoveryKey } from './crypto/recoverykey';
|
||||
import { keyFromAuthData } from './crypto/key_passphrase';
|
||||
import { randomString } from './randomstring';
|
||||
|
||||
// Disable warnings for now: we use deprecated bluebird functions
|
||||
// and need to migrate, but they spam the console with warnings.
|
||||
Promise.config({warnings: false});
|
||||
|
||||
import { encodeBase64, decodeBase64 } from '../lib/crypto/olmlib';
|
||||
|
||||
const SCROLLBACK_DELAY_MS = 3000;
|
||||
const CRYPTO_ENABLED = isCryptoAvailable();
|
||||
@@ -180,7 +176,8 @@ function keyFromRecoverySession(session, decryptionKey) {
|
||||
* The cross-signing API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @param {function} [opts.cryptoCallbacks.getCrossSigningKey]
|
||||
* Optional (required for cross-signing). Function to call when a cross-signing private key is needed.
|
||||
* Optional. Function to call when a cross-signing private key is needed.
|
||||
* Secure Secret Storage will be used by default if this is unset.
|
||||
* Args:
|
||||
* {string} type The type of key needed. Will be one of "master",
|
||||
* "self_signing", or "user_signing"
|
||||
@@ -192,8 +189,8 @@ function keyFromRecoverySession(session, decryptionKey) {
|
||||
* UInt8Array or rejects with an error.
|
||||
*
|
||||
* @param {function} [opts.cryptoCallbacks.saveCrossSigningKeys]
|
||||
* Optional (required for cross-signing). Called when new private keys
|
||||
* for cross-signing need to be saved.
|
||||
* Optional. Called when new private keys for cross-signing need to be saved.
|
||||
* Secure Secret Storage will be used by default if this is unset.
|
||||
* Args:
|
||||
* {object} keys the private keys to save. Map of key name to private key
|
||||
* as a UInt8Array. The getPrivateKey callback above will be called
|
||||
@@ -214,7 +211,7 @@ function keyFromRecoverySession(session, decryptionKey) {
|
||||
* @param {function} [opts.cryptoCallbacks.getSecretStorageKey]
|
||||
* Optional. Function called when an encryption key for secret storage
|
||||
* is required. One or more keys will be described in the keys object.
|
||||
* The callback function should return with an array of:
|
||||
* The callback function should return a promise with an array of:
|
||||
* [<key name>, <UInt8Array private key>] or null if it cannot provide
|
||||
* any of the keys.
|
||||
* Args:
|
||||
@@ -297,7 +294,7 @@ function MatrixClient(opts) {
|
||||
this._cryptoStore = opts.cryptoStore;
|
||||
this._sessionStore = opts.sessionStore;
|
||||
this._verificationMethods = opts.verificationMethods;
|
||||
this._cryptoCallbacks = opts.cryptoCallbacks;
|
||||
this._cryptoCallbacks = opts.cryptoCallbacks || {};
|
||||
|
||||
this._forceTURN = opts.forceTURN || false;
|
||||
this._fallbackICEServerAllowed = opts.fallbackICEServerAllowed || false;
|
||||
@@ -676,7 +673,7 @@ MatrixClient.prototype.initCrypto = async function() {
|
||||
"crypto.warning",
|
||||
"crypto.devicesUpdated",
|
||||
"deviceVerificationChanged",
|
||||
"userVerificationChanged",
|
||||
"userTrustStatusChanged",
|
||||
"crossSigning.keysChanged",
|
||||
]);
|
||||
|
||||
@@ -776,7 +773,8 @@ MatrixClient.prototype.getStoredDevice = async function(userId, deviceId) {
|
||||
* Mark the given device as verified
|
||||
*
|
||||
* @param {string} userId owner of the device
|
||||
* @param {string} deviceId unique identifier for the device
|
||||
* @param {string} deviceId unique identifier for the device or user's
|
||||
* cross-signing public key ID.
|
||||
*
|
||||
* @param {boolean=} verified whether to mark the device as verified. defaults
|
||||
* to 'true'.
|
||||
@@ -804,7 +802,8 @@ MatrixClient.prototype.setDeviceVerified = function(userId, deviceId, verified)
|
||||
* Mark the given device as blocked/unblocked
|
||||
*
|
||||
* @param {string} userId owner of the device
|
||||
* @param {string} deviceId unique identifier for the device
|
||||
* @param {string} deviceId unique identifier for the device or user's
|
||||
* cross-signing public key ID.
|
||||
*
|
||||
* @param {boolean=} blocked whether to mark the device as blocked. defaults
|
||||
* to 'true'.
|
||||
@@ -824,7 +823,8 @@ MatrixClient.prototype.setDeviceBlocked = function(userId, deviceId, blocked) {
|
||||
* Mark the given device as known/unknown
|
||||
*
|
||||
* @param {string} userId owner of the device
|
||||
* @param {string} deviceId unique identifier for the device
|
||||
* @param {string} deviceId unique identifier for the device or user's
|
||||
* cross-signing public key ID.
|
||||
*
|
||||
* @param {boolean=} known whether to mark the device as known. defaults
|
||||
* to 'true'.
|
||||
@@ -964,14 +964,6 @@ function wrapCryptoFuncs(MatrixClient, names) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether we already have cross-signing keys for the current user.
|
||||
* The cross-signing API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @function module:client~MatrixClient#doesCrossSigningHaveKeys
|
||||
* @return {boolean} Whether we have keys.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generate new cross-signing keys.
|
||||
* The cross-signing API is currently UNSTABLE and may change without notice.
|
||||
@@ -1025,15 +1017,34 @@ function wrapCryptoFuncs(MatrixClient, names) {
|
||||
* @returns {DeviceTrustLevel}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check the copy of our cross-signing key that we have in the device list and
|
||||
* see if we can get the private key. If so, mark it as trusted.
|
||||
* The cross-signing API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @function module:client~MatrixClient#checkOwnCrossSigningTrust
|
||||
*/
|
||||
|
||||
/**
|
||||
* Checks that a given cross-signing private key matches a given public key.
|
||||
* This can be used by the getCrossSigningKey callback to verify that the
|
||||
* private key it is about to supply is the one that was requested.
|
||||
* The cross-signing API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @function module:client~MatrixClient#checkCrossSigningPrivateKey
|
||||
* @param {Uint8Array} privateKey The private key
|
||||
* @param {string} expectedPublicKey The public key
|
||||
* @returns {boolean} true if the key matches, otherwise false
|
||||
*/
|
||||
|
||||
wrapCryptoFuncs(MatrixClient, [
|
||||
"doesCrossSigningHaveKeys",
|
||||
"resetCrossSigningKeys",
|
||||
"getCrossSigningId",
|
||||
"getStoredCrossSigningForUser",
|
||||
"checkUserTrust",
|
||||
"checkDeviceTrust",
|
||||
"checkOwnCrossSigningTrust",
|
||||
"checkPrivateKey",
|
||||
"checkCrossSigningPrivateKey",
|
||||
]);
|
||||
|
||||
/**
|
||||
@@ -1052,21 +1063,56 @@ MatrixClient.prototype.checkEventSenderTrust = async function(event) {
|
||||
return await this._crypto.checkDeviceTrust(event.getSender(), device.deviceId);
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a recovery key from a user-supplied passphrase.
|
||||
* The Secure Secret Storage API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @function module:client~MatrixClient#createRecoveryKeyFromPassphrase
|
||||
* @param {string} password Passphrase string that can be entered by the user
|
||||
* when restoring the backup as an alternative to entering the recovery key.
|
||||
* Optional.
|
||||
* @returns {Promise<String>} The user-facing recovery key string.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Bootstrap Secure Secret Storage if needed by creating a default key and signing it with
|
||||
* the cross-signing master key. If everything is already set up, then no
|
||||
* changes are made, so this is safe to run to ensure secret storage is ready
|
||||
* for use.
|
||||
* The Secure Secret Storage API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @function module:client~MatrixClient#bootstrapSecretStorage
|
||||
* @param {function} [opts.authUploadDeviceSigningKeys] Optional. Function
|
||||
* called to await an interactive auth flow when uploading device signing keys.
|
||||
* Args:
|
||||
* {function} A function that makes the request requiring auth. Receives the
|
||||
* auth data as an object.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add a key for encrypting secrets.
|
||||
* The Secure Secret Storage API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @function module:client~MatrixClient#addSecretKey
|
||||
* @function module:client~MatrixClient#addSecretStorageKey
|
||||
* @param {string} algorithm the algorithm used by the key
|
||||
* @param {object} opts the options for the algorithm. The properties used
|
||||
* depend on the algorithm given. This object may be modified to pass
|
||||
* information back about the key.
|
||||
* depend on the algorithm given.
|
||||
* @param {string} [keyName] the name of the key. If not given, a random
|
||||
* name will be generated.
|
||||
*
|
||||
* @return {string} the name of the key
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check whether we have a key with a given ID.
|
||||
* The Secure Secret Storage API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @function module:client~MatrixClient#hasSecretStorageKey
|
||||
* @param {string} [keyId = default key's ID] The ID of the key to check
|
||||
* for. Defaults to the default key ID if not provided.
|
||||
* @return {boolean} Whether we have the key.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Store an encrypted secret on the server
|
||||
* The Secure Secret Storage API is currently UNSTABLE and may change without notice.
|
||||
@@ -1128,14 +1174,30 @@ MatrixClient.prototype.checkEventSenderTrust = async function(event) {
|
||||
* @param {string} keyId The new default key ID
|
||||
*/
|
||||
|
||||
/**
|
||||
* Checks that a given secret storage private key matches a given public key.
|
||||
* This can be used by the getSecretStorageKey callback to verify that the
|
||||
* private key it is about to supply is the one that was requested.
|
||||
* The Secure Secret Storage API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @function module:client~MatrixClient#checkSecretStoragePrivateKey
|
||||
* @param {Uint8Array} privateKey The private key
|
||||
* @param {string} expectedPublicKey The public key
|
||||
* @returns {boolean} true if the key matches, otherwise false
|
||||
*/
|
||||
|
||||
wrapCryptoFuncs(MatrixClient, [
|
||||
"addSecretKey",
|
||||
"createRecoveryKeyFromPassphrase",
|
||||
"bootstrapSecretStorage",
|
||||
"addSecretStorageKey",
|
||||
"hasSecretStorageKey",
|
||||
"storeSecret",
|
||||
"getSecret",
|
||||
"isSecretStored",
|
||||
"requestSecret",
|
||||
"getDefaultSecretStorageKeyId",
|
||||
"setDefaultSecretStorageKeyId",
|
||||
"checkSecretStoragePrivateKey",
|
||||
]);
|
||||
|
||||
/**
|
||||
@@ -1375,38 +1437,50 @@ MatrixClient.prototype.disableKeyBackup = function() {
|
||||
* @param {string} password Passphrase string that can be entered by the user
|
||||
* when restoring the backup as an alternative to entering the recovery key.
|
||||
* Optional.
|
||||
* @param {boolean} [opts.secureSecretStorage = false] Whether to use Secure
|
||||
* Secret Storage to store the key encrypting key backups.
|
||||
* Optional, defaults to false.
|
||||
*
|
||||
* @returns {Promise<object>} Object that can be passed to createKeyBackupVersion and
|
||||
* additionally has a 'recovery_key' member with the user-facing recovery key string.
|
||||
*/
|
||||
MatrixClient.prototype.prepareKeyBackupVersion = async function(password) {
|
||||
MatrixClient.prototype.prepareKeyBackupVersion = async function(
|
||||
password,
|
||||
{ secureSecretStorage = false } = {},
|
||||
) {
|
||||
if (this._crypto === null) {
|
||||
throw new Error("End-to-end encryption disabled");
|
||||
}
|
||||
|
||||
const decryption = new global.Olm.PkDecryption();
|
||||
try {
|
||||
let publicKey;
|
||||
const authData = {};
|
||||
if (password) {
|
||||
const keyInfo = await keyFromPassphrase(password);
|
||||
publicKey = decryption.init_with_private_key(keyInfo.key);
|
||||
authData.private_key_salt = keyInfo.salt;
|
||||
authData.private_key_iterations = keyInfo.iterations;
|
||||
} else {
|
||||
publicKey = decryption.generate_key();
|
||||
}
|
||||
const [keyInfo, encodedPrivateKey, privateKey] =
|
||||
await this.createRecoveryKeyFromPassphrase(password);
|
||||
|
||||
authData.public_key = publicKey;
|
||||
|
||||
return {
|
||||
algorithm: olmlib.MEGOLM_BACKUP_ALGORITHM,
|
||||
auth_data: authData,
|
||||
recovery_key: encodeRecoveryKey(decryption.get_private_key()),
|
||||
};
|
||||
} finally {
|
||||
decryption.free();
|
||||
if (secureSecretStorage) {
|
||||
await this.storeSecret("m.megolm_backup.v1", encodeBase64(privateKey));
|
||||
logger.info("Key backup private key stored in secret storage");
|
||||
}
|
||||
|
||||
// Reshape objects into form expected for key backup
|
||||
const authData = {
|
||||
public_key: keyInfo.pubkey,
|
||||
};
|
||||
if (keyInfo.passphrase) {
|
||||
authData.private_key_salt = keyInfo.passphrase.salt;
|
||||
authData.private_key_iterations = keyInfo.passphrase.iterations;
|
||||
}
|
||||
return {
|
||||
algorithm: olmlib.MEGOLM_BACKUP_ALGORITHM,
|
||||
auth_data: authData,
|
||||
recovery_key: encodedPrivateKey,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Check whether the key backup private key is stored in secret storage.
|
||||
* @return {Promise<boolean>} Whether the backup key is stored.
|
||||
*/
|
||||
MatrixClient.prototype.isKeyBackupKeyStored = async function() {
|
||||
return this.isSecretStored("m.megolm_backup.v1", false /* checkKey */);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1426,13 +1500,19 @@ MatrixClient.prototype.createKeyBackupVersion = async function(info) {
|
||||
auth_data: info.auth_data,
|
||||
};
|
||||
|
||||
// Now sign the backup auth data. Do it as this device first because crypto._signObject
|
||||
// is dumb and bluntly replaces the whole signatures block...
|
||||
// this can probably go away very soon in favour of just signing with the SSK.
|
||||
// Sign the backup auth data with the device key for backwards compat with
|
||||
// older devices with cross-signing. This can probably go away very soon in
|
||||
// favour of just signing with the cross-singing master key.
|
||||
await this._crypto._signObject(data.auth_data);
|
||||
|
||||
if (this._crypto._crossSigningInfo.getId()) {
|
||||
// now also sign the auth data with the master key
|
||||
if (
|
||||
this._cryptoCallbacks.getCrossSigningKey &&
|
||||
this._crypto._crossSigningInfo.getId()
|
||||
) {
|
||||
// now also sign the auth data with the cross-signing master key
|
||||
// we check for the callback explicitly here because we still want to be able
|
||||
// to create an un-cross-signed key backup if there is a cross-signing key but
|
||||
// no callback supplied.
|
||||
await this._crypto._crossSigningInfo.signObject(data.auth_data, "master");
|
||||
}
|
||||
|
||||
@@ -1440,11 +1520,15 @@ MatrixClient.prototype.createKeyBackupVersion = async function(info) {
|
||||
undefined, "POST", "/room_keys/version", undefined, data,
|
||||
{prefix: httpApi.PREFIX_UNSTABLE},
|
||||
);
|
||||
this.enableKeyBackup({
|
||||
algorithm: info.algorithm,
|
||||
auth_data: info.auth_data,
|
||||
version: res.version,
|
||||
});
|
||||
|
||||
// We could assume everything's okay and enable directly, but this ensures
|
||||
// we run the same signature verification that will be used for future
|
||||
// sessions.
|
||||
await this.checkKeyBackup();
|
||||
if (!this.getKeyBackupEnabled()) {
|
||||
logger.error("Key backup not usable even though we just created it");
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
@@ -1548,6 +1632,18 @@ MatrixClient.prototype.isValidRecoveryKey = function(recoveryKey) {
|
||||
|
||||
MatrixClient.RESTORE_BACKUP_ERROR_BAD_KEY = 'RESTORE_BACKUP_ERROR_BAD_KEY';
|
||||
|
||||
/**
|
||||
* Restore from an existing key backup via a passphrase.
|
||||
*
|
||||
* @param {string} password Passphrase
|
||||
* @param {string} [targetRoomId] Room ID to target a specific room.
|
||||
* Restores all rooms if omitted.
|
||||
* @param {string} [targetSessionId] Session ID to target a specific session.
|
||||
* Restores all sessions if omitted.
|
||||
* @param {object} backupInfo Backup metadata from `checkKeyBackup`
|
||||
* @return {Promise<object>} Status of restoration with `total` and `imported`
|
||||
* key counts.
|
||||
*/
|
||||
MatrixClient.prototype.restoreKeyBackupWithPassword = async function(
|
||||
password, targetRoomId, targetSessionId, backupInfo,
|
||||
) {
|
||||
@@ -1557,6 +1653,39 @@ MatrixClient.prototype.restoreKeyBackupWithPassword = async function(
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Restore from an existing key backup via a private key stored in secret
|
||||
* storage.
|
||||
*
|
||||
* @param {object} backupInfo Backup metadata from `checkKeyBackup`
|
||||
* @param {string} [targetRoomId] Room ID to target a specific room.
|
||||
* Restores all rooms if omitted.
|
||||
* @param {string} [targetSessionId] Session ID to target a specific session.
|
||||
* Restores all sessions if omitted.
|
||||
* @return {Promise<object>} Status of restoration with `total` and `imported`
|
||||
* key counts.
|
||||
*/
|
||||
MatrixClient.prototype.restoreKeyBackupWithSecretStorage = async function(
|
||||
backupInfo, targetRoomId, targetSessionId,
|
||||
) {
|
||||
const privKey = decodeBase64(await this.getSecret("m.megolm_backup.v1"));
|
||||
return this._restoreKeyBackup(
|
||||
privKey, targetRoomId, targetSessionId, backupInfo,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Restore from an existing key backup via an encoded recovery key.
|
||||
*
|
||||
* @param {string} recoveryKey Encoded recovery key
|
||||
* @param {string} [targetRoomId] Room ID to target a specific room.
|
||||
* Restores all rooms if omitted.
|
||||
* @param {string} [targetSessionId] Session ID to target a specific session.
|
||||
* Restores all sessions if omitted.
|
||||
* @param {object} backupInfo Backup metadata from `checkKeyBackup`
|
||||
* @return {Promise<object>} Status of restoration with `total` and `imported`
|
||||
* key counts.
|
||||
*/
|
||||
MatrixClient.prototype.restoreKeyBackupWithRecoveryKey = function(
|
||||
recoveryKey, targetRoomId, targetSessionId, backupInfo,
|
||||
) {
|
||||
@@ -1871,33 +2000,33 @@ MatrixClient.prototype.joinRoom = function(roomIdOrAlias, opts, callback) {
|
||||
|
||||
const reqOpts = {qsStringifyOptions: {arrayFormat: 'repeat'}};
|
||||
|
||||
const defer = Promise.defer();
|
||||
|
||||
const self = this;
|
||||
sign_promise.then(function(signed_invite_object) {
|
||||
const data = {};
|
||||
if (signed_invite_object) {
|
||||
data.third_party_signed = signed_invite_object;
|
||||
}
|
||||
const prom = new Promise((resolve, reject) => {
|
||||
sign_promise.then(function(signed_invite_object) {
|
||||
const data = {};
|
||||
if (signed_invite_object) {
|
||||
data.third_party_signed = signed_invite_object;
|
||||
}
|
||||
|
||||
const path = utils.encodeUri("/join/$roomid", { $roomid: roomIdOrAlias});
|
||||
return self._http.authedRequest(
|
||||
undefined, "POST", path, queryString, data, reqOpts);
|
||||
}).then(function(res) {
|
||||
const roomId = res.room_id;
|
||||
const syncApi = new SyncApi(self, self._clientOpts);
|
||||
const room = syncApi.createRoom(roomId);
|
||||
if (opts.syncRoom) {
|
||||
// v2 will do this for us
|
||||
// return syncApi.syncRoom(room);
|
||||
}
|
||||
return Promise.resolve(room);
|
||||
}).done(function(room) {
|
||||
_resolve(callback, defer, room);
|
||||
}, function(err) {
|
||||
_reject(callback, defer, err);
|
||||
const path = utils.encodeUri("/join/$roomid", { $roomid: roomIdOrAlias});
|
||||
return self._http.authedRequest(
|
||||
undefined, "POST", path, queryString, data, reqOpts);
|
||||
}).then(function(res) {
|
||||
const roomId = res.room_id;
|
||||
const syncApi = new SyncApi(self, self._clientOpts);
|
||||
const room = syncApi.createRoom(roomId);
|
||||
if (opts.syncRoom) {
|
||||
// v2 will do this for us
|
||||
// return syncApi.syncRoom(room);
|
||||
}
|
||||
return Promise.resolve(room);
|
||||
}).then(function(room) {
|
||||
_resolve(callback, resolve, room);
|
||||
}, function(err) {
|
||||
_reject(callback, reject, err);
|
||||
});
|
||||
});
|
||||
return defer.promise;
|
||||
return prom;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -3213,42 +3342,45 @@ MatrixClient.prototype.scrollback = function(room, limit, callback) {
|
||||
// reduce the required number of events appropriately
|
||||
limit = limit - numAdded;
|
||||
|
||||
const defer = Promise.defer();
|
||||
const self = this;
|
||||
const prom = new Promise((resolve, reject) => {
|
||||
// wait for a time before doing this request
|
||||
// (which may be 0 in order not to special case the code paths)
|
||||
sleep(timeToWaitMs).then(function() {
|
||||
return self._createMessagesRequest(
|
||||
room.roomId,
|
||||
room.oldState.paginationToken,
|
||||
limit,
|
||||
'b');
|
||||
}).then(function(res) {
|
||||
const matrixEvents = utils.map(res.chunk, _PojoToMatrixEventMapper(self));
|
||||
if (res.state) {
|
||||
const stateEvents = utils.map(res.state, _PojoToMatrixEventMapper(self));
|
||||
room.currentState.setUnknownStateEvents(stateEvents);
|
||||
}
|
||||
room.addEventsToTimeline(matrixEvents, true, room.getLiveTimeline());
|
||||
room.oldState.paginationToken = res.end;
|
||||
if (res.chunk.length === 0) {
|
||||
room.oldState.paginationToken = null;
|
||||
}
|
||||
self.store.storeEvents(room, matrixEvents, res.end, true);
|
||||
self._ongoingScrollbacks[room.roomId] = null;
|
||||
_resolve(callback, resolve, room);
|
||||
}, function(err) {
|
||||
self._ongoingScrollbacks[room.roomId] = {
|
||||
errorTs: Date.now(),
|
||||
};
|
||||
_reject(callback, reject, err);
|
||||
});
|
||||
});
|
||||
|
||||
info = {
|
||||
promise: defer.promise,
|
||||
promise: prom,
|
||||
errorTs: null,
|
||||
};
|
||||
const self = this;
|
||||
// wait for a time before doing this request
|
||||
// (which may be 0 in order not to special case the code paths)
|
||||
Promise.delay(timeToWaitMs).then(function() {
|
||||
return self._createMessagesRequest(
|
||||
room.roomId,
|
||||
room.oldState.paginationToken,
|
||||
limit,
|
||||
'b');
|
||||
}).done(function(res) {
|
||||
const matrixEvents = utils.map(res.chunk, _PojoToMatrixEventMapper(self));
|
||||
if (res.state) {
|
||||
const stateEvents = utils.map(res.state, _PojoToMatrixEventMapper(self));
|
||||
room.currentState.setUnknownStateEvents(stateEvents);
|
||||
}
|
||||
room.addEventsToTimeline(matrixEvents, true, room.getLiveTimeline());
|
||||
room.oldState.paginationToken = res.end;
|
||||
if (res.chunk.length === 0) {
|
||||
room.oldState.paginationToken = null;
|
||||
}
|
||||
self.store.storeEvents(room, matrixEvents, res.end, true);
|
||||
self._ongoingScrollbacks[room.roomId] = null;
|
||||
_resolve(callback, defer, room);
|
||||
}, function(err) {
|
||||
self._ongoingScrollbacks[room.roomId] = {
|
||||
errorTs: Date.now(),
|
||||
};
|
||||
_reject(callback, defer, err);
|
||||
});
|
||||
|
||||
this._ongoingScrollbacks[room.roomId] = info;
|
||||
return defer.promise;
|
||||
return prom;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -3866,12 +3998,12 @@ MatrixClient.prototype.setRoomMutePushRule = function(scope, roomId, mute) {
|
||||
} else if (!hasDontNotifyRule) {
|
||||
// Remove the existing one before setting the mute push rule
|
||||
// This is a workaround to SYN-590 (Push rule update fails)
|
||||
deferred = Promise.defer();
|
||||
deferred = utils.defer();
|
||||
this.deletePushRule(scope, "room", roomPushRule.rule_id)
|
||||
.done(function() {
|
||||
.then(function() {
|
||||
self.addPushRule(scope, "room", roomId, {
|
||||
actions: ["dont_notify"],
|
||||
}).done(function() {
|
||||
}).then(function() {
|
||||
deferred.resolve();
|
||||
}, function(err) {
|
||||
deferred.reject(err);
|
||||
@@ -3885,26 +4017,26 @@ MatrixClient.prototype.setRoomMutePushRule = function(scope, roomId, mute) {
|
||||
}
|
||||
|
||||
if (deferred) {
|
||||
// Update this.pushRules when the operation completes
|
||||
const ruleRefreshDeferred = Promise.defer();
|
||||
deferred.done(function() {
|
||||
self.getPushRules().done(function(result) {
|
||||
self.pushRules = result;
|
||||
ruleRefreshDeferred.resolve();
|
||||
return new Promise((resolve, reject) => {
|
||||
// Update this.pushRules when the operation completes
|
||||
deferred.then(function() {
|
||||
self.getPushRules().then(function(result) {
|
||||
self.pushRules = result;
|
||||
resolve();
|
||||
}, function(err) {
|
||||
reject(err);
|
||||
});
|
||||
}, function(err) {
|
||||
ruleRefreshDeferred.reject(err);
|
||||
});
|
||||
}, function(err) {
|
||||
// Update it even if the previous operation fails. This can help the
|
||||
// app to recover when push settings has been modifed from another client
|
||||
self.getPushRules().done(function(result) {
|
||||
self.pushRules = result;
|
||||
ruleRefreshDeferred.reject(err);
|
||||
}, function(err2) {
|
||||
ruleRefreshDeferred.reject(err);
|
||||
// Update it even if the previous operation fails. This can help the
|
||||
// app to recover when push settings has been modifed from another client
|
||||
self.getPushRules().then(function(result) {
|
||||
self.pushRules = result;
|
||||
reject(err);
|
||||
}, function(err2) {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
return ruleRefreshDeferred.promise;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4391,7 +4523,7 @@ MatrixClient.prototype.startClient = async function(opts) {
|
||||
}
|
||||
|
||||
if (this._crypto) {
|
||||
this._crypto.uploadDeviceKeys().done();
|
||||
this._crypto.uploadDeviceKeys();
|
||||
this._crypto.start();
|
||||
}
|
||||
|
||||
@@ -4634,15 +4766,20 @@ function setupCallEventHandler(client) {
|
||||
// callId: [Candidate]
|
||||
};
|
||||
|
||||
// Maintain a buffer of events before the client has synced for the first time.
|
||||
// This buffer will be inspected to see if we should send incoming call
|
||||
// notifications. It needs to be buffered to correctly determine if an
|
||||
// incoming call has had a matching answer/hangup.
|
||||
// The sync code always emits one event at a time, so it will patiently
|
||||
// wait for us to finish processing a call invite before delivering the
|
||||
// next event, even if that next event is a hangup. We therefore accumulate
|
||||
// all our call events and then process them on the 'sync' event, ie.
|
||||
// each time a sync has completed. This way, we can avoid emitting incoming
|
||||
// call events if we get both the invite and answer/hangup in the same sync.
|
||||
// This happens quite often, eg. replaying sync from storage, catchup sync
|
||||
// after loading and after we've been offline for a bit.
|
||||
let callEventBuffer = [];
|
||||
let isClientPrepared = false;
|
||||
client.on("sync", function(state) {
|
||||
if (state === "PREPARED") {
|
||||
isClientPrepared = true;
|
||||
function evaluateEventBuffer() {
|
||||
if (client.getSyncState() === "SYNCING") {
|
||||
// don't process any events until they are all decrypted
|
||||
if (callEventBuffer.some((e) => e.isBeingDecrypted())) return;
|
||||
|
||||
const ignoreCallIds = {}; // Set<String>
|
||||
// inspect the buffer and mark all calls which have been answered
|
||||
// or hung up before passing them to the call event handler.
|
||||
@@ -4655,33 +4792,45 @@ function setupCallEventHandler(client) {
|
||||
}
|
||||
// now loop through the buffer chronologically and inject them
|
||||
callEventBuffer.forEach(function(e) {
|
||||
if (ignoreCallIds[e.getContent().call_id]) {
|
||||
// This call has previously been ansered or hung up: ignore it
|
||||
if (
|
||||
e.getType() === "m.call.invite" &&
|
||||
ignoreCallIds[e.getContent().call_id]
|
||||
) {
|
||||
// This call has previously been answered or hung up: ignore it
|
||||
return;
|
||||
}
|
||||
callEventHandler(e);
|
||||
});
|
||||
callEventBuffer = [];
|
||||
}
|
||||
});
|
||||
|
||||
client.on("event", onEvent);
|
||||
}
|
||||
client.on("sync", evaluateEventBuffer);
|
||||
|
||||
function onEvent(event) {
|
||||
if (event.getType().indexOf("m.call.") !== 0) {
|
||||
// not a call event
|
||||
if (event.isBeingDecrypted() || event.isDecryptionFailure()) {
|
||||
// not *yet* a call event, but might become one...
|
||||
event.once("Event.decrypted", onEvent);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!isClientPrepared) {
|
||||
// any call events or ones that might be once they're decrypted
|
||||
if (event.getType().indexOf("m.call.") === 0 || event.isBeingDecrypted()) {
|
||||
// queue up for processing once all events from this sync have been
|
||||
// processed (see above).
|
||||
callEventBuffer.push(event);
|
||||
return;
|
||||
}
|
||||
callEventHandler(event);
|
||||
|
||||
if (event.isBeingDecrypted() || event.isDecryptionFailure()) {
|
||||
// add an event listener for once the event is decrypted.
|
||||
event.once("Event.decrypted", () => {
|
||||
if (event.getType().indexOf("m.call.") === -1) return;
|
||||
|
||||
if (callEventBuffer.includes(event)) {
|
||||
// we were waiting for that event to decrypt, so recheck the buffer
|
||||
evaluateEventBuffer();
|
||||
} else {
|
||||
// This one wasn't buffered so just run the event handler for it
|
||||
// straight away
|
||||
callEventHandler(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
client.on("event", onEvent);
|
||||
|
||||
function callEventHandler(event) {
|
||||
const content = event.getContent();
|
||||
@@ -4833,7 +4982,7 @@ function checkTurnServers(client) {
|
||||
return; // guests can't access TURN servers
|
||||
}
|
||||
|
||||
client.turnServer().done(function(res) {
|
||||
client.turnServer().then(function(res) {
|
||||
if (res.uris) {
|
||||
logger.log("Got TURN URIs: " + res.uris + " refresh in " +
|
||||
res.ttl + " secs");
|
||||
@@ -4858,18 +5007,18 @@ function checkTurnServers(client) {
|
||||
});
|
||||
}
|
||||
|
||||
function _reject(callback, defer, err) {
|
||||
function _reject(callback, reject, err) {
|
||||
if (callback) {
|
||||
callback(err);
|
||||
}
|
||||
defer.reject(err);
|
||||
reject(err);
|
||||
}
|
||||
|
||||
function _resolve(callback, defer, res) {
|
||||
function _resolve(callback, resolve, res) {
|
||||
if (callback) {
|
||||
callback(null, res);
|
||||
}
|
||||
defer.resolve(res);
|
||||
resolve(res);
|
||||
}
|
||||
|
||||
function _PojoToMatrixEventMapper(client) {
|
||||
@@ -5201,6 +5350,8 @@ module.exports.CRYPTO_ENABLED = CRYPTO_ENABLED;
|
||||
* @param {object} data
|
||||
* @param {MatrixEvent} data.event the original verification request message
|
||||
* @param {Array} data.methods the verification methods that can be used
|
||||
* @param {Number} data.timeout the amount of milliseconds that should be waited
|
||||
* before cancelling the request automatically.
|
||||
* @param {Function} data.beginKeyVerification a function to call if a key
|
||||
* verification should be performed. The function takes one argument: the
|
||||
* name of the key verification method (taken from data.methods) to use.
|
||||
@@ -5331,5 +5482,4 @@ module.exports.CRYPTO_ENABLED = CRYPTO_ENABLED;
|
||||
* @property {Function} then promise.then(onFulfilled, onRejected, onProgress)
|
||||
* @property {Function} catch promise.catch(onRejected)
|
||||
* @property {Function} finally promise.finally(callback)
|
||||
* @property {Function} done promise.done(onFulfilled, onRejected, onProgress)
|
||||
*/
|
||||
|
||||
+67
-15
@@ -20,12 +20,15 @@ limitations under the License.
|
||||
* @module crypto/CrossSigning
|
||||
*/
|
||||
|
||||
import {pkSign, pkVerify} from './olmlib';
|
||||
import {pkSign, pkVerify, encodeBase64, decodeBase64} from './olmlib';
|
||||
import {EventEmitter} from 'events';
|
||||
import logger from '../logger';
|
||||
|
||||
function publicKeyFromKeyInfo(keyInfo) {
|
||||
return Object.entries(keyInfo.keys)[0];
|
||||
// `keys` is an object with { [`ed25519:${pubKey}`]: pubKey }
|
||||
// We assume only a single key, and we want the bare form without type
|
||||
// prefix, so we select the values.
|
||||
return Object.values(keyInfo.keys)[0];
|
||||
}
|
||||
|
||||
export class CrossSigningInfo extends EventEmitter {
|
||||
@@ -54,8 +57,9 @@ export class CrossSigningInfo extends EventEmitter {
|
||||
/**
|
||||
* Calls the app callback to ask for a private key
|
||||
* @param {string} type The key type ("master", "self_signing", or "user_signing")
|
||||
* @param {Uint8Array} expectedPubkey The matching public key or undefined to use
|
||||
* @param {string} expectedPubkey The matching public key or undefined to use
|
||||
* the stored public key for the given key type.
|
||||
* @returns {Array} An array with [ public key, Olm.PkSigning ]
|
||||
*/
|
||||
async getCrossSigningKey(type, expectedPubkey) {
|
||||
if (!this._callbacks.getCrossSigningKey) {
|
||||
@@ -69,7 +73,7 @@ export class CrossSigningInfo extends EventEmitter {
|
||||
const privkey = await this._callbacks.getCrossSigningKey(type, expectedPubkey);
|
||||
if (!privkey) {
|
||||
throw new Error(
|
||||
"getCrossSigningKey callback for " + type + " returned falsey",
|
||||
"getCrossSigningKey callback for " + type + " returned falsey",
|
||||
);
|
||||
}
|
||||
const signing = new global.Olm.PkSigning();
|
||||
@@ -101,12 +105,54 @@ export class CrossSigningInfo extends EventEmitter {
|
||||
};
|
||||
}
|
||||
|
||||
hasKeys() {
|
||||
return Object.keys(this.keys).length > 0;
|
||||
/**
|
||||
* Check whether the private keys exist in secret storage.
|
||||
* XXX: This could be static, be we often seem to have an instance when we
|
||||
* want to know this anyway...
|
||||
*
|
||||
* @param {SecretStorage} secretStorage The secret store using account data
|
||||
* @returns {boolean} Whether all private keys were found in storage
|
||||
*/
|
||||
isStoredInSecretStorage(secretStorage) {
|
||||
let stored = true;
|
||||
for (const type of ["master", "self_signing", "user_signing"]) {
|
||||
stored &= secretStorage.isStored(`m.cross_signing.${type}`, false);
|
||||
}
|
||||
return stored;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID used to identify the user
|
||||
* Store private keys in secret storage for use by other devices. This is
|
||||
* typically called in conjunction with the creation of new cross-signing
|
||||
* keys.
|
||||
*
|
||||
* @param {object} keys The keys to store
|
||||
* @param {SecretStorage} secretStorage The secret store using account data
|
||||
*/
|
||||
static async storeInSecretStorage(keys, secretStorage) {
|
||||
for (const type of Object.keys(keys)) {
|
||||
const encodedKey = encodeBase64(keys[type]);
|
||||
await secretStorage.store(`m.cross_signing.${type}`, encodedKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get private keys from secret storage created by some other device. This
|
||||
* also passes the private keys to the app-specific callback.
|
||||
*
|
||||
* @param {string} type The type of key to get. One of "master",
|
||||
* "self_signing", or "user_signing".
|
||||
* @param {SecretStorage} secretStorage The secret store using account data
|
||||
* @return {Uint8Array} The private key
|
||||
*/
|
||||
static async getFromSecretStorage(type, secretStorage) {
|
||||
const encodedKey = await secretStorage.get(`m.cross_signing.${type}`);
|
||||
return decodeBase64(encodedKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID used to identify the user. This can also be used to test for
|
||||
* the existence of a given key type.
|
||||
*
|
||||
* @param {string} type The type of key to get the ID of. One of "master",
|
||||
* "self_signing", or "user_signing". Defaults to "master".
|
||||
@@ -117,10 +163,16 @@ export class CrossSigningInfo extends EventEmitter {
|
||||
type = type || "master";
|
||||
if (!this.keys[type]) return null;
|
||||
const keyInfo = this.keys[type];
|
||||
return publicKeyFromKeyInfo(keyInfo)[1];
|
||||
return publicKeyFromKeyInfo(keyInfo);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create new cross-signing keys for the given key types. The public keys
|
||||
* will be held in this class, while the private keys are passed off to the
|
||||
* `saveCrossSigningKeys` application callback.
|
||||
*
|
||||
* @param {CrossSigningLevel} level The key types to reset
|
||||
*/
|
||||
async resetKeys(level) {
|
||||
if (!this._callbacks.saveCrossSigningKeys) {
|
||||
throw new Error("No saveCrossSigningKeys callback supplied");
|
||||
@@ -159,7 +211,7 @@ export class CrossSigningInfo extends EventEmitter {
|
||||
},
|
||||
};
|
||||
} else {
|
||||
[masterPub, masterSigning] = await this.getCrossSigningyKey("master");
|
||||
[masterPub, masterSigning] = await this.getCrossSigningKey("master");
|
||||
}
|
||||
|
||||
if (level & CrossSigningLevel.SELF_SIGNING) {
|
||||
@@ -219,7 +271,7 @@ export class CrossSigningInfo extends EventEmitter {
|
||||
if (!this.keys.master) {
|
||||
// this is the first key we've seen, so first-use is true
|
||||
this.firstUse = true;
|
||||
} else if (publicKeyFromKeyInfo(keys.master)[1] !== this.getId()) {
|
||||
} else if (publicKeyFromKeyInfo(keys.master) !== this.getId()) {
|
||||
// this is a different key, so first-use is false
|
||||
this.firstUse = false;
|
||||
} // otherwise, same key, so no change
|
||||
@@ -229,7 +281,7 @@ export class CrossSigningInfo extends EventEmitter {
|
||||
} else {
|
||||
throw new Error("Tried to set cross-signing keys without a master key");
|
||||
}
|
||||
const masterKey = publicKeyFromKeyInfo(signingKeys.master)[1];
|
||||
const masterKey = publicKeyFromKeyInfo(signingKeys.master);
|
||||
|
||||
// verify signatures
|
||||
if (keys.user_signing) {
|
||||
@@ -268,8 +320,8 @@ export class CrossSigningInfo extends EventEmitter {
|
||||
this.keys.master = keys.master;
|
||||
// if the master key is set, then the old self-signing and
|
||||
// user-signing keys are obsolete
|
||||
delete this.keys.self_signing;
|
||||
delete this.keys.user_signing;
|
||||
this.keys.self_signing = null;
|
||||
this.keys.user_signing = null;
|
||||
}
|
||||
if (keys.self_signing) {
|
||||
this.keys.self_signing = keys.self_signing;
|
||||
@@ -381,7 +433,7 @@ export class CrossSigningInfo extends EventEmitter {
|
||||
pkVerify(userSSK, userCrossSigning.getId(), userCrossSigning.userId);
|
||||
// ...and this device's key from their SSK...
|
||||
pkVerify(
|
||||
deviceObj, publicKeyFromKeyInfo(userSSK)[1], userCrossSigning.userId,
|
||||
deviceObj, publicKeyFromKeyInfo(userSSK), userCrossSigning.userId,
|
||||
);
|
||||
// ...then we trust this device as much as far as we trust the user
|
||||
return DeviceTrustLevel.fromUserTrustLevel(userTrust, localTrust);
|
||||
|
||||
@@ -23,7 +23,6 @@ limitations under the License.
|
||||
* Manages the list of other users' devices
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
import {EventEmitter} from 'events';
|
||||
|
||||
import logger from '../logger';
|
||||
@@ -31,6 +30,7 @@ import DeviceInfo from './deviceinfo';
|
||||
import {CrossSigningInfo} from './CrossSigning';
|
||||
import olmlib from './olmlib';
|
||||
import IndexedDBCryptoStore from './store/indexeddb-crypto-store';
|
||||
import {defer, sleep} from '../utils';
|
||||
|
||||
|
||||
/* State transition diagram for DeviceList._deviceTrackingStatus
|
||||
@@ -121,7 +121,8 @@ export default class DeviceList extends EventEmitter {
|
||||
'readonly', [IndexedDBCryptoStore.STORE_DEVICE_DATA], (txn) => {
|
||||
this._cryptoStore.getEndToEndDeviceData(txn, (deviceData) => {
|
||||
this._devices = deviceData ? deviceData.devices : {},
|
||||
this._ssks = deviceData ? deviceData.self_signing_keys || {} : {};
|
||||
this._crossSigningInfo = deviceData ?
|
||||
deviceData.crossSigningInfo || {} : {};
|
||||
this._deviceTrackingStatus = deviceData ?
|
||||
deviceData.trackingStatus : {};
|
||||
this._syncToken = deviceData ? deviceData.syncToken : null;
|
||||
@@ -212,7 +213,7 @@ export default class DeviceList extends EventEmitter {
|
||||
'readwrite', [IndexedDBCryptoStore.STORE_DEVICE_DATA], (txn) => {
|
||||
this._cryptoStore.storeEndToEndDeviceData({
|
||||
devices: this._devices,
|
||||
self_signing_keys: this._ssks,
|
||||
crossSigningInfo: this._crossSigningInfo,
|
||||
trackingStatus: this._deviceTrackingStatus,
|
||||
syncToken: this._syncToken,
|
||||
}, txn);
|
||||
@@ -710,7 +711,7 @@ class DeviceListUpdateSerialiser {
|
||||
});
|
||||
|
||||
if (!this._queuedQueryDeferred) {
|
||||
this._queuedQueryDeferred = Promise.defer();
|
||||
this._queuedQueryDeferred = defer();
|
||||
}
|
||||
|
||||
// We always take the new sync token and just use the latest one we've
|
||||
@@ -763,7 +764,7 @@ class DeviceListUpdateSerialiser {
|
||||
// this serves as an easy solution for now.
|
||||
let prom = Promise.resolve();
|
||||
for (const userId of downloadUsers) {
|
||||
prom = prom.delay(5).then(() => {
|
||||
prom = prom.then(sleep(5)).then(() => {
|
||||
return this._processQueryResponseForUser(
|
||||
userId, dk[userId], {
|
||||
master: masterKeys[userId],
|
||||
@@ -775,7 +776,7 @@ class DeviceListUpdateSerialiser {
|
||||
}
|
||||
|
||||
return prom;
|
||||
}).done(() => {
|
||||
}).then(() => {
|
||||
logger.log('Completed key download for ' + downloadUsers);
|
||||
|
||||
this._downloadInProgress = false;
|
||||
|
||||
@@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
|
||||
import logger from '../logger';
|
||||
import utils from '../utils';
|
||||
|
||||
|
||||
+84
-24
@@ -18,8 +18,6 @@ import {EventEmitter} from 'events';
|
||||
import logger from '../logger';
|
||||
import olmlib from './olmlib';
|
||||
import { randomString } from '../randomstring';
|
||||
import { keyFromPassphrase } from './key_passphrase';
|
||||
import { encodeRecoveryKey } from './recoverykey';
|
||||
import { pkVerify } from './olmlib';
|
||||
|
||||
export const SECRET_STORAGE_ALGORITHM_V1 = "m.secret_storage.v1.curve25519-aes-sha2";
|
||||
@@ -71,8 +69,7 @@ export default class SecretStorage extends EventEmitter {
|
||||
*
|
||||
* @param {string} algorithm the algorithm used by the key.
|
||||
* @param {object} opts the options for the algorithm. The properties used
|
||||
* depend on the algorithm given. This object may be modified to pass
|
||||
* information back about the key.
|
||||
* depend on the algorithm given.
|
||||
* @param {string} [keyId] the ID of the key. If not given, a random
|
||||
* ID will be generated.
|
||||
*
|
||||
@@ -92,21 +89,16 @@ export default class SecretStorage extends EventEmitter {
|
||||
{
|
||||
const decryption = new global.Olm.PkDecryption();
|
||||
try {
|
||||
if (opts.passphrase) {
|
||||
const key = await keyFromPassphrase(opts.passphrase);
|
||||
keyData.passphrase = {
|
||||
algorithm: "m.pbkdf2",
|
||||
iterations: key.iterations,
|
||||
salt: key.salt,
|
||||
};
|
||||
opts.encodedkey = encodeRecoveryKey(key.key);
|
||||
keyData.pubkey = decryption.init_with_private_key(key.key);
|
||||
} else if (opts.privkey) {
|
||||
keyData.pubkey = decryption.init_with_private_key(opts.privkey);
|
||||
opts.encodedkey = encodeRecoveryKey(opts.privkey);
|
||||
const { passphrase, pubkey } = opts;
|
||||
// Copies in public key details of the form generated by
|
||||
// the Crypto module's `createRecoveryKeyFromPassphrase`.
|
||||
if (passphrase && pubkey) {
|
||||
keyData.passphrase = passphrase;
|
||||
keyData.pubkey = pubkey;
|
||||
} else if (pubkey) {
|
||||
keyData.pubkey = pubkey;
|
||||
} else {
|
||||
keyData.pubkey = decryption.generate_key();
|
||||
opts.encodedkey = encodeRecoveryKey(decryption.get_private_key());
|
||||
}
|
||||
} finally {
|
||||
decryption.free();
|
||||
@@ -132,7 +124,48 @@ export default class SecretStorage extends EventEmitter {
|
||||
return keyId;
|
||||
}
|
||||
|
||||
// TODO: need a function to get all the secret keys
|
||||
/**
|
||||
* Signs a given secret storage key with the cross-signing master key.
|
||||
*
|
||||
* @param {string} [keyId = default key's ID] The ID of the key to sign.
|
||||
* Defaults to the default key ID if not provided.
|
||||
*/
|
||||
async signKey(keyId = this.getDefaultKeyId()) {
|
||||
if (!keyId) {
|
||||
throw new Error("signKey requires a key ID");
|
||||
}
|
||||
|
||||
const keyInfoEvent = this._baseApis.getAccountData(
|
||||
`m.secret_storage.key.${keyId}`,
|
||||
);
|
||||
if (!keyInfoEvent) {
|
||||
throw new Error(`Key ${keyId} does not exist in account data`);
|
||||
}
|
||||
const keyInfo = keyInfoEvent.getContent();
|
||||
|
||||
await this._crossSigningInfo.signObject(keyInfo, 'master');
|
||||
await this._baseApis.setAccountData(
|
||||
`m.secret_storage.key.${keyId}`, keyInfo,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether we have a key with a given ID.
|
||||
*
|
||||
* @param {string} [keyId = default key's ID] The ID of the key to check
|
||||
* for. Defaults to the default key ID if not provided.
|
||||
* @return {boolean} Whether we have the key.
|
||||
*/
|
||||
hasKey(keyId = this.getDefaultKeyId()) {
|
||||
if (!keyId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const keyInfo = this._baseApis.getAccountData(
|
||||
"m.secret_storage.key." + keyId,
|
||||
);
|
||||
return keyInfo && keyInfo.getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Store an encrypted secret on the server
|
||||
@@ -198,6 +231,27 @@ export default class SecretStorage extends EventEmitter {
|
||||
await this._baseApis.setAccountData(name, {encrypted});
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a secret defined to be the same as the given key.
|
||||
* No secret information will be stored, instead the secret will
|
||||
* be stored with a marker to say that the contents of the secret is
|
||||
* the value of the given key.
|
||||
* This is useful for migration from systems that predate SSSS such as
|
||||
* key backup.
|
||||
*
|
||||
* @param {string} name The name of the secret
|
||||
* @param {string} keyId The ID of the key whose value will be the
|
||||
* value of the secret
|
||||
* @returns {Promise} resolved when account data is saved
|
||||
*/
|
||||
storePassthrough(name, keyId) {
|
||||
return this._baseApis.setAccountData(name, {
|
||||
[keyId]: {
|
||||
passthrough: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a secret from storage.
|
||||
*
|
||||
@@ -243,8 +297,13 @@ export default class SecretStorage extends EventEmitter {
|
||||
// fetch private key from app
|
||||
[keyId, decryption] = await this._getSecretStorageKey(keys);
|
||||
|
||||
// decrypt secret
|
||||
const encInfo = secretContent.encrypted[keyId];
|
||||
|
||||
// We don't actually need the decryption object if it's a passthrough
|
||||
// since we just want to return the key itself.
|
||||
if (encInfo.passthrough) return decryption.get_private_key();
|
||||
|
||||
// decrypt secret
|
||||
switch (keys[keyId].algorithm) {
|
||||
case SECRET_STORAGE_ALGORITHM_V1:
|
||||
return decryption.decrypt(
|
||||
@@ -283,9 +342,12 @@ export default class SecretStorage extends EventEmitter {
|
||||
// encryption looks sane
|
||||
for (const keyId of Object.keys(secretContent.encrypted)) {
|
||||
// get key information from key storage
|
||||
const keyInfo = this._baseApis.getAccountData(
|
||||
const keyEvent = this._baseApis.getAccountData(
|
||||
"m.secret_storage.key." + keyId,
|
||||
).getContent();
|
||||
);
|
||||
if (!keyEvent) return false;
|
||||
const keyInfo = keyEvent.getContent();
|
||||
if (!keyInfo) return false;
|
||||
const encInfo = secretContent.encrypted[keyId];
|
||||
if (checkKey) {
|
||||
pkVerify(
|
||||
@@ -487,9 +549,7 @@ export default class SecretStorage extends EventEmitter {
|
||||
throw new Error("No getSecretStorageKey callback supplied");
|
||||
}
|
||||
|
||||
const returned = await Promise.resolve(
|
||||
this._cryptoCallbacks.getSecretStorageKey({keys}),
|
||||
);
|
||||
const returned = await this._cryptoCallbacks.getSecretStorageKey({ keys });
|
||||
|
||||
if (!returned) {
|
||||
throw new Error("getSecretStorageKey callback returned falsey");
|
||||
|
||||
@@ -20,8 +20,6 @@ limitations under the License.
|
||||
* @module
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
|
||||
/**
|
||||
* map of registered encryption algorithm classes. A map from string to {@link
|
||||
* module:crypto/algorithms/base.EncryptionAlgorithm|EncryptionAlgorithm} class
|
||||
|
||||
@@ -22,7 +22,6 @@ limitations under the License.
|
||||
* @module crypto/algorithms/megolm
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
import logger from '../../logger';
|
||||
|
||||
const utils = require("../../utils");
|
||||
@@ -508,7 +507,7 @@ MegolmEncryption.prototype.reshareKeyWithDevice = async function(
|
||||
userId,
|
||||
device,
|
||||
payload,
|
||||
),
|
||||
);
|
||||
|
||||
await this._baseApis.sendToDevice("m.room.encrypted", {
|
||||
[userId]: {
|
||||
@@ -1036,7 +1035,7 @@ MegolmDecryption.prototype.shareKeysWithDevice = function(keyRequest) {
|
||||
// TODO: retries
|
||||
return this._baseApis.sendToDevice("m.room.encrypted", contentMap);
|
||||
});
|
||||
}).done();
|
||||
});
|
||||
};
|
||||
|
||||
MegolmDecryption.prototype._buildKeyForwardingMessage = async function(
|
||||
|
||||
@@ -20,15 +20,12 @@ limitations under the License.
|
||||
*
|
||||
* @module crypto/algorithms/olm
|
||||
*/
|
||||
import Promise from 'bluebird';
|
||||
|
||||
import logger from '../../logger';
|
||||
const utils = require("../../utils");
|
||||
const olmlib = require("../olmlib");
|
||||
const DeviceInfo = require("../deviceinfo");
|
||||
const DeviceVerification = DeviceInfo.DeviceVerification;
|
||||
|
||||
|
||||
const base = require("./base");
|
||||
|
||||
/**
|
||||
@@ -139,7 +136,7 @@ OlmEncryption.prototype.encryptMessage = async function(room, eventType, content
|
||||
}
|
||||
}
|
||||
|
||||
return await Promise.all(promises).return(encryptedContent);
|
||||
return await Promise.all(promises).then(() => encryptedContent);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
+488
-531
File diff suppressed because it is too large
Load Diff
@@ -49,7 +49,7 @@ export async function keyFromPassphrase(password) {
|
||||
return { key, salt, iterations: DEFAULT_ITERATIONS };
|
||||
}
|
||||
|
||||
async function deriveKey(password, salt, iterations) {
|
||||
export async function deriveKey(password, salt, iterations) {
|
||||
const subtleCrypto = global.crypto.subtle;
|
||||
const TextEncoder = global.TextEncoder;
|
||||
if (!subtleCrypto || !TextEncoder) {
|
||||
|
||||
+20
-3
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
Copyright 2016 OpenMarket Ltd
|
||||
Copyright 2019 New Vector Ltd
|
||||
Copyright 2019 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.
|
||||
@@ -21,7 +22,6 @@ limitations under the License.
|
||||
* Utilities common to olm encryption algorithms
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
const anotherjson = require('another-json');
|
||||
|
||||
import logger from '../logger';
|
||||
@@ -303,8 +303,7 @@ async function _verifyKeyAndStartSession(olmDevice, oneTimeKey, userId, deviceIn
|
||||
*
|
||||
* @param {module:crypto/OlmDevice} olmDevice olm wrapper to use for verify op
|
||||
*
|
||||
* @param {Object} obj object to check signature on. Note that this will be
|
||||
* stripped of its 'signatures' and 'unsigned' properties.
|
||||
* @param {Object} obj object to check signature on.
|
||||
*
|
||||
* @param {string} signingUserId ID of the user whose signature should be checked
|
||||
*
|
||||
@@ -399,3 +398,21 @@ module.exports.pkVerify = function(obj, pubkey, userId) {
|
||||
util.free();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Encode a typed array of uint8 as base64.
|
||||
* @param {Uint8Array} uint8Array The data to encode.
|
||||
* @return {string} The base64.
|
||||
*/
|
||||
module.exports.encodeBase64 = function(uint8Array) {
|
||||
return Buffer.from(uint8Array).toString("base64");
|
||||
};
|
||||
|
||||
/**
|
||||
* Decode a base64 string to a typed array of uint8.
|
||||
* @param {string} base64 The base64 to decode.
|
||||
* @return {Uint8Array} The decoded data.
|
||||
*/
|
||||
module.exports.decodeBase64 = function(base64) {
|
||||
return Buffer.from(base64, "base64");
|
||||
};
|
||||
|
||||
@@ -15,8 +15,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
|
||||
import logger from '../../logger';
|
||||
import utils from '../../utils';
|
||||
|
||||
@@ -58,35 +56,34 @@ export class Backend {
|
||||
getOrAddOutgoingRoomKeyRequest(request) {
|
||||
const requestBody = request.requestBody;
|
||||
|
||||
const deferred = Promise.defer();
|
||||
const txn = this._db.transaction("outgoingRoomKeyRequests", "readwrite");
|
||||
txn.onerror = deferred.reject;
|
||||
return new Promise((resolve, reject) => {
|
||||
const txn = this._db.transaction("outgoingRoomKeyRequests", "readwrite");
|
||||
txn.onerror = reject;
|
||||
|
||||
// first see if we already have an entry for this request.
|
||||
this._getOutgoingRoomKeyRequest(txn, requestBody, (existing) => {
|
||||
if (existing) {
|
||||
// this entry matches the request - return it.
|
||||
// first see if we already have an entry for this request.
|
||||
this._getOutgoingRoomKeyRequest(txn, requestBody, (existing) => {
|
||||
if (existing) {
|
||||
// this entry matches the request - return it.
|
||||
logger.log(
|
||||
`already have key request outstanding for ` +
|
||||
`${requestBody.room_id} / ${requestBody.session_id}: ` +
|
||||
`not sending another`,
|
||||
);
|
||||
resolve(existing);
|
||||
return;
|
||||
}
|
||||
|
||||
// we got to the end of the list without finding a match
|
||||
// - add the new request.
|
||||
logger.log(
|
||||
`already have key request outstanding for ` +
|
||||
`${requestBody.room_id} / ${requestBody.session_id}: ` +
|
||||
`not sending another`,
|
||||
`enqueueing key request for ${requestBody.room_id} / ` +
|
||||
requestBody.session_id,
|
||||
);
|
||||
deferred.resolve(existing);
|
||||
return;
|
||||
}
|
||||
|
||||
// we got to the end of the list without finding a match
|
||||
// - add the new request.
|
||||
logger.log(
|
||||
`enqueueing key request for ${requestBody.room_id} / ` +
|
||||
requestBody.session_id,
|
||||
);
|
||||
txn.oncomplete = () => { deferred.resolve(request); };
|
||||
const store = txn.objectStore("outgoingRoomKeyRequests");
|
||||
store.add(request);
|
||||
txn.oncomplete = () => { resolve(request); };
|
||||
const store = txn.objectStore("outgoingRoomKeyRequests");
|
||||
store.add(request);
|
||||
});
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,15 +97,14 @@ export class Backend {
|
||||
* not found
|
||||
*/
|
||||
getOutgoingRoomKeyRequest(requestBody) {
|
||||
const deferred = Promise.defer();
|
||||
return new Promise((resolve, reject) => {
|
||||
const txn = this._db.transaction("outgoingRoomKeyRequests", "readonly");
|
||||
txn.onerror = reject;
|
||||
|
||||
const txn = this._db.transaction("outgoingRoomKeyRequests", "readonly");
|
||||
txn.onerror = deferred.reject;
|
||||
|
||||
this._getOutgoingRoomKeyRequest(txn, requestBody, (existing) => {
|
||||
deferred.resolve(existing);
|
||||
this._getOutgoingRoomKeyRequest(txn, requestBody, (existing) => {
|
||||
resolve(existing);
|
||||
});
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,8 +15,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
|
||||
import logger from '../../logger';
|
||||
import LocalStorageCryptoStore from './localStorage-crypto-store';
|
||||
import MemoryCryptoStore from './memory-crypto-store';
|
||||
@@ -287,7 +285,9 @@ export default class IndexedDBCryptoStore {
|
||||
* @param {function(string)} func Called with the account pickle
|
||||
*/
|
||||
getAccount(txn, func) {
|
||||
this._backendPromise.value().getAccount(txn, func);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.getAccount(txn, func);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -298,7 +298,9 @@ export default class IndexedDBCryptoStore {
|
||||
* @param {string} newData The new account pickle to store.
|
||||
*/
|
||||
storeAccount(txn, newData) {
|
||||
this._backendPromise.value().storeAccount(txn, newData);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.storeAccount(txn, newData);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -310,7 +312,9 @@ export default class IndexedDBCryptoStore {
|
||||
* { key_type: base64 encoded seed } where key type = user_signing_key_seed or self_signing_key_seed
|
||||
*/
|
||||
getCrossSigningKeys(txn, func) {
|
||||
this._backendPromise.value().getCrossSigningKeys(txn, func);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.getCrossSigningKeys(txn, func);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,7 +324,9 @@ export default class IndexedDBCryptoStore {
|
||||
* @param {string} keys keys object as getCrossSigningKeys()
|
||||
*/
|
||||
storeCrossSigningKeys(txn, keys) {
|
||||
this._backendPromise.value().storeCrossSigningKeys(txn, keys);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.storeCrossSigningKeys(txn, keys);
|
||||
});
|
||||
}
|
||||
|
||||
// Olm sessions
|
||||
@@ -331,7 +337,9 @@ export default class IndexedDBCryptoStore {
|
||||
* @param {function(int)} func Called with the count of sessions
|
||||
*/
|
||||
countEndToEndSessions(txn, func) {
|
||||
this._backendPromise.value().countEndToEndSessions(txn, func);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.countEndToEndSessions(txn, func);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -347,7 +355,9 @@ export default class IndexedDBCryptoStore {
|
||||
* a message.
|
||||
*/
|
||||
getEndToEndSession(deviceKey, sessionId, txn, func) {
|
||||
this._backendPromise.value().getEndToEndSession(deviceKey, sessionId, txn, func);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.getEndToEndSession(deviceKey, sessionId, txn, func);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -362,7 +372,9 @@ export default class IndexedDBCryptoStore {
|
||||
* a message.
|
||||
*/
|
||||
getEndToEndSessions(deviceKey, txn, func) {
|
||||
this._backendPromise.value().getEndToEndSessions(deviceKey, txn, func);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.getEndToEndSessions(deviceKey, txn, func);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -373,7 +385,9 @@ export default class IndexedDBCryptoStore {
|
||||
* and session keys.
|
||||
*/
|
||||
getAllEndToEndSessions(txn, func) {
|
||||
this._backendPromise.value().getAllEndToEndSessions(txn, func);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.getAllEndToEndSessions(txn, func);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -384,9 +398,11 @@ export default class IndexedDBCryptoStore {
|
||||
* @param {*} txn An active transaction. See doTxn().
|
||||
*/
|
||||
storeEndToEndSession(deviceKey, sessionId, sessionInfo, txn) {
|
||||
this._backendPromise.value().storeEndToEndSession(
|
||||
deviceKey, sessionId, sessionInfo, txn,
|
||||
);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.storeEndToEndSession(
|
||||
deviceKey, sessionId, sessionInfo, txn,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// Inbound group sessions
|
||||
@@ -401,9 +417,11 @@ export default class IndexedDBCryptoStore {
|
||||
* to Base64 end-to-end session.
|
||||
*/
|
||||
getEndToEndInboundGroupSession(senderCurve25519Key, sessionId, txn, func) {
|
||||
this._backendPromise.value().getEndToEndInboundGroupSession(
|
||||
senderCurve25519Key, sessionId, txn, func,
|
||||
);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.getEndToEndInboundGroupSession(
|
||||
senderCurve25519Key, sessionId, txn, func,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -414,7 +432,9 @@ export default class IndexedDBCryptoStore {
|
||||
* sessionData}, then once with null to indicate the end of the list.
|
||||
*/
|
||||
getAllEndToEndInboundGroupSessions(txn, func) {
|
||||
this._backendPromise.value().getAllEndToEndInboundGroupSessions(txn, func);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.getAllEndToEndInboundGroupSessions(txn, func);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -427,9 +447,11 @@ export default class IndexedDBCryptoStore {
|
||||
* @param {*} txn An active transaction. See doTxn().
|
||||
*/
|
||||
addEndToEndInboundGroupSession(senderCurve25519Key, sessionId, sessionData, txn) {
|
||||
this._backendPromise.value().addEndToEndInboundGroupSession(
|
||||
senderCurve25519Key, sessionId, sessionData, txn,
|
||||
);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.addEndToEndInboundGroupSession(
|
||||
senderCurve25519Key, sessionId, sessionData, txn,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -442,9 +464,11 @@ export default class IndexedDBCryptoStore {
|
||||
* @param {*} txn An active transaction. See doTxn().
|
||||
*/
|
||||
storeEndToEndInboundGroupSession(senderCurve25519Key, sessionId, sessionData, txn) {
|
||||
this._backendPromise.value().storeEndToEndInboundGroupSession(
|
||||
senderCurve25519Key, sessionId, sessionData, txn,
|
||||
);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.storeEndToEndInboundGroupSession(
|
||||
senderCurve25519Key, sessionId, sessionData, txn,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// End-to-end device tracking
|
||||
@@ -460,7 +484,9 @@ export default class IndexedDBCryptoStore {
|
||||
* @param {*} txn An active transaction. See doTxn().
|
||||
*/
|
||||
storeEndToEndDeviceData(deviceData, txn) {
|
||||
this._backendPromise.value().storeEndToEndDeviceData(deviceData, txn);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.storeEndToEndDeviceData(deviceData, txn);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -471,7 +497,9 @@ export default class IndexedDBCryptoStore {
|
||||
* device data
|
||||
*/
|
||||
getEndToEndDeviceData(txn, func) {
|
||||
this._backendPromise.value().getEndToEndDeviceData(txn, func);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.getEndToEndDeviceData(txn, func);
|
||||
});
|
||||
}
|
||||
|
||||
// End to End Rooms
|
||||
@@ -483,7 +511,9 @@ export default class IndexedDBCryptoStore {
|
||||
* @param {*} txn An active transaction. See doTxn().
|
||||
*/
|
||||
storeEndToEndRoom(roomId, roomInfo, txn) {
|
||||
this._backendPromise.value().storeEndToEndRoom(roomId, roomInfo, txn);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.storeEndToEndRoom(roomId, roomInfo, txn);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -492,7 +522,9 @@ export default class IndexedDBCryptoStore {
|
||||
* @param {function(Object)} func Function called with the end to end encrypted rooms
|
||||
*/
|
||||
getEndToEndRooms(txn, func) {
|
||||
this._backendPromise.value().getEndToEndRooms(txn, func);
|
||||
this._backendPromise.then(backend => {
|
||||
backend.getEndToEndRooms(txn, func);
|
||||
});
|
||||
}
|
||||
|
||||
// session backups
|
||||
|
||||
@@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
|
||||
import logger from '../../logger';
|
||||
import MemoryCryptoStore from './memory-crypto-store';
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
|
||||
import logger from '../../logger';
|
||||
import utils from '../../utils';
|
||||
|
||||
@@ -69,7 +67,7 @@ export default class MemoryCryptoStore {
|
||||
getOrAddOutgoingRoomKeyRequest(request) {
|
||||
const requestBody = request.requestBody;
|
||||
|
||||
return Promise.try(() => {
|
||||
return utils.promiseTry(() => {
|
||||
// first see if we already have an entry for this request.
|
||||
const existing = this._getOutgoingRoomKeyRequest(requestBody);
|
||||
|
||||
|
||||
+19
-100
@@ -40,51 +40,36 @@ export default class VerificationBase extends EventEmitter {
|
||||
*
|
||||
* @class
|
||||
*
|
||||
* @param {module:base-apis~Channel} channel the verification channel to send verification messages over.
|
||||
*
|
||||
* @param {module:base-apis~MatrixBaseApis} baseApis base matrix api interface
|
||||
*
|
||||
* @param {string} userId the user ID that is being verified
|
||||
*
|
||||
* @param {string} deviceId the device ID that is being verified
|
||||
*
|
||||
* @param {string} transactionId the transaction ID to be used when sending events
|
||||
*
|
||||
* @param {string} [roomId] the room to use for verification
|
||||
*
|
||||
* @param {object} [startEvent] the m.key.verification.start event that
|
||||
* initiated this verification, if any
|
||||
*
|
||||
* @param {object} [request] the key verification request object related to
|
||||
* this verification, if any
|
||||
*/
|
||||
constructor(baseApis, userId, deviceId, transactionId, roomId, startEvent, request) {
|
||||
constructor(channel, baseApis, userId, deviceId, startEvent, request) {
|
||||
super();
|
||||
this._channel = channel;
|
||||
this._baseApis = baseApis;
|
||||
this.userId = userId;
|
||||
this.deviceId = deviceId;
|
||||
this.transactionId = transactionId;
|
||||
if (typeof(roomId) === "string" || roomId instanceof String) {
|
||||
this.roomId = roomId;
|
||||
this.startEvent = startEvent;
|
||||
this.request = request;
|
||||
} else {
|
||||
// if room ID was omitted, but start event and request were not
|
||||
this.startEvent= roomId;
|
||||
this.request = startEvent;
|
||||
}
|
||||
this.startEvent = startEvent;
|
||||
this.request = request;
|
||||
|
||||
this.cancelled = false;
|
||||
this._done = false;
|
||||
this._promise = null;
|
||||
this._transactionTimeoutTimer = null;
|
||||
this._eventsSubscription = null;
|
||||
|
||||
// At this point, the verification request was received so start the timeout timer.
|
||||
this._resetTimer();
|
||||
|
||||
if (this.roomId) {
|
||||
this._sendWithTxnId = this._sendMessage;
|
||||
} else {
|
||||
this._sendWithTxnId = this._sendToDevice;
|
||||
}
|
||||
}
|
||||
|
||||
_resetTimer() {
|
||||
@@ -107,55 +92,8 @@ export default class VerificationBase extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
_contentFromEventWithTxnId(event) {
|
||||
if (this.roomId) { // verification as timeline event
|
||||
// ensure m.related_to is included in e2ee rooms
|
||||
// as the field is excluded from encryption
|
||||
const content = Object.assign({}, event.getContent());
|
||||
content["m.relates_to"] = event.getRelation();
|
||||
return content;
|
||||
} else { // verification as to_device event
|
||||
return event.getContent();
|
||||
}
|
||||
}
|
||||
|
||||
/* creates a content object with the transaction id added to it */
|
||||
_contentWithTxnId(content) {
|
||||
const copy = Object.assign({}, content);
|
||||
if (this.roomId) { // verification as timeline event
|
||||
copy["m.relates_to"] = {
|
||||
rel_type: "m.reference",
|
||||
event_id: this.transactionId,
|
||||
};
|
||||
} else { // verification as to_device event
|
||||
copy.transaction_id = this.transactionId;
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
_send(type, contentWithoutTxnId) {
|
||||
const content = this._contentWithTxnId(contentWithoutTxnId);
|
||||
return this._sendWithTxnId(type, content);
|
||||
}
|
||||
|
||||
/* send a message to the other participant, using to-device messages
|
||||
*/
|
||||
_sendToDevice(type, content) {
|
||||
if (this._done) {
|
||||
return Promise.reject(new Error("Verification is already done"));
|
||||
}
|
||||
return this._baseApis.sendToDevice(type, {
|
||||
[this.userId]: { [this.deviceId]: content },
|
||||
});
|
||||
}
|
||||
|
||||
/* send a message to the other participant, using in-roomm messages
|
||||
*/
|
||||
_sendMessage(type, content) {
|
||||
if (this._done) {
|
||||
return Promise.reject(new Error("Verification is already done"));
|
||||
}
|
||||
return this._baseApis.sendEvent(this.roomId, type, content);
|
||||
_send(type, uncompletedContent) {
|
||||
return this._channel.send(type, uncompletedContent);
|
||||
}
|
||||
|
||||
_waitForEvent(type) {
|
||||
@@ -173,10 +111,14 @@ export default class VerificationBase extends EventEmitter {
|
||||
if (this._done) {
|
||||
return;
|
||||
} else if (e.getType() === this._expectedEvent) {
|
||||
this._expectedEvent = undefined;
|
||||
this._rejectEvent = undefined;
|
||||
this._resetTimer();
|
||||
this._resolveEvent(e);
|
||||
// if we receive an expected m.key.verification.done, then just
|
||||
// ignore it, since we don't need to do anything about it
|
||||
if (this._expectedEvent !== "m.key.verification.done") {
|
||||
this._expectedEvent = undefined;
|
||||
this._rejectEvent = undefined;
|
||||
this._resetTimer();
|
||||
this._resolveEvent(e);
|
||||
}
|
||||
} else if (e.getType() === "m.key.verification.cancel") {
|
||||
const reject = this._reject;
|
||||
this._reject = undefined;
|
||||
@@ -199,7 +141,7 @@ export default class VerificationBase extends EventEmitter {
|
||||
done() {
|
||||
this._endTimer(); // always kill the activity timer
|
||||
if (!this._done) {
|
||||
if (this.roomId) {
|
||||
if (this._channel.needsDoneMessage) {
|
||||
// verification in DM requires a done message
|
||||
this._send("m.key.verification.done", {});
|
||||
}
|
||||
@@ -211,7 +153,7 @@ export default class VerificationBase extends EventEmitter {
|
||||
this._endTimer(); // always kill the activity timer
|
||||
if (!this._done) {
|
||||
this.cancelled = true;
|
||||
if (this.userId && this.deviceId && this.transactionId) {
|
||||
if (this.userId && this.deviceId) {
|
||||
// send a cancellation to the other user (if it wasn't
|
||||
// cancelled by the other user)
|
||||
if (e === timeoutException) {
|
||||
@@ -225,13 +167,11 @@ export default class VerificationBase extends EventEmitter {
|
||||
content.code = content.code || "m.unknown";
|
||||
content.reason = content.reason || content.body
|
||||
|| "Unknown reason";
|
||||
content.transaction_id = this.transactionId;
|
||||
this._send("m.key.verification.cancel", content);
|
||||
} else {
|
||||
this._send("m.key.verification.cancel", {
|
||||
code: "m.unknown",
|
||||
reason: content.body || "Unknown reason",
|
||||
transaction_id: this.transactionId,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -239,7 +179,6 @@ export default class VerificationBase extends EventEmitter {
|
||||
this._send("m.key.verification.cancel", {
|
||||
code: "m.unknown",
|
||||
reason: e.toString(),
|
||||
transaction_id: this.transactionId,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -248,10 +187,6 @@ export default class VerificationBase extends EventEmitter {
|
||||
// but no reject function. If cancel is called again, we'd error.
|
||||
if (this._reject) this._reject(e);
|
||||
} else {
|
||||
// unsubscribe from events, this happens in _reject usually but we don't have one here
|
||||
if (this._eventsSubscription) {
|
||||
this._eventsSubscription = this._eventsSubscription();
|
||||
}
|
||||
// FIXME: this causes an "Uncaught promise" console message
|
||||
// if nothing ends up chaining this promise.
|
||||
this._promise = Promise.reject(e);
|
||||
@@ -275,23 +210,11 @@ export default class VerificationBase extends EventEmitter {
|
||||
this._resolve = (...args) => {
|
||||
this._done = true;
|
||||
this._endTimer();
|
||||
if (this.handler) {
|
||||
// these listeners are attached in Crypto.acceptVerificationDM
|
||||
if (this._eventsSubscription) {
|
||||
this._eventsSubscription = this._eventsSubscription();
|
||||
}
|
||||
}
|
||||
resolve(...args);
|
||||
};
|
||||
this._reject = (...args) => {
|
||||
this._done = true;
|
||||
this._endTimer();
|
||||
if (this.handler) {
|
||||
// these listeners are attached in Crypto.acceptVerificationDM
|
||||
if (this._eventsSubscription) {
|
||||
this._eventsSubscription = this._eventsSubscription();
|
||||
}
|
||||
}
|
||||
reject(...args);
|
||||
};
|
||||
});
|
||||
@@ -344,8 +267,4 @@ export default class VerificationBase extends EventEmitter {
|
||||
await this._baseApis.setDeviceVerified(userId, deviceId);
|
||||
}
|
||||
}
|
||||
|
||||
setEventsSubscription(subscription) {
|
||||
this._eventsSubscription = subscription;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,3 +85,13 @@ export const newUserMismatchError = errorFactory("m.user_error", "User mismatch"
|
||||
export const newInvalidMessageError = errorFactory(
|
||||
"m.invalid_message", "Invalid message",
|
||||
);
|
||||
|
||||
export function errorFromEvent(event) {
|
||||
const content = event.getContent();
|
||||
if (content) {
|
||||
const {code, reason} = content;
|
||||
return {code, reason};
|
||||
} else {
|
||||
return {code: "Unknown error", reason: "m.unknown"};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +205,8 @@ export default class SAS extends Base {
|
||||
}
|
||||
|
||||
async _doSendVerification() {
|
||||
const initialMessage = this._contentWithTxnId({
|
||||
const type = "m.key.verification.start";
|
||||
const initialMessage = this._channel.completeContent(type, {
|
||||
method: SAS.NAME,
|
||||
from_device: this._baseApis.deviceId,
|
||||
key_agreement_protocols: KEY_AGREEMENT_LIST,
|
||||
@@ -216,8 +217,7 @@ export default class SAS extends Base {
|
||||
});
|
||||
// add the transaction id to the message beforehand because
|
||||
// it needs to be included in the commitment hash later on
|
||||
this._sendWithTxnId("m.key.verification.start", initialMessage);
|
||||
|
||||
this._channel.sendCompleted(type, initialMessage);
|
||||
|
||||
let e = await this._waitForEvent("m.key.verification.accept");
|
||||
let content = e.getContent();
|
||||
@@ -254,7 +254,7 @@ export default class SAS extends Base {
|
||||
const sasInfo = "MATRIX_KEY_VERIFICATION_SAS"
|
||||
+ this._baseApis.getUserId() + this._baseApis.deviceId
|
||||
+ this.userId + this.deviceId
|
||||
+ this.transactionId;
|
||||
+ this._channel.transactionId;
|
||||
const sasBytes = olmSAS.generate_bytes(sasInfo, 6);
|
||||
const verifySAS = new Promise((resolve, reject) => {
|
||||
this.emit("show_sas", {
|
||||
@@ -270,7 +270,14 @@ export default class SAS extends Base {
|
||||
|
||||
|
||||
[e] = await Promise.all([
|
||||
this._waitForEvent("m.key.verification.mac"),
|
||||
this._waitForEvent("m.key.verification.mac")
|
||||
.then((e) => {
|
||||
// we don't expect any more messages from the other
|
||||
// party, and they may send a m.key.verification.done
|
||||
// when they're done on their end
|
||||
this._expectedEvent = "m.key.verification.done";
|
||||
return e;
|
||||
}),
|
||||
verifySAS,
|
||||
]);
|
||||
content = e.getContent();
|
||||
@@ -283,7 +290,7 @@ export default class SAS extends Base {
|
||||
async _doRespondVerification() {
|
||||
// as m.related_to is not included in the encrypted content in e2e rooms,
|
||||
// we need to make sure it is added
|
||||
let content = this._contentFromEventWithTxnId(this.startEvent);
|
||||
let content = this._channel.completedContentFromEvent(this.startEvent);
|
||||
|
||||
// Note: we intersect using our pre-made lists, rather than the sets,
|
||||
// so that the result will be in our order of preference. Then
|
||||
@@ -331,7 +338,7 @@ export default class SAS extends Base {
|
||||
const sasInfo = "MATRIX_KEY_VERIFICATION_SAS"
|
||||
+ this.userId + this.deviceId
|
||||
+ this._baseApis.getUserId() + this._baseApis.deviceId
|
||||
+ this.transactionId;
|
||||
+ this._channel.transactionId;
|
||||
const sasBytes = olmSAS.generate_bytes(sasInfo, 6);
|
||||
const verifySAS = new Promise((resolve, reject) => {
|
||||
this.emit("show_sas", {
|
||||
@@ -347,7 +354,14 @@ export default class SAS extends Base {
|
||||
|
||||
|
||||
[e] = await Promise.all([
|
||||
this._waitForEvent("m.key.verification.mac"),
|
||||
this._waitForEvent("m.key.verification.mac")
|
||||
.then((e) => {
|
||||
// we don't expect any more messages from the other
|
||||
// party, and they may send a m.key.verification.done
|
||||
// when they're done on their end
|
||||
this._expectedEvent = "m.key.verification.done";
|
||||
return e;
|
||||
}),
|
||||
verifySAS,
|
||||
]);
|
||||
content = e.getContent();
|
||||
@@ -363,7 +377,7 @@ export default class SAS extends Base {
|
||||
const baseInfo = "MATRIX_KEY_VERIFICATION_MAC"
|
||||
+ this._baseApis.getUserId() + this._baseApis.deviceId
|
||||
+ this.userId + this.deviceId
|
||||
+ this.transactionId;
|
||||
+ this._channel.transactionId;
|
||||
|
||||
const deviceKeyId = `ed25519:${this._baseApis.deviceId}`;
|
||||
mac[deviceKeyId] = olmSAS[macMethods[method]](
|
||||
@@ -393,7 +407,7 @@ export default class SAS extends Base {
|
||||
const baseInfo = "MATRIX_KEY_VERIFICATION_MAC"
|
||||
+ this.userId + this.deviceId
|
||||
+ this._baseApis.getUserId() + this._baseApis.deviceId
|
||||
+ this.transactionId;
|
||||
+ this._channel.transactionId;
|
||||
|
||||
if (content.keys !== olmSAS[macMethods[method]](
|
||||
Object.keys(content.mac).sort().join(","),
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
Copyright 2018 New Vector Ltd
|
||||
Copyright 2019 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.
|
||||
*/
|
||||
|
||||
import VerificationRequest, {REQUEST_TYPE, START_TYPE} from "./VerificationRequest";
|
||||
const MESSAGE_TYPE = "m.room.message";
|
||||
const M_REFERENCE = "m.reference";
|
||||
const M_RELATES_TO = "m.relates_to";
|
||||
|
||||
/**
|
||||
* A key verification channel that sends verification events in the timeline of a room.
|
||||
* Uses the event id of the initial m.key.verification.request event as a transaction id.
|
||||
*/
|
||||
export default class InRoomChannel {
|
||||
/**
|
||||
* @param {MatrixClient} client the matrix client, to send messages with and get current user & device from.
|
||||
* @param {string} roomId id of the room where verification events should be posted in, should be a DM with the given user.
|
||||
* @param {string} userId id of user that the verification request is directed at, should be present in the room.
|
||||
*/
|
||||
constructor(client, roomId, userId) {
|
||||
this._client = client;
|
||||
this._roomId = roomId;
|
||||
this._userId = userId;
|
||||
this._requestEventId = null;
|
||||
}
|
||||
|
||||
/** Whether this channel needs m.key.verification.done messages to be sent after a successful verification */
|
||||
get needsDoneMessage() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** The transaction id generated/used by this verification channel */
|
||||
get transactionId() {
|
||||
return this._requestEventId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {MatrixEvent} event the event to get the timestamp of
|
||||
* @return {number} the timestamp when the event was sent
|
||||
*/
|
||||
static getTimestamp(event) {
|
||||
return event.getTs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given event type should be allowed to initiate a new VerificationRequest over this channel
|
||||
* @param {string} type the event type to check
|
||||
* @returns {bool} boolean flag
|
||||
*/
|
||||
static canCreateRequest(type) {
|
||||
return type === REQUEST_TYPE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the transaction id used by a given key verification event, if any
|
||||
* @param {MatrixEvent} event the event
|
||||
* @returns {string} the transaction id
|
||||
*/
|
||||
static getTransactionId(event) {
|
||||
if (InRoomChannel.getEventType(event) === REQUEST_TYPE) {
|
||||
return event.getId();
|
||||
} else {
|
||||
const relation = event.getRelation();
|
||||
if (relation && relation.rel_type === M_REFERENCE) {
|
||||
return relation.event_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this event is a well-formed key verification event.
|
||||
* This only does checks that don't rely on the current state of a potentially already channel
|
||||
* so we can prevent channels being created by invalid events.
|
||||
* `handleEvent` can do more checks and choose to ignore invalid events.
|
||||
* @param {MatrixEvent} event the event to validate
|
||||
* @param {MatrixClient} client the client to get the current user and device id from
|
||||
* @returns {bool} whether the event is valid and should be passed to handleEvent
|
||||
*/
|
||||
static validateEvent(event, client) {
|
||||
const txnId = InRoomChannel.getTransactionId(event);
|
||||
if (typeof txnId !== "string" || txnId.length === 0) {
|
||||
return false;
|
||||
}
|
||||
const type = InRoomChannel.getEventType(event);
|
||||
const content = event.getContent();
|
||||
if (type === REQUEST_TYPE) {
|
||||
if (typeof content.to !== "string" || !content.to.length) {
|
||||
return false;
|
||||
}
|
||||
const ownUserId = client.getUserId();
|
||||
// ignore requests that are not direct to or sent by the syncing user
|
||||
if (event.getSender() !== ownUserId && content.to !== ownUserId) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return VerificationRequest.validateEvent(
|
||||
type, event, InRoomChannel.getTimestamp(event), client);
|
||||
}
|
||||
|
||||
/**
|
||||
* As m.key.verification.request events are as m.room.message events with the InRoomChannel
|
||||
* to have a fallback message in non-supporting clients, we map the real event type
|
||||
* to the symbolic one to keep things in unison with ToDeviceChannel
|
||||
* @param {MatrixEvent} event the event to get the type of
|
||||
* @returns {string} the "symbolic" event type
|
||||
*/
|
||||
static getEventType(event) {
|
||||
const type = event.getType();
|
||||
if (type === MESSAGE_TYPE) {
|
||||
const content = event.getContent();
|
||||
if (content) {
|
||||
const {msgtype} = content;
|
||||
if (msgtype === REQUEST_TYPE) {
|
||||
return REQUEST_TYPE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the state of the channel, request, and verifier in response to a key verification event.
|
||||
* @param {MatrixEvent} event to handle
|
||||
* @param {VerificationRequest} request the request to forward handling to
|
||||
* @returns {Promise} a promise that resolves when any requests as an anwser to the passed-in event are sent.
|
||||
*/
|
||||
async handleEvent(event, request) {
|
||||
const type = InRoomChannel.getEventType(event);
|
||||
// do validations that need state (roomId, userId),
|
||||
// ignore if invalid
|
||||
if (event.getRoomId() !== this._roomId || event.getSender() !== this._userId) {
|
||||
return;
|
||||
}
|
||||
// set transactionId when receiving a .request
|
||||
if (!this._requestEventId && type === REQUEST_TYPE) {
|
||||
this._requestEventId = event.getId();
|
||||
}
|
||||
|
||||
return await request.handleEvent(type, event, InRoomChannel.getTimestamp(event));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the transaction id (relation) back to a received event
|
||||
* so it has the same format as returned by `completeContent` before sending.
|
||||
* The relation can not appear on the event content because of encryption,
|
||||
* relations are excluded from encryption.
|
||||
* @param {MatrixEvent} event the received event
|
||||
* @returns {Object} the content object with the relation added again
|
||||
*/
|
||||
completedContentFromEvent(event) {
|
||||
// ensure m.related_to is included in e2ee rooms
|
||||
// as the field is excluded from encryption
|
||||
const content = Object.assign({}, event.getContent());
|
||||
content[M_RELATES_TO] = event.getRelation();
|
||||
return content;
|
||||
}
|
||||
/**
|
||||
* Add all the fields to content needed for sending it over this channel.
|
||||
* This is public so verification methods (SAS uses this) can get the exact
|
||||
* content that will be sent independent of the used channel,
|
||||
* as they need to calculate the hash of it.
|
||||
* @param {string} type the event type
|
||||
* @param {object} content the (incomplete) content
|
||||
* @returns {object} the complete content, as it will be sent.
|
||||
*/
|
||||
completeContent(type, content) {
|
||||
content = Object.assign({}, content);
|
||||
if (type === REQUEST_TYPE || type === START_TYPE) {
|
||||
content.from_device = this._client.getDeviceId();
|
||||
}
|
||||
if (type === REQUEST_TYPE) {
|
||||
// type is mapped to m.room.message in the send method
|
||||
content = {
|
||||
body: this._client.getUserId() + " is requesting to verify " +
|
||||
"your key, but your client does not support in-chat key " +
|
||||
"verification. You will need to use legacy key " +
|
||||
"verification to verify keys.",
|
||||
msgtype: REQUEST_TYPE,
|
||||
to: this._userId,
|
||||
from_device: content.from_device,
|
||||
methods: content.methods,
|
||||
};
|
||||
} else {
|
||||
content[M_RELATES_TO] = {
|
||||
rel_type: M_REFERENCE,
|
||||
event_id: this.transactionId,
|
||||
};
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an event over the channel with the content not having gone through `completeContent`.
|
||||
* @param {string} type the event type
|
||||
* @param {object} uncompletedContent the (incomplete) content
|
||||
* @returns {Promise} the promise of the request
|
||||
*/
|
||||
send(type, uncompletedContent) {
|
||||
const content = this.completeContent(type, uncompletedContent);
|
||||
return this.sendCompleted(type, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an event over the channel with the content having gone through `completeContent` already.
|
||||
* @param {string} type the event type
|
||||
* @param {object} content
|
||||
* @returns {Promise} the promise of the request
|
||||
*/
|
||||
async sendCompleted(type, content) {
|
||||
let sendType = type;
|
||||
if (type === REQUEST_TYPE) {
|
||||
sendType = MESSAGE_TYPE;
|
||||
}
|
||||
const response = await this._client.sendEvent(this._roomId, sendType, content);
|
||||
if (type === REQUEST_TYPE) {
|
||||
this._requestEventId = response.event_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
Copyright 2019 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.
|
||||
*/
|
||||
|
||||
/** a key verification channel that wraps over an actual channel to pass it to a verifier,
|
||||
* to notify the VerificationRequest when the verifier tries to send anything over the channel.
|
||||
* This way, the VerificationRequest can update its state based on events sent by the verifier.
|
||||
* Anything that is not sending is just routing through to the wrapped channel.
|
||||
*/
|
||||
export default class RequestCallbackChannel {
|
||||
constructor(request, channel) {
|
||||
this._request = request;
|
||||
this._channel = channel;
|
||||
}
|
||||
|
||||
get transactionId() {
|
||||
return this._channel.transactionId;
|
||||
}
|
||||
|
||||
get needsDoneMessage() {
|
||||
return this._channel.needsDoneMessage;
|
||||
}
|
||||
|
||||
handleEvent(event, request) {
|
||||
return this._channel.handleEvent(event, request);
|
||||
}
|
||||
|
||||
completedContentFromEvent(event) {
|
||||
return this._channel.completedContentFromEvent(event);
|
||||
}
|
||||
|
||||
completeContent(type, content) {
|
||||
return this._channel.completeContent(type, content);
|
||||
}
|
||||
|
||||
async send(type, uncompletedContent) {
|
||||
this._request.handleVerifierSend(type, uncompletedContent);
|
||||
const result = await this._channel.send(type, uncompletedContent);
|
||||
return result;
|
||||
}
|
||||
|
||||
async sendCompleted(type, content) {
|
||||
this._request.handleVerifierSend(type, content);
|
||||
const result = await this._channel.sendCompleted(type, content);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
Copyright 2018 New Vector Ltd
|
||||
Copyright 2019 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.
|
||||
*/
|
||||
|
||||
import { randomString } from '../../../randomstring';
|
||||
import logger from '../../../logger';
|
||||
import VerificationRequest, {
|
||||
PHASE_STARTED,
|
||||
REQUEST_TYPE,
|
||||
START_TYPE,
|
||||
CANCEL_TYPE,
|
||||
} from "./VerificationRequest";
|
||||
|
||||
import {
|
||||
newUnexpectedMessageError,
|
||||
errorFromEvent,
|
||||
} from "../Error";
|
||||
/**
|
||||
* A key verification channel that sends verification events over to_device messages.
|
||||
* Generates its own transaction ids.
|
||||
*/
|
||||
export default class ToDeviceChannel {
|
||||
// userId and devices of user we're about to verify
|
||||
constructor(client, userId, devices, transactionId = null, deviceId = null) {
|
||||
this._client = client;
|
||||
this._userId = userId;
|
||||
this._devices = devices;
|
||||
this.transactionId = transactionId;
|
||||
this._deviceId = deviceId;
|
||||
}
|
||||
|
||||
static getEventType(event) {
|
||||
return event.getType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the transaction id used by a given key verification event, if any
|
||||
* @param {MatrixEvent} event the event
|
||||
* @returns {string} the transaction id
|
||||
*/
|
||||
static getTransactionId(event) {
|
||||
const content = event.getContent();
|
||||
return content && content.transaction_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given event type should be allowed to initiate a new VerificationRequest over this channel
|
||||
* @param {string} type the event type to check
|
||||
* @returns {bool} boolean flag
|
||||
*/
|
||||
static canCreateRequest(type) {
|
||||
return type === REQUEST_TYPE || type === START_TYPE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this event is a well-formed key verification event.
|
||||
* This only does checks that don't rely on the current state of a potentially already channel
|
||||
* so we can prevent channels being created by invalid events.
|
||||
* `handleEvent` can do more checks and choose to ignore invalid events.
|
||||
* @param {MatrixEvent} event the event to validate
|
||||
* @param {MatrixClient} client the client to get the current user and device id from
|
||||
* @returns {bool} whether the event is valid and should be passed to handleEvent
|
||||
*/
|
||||
static validateEvent(event, client) {
|
||||
if (event.isCancelled()) {
|
||||
logger.warn("Ignoring flagged verification request from "
|
||||
+ event.getSender());
|
||||
return false;
|
||||
}
|
||||
const content = event.getContent();
|
||||
if (!content) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!content.transaction_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const type = event.getType();
|
||||
|
||||
if (type === REQUEST_TYPE) {
|
||||
if (!Number.isFinite(content.timestamp)) {
|
||||
return false;
|
||||
}
|
||||
if (event.getSender() === client.getUserId() &&
|
||||
content.from_device == client.getDeviceId()
|
||||
) {
|
||||
// ignore requests from ourselves, because it doesn't make sense for a
|
||||
// device to verify itself
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return VerificationRequest.validateEvent(
|
||||
type, event, ToDeviceChannel.getTimestamp(event), client);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {MatrixEvent} event the event to get the timestamp of
|
||||
* @return {number} the timestamp when the event was sent
|
||||
*/
|
||||
static getTimestamp(event) {
|
||||
const content = event.getContent();
|
||||
return content && content.timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the state of the channel, request, and verifier in response to a key verification event.
|
||||
* @param {MatrixEvent} event to handle
|
||||
* @param {VerificationRequest} request the request to forward handling to
|
||||
* @returns {Promise} a promise that resolves when any requests as an anwser to the passed-in event are sent.
|
||||
*/
|
||||
async handleEvent(event, request) {
|
||||
const type = event.getType();
|
||||
const content = event.getContent();
|
||||
if (type === REQUEST_TYPE || type === START_TYPE) {
|
||||
if (!this.transactionId) {
|
||||
this.transactionId = content.transaction_id;
|
||||
}
|
||||
const deviceId = content.from_device;
|
||||
// adopt deviceId if not set before and valid
|
||||
if (!this._deviceId && this._devices.includes(deviceId)) {
|
||||
this._deviceId = deviceId;
|
||||
}
|
||||
// if no device id or different from addopted one, cancel with sender
|
||||
if (!this._deviceId || this._deviceId !== deviceId) {
|
||||
// also check that message came from the device we sent the request to earlier on
|
||||
// and do send a cancel message to that device
|
||||
// (but don't cancel the request for the device we should be talking to)
|
||||
const cancelContent =
|
||||
this.completeContent(errorFromEvent(newUnexpectedMessageError()));
|
||||
return this._sendToDevices(CANCEL_TYPE, cancelContent, [deviceId]);
|
||||
}
|
||||
}
|
||||
|
||||
const wasStarted = request.phase === PHASE_STARTED;
|
||||
await request.handleEvent(
|
||||
event.getType(), event, ToDeviceChannel.getTimestamp(event));
|
||||
const isStarted = request.phase === PHASE_STARTED;
|
||||
|
||||
// the request has picked a start event, tell the other devices about it
|
||||
if (type === START_TYPE && !wasStarted && isStarted && this._deviceId) {
|
||||
const nonChosenDevices = this._devices.filter(d => d !== this._deviceId);
|
||||
if (nonChosenDevices.length) {
|
||||
const message = this.completeContent({
|
||||
code: "m.accepted",
|
||||
reason: "Verification request accepted by another device",
|
||||
});
|
||||
await this._sendToDevices(CANCEL_TYPE, message, nonChosenDevices);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* See {InRoomChannel.completedContentFromEvent} why this is needed.
|
||||
* @param {MatrixEvent} event the received event
|
||||
* @returns {Object} the content object
|
||||
*/
|
||||
completedContentFromEvent(event) {
|
||||
return event.getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all the fields to content needed for sending it over this channel.
|
||||
* This is public so verification methods (SAS uses this) can get the exact
|
||||
* content that will be sent independent of the used channel,
|
||||
* as they need to calculate the hash of it.
|
||||
* @param {string} type the event type
|
||||
* @param {object} content the (incomplete) content
|
||||
* @returns {object} the complete content, as it will be sent.
|
||||
*/
|
||||
completeContent(type, content) {
|
||||
// make a copy
|
||||
content = Object.assign({}, content);
|
||||
if (this.transactionId) {
|
||||
content.transaction_id = this.transactionId;
|
||||
}
|
||||
if (type === REQUEST_TYPE || type === START_TYPE) {
|
||||
content.from_device = this._client.getDeviceId();
|
||||
}
|
||||
if (type === REQUEST_TYPE) {
|
||||
content.timestamp = Date.now();
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an event over the channel with the content not having gone through `completeContent`.
|
||||
* @param {string} type the event type
|
||||
* @param {object} uncompletedContent the (incomplete) content
|
||||
* @returns {Promise} the promise of the request
|
||||
*/
|
||||
send(type, uncompletedContent = {}) {
|
||||
// create transaction id when sending request
|
||||
if ((type === REQUEST_TYPE || type === START_TYPE) && !this.transactionId) {
|
||||
this.transactionId = ToDeviceChannel.makeTransactionId();
|
||||
}
|
||||
const content = this.completeContent(type, uncompletedContent);
|
||||
return this.sendCompleted(type, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an event over the channel with the content having gone through `completeContent` already.
|
||||
* @param {string} type the event type
|
||||
* @param {object} content
|
||||
* @returns {Promise} the promise of the request
|
||||
*/
|
||||
sendCompleted(type, content) {
|
||||
if (type === REQUEST_TYPE) {
|
||||
return this._sendToDevices(type, content, this._devices);
|
||||
} else {
|
||||
return this._sendToDevices(type, content, [this._deviceId]);
|
||||
}
|
||||
}
|
||||
|
||||
_sendToDevices(type, content, devices) {
|
||||
if (devices.length) {
|
||||
const msgMap = {};
|
||||
for (const deviceId of devices) {
|
||||
msgMap[deviceId] = content;
|
||||
}
|
||||
|
||||
return this._client.sendToDevice(type, {[this._userId]: msgMap});
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow Crypto module to create and know the transaction id before the .start event gets sent.
|
||||
* @returns {string} the transaction id
|
||||
*/
|
||||
static makeTransactionId() {
|
||||
return randomString(32);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,414 @@
|
||||
/*
|
||||
Copyright 2018 New Vector Ltd
|
||||
Copyright 2019 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.
|
||||
*/
|
||||
|
||||
import logger from '../../../logger';
|
||||
import RequestCallbackChannel from "./RequestCallbackChannel";
|
||||
import {EventEmitter} from 'events';
|
||||
import {
|
||||
newUnknownMethodError,
|
||||
newUnexpectedMessageError,
|
||||
errorFromEvent,
|
||||
errorFactory,
|
||||
} from "../Error";
|
||||
|
||||
// the recommended amount of time before a verification request
|
||||
// should be (automatically) cancelled without user interaction
|
||||
// and ignored.
|
||||
const VERIFICATION_REQUEST_TIMEOUT = 10 * 60 * 1000; //10m
|
||||
// to avoid almost expired verification notifications
|
||||
// from showing a notification and almost immediately
|
||||
// disappearing, also ignore verification requests that
|
||||
// are this amount of time away from expiring.
|
||||
const VERIFICATION_REQUEST_MARGIN = 3 * 1000; //3s
|
||||
|
||||
|
||||
export const EVENT_PREFIX = "m.key.verification.";
|
||||
export const REQUEST_TYPE = EVENT_PREFIX + "request";
|
||||
export const START_TYPE = EVENT_PREFIX + "start";
|
||||
export const CANCEL_TYPE = EVENT_PREFIX + "cancel";
|
||||
export const DONE_TYPE = EVENT_PREFIX + "done";
|
||||
// export const READY_TYPE = EVENT_PREFIX + "ready";
|
||||
|
||||
export const PHASE_UNSENT = 1;
|
||||
export const PHASE_REQUESTED = 2;
|
||||
// const PHASE_READY = 3;
|
||||
export const PHASE_STARTED = 4;
|
||||
export const PHASE_CANCELLED = 5;
|
||||
export const PHASE_DONE = 6;
|
||||
|
||||
|
||||
/**
|
||||
* State machine for verification requests.
|
||||
* Things that differ based on what channel is used to
|
||||
* send and receive verification events are put in `InRoomChannel` or `ToDeviceChannel`.
|
||||
* @event "change" whenever the state of the request object has changed.
|
||||
*/
|
||||
export default class VerificationRequest extends EventEmitter {
|
||||
constructor(channel, verificationMethods, userId, client) {
|
||||
super();
|
||||
this.channel = channel;
|
||||
this._verificationMethods = verificationMethods;
|
||||
this._client = client;
|
||||
this._commonMethods = [];
|
||||
this._setPhase(PHASE_UNSENT, false);
|
||||
this._requestEvent = null;
|
||||
this._otherUserId = userId;
|
||||
this._initiatedByMe = null;
|
||||
this._startTimestamp = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stateless validation logic not specific to the channel.
|
||||
* Invoked by the same static method in either channel.
|
||||
* @param {string} type the "symbolic" event type, as returned by the `getEventType` function on the channel.
|
||||
* @param {MatrixEvent} event the event to validate. Don't call getType() on it but use the `type` parameter instead.
|
||||
* @param {number} timestamp the timestamp in milliseconds when this event was sent.
|
||||
* @param {MatrixClient} client the client to get the current user and device id from
|
||||
* @returns {bool} whether the event is valid and should be passed to handleEvent
|
||||
*/
|
||||
static validateEvent(type, event, timestamp, client) {
|
||||
const content = event.getContent();
|
||||
|
||||
if (!type.startsWith(EVENT_PREFIX)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type === REQUEST_TYPE) {
|
||||
if (!Array.isArray(content.methods)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (type === REQUEST_TYPE || type === START_TYPE) {
|
||||
if (typeof content.from_device !== "string" ||
|
||||
content.from_device.length === 0
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// a timestamp is not provided on all to_device events
|
||||
if (Number.isFinite(timestamp)) {
|
||||
const elapsed = Date.now() - timestamp;
|
||||
// ignore if event is too far in the past or too far in the future
|
||||
if (elapsed > (VERIFICATION_REQUEST_TIMEOUT - VERIFICATION_REQUEST_MARGIN) ||
|
||||
elapsed < -(VERIFICATION_REQUEST_TIMEOUT / 2)) {
|
||||
logger.log("received verification that is too old or from the future");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** once the phase is PHASE_STARTED, common methods supported by both sides */
|
||||
get methods() {
|
||||
return this._commonMethods;
|
||||
}
|
||||
|
||||
/** the timeout of the request, provided for compatibility with previous verification code */
|
||||
get timeout() {
|
||||
const elapsed = Date.now() - this._startTimestamp;
|
||||
return Math.max(0, VERIFICATION_REQUEST_TIMEOUT - elapsed);
|
||||
}
|
||||
|
||||
/** the m.key.verification.request event that started this request, provided for compatibility with previous verification code */
|
||||
get event() {
|
||||
return this._requestEvent;
|
||||
}
|
||||
|
||||
/** current phase of the request. Some properties might only be defined in a current phase. */
|
||||
get phase() {
|
||||
return this._phase;
|
||||
}
|
||||
|
||||
/** The verifier to do the actual verification, once the method has been established. Only defined when the `phase` is PHASE_STARTED. */
|
||||
get verifier() {
|
||||
return this._verifier;
|
||||
}
|
||||
|
||||
/** whether this request has sent it's initial event and needs more events to complete */
|
||||
get pending() {
|
||||
return this._phase !== PHASE_UNSENT
|
||||
&& this._phase !== PHASE_DONE
|
||||
&& this._phase !== PHASE_CANCELLED;
|
||||
}
|
||||
|
||||
/** Whether this request was initiated by the syncing user.
|
||||
* For InRoomChannel, this is who sent the .request event.
|
||||
* For ToDeviceChannel, this is who sent the .start event
|
||||
*/
|
||||
get initiatedByMe() {
|
||||
return this._initiatedByMe;
|
||||
}
|
||||
|
||||
/** the id of the user that initiated the request */
|
||||
get requestingUserId() {
|
||||
if (this.initiatedByMe) {
|
||||
return this._client.getUserId();
|
||||
} else {
|
||||
return this._otherUserId;
|
||||
}
|
||||
}
|
||||
|
||||
/** the id of the user that (will) receive(d) the request */
|
||||
get receivingUserId() {
|
||||
if (this.initiatedByMe) {
|
||||
return this._otherUserId;
|
||||
} else {
|
||||
return this._client.getUserId();
|
||||
}
|
||||
}
|
||||
|
||||
/* Start the key verification, creating a verifier and sending a .start event.
|
||||
* If no previous events have been sent, pass in `targetDevice` to set who to direct this request to.
|
||||
* @param {string} method the name of the verification method to use.
|
||||
* @param {string?} targetDevice.userId the id of the user to direct this request to
|
||||
* @param {string?} targetDevice.deviceId the id of the device to direct this request to
|
||||
* @returns {VerifierBase} the verifier of the given method
|
||||
*/
|
||||
beginKeyVerification(method, targetDevice = null) {
|
||||
// need to allow also when unsent in case of to_device
|
||||
if (!this._verifier) {
|
||||
if (this._hasValidPreStartPhase()) {
|
||||
// when called on a request that was initiated with .request event
|
||||
// check the method is supported by both sides
|
||||
if (this._commonMethods.length && !this._commonMethods.includes(method)) {
|
||||
throw newUnknownMethodError();
|
||||
}
|
||||
this._verifier = this._createVerifier(method, null, targetDevice);
|
||||
if (!this._verifier) {
|
||||
throw newUnknownMethodError();
|
||||
}
|
||||
}
|
||||
}
|
||||
return this._verifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* sends the initial .request event.
|
||||
* @returns {Promise} resolves when the event has been sent.
|
||||
*/
|
||||
async sendRequest() {
|
||||
if (this._phase === PHASE_UNSENT) {
|
||||
this._initiatedByMe = true;
|
||||
this._setPhase(PHASE_REQUESTED, false);
|
||||
const methods = [...this._verificationMethods.keys()];
|
||||
await this.channel.send(REQUEST_TYPE, {methods});
|
||||
this.emit("change");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the request, sending a cancellation to the other party
|
||||
* @param {string?} error.reason the error reason to send the cancellation with
|
||||
* @param {string?} error.code the error code to send the cancellation with
|
||||
* @returns {Promise} resolves when the event has been sent.
|
||||
*/
|
||||
async cancel({reason = "User declined", code = "m.user"} = {}) {
|
||||
if (this._phase !== PHASE_CANCELLED) {
|
||||
if (this._verifier) {
|
||||
return this._verifier.cancel(errorFactory(code, reason));
|
||||
} else {
|
||||
this._setPhase(PHASE_CANCELLED, false);
|
||||
await this.channel.send(CANCEL_TYPE, {code, reason});
|
||||
}
|
||||
this.emit("change");
|
||||
}
|
||||
}
|
||||
|
||||
/** @returns {Promise} with the verifier once it becomes available. Can be used after calling `sendRequest`. */
|
||||
waitForVerifier() {
|
||||
if (this.verifier) {
|
||||
return Promise.resolve(this.verifier);
|
||||
} else {
|
||||
return new Promise(resolve => {
|
||||
const checkVerifier = () => {
|
||||
if (this.verifier) {
|
||||
this.off("change", checkVerifier);
|
||||
resolve(this.verifier);
|
||||
}
|
||||
};
|
||||
this.on("change", checkVerifier);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
_setPhase(phase, notify = true) {
|
||||
this._phase = phase;
|
||||
if (notify) {
|
||||
this.emit("change");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the state of the request and verifier in response to a key verification event.
|
||||
* @param {string} type the "symbolic" event type, as returned by the `getEventType` function on the channel.
|
||||
* @param {MatrixEvent} event the event to handle. Don't call getType() on it but use the `type` parameter instead.
|
||||
* @param {number} timestamp the timestamp in milliseconds when this event was sent.
|
||||
* @returns {Promise} a promise that resolves when any requests as an anwser to the passed-in event are sent.
|
||||
*/
|
||||
async handleEvent(type, event, timestamp) {
|
||||
const content = event.getContent();
|
||||
if (type === REQUEST_TYPE || type === START_TYPE) {
|
||||
if (this._startTimestamp === null) {
|
||||
this._startTimestamp = timestamp;
|
||||
}
|
||||
}
|
||||
if (type === REQUEST_TYPE) {
|
||||
await this._handleRequest(content, event);
|
||||
} else if (type === START_TYPE) {
|
||||
await this._handleStart(content, event);
|
||||
}
|
||||
|
||||
if (this._verifier) {
|
||||
if (type === CANCEL_TYPE || (this._verifier.events
|
||||
&& this._verifier.events.includes(type))) {
|
||||
this._verifier.handleEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
if (type === CANCEL_TYPE) {
|
||||
this._handleCancel();
|
||||
} else if (type === DONE_TYPE) {
|
||||
this._handleDone();
|
||||
}
|
||||
}
|
||||
|
||||
async _handleRequest(content, event) {
|
||||
if (this._phase === PHASE_UNSENT) {
|
||||
const otherMethods = content.methods;
|
||||
this._commonMethods = otherMethods.
|
||||
filter(m => this._verificationMethods.has(m));
|
||||
this._requestEvent = event;
|
||||
this._initiatedByMe = this._wasSentByMe(event);
|
||||
this._setPhase(PHASE_REQUESTED);
|
||||
} else if (this._phase !== PHASE_REQUESTED) {
|
||||
logger.warn("Ignoring flagged verification request from " +
|
||||
event.getSender());
|
||||
await this.cancel(errorFromEvent(newUnexpectedMessageError()));
|
||||
}
|
||||
}
|
||||
|
||||
_hasValidPreStartPhase() {
|
||||
return this._phase === PHASE_REQUESTED ||
|
||||
(
|
||||
this.channel.constructor.canCreateRequest(START_TYPE) &&
|
||||
this._phase === PHASE_UNSENT
|
||||
);
|
||||
}
|
||||
|
||||
async _handleStart(content, event) {
|
||||
if (this._hasValidPreStartPhase()) {
|
||||
const {method} = content;
|
||||
if (!this._verificationMethods.has(method)) {
|
||||
await this.cancel(errorFromEvent(newUnknownMethodError()));
|
||||
} else {
|
||||
// if not in requested phase
|
||||
if (this.phase === PHASE_UNSENT) {
|
||||
this._initiatedByMe = this._wasSentByMe(event);
|
||||
}
|
||||
this._verifier = this._createVerifier(method, event);
|
||||
this._setPhase(PHASE_STARTED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by RequestCallbackChannel when the verifier sends an event
|
||||
* @param {string} type the "symbolic" event type
|
||||
* @param {object} content the completed or uncompleted content for the event to be sent
|
||||
*/
|
||||
handleVerifierSend(type, content) {
|
||||
if (type === CANCEL_TYPE) {
|
||||
this._handleCancel();
|
||||
} else if (type === START_TYPE) {
|
||||
if (this._phase === PHASE_UNSENT || this._phase === PHASE_REQUESTED) {
|
||||
// if unsent, we're sending a (first) .start event and hence requesting the verification.
|
||||
// in any other situation, the request was initiated by the other party.
|
||||
this._initiatedByMe = this.phase === PHASE_UNSENT;
|
||||
this._setPhase(PHASE_STARTED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_handleCancel() {
|
||||
if (this._phase !== PHASE_CANCELLED) {
|
||||
this._setPhase(PHASE_CANCELLED);
|
||||
}
|
||||
}
|
||||
|
||||
_handleDone() {
|
||||
if (this._phase === PHASE_STARTED) {
|
||||
this._setPhase(PHASE_DONE);
|
||||
}
|
||||
}
|
||||
|
||||
_createVerifier(method, startEvent = null, targetDevice = null) {
|
||||
const startSentByMe = startEvent && this._wasSentByMe(startEvent);
|
||||
const {userId, deviceId} = this._getVerifierTarget(startEvent, targetDevice);
|
||||
|
||||
const VerifierCtor = this._verificationMethods.get(method);
|
||||
if (!VerifierCtor) {
|
||||
console.warn("could not find verifier constructor for method", method);
|
||||
return;
|
||||
}
|
||||
// invokes handleVerifierSend when verifier sends something
|
||||
const callbackMedium = new RequestCallbackChannel(this, this.channel);
|
||||
return new VerifierCtor(
|
||||
callbackMedium,
|
||||
this._client,
|
||||
userId,
|
||||
deviceId,
|
||||
startSentByMe ? null : startEvent,
|
||||
);
|
||||
}
|
||||
|
||||
_getVerifierTarget(startEvent, targetDevice) {
|
||||
// targetDevice should be set when creating a verifier for to_device before the .start event has been sent,
|
||||
// so the userId and deviceId are provided
|
||||
if (targetDevice) {
|
||||
return targetDevice;
|
||||
} else {
|
||||
let targetEvent;
|
||||
if (startEvent && !this._wasSentByMe(startEvent)) {
|
||||
targetEvent = startEvent;
|
||||
} else if (this._requestEvent && !this._wasSentByMe(this._requestEvent)) {
|
||||
targetEvent = this._requestEvent;
|
||||
} else {
|
||||
throw new Error(
|
||||
"can't determine who the verifier should be targeted at. " +
|
||||
"No .request or .start event and no targetDevice");
|
||||
}
|
||||
const userId = targetEvent.getSender();
|
||||
const content = targetEvent.getContent();
|
||||
const deviceId = content && content.from_device;
|
||||
return {userId, deviceId};
|
||||
}
|
||||
}
|
||||
|
||||
// only for .request and .start
|
||||
_wasSentByMe(event) {
|
||||
if (event.getSender() !== this._client.getUserId()) {
|
||||
return false;
|
||||
}
|
||||
const content = event.getContent();
|
||||
if (!content || content.from_device !== this._client.getDeviceId()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+3
-4
@@ -19,7 +19,6 @@ limitations under the License.
|
||||
* This is an internal module. See {@link MatrixHttpApi} for the public class.
|
||||
* @module http-api
|
||||
*/
|
||||
import Promise from 'bluebird';
|
||||
const parseContentType = require('content-type').parse;
|
||||
|
||||
const utils = require("./utils");
|
||||
@@ -256,7 +255,7 @@ module.exports.MatrixHttpApi.prototype = {
|
||||
}
|
||||
|
||||
if (global.XMLHttpRequest) {
|
||||
const defer = Promise.defer();
|
||||
const defer = utils.defer();
|
||||
const xhr = new global.XMLHttpRequest();
|
||||
upload.xhr = xhr;
|
||||
const cb = requestCallback(defer, opts.callback, this.opts.onlyData);
|
||||
@@ -418,7 +417,7 @@ module.exports.MatrixHttpApi.prototype = {
|
||||
opts.headers['Authorization'] = `Bearer ${accessToken}`;
|
||||
}
|
||||
|
||||
const defer = Promise.defer();
|
||||
const defer = utils.defer();
|
||||
this.opts.request(
|
||||
opts,
|
||||
requestCallback(defer, callback, this.opts.onlyData),
|
||||
@@ -682,7 +681,7 @@ module.exports.MatrixHttpApi.prototype = {
|
||||
}
|
||||
}
|
||||
|
||||
const defer = Promise.defer();
|
||||
const defer = utils.defer();
|
||||
|
||||
let timeoutId;
|
||||
let timedOut = false;
|
||||
|
||||
@@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
|
||||
/**
|
||||
* Check if an IndexedDB database exists. The only way to do so is to try opening it, so
|
||||
* we do that and then delete it did not exist before.
|
||||
|
||||
@@ -18,7 +18,6 @@ limitations under the License.
|
||||
"use strict";
|
||||
|
||||
/** @module interactive-auth */
|
||||
import Promise from 'bluebird';
|
||||
const url = require("url");
|
||||
|
||||
const utils = require("./utils");
|
||||
|
||||
+9
-1
@@ -21,7 +21,6 @@ limitations under the License.
|
||||
* @module models/event
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
import {EventEmitter} from 'events';
|
||||
import utils from '../utils.js';
|
||||
import logger from '../logger';
|
||||
@@ -297,6 +296,15 @@ utils.extend(module.exports.MatrixEvent.prototype, {
|
||||
return this.getUnsigned().age || this.event.age; // v2 / v1
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the age of the event when this function was called.
|
||||
* Relies on the local clock being in sync with the clock of the original homeserver.
|
||||
* @return {Number} The age of this event in milliseconds.
|
||||
*/
|
||||
getLocalAge: function() {
|
||||
return Date.now() - this.getTs();
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the event state_key if it has one. This will return <code>undefined
|
||||
* </code> for message events.
|
||||
|
||||
@@ -15,7 +15,7 @@ limitations under the License.
|
||||
*/
|
||||
|
||||
import EventEmitter from 'events';
|
||||
import { EventStatus } from '../../lib/models/event';
|
||||
import { EventStatus } from '../models/event';
|
||||
|
||||
/**
|
||||
* A container for relation events that supports easy access to common ways of
|
||||
|
||||
@@ -311,10 +311,18 @@ function calculateDisplayName(selfUserId, displayName, roomState) {
|
||||
// Next check if the name contains something that look like a mxid
|
||||
// If it does, it may be someone trying to impersonate someone else
|
||||
// Show full mxid in this case
|
||||
// Also show mxid if there are other people with the same or similar
|
||||
// displayname, after hidden character removal.
|
||||
let disambiguate = /@.+:.+/.test(displayName);
|
||||
|
||||
if (!disambiguate) {
|
||||
// Also show mxid if the display name contains any LTR/RTL characters as these
|
||||
// make it very difficult for us to find similar *looking* display names
|
||||
// E.g "Mark" could be cloned by writing "kraM" but in RTL.
|
||||
disambiguate = /[\u200E\u200F\u202A-\u202F]/.test(displayName);
|
||||
}
|
||||
|
||||
if (!disambiguate) {
|
||||
// Also show mxid if there are other people with the same or similar
|
||||
// displayname, after hidden character removal.
|
||||
const userIds = roomState.getUserIdsWithDisplayName(displayName);
|
||||
disambiguate = userIds.some((u) => u !== selfUserId);
|
||||
}
|
||||
|
||||
+11
-5
@@ -839,9 +839,15 @@ Room.prototype.getAliases = function() {
|
||||
for (let i = 0; i < aliasEvents.length; ++i) {
|
||||
const aliasEvent = aliasEvents[i];
|
||||
if (utils.isArray(aliasEvent.getContent().aliases)) {
|
||||
Array.prototype.push.apply(
|
||||
aliasStrings, aliasEvent.getContent().aliases,
|
||||
);
|
||||
const filteredAliases = aliasEvent.getContent().aliases.filter(a => {
|
||||
if (typeof(a) !== "string") return false;
|
||||
if (a[0] !== '#') return false;
|
||||
if (!a.endsWith(`:${aliasEvent.getStateKey()}`)) return false;
|
||||
|
||||
// It's probably valid by here.
|
||||
return true;
|
||||
});
|
||||
Array.prototype.push.apply(aliasStrings, filteredAliases);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -895,7 +901,7 @@ Room.prototype.addEventsToTimeline = function(events, toStartOfTimeline,
|
||||
* @return {RoomMember} The member or <code>null</code>.
|
||||
*/
|
||||
Room.prototype.getMember = function(userId) {
|
||||
return this.currentState.getMember(userId);
|
||||
return this.currentState.getMember(userId);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -903,7 +909,7 @@ Room.prototype.addEventsToTimeline = function(events, toStartOfTimeline,
|
||||
* @return {RoomMember[]} A list of currently joined members.
|
||||
*/
|
||||
Room.prototype.getJoinedMembers = function() {
|
||||
return this.getMembersWithMembership("join");
|
||||
return this.getMembersWithMembership("join");
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,7 +15,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import {escapeRegExp, globToRegexp} from "./utils";
|
||||
import {escapeRegExp, globToRegexp, isNullOrUndefined} from "./utils";
|
||||
|
||||
/**
|
||||
* @module pushprocessor
|
||||
@@ -268,7 +268,7 @@ function PushProcessor(client) {
|
||||
}
|
||||
|
||||
const val = valueForDottedKey(cond.key, ev);
|
||||
if (!val || typeof val != 'string') {
|
||||
if (typeof val !== 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -304,10 +304,10 @@ function PushProcessor(client) {
|
||||
|
||||
// special-case the first component to deal with encrypted messages
|
||||
const firstPart = parts[0];
|
||||
if (firstPart == 'content') {
|
||||
if (firstPart === 'content') {
|
||||
val = ev.getContent();
|
||||
parts.shift();
|
||||
} else if (firstPart == 'type') {
|
||||
} else if (firstPart === 'type') {
|
||||
val = ev.getType();
|
||||
parts.shift();
|
||||
} else {
|
||||
@@ -316,11 +316,11 @@ function PushProcessor(client) {
|
||||
}
|
||||
|
||||
while (parts.length > 0) {
|
||||
const thispart = parts.shift();
|
||||
if (!val[thispart]) {
|
||||
const thisPart = parts.shift();
|
||||
if (isNullOrUndefined(val[thisPart])) {
|
||||
return null;
|
||||
}
|
||||
val = val[thispart];
|
||||
val = val[thisPart];
|
||||
}
|
||||
return val;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
Copyright 2018 New Vector Ltd
|
||||
Copyright 2019 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.
|
||||
@@ -14,7 +15,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
export function randomString(len) {
|
||||
export function randomString(len: number): string {
|
||||
let ret = "";
|
||||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
@@ -48,7 +48,7 @@ const debuglog = function() {};
|
||||
*
|
||||
* Intended for use by the unit tests.
|
||||
*
|
||||
* @param {function} f function which should return a millisecond counter
|
||||
* @param {function} [f] function which should return a millisecond counter
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
|
||||
+6
-2
@@ -20,7 +20,6 @@ limitations under the License.
|
||||
* @module scheduler
|
||||
*/
|
||||
const utils = require("./utils");
|
||||
import Promise from 'bluebird';
|
||||
import logger from './logger';
|
||||
|
||||
const DEBUG = false; // set true to enable console logging.
|
||||
@@ -121,7 +120,7 @@ MatrixScheduler.prototype.queueEvent = function(event) {
|
||||
if (!this._queues[queueName]) {
|
||||
this._queues[queueName] = [];
|
||||
}
|
||||
const defer = Promise.defer();
|
||||
const defer = utils.defer();
|
||||
this._queues[queueName].push({
|
||||
event: event,
|
||||
defer: defer,
|
||||
@@ -157,6 +156,11 @@ MatrixScheduler.RETRY_BACKOFF_RATELIMIT = function(event, attempts, err) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// if event that we are trying to send is too large in any way then retrying won't help
|
||||
if (err.name === "M_TOO_LARGE") {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (err.name === "M_LIMIT_EXCEEDED") {
|
||||
const waitTime = err.data.retry_after_ms;
|
||||
if (waitTime) {
|
||||
|
||||
@@ -15,7 +15,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
import SyncAccumulator from "../sync-accumulator";
|
||||
import utils from "../utils";
|
||||
import * as IndexedDBHelpers from "../indexeddb-helpers";
|
||||
@@ -436,7 +435,7 @@ LocalIndexedDBStoreBackend.prototype = {
|
||||
*/
|
||||
_persistSyncData: function(nextBatch, roomsData, groupsData) {
|
||||
logger.log("Persisting sync data up to ", nextBatch);
|
||||
return Promise.try(() => {
|
||||
return utils.promiseTry(() => {
|
||||
const txn = this.db.transaction(["sync"], "readwrite");
|
||||
const store = txn.objectStore("sync");
|
||||
store.put({
|
||||
@@ -456,7 +455,7 @@ LocalIndexedDBStoreBackend.prototype = {
|
||||
* @return {Promise} Resolves if the events were persisted.
|
||||
*/
|
||||
_persistAccountData: function(accountData) {
|
||||
return Promise.try(() => {
|
||||
return utils.promiseTry(() => {
|
||||
const txn = this.db.transaction(["accountData"], "readwrite");
|
||||
const store = txn.objectStore("accountData");
|
||||
for (let i = 0; i < accountData.length; i++) {
|
||||
@@ -475,7 +474,7 @@ LocalIndexedDBStoreBackend.prototype = {
|
||||
* @return {Promise} Resolves if the users were persisted.
|
||||
*/
|
||||
_persistUserPresenceEvents: function(tuples) {
|
||||
return Promise.try(() => {
|
||||
return utils.promiseTry(() => {
|
||||
const txn = this.db.transaction(["users"], "readwrite");
|
||||
const store = txn.objectStore("users");
|
||||
for (const tuple of tuples) {
|
||||
@@ -495,7 +494,7 @@ LocalIndexedDBStoreBackend.prototype = {
|
||||
* @return {Promise<Object[]>} A list of presence events in their raw form.
|
||||
*/
|
||||
getUserPresenceEvents: function() {
|
||||
return Promise.try(() => {
|
||||
return utils.promiseTry(() => {
|
||||
const txn = this.db.transaction(["users"], "readonly");
|
||||
const store = txn.objectStore("users");
|
||||
return selectQuery(store, undefined, (cursor) => {
|
||||
@@ -512,7 +511,7 @@ LocalIndexedDBStoreBackend.prototype = {
|
||||
logger.log(
|
||||
`LocalIndexedDBStoreBackend: loading account data...`,
|
||||
);
|
||||
return Promise.try(() => {
|
||||
return utils.promiseTry(() => {
|
||||
const txn = this.db.transaction(["accountData"], "readonly");
|
||||
const store = txn.objectStore("accountData");
|
||||
return selectQuery(store, undefined, (cursor) => {
|
||||
@@ -534,7 +533,7 @@ LocalIndexedDBStoreBackend.prototype = {
|
||||
logger.log(
|
||||
`LocalIndexedDBStoreBackend: loading sync data...`,
|
||||
);
|
||||
return Promise.try(() => {
|
||||
return utils.promiseTry(() => {
|
||||
const txn = this.db.transaction(["sync"], "readonly");
|
||||
const store = txn.objectStore("sync");
|
||||
return selectQuery(store, undefined, (cursor) => {
|
||||
|
||||
@@ -15,8 +15,8 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
import logger from '../logger';
|
||||
import {defer} from '../utils';
|
||||
|
||||
/**
|
||||
* An IndexedDB store backend where the actual backend sits in a web
|
||||
@@ -152,7 +152,7 @@ RemoteIndexedDBStoreBackend.prototype = {
|
||||
// the promise automatically gets rejected
|
||||
return Promise.resolve().then(() => {
|
||||
const seq = this._nextSeq++;
|
||||
const def = Promise.defer();
|
||||
const def = defer();
|
||||
|
||||
this._inFlight[seq] = def;
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import Promise from 'bluebird';
|
||||
import LocalIndexedDBStoreBackend from "./indexeddb-local-backend.js";
|
||||
import logger from '../logger';
|
||||
|
||||
@@ -123,7 +122,7 @@ class IndexedDBStoreWorker {
|
||||
return;
|
||||
}
|
||||
|
||||
prom.done((ret) => {
|
||||
prom.then((ret) => {
|
||||
this.postMessage.call(null, {
|
||||
command: 'cmd_success',
|
||||
seq: msg.seq,
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
|
||||
/* eslint-disable babel/no-invalid-this */
|
||||
|
||||
import Promise from 'bluebird';
|
||||
import {MemoryStore} from "./memory";
|
||||
import utils from "../utils";
|
||||
import {EventEmitter} from 'events';
|
||||
|
||||
@@ -22,7 +22,6 @@ limitations under the License.
|
||||
*/
|
||||
const utils = require("../utils");
|
||||
const User = require("../models/user");
|
||||
import Promise from 'bluebird';
|
||||
|
||||
/**
|
||||
* Construct a new in-memory data store for the Matrix Client.
|
||||
|
||||
@@ -16,7 +16,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
"use strict";
|
||||
import Promise from 'bluebird';
|
||||
/**
|
||||
* This is an internal module.
|
||||
* @module store/stub
|
||||
|
||||
+7
-8
@@ -25,7 +25,6 @@ limitations under the License.
|
||||
* an alternative syncing API, we may want to have a proper syncing interface
|
||||
* for HTTP and WS at some point.
|
||||
*/
|
||||
import Promise from 'bluebird';
|
||||
const User = require("./models/user");
|
||||
const Room = require("./models/room");
|
||||
const Group = require('./models/group');
|
||||
@@ -360,7 +359,7 @@ SyncApi.prototype._peekPoll = function(peekRoom, token) {
|
||||
room_id: peekRoom.roomId,
|
||||
timeout: 30 * 1000,
|
||||
from: token,
|
||||
}, undefined, 50 * 1000).done(function(res) {
|
||||
}, undefined, 50 * 1000).then(function(res) {
|
||||
if (self._peekRoomId !== peekRoom.roomId) {
|
||||
debuglog("Stopped peeking in room %s", peekRoom.roomId);
|
||||
return;
|
||||
@@ -1150,7 +1149,7 @@ SyncApi.prototype._processSyncResponse = async function(
|
||||
});
|
||||
|
||||
// Handle joins
|
||||
await Promise.mapSeries(joinRooms, async function(joinObj) {
|
||||
await utils.promiseMapSeries(joinRooms, async function(joinObj) {
|
||||
const room = joinObj.room;
|
||||
const stateEvents = self._mapSyncEventsFormat(joinObj.state, room);
|
||||
const timelineEvents = self._mapSyncEventsFormat(joinObj.timeline, room);
|
||||
@@ -1278,8 +1277,8 @@ SyncApi.prototype._processSyncResponse = async function(
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.mapSeries(stateEvents, processRoomEvent);
|
||||
await Promise.mapSeries(timelineEvents, processRoomEvent);
|
||||
await utils.promiseMapSeries(stateEvents, processRoomEvent);
|
||||
await utils.promiseMapSeries(timelineEvents, processRoomEvent);
|
||||
ephemeralEvents.forEach(function(e) {
|
||||
client.emit("event", e);
|
||||
});
|
||||
@@ -1383,7 +1382,7 @@ SyncApi.prototype._startKeepAlives = function(delay) {
|
||||
self._pokeKeepAlive();
|
||||
}
|
||||
if (!this._connectionReturnedDefer) {
|
||||
this._connectionReturnedDefer = Promise.defer();
|
||||
this._connectionReturnedDefer = utils.defer();
|
||||
}
|
||||
return this._connectionReturnedDefer.promise;
|
||||
};
|
||||
@@ -1417,7 +1416,7 @@ SyncApi.prototype._pokeKeepAlive = function(connDidFail) {
|
||||
prefix: '',
|
||||
localTimeoutMs: 15 * 1000,
|
||||
},
|
||||
).done(function() {
|
||||
).then(function() {
|
||||
success();
|
||||
}, function(err) {
|
||||
if (err.httpStatus == 400 || err.httpStatus == 404) {
|
||||
@@ -1541,7 +1540,7 @@ SyncApi.prototype._resolveInvites = function(room) {
|
||||
} else {
|
||||
promise = client.getProfileInfo(member.userId);
|
||||
}
|
||||
promise.done(function(info) {
|
||||
promise.then(function(info) {
|
||||
// slightly naughty by doctoring the invite event but this means all
|
||||
// the code paths remain the same between invite/join display name stuff
|
||||
// which is a worthy trade-off for some minor pollution.
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
|
||||
/** @module timeline-window */
|
||||
|
||||
import Promise from 'bluebird';
|
||||
const EventTimeline = require("./models/event-timeline");
|
||||
import logger from './logger';
|
||||
|
||||
@@ -132,14 +131,15 @@ TimelineWindow.prototype.load = function(initialEventId, initialWindowSize) {
|
||||
// feeling snappy.
|
||||
//
|
||||
if (initialEventId) {
|
||||
const prom = this._client.getEventTimeline(this._timelineSet, initialEventId);
|
||||
|
||||
if (prom.isFulfilled()) {
|
||||
initFields(prom.value());
|
||||
return Promise.resolve();
|
||||
} else {
|
||||
return prom.then(initFields);
|
||||
const timeline = this._timelineSet.getTimelineForEvent(initialEventId);
|
||||
if (timeline) {
|
||||
// hot-path optimization to save a reactor tick by replicating the sync check getTimelineForEvent does.
|
||||
initFields(timeline);
|
||||
return Promise.resolve(timeline);
|
||||
}
|
||||
|
||||
const prom = this._client.getEventTimeline(this._timelineSet, initialEventId);
|
||||
return prom.then(initFields);
|
||||
} else {
|
||||
const tl = this._timelineSet.getLiveTimeline();
|
||||
initFields(tl);
|
||||
|
||||
+40
-1
@@ -675,7 +675,14 @@ module.exports.isNumber = function(value) {
|
||||
module.exports.removeHiddenChars = function(str) {
|
||||
return unhomoglyph(str.normalize('NFD').replace(removeHiddenCharsRegex, ''));
|
||||
};
|
||||
const removeHiddenCharsRegex = /[\u200B-\u200D\u0300-\u036f\uFEFF\s]/g;
|
||||
// Regex matching bunch of unicode control characters and otherwise misleading/invisible characters.
|
||||
// Includes:
|
||||
// various width spaces U+2000 - U+200D
|
||||
// LTR and RTL marks U+200E and U+200F
|
||||
// LTR/RTL and other directional formatting marks U+202A - U+202F
|
||||
// Combining characters U+0300 - U+036F
|
||||
// Zero width no-break space (BOM) U+FEFF
|
||||
const removeHiddenCharsRegex = /[\u2000-\u200F\u202A-\u202F\u0300-\u036f\uFEFF\s]/g;
|
||||
|
||||
function escapeRegExp(string) {
|
||||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
@@ -708,3 +715,35 @@ module.exports.ensureNoTrailingSlash = function(url) {
|
||||
return url;
|
||||
}
|
||||
};
|
||||
|
||||
// Returns a promise which resolves with a given value after the given number of ms
|
||||
module.exports.sleep = (ms, value) => new Promise((resolve => {
|
||||
setTimeout(resolve, ms, value);
|
||||
}));
|
||||
|
||||
module.exports.isNullOrUndefined = function(val) {
|
||||
return val === null || val === undefined;
|
||||
};
|
||||
|
||||
// Returns a Deferred
|
||||
module.exports.defer = () => {
|
||||
let resolve;
|
||||
let reject;
|
||||
|
||||
const promise = new Promise((_resolve, _reject) => {
|
||||
resolve = _resolve;
|
||||
reject = _reject;
|
||||
});
|
||||
|
||||
return {resolve, reject, promise};
|
||||
};
|
||||
|
||||
module.exports.promiseMapSeries = async (promises, fn) => {
|
||||
for (const o of await promises) {
|
||||
await fn(await o);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.promiseTry = (fn) => {
|
||||
return new Promise((resolve) => resolve(fn()));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user