瀏覽代碼

feat(JaaS-JWT) Get JWT for JaaS (#9512)

* Added get JWT for JaaS

* Code review fix
j8
robertpin 3 年之前
父節點
當前提交
bad58f6508
沒有連結到貢獻者的電子郵件帳戶。
共有 2 個檔案被更改,包括 71 行新增2 行删除
  1. 11
    2
      connection.js
  2. 60
    0
      react/features/jaas/functions.js

+ 11
- 2
connection.js 查看文件

11
     connectionFailed
11
     connectionFailed
12
 } from './react/features/base/connection/actions';
12
 } from './react/features/base/connection/actions';
13
 import { openDialog } from './react/features/base/dialog/actions';
13
 import { openDialog } from './react/features/base/dialog/actions';
14
+import { setJWT } from './react/features/base/jwt';
14
 import {
15
 import {
15
     isFatalJitsiConnectionError,
16
     isFatalJitsiConnectionError,
16
     JitsiConnectionErrors,
17
     JitsiConnectionErrors,
17
     JitsiConnectionEvents
18
     JitsiConnectionEvents
18
 } from './react/features/base/lib-jitsi-meet';
19
 } from './react/features/base/lib-jitsi-meet';
20
+import { isVpaasMeeting } from './react/features/billing-counter/functions';
21
+import { getJaasJWT } from './react/features/jaas/functions';
19
 import { setPrejoinDisplayNameRequired } from './react/features/prejoin/actions';
22
 import { setPrejoinDisplayNameRequired } from './react/features/prejoin/actions';
20
 const logger = Logger.getLogger(__filename);
23
 const logger = Logger.getLogger(__filename);
21
 
24
 
82
  * @returns {Promise<JitsiConnection>} connection if
85
  * @returns {Promise<JitsiConnection>} connection if
83
  * everything is ok, else error.
86
  * everything is ok, else error.
84
  */
87
  */
85
-export function connect(id, password, roomName) {
88
+export async function connect(id, password, roomName) {
86
     const connectionConfig = Object.assign({}, config);
89
     const connectionConfig = Object.assign({}, config);
87
-    const { jwt } = APP.store.getState()['features/base/jwt'];
90
+    const state = APP.store.getState();
91
+    let { jwt } = state['features/base/jwt'];
92
+
93
+    if (!jwt && isVpaasMeeting(state)) {
94
+        jwt = await getJaasJWT(state);
95
+        APP.store.dispatch(setJWT(jwt));
96
+    }
88
 
97
 
89
     // Use Websocket URL for the web app if configured. Note that there is no 'isWeb' check, because there's assumption
98
     // Use Websocket URL for the web app if configured. Note that there is no 'isWeb' check, because there's assumption
90
     // that this code executes only on web browsers/electron. This needs to be changed when mobile and web are unified.
99
     // that this code executes only on web browsers/electron. This needs to be changed when mobile and web are unified.

+ 60
- 0
react/features/jaas/functions.js 查看文件

1
 // @flow
1
 // @flow
2
 
2
 
3
+import { getVpaasTenant } from '../billing-counter/functions';
4
+
5
+import logger from './logger';
6
+
3
 /**
7
 /**
4
  * Sends a request for retrieving jaas customer details.
8
  * Sends a request for retrieving jaas customer details.
5
  *
9
  *
46
 export function isFeatureDisabled(state: Object, feature: string) {
50
 export function isFeatureDisabled(state: Object, feature: string) {
47
     return state['features/jaas'].disabledFeatures.includes(feature);
51
     return state['features/jaas'].disabledFeatures.includes(feature);
48
 }
52
 }
53
+
54
+/**
55
+ * Sends a request for retrieving jaas JWT.
56
+ *
57
+ * @param {Object} reqData - The request info.
58
+ * @param {string} reqData.appId - The client appId.
59
+ * @param {string} reqData.baseUrl - The base url for the request.
60
+ * @returns {void}
61
+ */
62
+export async function sendGetJWTRequest({ appId, baseUrl }: {
63
+    appId: string,
64
+    baseUrl: string
65
+}) {
66
+    const fullUrl = `${baseUrl}/v1/public/token/${encodeURIComponent(appId)}`;
67
+
68
+    try {
69
+        const res = await fetch(fullUrl, {
70
+            method: 'GET'
71
+        });
72
+
73
+        if (res.ok) {
74
+            return res.json();
75
+        }
76
+
77
+        throw new Error('Request not successful');
78
+    } catch (err) {
79
+        throw new Error(err);
80
+
81
+    }
82
+}
83
+
84
+/**
85
+ * Gets a jaas JWT.
86
+ *
87
+ * @param {Object} state - Redux state.
88
+ * @returns {string} The JWT.
89
+ */
90
+export async function getJaasJWT(state: Object) {
91
+    const baseUrl = state['features/base/config'].jaasTokenUrl;
92
+    const appId = getVpaasTenant(state);
93
+
94
+    const shouldSendRequest = Boolean(baseUrl && appId);
95
+
96
+    if (shouldSendRequest) {
97
+        try {
98
+            const jwt = await sendGetJWTRequest({
99
+                appId,
100
+                baseUrl
101
+            });
102
+
103
+            return jwt.token;
104
+        } catch (err) {
105
+            logger.error('Could not send request', err);
106
+        }
107
+    }
108
+}

Loading…
取消
儲存