@webex/test-users 2.59.2 → 2.59.3-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/whistler.js CHANGED
@@ -1,175 +1,175 @@
1
- import btoa from 'btoa';
2
- import {request} from '@webex/http-core';
3
-
4
- /**
5
- * Fetches credentials/access_token to talk to the whistler endpoint
6
- *
7
- * @param {Object} options
8
- * @param {string} options.clientId
9
- * @param {string} options.clientSecret
10
- * @param {string} options.idbrokerUrl
11
- * @param {string} options.orgId
12
- * @param {string} options.machineAccount
13
- * @param {string} options.machinePassword
14
- * @private
15
- * @returns {Promise<string>}
16
- */
17
- const getClientCredentials = ({
18
- clientId,
19
- clientSecret,
20
- orgId,
21
- idbrokerUrl,
22
- machineAccount,
23
- machinePassword,
24
- }) =>
25
- request({
26
- method: 'POST',
27
- uri: `${idbrokerUrl}/idb/token/${orgId}/v2/actions/GetBearerToken/invoke`,
28
- json: true,
29
- body: {
30
- uid: machineAccount,
31
- password: machinePassword,
32
- },
33
- })
34
- .then((res) =>
35
- request({
36
- method: 'POST',
37
- uri: `${idbrokerUrl}/idb/oauth2/v1/access_token`,
38
- json: true,
39
- form: {
40
- assertion: res.body.BearerToken,
41
- grant_type: 'urn:ietf:params:oauth:grant-type:saml2-bearer',
42
- scope: 'webexsquare:get_conversation webexsquare:admin',
43
- self_contained_token: true,
44
- client_id: clientId,
45
- client_secret: clientSecret,
46
- },
47
- headers: {
48
- // Note: we can't request's auth hash here because this endpoint expects
49
- // us to send the auth header *without including "Basic "* before the
50
- // token string
51
- // authorization: `Basic + ${btoa(`${clientId}:${clientSecret}`)}`
52
- authorization: btoa(`${clientId}:${clientSecret}`),
53
- },
54
- })
55
- )
56
- .then((res) => `${res.body.token_type} ${res.body.access_token}`);
57
-
58
- /**
59
- * @typedef {Object} TestUserObject
60
- * @property {string} password
61
- * @property {string} emailAddress
62
- * @property {string} displayName
63
- * @property {string} token
64
- * @property {string} reservationUrl
65
- * @property {object} responseMetaData - whistler given properties
66
- */
67
-
68
- /**
69
- * @typedef {Object} CreateUserOptions
70
- * @param {Object} [options]
71
- * @param {string} [whistlerServiceUrl] defaults to WHISTLER_API_SERVICE_URL
72
- * @param {string} [options.clientId] defaults to WEBEX_CLIENT_ID
73
- * @param {string} [options.clientSecret] defaults to WEBEX_CLIENT_SECRET
74
- * @param {string} [options.idbrokerUrl] defaults to IDBROKER_BASE_URL
75
- * @param {string} [options.orgId] organization ID to create the user under
76
- * @param {string} [options.machineAccount] defaults to WHISTLER_MACHINE_ACCOUNT
77
- * @param {string} [options.machinePassword] defaults to WHISTLER_MACHINE_PASSWORD
78
- */
79
-
80
- /**
81
- * Creates a test user
82
- * @param {CreateUserOptions} options
83
- * @returns {Promise.<TestUserObject>}
84
- */
85
- export default function createTestUser(options = {}) {
86
- const clientId = options.clientId || process.env.WEBEX_CLIENT_ID;
87
- const clientSecret = options.clientSecret || process.env.WEBEX_CLIENT_SECRET;
88
- const machineAccount = options.machineAccount || process.env.WHISTLER_MACHINE_ACCOUNT;
89
- const machinePassword = options.machinePassword || process.env.WHISTLER_MACHINE_PASSWORD;
90
- const idbrokerUrl = options.idbrokerUrl || process.env.IDBROKER_BASE_URL;
91
- const orgId = options.orgId || process.env.WHISTLER_TEST_ORG_ID;
92
- const whistlerServiceUrl = options.whistlerServiceUrl || process.env.WHISTLER_API_SERVICE_URL;
93
- const {reservationGroup, userScopes} = options;
94
-
95
- if (!clientId) {
96
- throw new Error('options.clientId or process.env.WEBEX_CLIENT_ID must be defined');
97
- }
98
-
99
- if (!clientSecret) {
100
- throw new Error('options.clientSecret or process.env.WEBEX_CLIENT_SECRET must be defined');
101
- }
102
-
103
- if (!machineAccount) {
104
- throw new Error(
105
- 'options.machineAccount or process.env.WHISTLER_MACHINE_ACCOUNT must be defined'
106
- );
107
- }
108
-
109
- if (!machinePassword) {
110
- throw new Error(
111
- 'options.machinePassword or process.env.WHISTLER_MACHINE_PASSWORD must be defined'
112
- );
113
- }
114
- if (!idbrokerUrl) {
115
- throw new Error('options.idbrokerUrl or process.env.IDBROKER_BASE_URL must be defined');
116
- }
117
-
118
- if (!orgId) {
119
- throw new Error('options.orgId or process.env.WHISTLER_TEST_ORG_ID must be defined');
120
- }
121
-
122
- if (!whistlerServiceUrl) {
123
- throw new Error(
124
- 'options.whistlerServiceUrl or process.env.WHISTLER_API_SERVICE_URL must be defined'
125
- );
126
- }
127
-
128
- // For reservation groups and user scopes
129
- // Please check https://confluence-eng-gpk2.cisco.com/conf/pages/viewpage.action?spaceKey=LOCUS&title=Whistler+APIs#WhistlerAPIs-GET/reservations/testUser
130
- return getClientCredentials({
131
- clientId,
132
- clientSecret,
133
- machineAccount,
134
- machinePassword,
135
- idbrokerUrl,
136
- orgId,
137
- })
138
- .then((authorization) =>
139
- request({
140
- method: 'GET',
141
- uri: `${whistlerServiceUrl}/reservations/testUser`,
142
- qs: {
143
- reservationGroup,
144
- userScopes,
145
- isAccessTokenRequired: true,
146
- },
147
- headers: {
148
- authorization,
149
- },
150
- })
151
- )
152
- .then((res) => ({
153
- password: res.body.responseMetaData.ciPassword,
154
- emailAddress: res.body.responseMetaData.name,
155
- displayName: res.body.responseMetaData.webExUserName,
156
- token: res.body.responseMetaData.ciAccessToken,
157
- reservationUrl: res.body.reservationUrl,
158
- ...res.body.responseMetaData,
159
- }));
160
- }
161
-
162
- /**
163
- *
164
- * @param {Object} options
165
- * @returns {Promise}
166
- */
167
- export function removeTestUser(options = {}) {
168
- return request({
169
- method: 'DELETE',
170
- headers: {
171
- authorization: `Bearer ${options.token}`,
172
- },
173
- uri: options.reservationUrl,
174
- });
175
- }
1
+ import btoa from 'btoa';
2
+ import {request} from '@webex/http-core';
3
+
4
+ /**
5
+ * Fetches credentials/access_token to talk to the whistler endpoint
6
+ *
7
+ * @param {Object} options
8
+ * @param {string} options.clientId
9
+ * @param {string} options.clientSecret
10
+ * @param {string} options.idbrokerUrl
11
+ * @param {string} options.orgId
12
+ * @param {string} options.machineAccount
13
+ * @param {string} options.machinePassword
14
+ * @private
15
+ * @returns {Promise<string>}
16
+ */
17
+ const getClientCredentials = ({
18
+ clientId,
19
+ clientSecret,
20
+ orgId,
21
+ idbrokerUrl,
22
+ machineAccount,
23
+ machinePassword,
24
+ }) =>
25
+ request({
26
+ method: 'POST',
27
+ uri: `${idbrokerUrl}/idb/token/${orgId}/v2/actions/GetBearerToken/invoke`,
28
+ json: true,
29
+ body: {
30
+ uid: machineAccount,
31
+ password: machinePassword,
32
+ },
33
+ })
34
+ .then((res) =>
35
+ request({
36
+ method: 'POST',
37
+ uri: `${idbrokerUrl}/idb/oauth2/v1/access_token`,
38
+ json: true,
39
+ form: {
40
+ assertion: res.body.BearerToken,
41
+ grant_type: 'urn:ietf:params:oauth:grant-type:saml2-bearer',
42
+ scope: 'webexsquare:get_conversation webexsquare:admin',
43
+ self_contained_token: true,
44
+ client_id: clientId,
45
+ client_secret: clientSecret,
46
+ },
47
+ headers: {
48
+ // Note: we can't request's auth hash here because this endpoint expects
49
+ // us to send the auth header *without including "Basic "* before the
50
+ // token string
51
+ // authorization: `Basic + ${btoa(`${clientId}:${clientSecret}`)}`
52
+ authorization: btoa(`${clientId}:${clientSecret}`),
53
+ },
54
+ })
55
+ )
56
+ .then((res) => `${res.body.token_type} ${res.body.access_token}`);
57
+
58
+ /**
59
+ * @typedef {Object} TestUserObject
60
+ * @property {string} password
61
+ * @property {string} emailAddress
62
+ * @property {string} displayName
63
+ * @property {string} token
64
+ * @property {string} reservationUrl
65
+ * @property {object} responseMetaData - whistler given properties
66
+ */
67
+
68
+ /**
69
+ * @typedef {Object} CreateUserOptions
70
+ * @param {Object} [options]
71
+ * @param {string} [whistlerServiceUrl] defaults to WHISTLER_API_SERVICE_URL
72
+ * @param {string} [options.clientId] defaults to WEBEX_CLIENT_ID
73
+ * @param {string} [options.clientSecret] defaults to WEBEX_CLIENT_SECRET
74
+ * @param {string} [options.idbrokerUrl] defaults to IDBROKER_BASE_URL
75
+ * @param {string} [options.orgId] organization ID to create the user under
76
+ * @param {string} [options.machineAccount] defaults to WHISTLER_MACHINE_ACCOUNT
77
+ * @param {string} [options.machinePassword] defaults to WHISTLER_MACHINE_PASSWORD
78
+ */
79
+
80
+ /**
81
+ * Creates a test user
82
+ * @param {CreateUserOptions} options
83
+ * @returns {Promise.<TestUserObject>}
84
+ */
85
+ export default function createTestUser(options = {}) {
86
+ const clientId = options.clientId || process.env.WEBEX_CLIENT_ID;
87
+ const clientSecret = options.clientSecret || process.env.WEBEX_CLIENT_SECRET;
88
+ const machineAccount = options.machineAccount || process.env.WHISTLER_MACHINE_ACCOUNT;
89
+ const machinePassword = options.machinePassword || process.env.WHISTLER_MACHINE_PASSWORD;
90
+ const idbrokerUrl = options.idbrokerUrl || process.env.IDBROKER_BASE_URL;
91
+ const orgId = options.orgId || process.env.WHISTLER_TEST_ORG_ID;
92
+ const whistlerServiceUrl = options.whistlerServiceUrl || process.env.WHISTLER_API_SERVICE_URL;
93
+ const {reservationGroup, userScopes} = options;
94
+
95
+ if (!clientId) {
96
+ throw new Error('options.clientId or process.env.WEBEX_CLIENT_ID must be defined');
97
+ }
98
+
99
+ if (!clientSecret) {
100
+ throw new Error('options.clientSecret or process.env.WEBEX_CLIENT_SECRET must be defined');
101
+ }
102
+
103
+ if (!machineAccount) {
104
+ throw new Error(
105
+ 'options.machineAccount or process.env.WHISTLER_MACHINE_ACCOUNT must be defined'
106
+ );
107
+ }
108
+
109
+ if (!machinePassword) {
110
+ throw new Error(
111
+ 'options.machinePassword or process.env.WHISTLER_MACHINE_PASSWORD must be defined'
112
+ );
113
+ }
114
+ if (!idbrokerUrl) {
115
+ throw new Error('options.idbrokerUrl or process.env.IDBROKER_BASE_URL must be defined');
116
+ }
117
+
118
+ if (!orgId) {
119
+ throw new Error('options.orgId or process.env.WHISTLER_TEST_ORG_ID must be defined');
120
+ }
121
+
122
+ if (!whistlerServiceUrl) {
123
+ throw new Error(
124
+ 'options.whistlerServiceUrl or process.env.WHISTLER_API_SERVICE_URL must be defined'
125
+ );
126
+ }
127
+
128
+ // For reservation groups and user scopes
129
+ // Please check https://confluence-eng-gpk2.cisco.com/conf/pages/viewpage.action?spaceKey=LOCUS&title=Whistler+APIs#WhistlerAPIs-GET/reservations/testUser
130
+ return getClientCredentials({
131
+ clientId,
132
+ clientSecret,
133
+ machineAccount,
134
+ machinePassword,
135
+ idbrokerUrl,
136
+ orgId,
137
+ })
138
+ .then((authorization) =>
139
+ request({
140
+ method: 'GET',
141
+ uri: `${whistlerServiceUrl}/reservations/testUser`,
142
+ qs: {
143
+ reservationGroup,
144
+ userScopes,
145
+ isAccessTokenRequired: true,
146
+ },
147
+ headers: {
148
+ authorization,
149
+ },
150
+ })
151
+ )
152
+ .then((res) => ({
153
+ password: res.body.responseMetaData.ciPassword,
154
+ emailAddress: res.body.responseMetaData.name,
155
+ displayName: res.body.responseMetaData.webExUserName,
156
+ token: res.body.responseMetaData.ciAccessToken,
157
+ reservationUrl: res.body.reservationUrl,
158
+ ...res.body.responseMetaData,
159
+ }));
160
+ }
161
+
162
+ /**
163
+ *
164
+ * @param {Object} options
165
+ * @returns {Promise}
166
+ */
167
+ export function removeTestUser(options = {}) {
168
+ return request({
169
+ method: 'DELETE',
170
+ headers: {
171
+ authorization: `Bearer ${options.token}`,
172
+ },
173
+ uri: options.reservationUrl,
174
+ });
175
+ }