Ver código fonte

JitsiAuthConnection -> authenticateAndUpgradeRole

With JitsiAuthConnection the API consumer has to:
1. JitsiConference.createAuthenticationConnection,
2. JitsiAuthConnecition.authenticateAndUpgradeRole,
3. Wait on the Promise of 2,
4. Maybe cancel the JitsiAuthConnection of 1.

With authenticateAndUpgradeRole the API consumer has to:
1. JitsiConference.authenticateAndUpgradeRole,
2. Wait on the thenableWithCancel of 1.
3. Maybe cancel the thenableWithCancel of 1.
dev1
Lyubo Marinov 8 anos atrás
pai
commit
e67b271192
4 arquivos alterados com 164 adições e 155 exclusões
  1. 0
    141
      JitsiAuthConnection.js
  2. 8
    6
      JitsiConference.js
  3. 149
    0
      authenticateAndUpgradeRole.js
  4. 7
    8
      modules/xmpp/moderator.js

+ 0
- 141
JitsiAuthConnection.js Ver arquivo

@@ -1,141 +0,0 @@
1
-import * as JitsiConnectionEvents from './JitsiConnectionEvents';
2
-import XMPP from './modules/xmpp/xmpp';
3
-
4
-/**
5
- * Class creates separate XMPP connection and tries to connect using given
6
- * credentials. Once connected will contact Jicofo to obtain and store session
7
- * ID which is then used by the parent conference to upgrade user's role to
8
- * moderator. It's also used to join the conference when starting from anonymous
9
- * domain and only authenticated users are allowed to create new rooms.
10
- */
11
-export default class JitsiAuthConnection {
12
-    /**
13
-     * Creates new <tt>JitsiAuthConnection</tt> for given conference.
14
-     * @param {JitsiConference} conference
15
-     * @constructor
16
-     */
17
-    constructor(conference) {
18
-        this.conference = conference;
19
-        this.xmpp = new XMPP(conference.connection.options);
20
-        this.canceled = false;
21
-        this._promise = null;
22
-    }
23
-
24
-    /**
25
-     * @typedef {Object} UpgradeRoleError
26
-     * @property {JitsiConnectionErrors} [connectionError] - One of
27
-     * {@link JitsiConnectionErrors} which occurred when trying to connect to
28
-     * the XMPP server.
29
-     * @property {String} [authenticationError] - One of XMPP error conditions
30
-     * returned by Jicofo on authentication attempt. See
31
-     * https://xmpp.org/rfcs/rfc3920.html#streams-error.
32
-     * @property {String} [message] - More details about the error.
33
-     *
34
-     * NOTE If neither one of the errors is present it means that the operation
35
-     * has been canceled.
36
-     */
37
-    /**
38
-     * Connects to the XMPP server using given credentials and contacts Jicofo
39
-     * in order to obtain a session ID (which is then stored in the local
40
-     * storage). User's role of the parent conference will be upgraded to
41
-     * moderator (by Jicofo).
42
-     *
43
-     * @param {Object} options
44
-     * @param {string} options.id - XMPP user's ID (user@xmpp-server.com).
45
-     * @param {string} options.password - User's password used to login.
46
-     * @param {string} [options.roomPassword] - Room password required to join
47
-     * the MUC room.
48
-     * @param {Function} [options.onLoginSuccessful] - Callback called when
49
-     * logging into the XMPP server was successful. The next step will be to
50
-     * obtain new session ID from Jicofo and join the MUC using it which will
51
-     * effectively upgrade the current user's role to moderator.
52
-     *
53
-     * @returns {Promise} Resolved in case the authentication was successful
54
-     * and the session Id has been stored in the settings. Will be rejected with
55
-     * {@link UpgradeRoleError} which will have either 'connectionError'
56
-     * or 'authenticationError' field set depending on which of the steps has
57
-     * failed. If {@link cancel} is called before the operation is finished then
58
-     * the promise will be resolved with an empty object (no error set).
59
-     */
60
-    authenticateAndUpgradeRole({ id,
61
-                                 password,
62
-                                 roomPassword,
63
-                                 onLoginSuccessful }) {
64
-        if (this._promise) {
65
-            return this._promise;
66
-        }
67
-        this._promise = new Promise((resolve, reject) => {
68
-            const connectionEstablished = () => {
69
-                if (this.canceled) {
70
-                    return;
71
-                }
72
-
73
-                // Let the caller know that the XMPP login was successful
74
-                onLoginSuccessful && onLoginSuccessful();
75
-
76
-                // Now authenticate with Jicofo and get new session ID
77
-                this.room
78
-                    = this.xmpp.createRoom(
79
-                        this.conference.options.name,
80
-                        this.conference.options.config);
81
-                this.room.moderator.authenticate()
82
-                    .then(() => {
83
-                        if (this.canceled) {
84
-                            return;
85
-                        }
86
-
87
-                        this.xmpp.disconnect();
88
-
89
-                        // At this point we should have new session ID stored in
90
-                        // the settings. Jicofo will allow to join the room.
91
-                        this.conference.join(roomPassword);
92
-
93
-                        resolve();
94
-                    })
95
-                    .catch(error => {
96
-                        this.xmpp.disconnect();
97
-
98
-                        reject({
99
-                            authenticationError: error.error,
100
-                            message: error.message
101
-                        });
102
-                    });
103
-            };
104
-
105
-            this.xmpp.addListener(
106
-                JitsiConnectionEvents.CONNECTION_ESTABLISHED,
107
-                connectionEstablished);
108
-            this.xmpp.addListener(
109
-                JitsiConnectionEvents.CONNECTION_FAILED,
110
-                (error, msg) => reject({
111
-                    connectionError: error,
112
-                    message: msg
113
-                }));
114
-            this.xmpp.addListener(
115
-                JitsiConnectionEvents.CONNECTION_DISCONNECTED,
116
-                () => {
117
-                    if (this.canceled) {
118
-                        reject({ });
119
-                    }
120
-                });
121
-
122
-            if (this.canceled) {
123
-                reject({ });
124
-            } else {
125
-                this.xmpp.connect(id, password);
126
-            }
127
-        });
128
-
129
-        return this._promise;
130
-    }
131
-
132
-    /**
133
-     * Cancels the authentication if it's currently in progress. The promise
134
-     * returned by {@link authenticateAndUpgradeRole} will be rejected with
135
-     * an empty object (none of the error fields set).
136
-     */
137
-    cancel() {
138
-        this.canceled = true;
139
-        this.xmpp.disconnect();
140
-    }
141
-}

+ 8
- 6
JitsiConference.js Ver arquivo

@@ -6,7 +6,7 @@ import ConnectionQuality from './modules/connectivity/ConnectionQuality';
6 6
 import { getLogger } from 'jitsi-meet-logger';
7 7
 import GlobalOnErrorHandler from './modules/util/GlobalOnErrorHandler';
8 8
 import EventEmitter from 'events';
9
-import JitsiAuthConnection from './JitsiAuthConnection';
9
+import authenticateAndUpgradeRole from './authenticateAndUpgradeRole';
10 10
 import * as JitsiConferenceErrors from './JitsiConferenceErrors';
11 11
 import JitsiConferenceEventManager from './JitsiConferenceEventManager';
12 12
 import * as JitsiConferenceEvents from './JitsiConferenceEvents';
@@ -305,13 +305,15 @@ JitsiConference.prototype.join = function(password) {
305 305
 };
306 306
 
307 307
 /**
308
- * Creates new {@link JitsiAuthConnection} which authenticates and upgrades
309
- * the user's role to moderator.
308
+ * Authenticates and upgrades the role of the local participant/user.
310 309
  *
311
- * @returns {JitsiAuthConnection}
310
+ * @returns {Object} A <tt>thenable</tt> which (1) settles when the process of
311
+ * authenticating and upgrading the role of the local participant/user finishes
312
+ * and (2) has a <tt>cancel</tt> method that allows the caller to interrupt the
313
+ * process.
312 314
  */
313
-JitsiConference.prototype.createAuthenticationConnection = function() {
314
-    return new JitsiAuthConnection(this);
315
+JitsiConference.prototype.authenticateAndUpgradeRole = function(...args) {
316
+    return authenticateAndUpgradeRole.apply(this, args);
315 317
 };
316 318
 
317 319
 /**

+ 149
- 0
authenticateAndUpgradeRole.js Ver arquivo

@@ -0,0 +1,149 @@
1
+import {
2
+    CONNECTION_DISCONNECTED,
3
+    CONNECTION_ESTABLISHED,
4
+    CONNECTION_FAILED
5
+} from './JitsiConnectionEvents';
6
+import XMPP from './modules/xmpp/xmpp';
7
+
8
+/**
9
+ * @typedef {Object} UpgradeRoleError
10
+ *
11
+ * @property {JitsiConnectionErrors} [connectionError] - One of
12
+ * {@link JitsiConnectionErrors} which occurred when trying to connect to the
13
+ * XMPP server.
14
+ * @property {String} [authenticationError] - One of XMPP error conditions
15
+ * returned by Jicofo on authentication attempt. See
16
+ * {@link https://xmpp.org/rfcs/rfc3920.html#streams-error}.
17
+ * @property {String} [message] - More details about the error.
18
+ *
19
+ * NOTE If neither one of the errors is present, then the operation has been
20
+ * canceled.
21
+ */
22
+
23
+/* eslint-disable no-invalid-this */
24
+
25
+/**
26
+ * Connects to the XMPP server using the specified credentials and contacts
27
+ * Jicofo in order to obtain a session ID (which is then stored in the local
28
+ * storage). The user's role of the parent conference will be upgraded to
29
+ * moderator (by Jicofo). It's also used to join the conference when starting
30
+ * from anonymous domain and only authenticated users are allowed to create new
31
+ * rooms.
32
+ *
33
+ * @param {Object} options
34
+ * @param {string} options.id - XMPP user's ID to log in. For example,
35
+ * user@xmpp-server.com.
36
+ * @param {string} options.password - XMPP user's password to log in with.
37
+ * @param {string} [options.roomPassword] - The password to join the MUC with.
38
+ * @param {Function} [options.onLoginSuccessful] - Callback called when logging
39
+ * into the XMPP server was successful. The next step will be to obtain a new
40
+ * session ID from Jicofo and join the MUC using it which will effectively
41
+ * upgrade the user's role to moderator.
42
+ * @returns {Object} A <tt>thenable</tt> which (1) settles when the process of
43
+ * authenticating and upgrading the role of the specified XMPP user finishes and
44
+ * (2) has a <tt>cancel</tt> method that allows the caller to interrupt the
45
+ * process. If the process finishes successfully, the session ID has been stored
46
+ * in the settings and the <tt>thenable</tt> is resolved. If the process
47
+ * finishes with failure, the <tt>thenable</tt> is rejected with reason of type
48
+ * {@link UpgradeRoleError} which will have either <tt>connectionError</tt> or
49
+ * <tt>authenticationError</tt> property set depending on which of the steps has
50
+ * failed. If <tt>cancel</tt> is called before the process finishes, then the
51
+ * thenable will be rejected with an empty object (i.e. no error property will
52
+ * be set on the rejection reason).
53
+ */
54
+export default function authenticateAndUpgradeRole({
55
+    // 1. Log the specified XMPP user in.
56
+    id,
57
+    password,
58
+
59
+    // 2. Let the API client/consumer know as soon as the XMPP user has been
60
+    //    successfully logged in.
61
+    onLoginSuccessful,
62
+
63
+    // 3. Join the MUC.
64
+    roomPassword
65
+}) {
66
+    let canceled = false;
67
+    let rejectPromise;
68
+    let xmpp = new XMPP(this.connection.options);
69
+
70
+    const process = new Promise((resolve, reject) => {
71
+        // The process is represented by a Thenable with a cancel method. The
72
+        // Thenable is implemented using Promise and the cancel using the
73
+        // Promise's reject function.
74
+        rejectPromise = reject;
75
+
76
+
77
+        xmpp.addListener(
78
+            CONNECTION_DISCONNECTED,
79
+            () => {
80
+                xmpp = undefined;
81
+            });
82
+        xmpp.addListener(
83
+            CONNECTION_ESTABLISHED,
84
+            () => {
85
+                if (canceled) {
86
+                    return;
87
+                }
88
+
89
+                // Let the caller know that the XMPP login was successful.
90
+                onLoginSuccessful && onLoginSuccessful();
91
+
92
+                // Now authenticate with Jicofo and get a new session ID.
93
+                const room
94
+                    = xmpp.createRoom(this.options.name, this.options.config);
95
+
96
+                room.moderator.authenticate()
97
+                    .then(() => {
98
+                        xmpp && xmpp.disconnect();
99
+
100
+                        if (canceled) {
101
+                            return;
102
+                        }
103
+
104
+                        // At this point we should have the new session ID
105
+                        // stored in the settings. Jicofo will allow to join the
106
+                        // room.
107
+                        this.join(roomPassword);
108
+
109
+                        resolve();
110
+                    })
111
+                    .catch(({ error, message }) => {
112
+                        xmpp.disconnect();
113
+
114
+                        reject({
115
+                            authenticationError: error,
116
+                            message
117
+                        });
118
+                    });
119
+            });
120
+        xmpp.addListener(
121
+            CONNECTION_FAILED,
122
+            (connectionError, message) => {
123
+                reject({
124
+                    connectionError,
125
+                    message
126
+                });
127
+                xmpp = undefined;
128
+            });
129
+
130
+        canceled || xmpp.connect(id, password);
131
+    });
132
+
133
+    /**
134
+     * Cancels the process, if it's in progress, of authenticating and upgrading
135
+     * the role of the local participant/user.
136
+     *
137
+     * @public
138
+     * @returns {void}
139
+     */
140
+    process.cancel = () => {
141
+        canceled = true;
142
+        rejectPromise({});
143
+        xmpp && xmpp.disconnect();
144
+    };
145
+
146
+    return process;
147
+}
148
+
149
+/* eslint-enable no-invalid-this */

+ 7
- 8
modules/xmpp/moderator.js Ver arquivo

@@ -465,14 +465,13 @@ Moderator.prototype.authenticate = function() {
465 465
             result => {
466 466
                 this.parseSessionId(result);
467 467
                 resolve();
468
-            }, errorIq => {
469
-                reject({
470
-                    error: $(errorIq).find('iq>error :first')
471
-                                     .prop('tagName'),
472
-                    message: $(errorIq).find('iq>error>text')
473
-                                       .text()
474
-                });
475
-            }
468
+            },
469
+            errorIq => reject({
470
+                error: $(errorIq).find('iq>error :first')
471
+                                 .prop('tagName'),
472
+                message: $(errorIq).find('iq>error>text')
473
+                                   .text()
474
+            })
476 475
         );
477 476
     });
478 477
 };

Carregando…
Cancelar
Salvar