Skip to content

Commit 5577315

Browse files
author
PureCloud Jenkins
committed
23.2.0
1 parent 26cbec9 commit 5577315

53 files changed

Lines changed: 356 additions & 313 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ npm install purecloud-platform-client-v2
2525
Reference from the CDN:
2626

2727
~~~ html
28-
<!-- Replace `23.1.0` with the version you want to use. -->
29-
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.1.0/purecloud-platform-client-v2.min.js"></script>
28+
<!-- Replace `23.2.0` with the version you want to use. -->
29+
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.2.0/purecloud-platform-client-v2.min.js"></script>
3030
~~~
3131

3232
View the documentation on the [PureCloud Developer Center](https://developer.mypurecloud.com/api/rest/client-libraries/javascript/).
@@ -41,7 +41,7 @@ Reference the SDK in your HTML document. For convenience, all modules are bundle
4141

4242
~~~ html
4343
<!-- Include the full library -->
44-
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.1.0/purecloud-platform-client-v2.min.js"></script>
44+
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.2.0/purecloud-platform-client-v2.min.js"></script>
4545
~~~
4646

4747

@@ -79,11 +79,16 @@ client.loginClientCredentialsGrant(clientId, clientSecret)
7979

8080
The Implicit grant only works when used in a browser. This is because a node.js application does not have a browser interface that can display the PureCloud login window.
8181

82+
Optional parameters may be specified in the optional third parameter for `loginImplicitGrant`. This parameter accepts an object with key/value pairs. Supported properties:
83+
84+
* `state` - An arbitrary string used to associate a login request with a login response. This value will be provided in the `state` property on the object when the promise is resolved. The state in the resolved promise will be identical to what was passed into `loginImplicitGrant`, except when the state is retrieved from the auth hash upon completing a login; in that case, the state from the auth hash will override the passed in state.
85+
8286
~~~ javascript
8387
const platformClient = require('platformClient');
8488
var client = platformClient.ApiClient.instance;
85-
client.loginImplicitGrant(clientId, redirectUri)
86-
.then(function() {
89+
client.loginImplicitGrant(clientId, redirectUri, { state: state })
90+
.then(function(data) {
91+
console.log(data);
8792
// Do authenticated things
8893
})
8994
.catch(function(err) {

build/README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ npm install purecloud-platform-client-v2
2525
Reference from the CDN:
2626

2727
~~~ html
28-
<!-- Replace `23.1.0` with the version you want to use. -->
29-
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.1.0/purecloud-platform-client-v2.min.js"></script>
28+
<!-- Replace `23.2.0` with the version you want to use. -->
29+
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.2.0/purecloud-platform-client-v2.min.js"></script>
3030
~~~
3131

3232
View the documentation on the [PureCloud Developer Center](https://developer.mypurecloud.com/api/rest/client-libraries/javascript/).
@@ -41,7 +41,7 @@ Reference the SDK in your HTML document. For convenience, all modules are bundle
4141

4242
~~~ html
4343
<!-- Include the full library -->
44-
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.1.0/purecloud-platform-client-v2.min.js"></script>
44+
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.2.0/purecloud-platform-client-v2.min.js"></script>
4545
~~~
4646

4747

@@ -79,11 +79,16 @@ client.loginClientCredentialsGrant(clientId, clientSecret)
7979

8080
The Implicit grant only works when used in a browser. This is because a node.js application does not have a browser interface that can display the PureCloud login window.
8181

82+
Optional parameters may be specified in the optional third parameter for `loginImplicitGrant`. This parameter accepts an object with key/value pairs. Supported properties:
83+
84+
* `state` - An arbitrary string used to associate a login request with a login response. This value will be provided in the `state` property on the object when the promise is resolved. The state in the resolved promise will be identical to what was passed into `loginImplicitGrant`, except when the state is retrieved from the auth hash upon completing a login; in that case, the state from the auth hash will override the passed in state.
85+
8286
~~~ javascript
8387
const platformClient = require('platformClient');
8488
var client = platformClient.ApiClient.instance;
85-
client.loginImplicitGrant(clientId, redirectUri)
86-
.then(function() {
89+
client.loginImplicitGrant(clientId, redirectUri, { state: state })
90+
.then(function(data) {
91+
console.log(data);
8792
// Do authenticated things
8893
})
8994
.catch(function(err) {

build/docs/index.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ npm install purecloud-platform-client-v2
2525
Reference from the CDN:
2626

2727
~~~ html
28-
<!-- Replace `23.1.0` with the version you want to use. -->
29-
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.1.0/purecloud-platform-client-v2.min.js"></script>
28+
<!-- Replace `23.2.0` with the version you want to use. -->
29+
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.2.0/purecloud-platform-client-v2.min.js"></script>
3030
~~~
3131

3232
View the documentation on the [PureCloud Developer Center](https://developer.mypurecloud.com/api/rest/client-libraries/javascript/).
@@ -41,7 +41,7 @@ Reference the SDK in your HTML document. For convenience, all modules are bundle
4141

4242
~~~ html
4343
<!-- Include the full library -->
44-
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.1.0/purecloud-platform-client-v2.min.js"></script>
44+
<script src="https://sdk-cdn.mypurecloud.com/javascript/23.2.0/purecloud-platform-client-v2.min.js"></script>
4545
~~~
4646

4747

@@ -79,11 +79,16 @@ client.loginClientCredentialsGrant(clientId, clientSecret)
7979

8080
The Implicit grant only works when used in a browser. This is because a node.js application does not have a browser interface that can display the PureCloud login window.
8181

82+
Optional parameters may be specified in the optional third parameter for `loginImplicitGrant`. This parameter accepts an object with key/value pairs. Supported properties:
83+
84+
* `state` - An arbitrary string used to associate a login request with a login response. This value will be provided in the `state` property on the object when the promise is resolved. The state in the resolved promise will be identical to what was passed into `loginImplicitGrant`, except when the state is retrieved from the auth hash upon completing a login; in that case, the state from the auth hash will override the passed in state.
85+
8286
~~~ javascript
8387
const platformClient = require('platformClient');
8488
var client = platformClient.ApiClient.instance;
85-
client.loginImplicitGrant(clientId, redirectUri)
86-
.then(function() {
89+
client.loginImplicitGrant(clientId, redirectUri, { state: state })
90+
.then(function(data) {
91+
console.log(data);
8792
// Do authenticated things
8893
})
8994
.catch(function(err) {

build/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "purecloud-platform-client-v2",
3-
"version": "23.1.0",
3+
"version": "23.2.0",
44
"description": "A JavaScript library to interface with the PureCloud Platform API",
55
"license": "MIT",
66
"main": "src/purecloud-platform-client-v2/index.js",

build/src/purecloud-platform-client-v2/ApiClient.js

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
/**
1919
* @module purecloud-platform-client-v2/ApiClient
20-
* @version 23.1.0
20+
* @version 23.2.0
2121
*/
2222

2323
/**
@@ -61,9 +61,6 @@
6161

6262
// Expose superagent module for use with superagent-proxy
6363
this.superagent = superagent;
64-
65-
// Check for auth token in hash
66-
this._setValuesFromUrlHash();
6764
};
6865

6966
/**
@@ -98,8 +95,24 @@
9895
/**
9996
* @description Saves the auth token to local storage, if enabled.
10097
*/
101-
exports.prototype._saveSettings = function _saveSettings() {
98+
exports.prototype._saveSettings = function _saveSettings(opts) {
10299
try {
100+
if (!this.authData) this.authData = {};
101+
102+
if (opts.accessToken) {
103+
this.authData.accessToken = opts.accessToken;
104+
this.authentications['PureCloud Auth'].accessToken = opts.accessToken;
105+
}
106+
107+
if (opts.state) {
108+
this.authData.state = opts.state;
109+
}
110+
111+
if (opts.tokenExpiryTime) {
112+
this.authData.tokenExpiryTime = opts.tokenExpiryTime;
113+
this.authData.tokenExpiryTimeString = opts.tokenExpiryTimeString;
114+
}
115+
103116
// Don't save settings if we aren't supposed to be persisting them
104117
if (this.persistSettings !== true) return;
105118

@@ -109,14 +122,13 @@
109122
return;
110123
}
111124

112-
// Save settings
113-
if (this.authentications['PureCloud Auth'].accessToken) {
114-
localStorage.setItem(`${this.settingsPrefix}_access_token`, this.authentications['PureCloud Auth'].accessToken);
115-
this._debugTrace('Access token saved to local storage');
116-
} else {
117-
this._debugTrace('Access token cleared');
118-
localStorage.removeItem(`${this.settingsPrefix}_access_token`);
119-
}
125+
// Remove state from data so it's not persisted
126+
let tempData = JSON.parse(JSON.stringify(this.authData));
127+
delete tempData.state;
128+
129+
// Save updated auth data
130+
localStorage.setItem(`${this.settingsPrefix}_auth_data`, JSON.stringify(tempData));
131+
this._debugTrace('Auth data saved to local storage');
120132
} catch (e) {
121133
console.error(e);
122134
}
@@ -135,11 +147,13 @@
135147
return;
136148
}
137149

138-
var token = localStorage.getItem(`${this.settingsPrefix}_access_token`);
139-
if (token) {
140-
this.setAccessToken(token);
141-
this._debugTrace('Access token retrieved from local storage');
142-
}
150+
// Load current auth data
151+
this.authData = localStorage.getItem(`${this.settingsPrefix}_auth_data`);
152+
if (!this.authData)
153+
this.authData = {};
154+
else
155+
this.authData = JSON.parse(this.authData);
156+
if (this.authData.accessToken) this.setAccessToken(this.authData.accessToken);
143157
};
144158

145159
/**
@@ -171,23 +185,36 @@
171185
* @description Initiates the implicit grant login flow. Will attempt to load the token from local storage, if enabled.
172186
* @param {string} clientId - The client ID of an OAuth Implicit Grant client
173187
* @param {string} redirectUri - The redirect URI of the OAuth Implicit Grant client
188+
* @param {object} opts - (optional) Additional options
189+
* @param {string} opts.state - (optional) An arbitrary string to be passed back with the login response. Used for client apps to associate login responses with a request.
174190
*/
175-
exports.prototype.loginImplicitGrant = function loginImplicitGrant(clientId, redirectUri) {
191+
exports.prototype.loginImplicitGrant = function loginImplicitGrant(clientId, redirectUri, opts) {
176192
var self = this;
177193
this.clientId = clientId;
178194
this.redirectUri = redirectUri;
179195

196+
// Check for auth token in hash
197+
this._setValuesFromUrlHash();
198+
199+
if (!opts) opts = {};
200+
180201
return new Promise(function(resolve, reject) {
181202
self._testTokenAccess()
182203
.then(function() {
183-
resolve();
204+
if (!self.authData.state && opts.state)
205+
self.authData.state = opts.state;
206+
resolve(self.authData);
184207
})
185208
.catch(function(error) {
209+
self._debugTrace('Error encountered during login. This is normal if the application has not yet been authorized.');
210+
self._debugTrace(error);
186211
var query = {
187212
client_id: encodeURIComponent(self.clientId),
188213
redirect_uri: encodeURI(self.redirectUri),
189214
response_type: 'token'
190215
};
216+
if (opts.state)
217+
query.state = encodeURIComponent(opts.state);
191218

192219
var url = self._buildAuthUrl('oauth/authorize', query);
193220
self._debugTrace(`Implicit grant: redirecting to ${url} for authorization...`);
@@ -253,11 +280,10 @@
253280
self.callApi('/api/v2/authorization/permissions', 'GET',
254281
null, null, null, null, null, ['PureCloud Auth'], ['application/json'], ['application/json'])
255282
.then(function(roles) {
256-
self._saveSettings();
257283
resolve();
258284
})
259285
.catch(function(error) {
260-
self.setAccessToken();
286+
self._saveSettings({ accessToken: undefined });
261287
reject(error);
262288
});
263289
});
@@ -279,10 +305,24 @@
279305
return obj;
280306
}, {});
281307

282-
// Set access token
283-
if(hash.access_token) {
308+
// Everything goes in here because we only want to act if we found an access token
309+
if (hash.access_token) {
310+
let opts = {};
311+
312+
if (hash.state) {
313+
/* Auth does some interesting things with encoding. It encodes the data twice, except
314+
* for spaces, then replaces all spaces with a plus sign. This process must be done
315+
* in reverse order to properly extract the state data.
316+
*/
317+
opts.state = decodeURIComponent(decodeURIComponent(hash.state.replace(/\+/g, '%20')));
318+
}
319+
320+
if (hash.expires_in) {
321+
opts.tokenExpiryTime = (new Date()).getTime() + (parseInt(decodeURIComponent(decodeURIComponent(hash.expires_in.replace(/\+/g, '%20')))) * 1000);
322+
opts.tokenExpiryTimeString = (new Date(opts.tokenExpiryTime)).toUTCString();
323+
}
284324
// Set access token
285-
this.setAccessToken(hash.access_token);
325+
opts.accessToken = decodeURIComponent(decodeURIComponent(hash.access_token.replace(/\+/g, '%20')));
286326

287327
// Remove hash from URL
288328
// Credit: https://stackoverflow.com/questions/1397329/how-to-remove-the-hash-from-window-location-with-javascript-without-page-refresh/5298684#5298684
@@ -301,6 +341,8 @@
301341
document.body.scrollTop = scrollV;
302342
document.body.scrollLeft = scrollH;
303343
}
344+
345+
this._saveSettings(opts);
304346
}
305347
};
306348

@@ -309,10 +351,7 @@
309351
* @param {string} token - The access token
310352
*/
311353
exports.prototype.setAccessToken = function(token) {
312-
// Set token for API use
313-
this.authentications['PureCloud Auth'].accessToken = token;
314-
315-
this._saveSettings();
354+
this._saveSettings({ accessToken: token });
316355
};
317356

318357
/**
@@ -345,7 +384,12 @@
345384
*/
346385
exports.prototype.logout = function() {
347386
if(exports.hasLocalStorage) {
348-
this.setAccessToken();
387+
this._saveSettings({
388+
accessToken: undefined,
389+
state: undefined,
390+
tokenExpiryTime: undefined,
391+
tokenExpiryTimeString: undefined
392+
});
349393
}
350394

351395
var query = {
@@ -650,7 +694,7 @@
650694

651695
// set header parameters
652696
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
653-
//request.set({ 'purecloud-sdk': '23.1.0' });
697+
//request.set({ 'purecloud-sdk': '23.2.0' });
654698

655699
// set request timeout
656700
request.timeout(this.timeout);

build/src/purecloud-platform-client-v2/api/AlertingApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Alerting service.
2020
* @module purecloud-platform-client-v2/api/AlertingApi
21-
* @version 23.1.0
21+
* @version 23.2.0
2222
*/
2323

2424
/**

build/src/purecloud-platform-client-v2/api/AnalyticsApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Analytics service.
2020
* @module purecloud-platform-client-v2/api/AnalyticsApi
21-
* @version 23.1.0
21+
* @version 23.2.0
2222
*/
2323

2424
/**

build/src/purecloud-platform-client-v2/api/ArchitectApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Architect service.
2020
* @module purecloud-platform-client-v2/api/ArchitectApi
21-
* @version 23.1.0
21+
* @version 23.2.0
2222
*/
2323

2424
/**

build/src/purecloud-platform-client-v2/api/AttributesApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Attributes service.
2020
* @module purecloud-platform-client-v2/api/AttributesApi
21-
* @version 23.1.0
21+
* @version 23.2.0
2222
*/
2323

2424
/**

build/src/purecloud-platform-client-v2/api/AuthorizationApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Authorization service.
2020
* @module purecloud-platform-client-v2/api/AuthorizationApi
21-
* @version 23.1.0
21+
* @version 23.2.0
2222
*/
2323

2424
/**

0 commit comments

Comments
 (0)