|
@@ -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 */
|