Переглянути джерело

feat(jwtError): Send explicit error on auth failed with invalid jwt

dev1
hmuresan 4 роки тому
джерело
коміт
087a8e19eb

+ 16
- 0
modules/xmpp/StropheLastSuccess.js Переглянути файл

@@ -7,6 +7,7 @@ export default class LastRequestTracker {
7 7
      */
8 8
     constructor() {
9 9
         this._lastSuccess = null;
10
+        this._lastFailedMessage = null;
10 11
     }
11 12
 
12 13
     /**
@@ -19,6 +20,12 @@ export default class LastRequestTracker {
19 20
         const originalRawInput = stropheConnection.rawInput;
20 21
 
21 22
         stropheConnection.rawInput = (...args) => {
23
+            const rawMessage = args[0];
24
+
25
+            if (rawMessage.includes('failure')) {
26
+                this._lastFailedMessage = rawMessage;
27
+            }
28
+
22 29
             // It's okay to use rawInput callback only once the connection has been established, otherwise it will
23 30
             // treat 'item-not-found' or other connection error on websocket reconnect as successful stanza received.
24 31
             if (xmppConnection.connected) {
@@ -28,6 +35,15 @@ export default class LastRequestTracker {
28 35
         };
29 36
     }
30 37
 
38
+    /**
39
+     * Returns the last raw failed incoming message on the xmpp connection.
40
+     *
41
+     * @returns {string|null}
42
+     */
43
+    getLastFailedMessage() {
44
+        return this._lastFailedMessage;
45
+    }
46
+
31 47
     /**
32 48
      * Returns how many milliseconds have passed since the last successful BOSH request.
33 49
      *

+ 12
- 3
modules/xmpp/XmppConnection.js Переглянути файл

@@ -67,8 +67,8 @@ export default class XmppConnection extends Listenable {
67 67
         // The default maxRetries is 5, which is too long.
68 68
         this._stropheConn.maxRetries = 3;
69 69
 
70
-        this._lastSuccessTracker = new LastSuccessTracker();
71
-        this._lastSuccessTracker.startTracking(this, this._stropheConn);
70
+        this._rawInputTracker = new LastSuccessTracker();
71
+        this._rawInputTracker.startTracking(this, this._stropheConn);
72 72
 
73 73
         this._resumeTask = new ResumeTask(this._stropheConn);
74 74
 
@@ -348,7 +348,16 @@ export default class XmppConnection extends Listenable {
348 348
      * @returns {number|null}
349 349
      */
350 350
     getTimeSinceLastSuccess() {
351
-        return this._lastSuccessTracker.getTimeSinceLastSuccess();
351
+        return this._rawInputTracker.getTimeSinceLastSuccess();
352
+    }
353
+
354
+    /**
355
+     * See {@link LastRequestTracker.getLastFailedMessage}.
356
+     *
357
+     * @returns {string|null}
358
+     */
359
+    getLastFailedMessage() {
360
+        return this._rawInputTracker.getLastFailedMessage();
352 361
     }
353 362
 
354 363
     /**

+ 24
- 1
modules/xmpp/xmpp.js Переглянути файл

@@ -23,6 +23,11 @@ import initStropheUtil from './strophe.util';
23 23
 
24 24
 const logger = getLogger(__filename);
25 25
 
26
+/**
27
+* Regex to extract exact error message on jwt error.
28
+*/
29
+const FAILURE_REGEX = /<failure.*><not-allowed\/><text>(.*)<\/text><\/failure>/gi;
30
+
26 31
 /**
27 32
  * Creates XMPP connection.
28 33
  *
@@ -403,15 +408,33 @@ export default class XMPP extends Listenable {
403 408
                 }
404 409
             }
405 410
         } else if (status === Strophe.Status.AUTHFAIL) {
411
+            const lastFailedRawMessage = this.getConnection().getLastFailedMessage();
412
+
406 413
             // wrong password or username, prompt user
407 414
             this.eventEmitter.emit(
408 415
                 JitsiConnectionEvents.CONNECTION_FAILED,
409 416
                 JitsiConnectionErrors.PASSWORD_REQUIRED,
410
-                msg,
417
+                msg || this._parseConnectionFailedMessage(lastFailedRawMessage),
411 418
                 credentials);
412 419
         }
413 420
     }
414 421
 
422
+    /**
423
+    * Parses a raw failure xmpp xml message received on auth failed.
424
+    *
425
+    * @param {string} msg - The raw failure message from xmpp.
426
+    * @returns {string|null} - The parsed message from the raw xmpp message.
427
+    */
428
+    _parseConnectionFailedMessage(msg) {
429
+        if (!msg) {
430
+            return null;
431
+        }
432
+
433
+        const matches = FAILURE_REGEX.exec(msg);
434
+
435
+        return matches ? matches[1] : null;
436
+    }
437
+
415 438
     /**
416 439
      *
417 440
      * @param jid

Завантаження…
Відмінити
Зберегти