Skip to content

Commit 0a86870

Browse files
areClient Engineering Bot
andauthored
feat: add users and spaces to grantToken, allow userId in configuration (#282)
* feat: add users and spaces to grantToken, allow userId in configuration * fix: add previous release to changelog and pubnub.yml * PubNub SDK v7.2.0 release. * fix: fix signature in a test Co-authored-by: Client Engineering Bot <60980775+Client Engineering [email protected]>
1 parent bd5bd83 commit 0a86870

File tree

12 files changed

+4252
-4545
lines changed

12 files changed

+4252
-4545
lines changed

.pubnub.yml

Lines changed: 3649 additions & 4387 deletions
Large diffs are not rendered by default.

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
## v7.2.0
2+
July 01 2022
3+
4+
#### Added
5+
- Allows to specify users and spaces in grantToken method.
6+
- Allows to use userId instead of uuid in configuration.
7+
18
## v7.1.2
29
June 22 2022
310

11+
#### Fixed
12+
- Fixes parseToken issues on Web and React Native.
13+
414
## v7.1.1
515
June 14 2022
616

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ You will need the publish and subscribe keys to authenticate your app. Get your
2222
npm install pubnub
2323
```
2424
* or download one of our builds from our CDN:
25-
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.1.2.js
26-
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.1.2.min.js
25+
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.2.0.js
26+
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.2.0.min.js
2727
2828
2. Configure your keys:
2929

dist/web/pubnub.js

Lines changed: 138 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,18 @@
668668
if (setup.heartbeatInterval != null) {
669669
this.setHeartbeatInterval(setup.heartbeatInterval);
670670
}
671-
this.setUUID(setup.uuid);
671+
if (typeof setup.userId === 'string') {
672+
if (typeof setup.uuid === 'string') {
673+
throw new Error('Only one of the following configuration options has to be provided: `uuid` or `userId`');
674+
}
675+
this.setUserId(setup.userId);
676+
}
677+
else {
678+
if (typeof setup.uuid !== 'string') {
679+
throw new Error('One of the following configuration options has to be provided: `uuid` or `userId`');
680+
}
681+
this.setUUID(setup.uuid);
682+
}
672683
}
673684
// exposed setters
674685
default_1.prototype.getAuthKey = function () {
@@ -692,6 +703,16 @@
692703
this.UUID = val;
693704
return this;
694705
};
706+
default_1.prototype.getUserId = function () {
707+
return this.UUID;
708+
};
709+
default_1.prototype.setUserId = function (value) {
710+
if (!value || typeof value !== 'string' || value.trim().length === 0) {
711+
throw new Error('Missing or invalid userId parameter. Provide a valid string userId');
712+
}
713+
this.UUID = value;
714+
return this;
715+
};
695716
default_1.prototype.getFilterExpression = function () {
696717
return this.filterExpression;
697718
};
@@ -747,7 +768,7 @@
747768
return this;
748769
};
749770
default_1.prototype.getVersion = function () {
750-
return '7.1.2';
771+
return '7.2.0';
751772
};
752773
default_1.prototype._addPnsdkSuffix = function (name, suffix) {
753774
this._PNSDKSuffix[name] = suffix;
@@ -5620,13 +5641,20 @@
56205641
handleResponse: handleResponse$9
56215642
});
56225643

5623-
/* */
56245644
function getOperation$8() {
56255645
return OPERATIONS.PNAccessManagerGrantToken;
56265646
}
5647+
function hasVspTerms(incomingParams) {
5648+
var _a, _b, _c, _d;
5649+
var hasAuthorizedUserId = (incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.authorizedUserId) !== undefined;
5650+
var hasUserResources = ((_a = incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.resources) === null || _a === void 0 ? void 0 : _a.users) !== undefined;
5651+
var hasSpaceResources = ((_b = incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.resources) === null || _b === void 0 ? void 0 : _b.spaces) !== undefined;
5652+
var hasUserPatterns = ((_c = incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.patterns) === null || _c === void 0 ? void 0 : _c.users) !== undefined;
5653+
var hasSpacePatterns = ((_d = incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.patterns) === null || _d === void 0 ? void 0 : _d.spaces) !== undefined;
5654+
return hasUserPatterns || hasUserResources || hasSpacePatterns || hasSpaceResources || hasAuthorizedUserId;
5655+
}
56275656
function extractPermissions(permissions) {
56285657
var permissionsResult = 0;
5629-
/* eslint-disable */
56305658
if (permissions.join) {
56315659
permissionsResult |= 128;
56325660
}
@@ -5648,10 +5676,81 @@
56485676
if (permissions.read) {
56495677
permissionsResult |= 1;
56505678
}
5651-
/* eslint-enable */
56525679
return permissionsResult;
56535680
}
5654-
function prepareMessagePayload$2(modules, incomingParams) {
5681+
function prepareMessagePayloadVsp(_modules, _a) {
5682+
var ttl = _a.ttl, resources = _a.resources, patterns = _a.patterns, meta = _a.meta, authorizedUserId = _a.authorizedUserId;
5683+
var params = {
5684+
ttl: 0,
5685+
permissions: {
5686+
resources: {
5687+
channels: {},
5688+
groups: {},
5689+
uuids: {},
5690+
users: {},
5691+
spaces: {}, // not used, needed for api backward compatibility
5692+
},
5693+
patterns: {
5694+
channels: {},
5695+
groups: {},
5696+
uuids: {},
5697+
users: {},
5698+
spaces: {}, // not used, needed for api backward compatibility
5699+
},
5700+
meta: {},
5701+
},
5702+
};
5703+
if (resources) {
5704+
var users_1 = resources.users, spaces_1 = resources.spaces, groups_1 = resources.groups;
5705+
if (users_1) {
5706+
Object.keys(users_1).forEach(function (userID) {
5707+
params.permissions.resources.uuids[userID] = extractPermissions(users_1[userID]);
5708+
});
5709+
}
5710+
if (spaces_1) {
5711+
Object.keys(spaces_1).forEach(function (spaceId) {
5712+
params.permissions.resources.channels[spaceId] = extractPermissions(spaces_1[spaceId]);
5713+
});
5714+
}
5715+
if (groups_1) {
5716+
Object.keys(groups_1).forEach(function (group) {
5717+
params.permissions.resources.groups[group] = extractPermissions(groups_1[group]);
5718+
});
5719+
}
5720+
}
5721+
if (patterns) {
5722+
var users_2 = patterns.users, spaces_2 = patterns.spaces, groups_2 = patterns.groups;
5723+
if (users_2) {
5724+
Object.keys(users_2).forEach(function (userId) {
5725+
params.permissions.patterns.uuids[userId] = extractPermissions(users_2[userId]);
5726+
});
5727+
}
5728+
if (spaces_2) {
5729+
Object.keys(spaces_2).forEach(function (spaceId) {
5730+
params.permissions.patterns.channels[spaceId] = extractPermissions(spaces_2[spaceId]);
5731+
});
5732+
}
5733+
if (groups_2) {
5734+
Object.keys(groups_2).forEach(function (group) {
5735+
params.permissions.patterns.groups[group] = extractPermissions(groups_2[group]);
5736+
});
5737+
}
5738+
}
5739+
if (ttl || ttl === 0) {
5740+
params.ttl = ttl;
5741+
}
5742+
if (meta) {
5743+
params.permissions.meta = meta;
5744+
}
5745+
if (authorizedUserId) {
5746+
params.permissions.uuid = "".concat(authorizedUserId); // ensure this is a string
5747+
}
5748+
return params;
5749+
}
5750+
function prepareMessagePayload$2(_modules, incomingParams) {
5751+
if (hasVspTerms(incomingParams)) {
5752+
return prepareMessagePayloadVsp(_modules, incomingParams);
5753+
}
56555754
var ttl = incomingParams.ttl, resources = incomingParams.resources, patterns = incomingParams.patterns, meta = incomingParams.meta, authorized_uuid = incomingParams.authorized_uuid;
56565755
var params = {
56575756
ttl: 0,
@@ -5674,7 +5773,7 @@
56745773
},
56755774
};
56765775
if (resources) {
5677-
var uuids_1 = resources.uuids, channels_1 = resources.channels, groups_1 = resources.groups;
5776+
var uuids_1 = resources.uuids, channels_1 = resources.channels, groups_3 = resources.groups;
56785777
if (uuids_1) {
56795778
Object.keys(uuids_1).forEach(function (uuid) {
56805779
params.permissions.resources.uuids[uuid] = extractPermissions(uuids_1[uuid]);
@@ -5685,14 +5784,14 @@
56855784
params.permissions.resources.channels[channel] = extractPermissions(channels_1[channel]);
56865785
});
56875786
}
5688-
if (groups_1) {
5689-
Object.keys(groups_1).forEach(function (group) {
5690-
params.permissions.resources.groups[group] = extractPermissions(groups_1[group]);
5787+
if (groups_3) {
5788+
Object.keys(groups_3).forEach(function (group) {
5789+
params.permissions.resources.groups[group] = extractPermissions(groups_3[group]);
56915790
});
56925791
}
56935792
}
56945793
if (patterns) {
5695-
var uuids_2 = patterns.uuids, channels_2 = patterns.channels, groups_2 = patterns.groups;
5794+
var uuids_2 = patterns.uuids, channels_2 = patterns.channels, groups_4 = patterns.groups;
56965795
if (uuids_2) {
56975796
Object.keys(uuids_2).forEach(function (uuid) {
56985797
params.permissions.patterns.uuids[uuid] = extractPermissions(uuids_2[uuid]);
@@ -5703,9 +5802,9 @@
57035802
params.permissions.patterns.channels[channel] = extractPermissions(channels_2[channel]);
57045803
});
57055804
}
5706-
if (groups_2) {
5707-
Object.keys(groups_2).forEach(function (group) {
5708-
params.permissions.patterns.groups[group] = extractPermissions(groups_2[group]);
5805+
if (groups_4) {
5806+
Object.keys(groups_4).forEach(function (group) {
5807+
params.permissions.patterns.groups[group] = extractPermissions(groups_4[group]);
57095808
});
57105809
}
57115810
}
@@ -5721,6 +5820,7 @@
57215820
return params;
57225821
}
57235822
function validateParams$8(modules, incomingParams) {
5823+
var _a, _b, _c, _d, _e, _f;
57245824
var config = modules.config;
57255825
if (!config.subscribeKey)
57265826
return 'Missing Subscribe Key';
@@ -5730,14 +5830,36 @@
57305830
return 'Missing Secret Key';
57315831
if (!incomingParams.resources && !incomingParams.patterns)
57325832
return 'Missing either Resources or Patterns.';
5833+
var hasAuthorizedUuid = (incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.authorized_uuid) !== undefined;
5834+
var hasUuidResources = ((_a = incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.resources) === null || _a === void 0 ? void 0 : _a.uuids) !== undefined;
5835+
var hasChannelResources = ((_b = incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.resources) === null || _b === void 0 ? void 0 : _b.channels) !== undefined;
5836+
var hasGroupResources = ((_c = incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.resources) === null || _c === void 0 ? void 0 : _c.groups) !== undefined;
5837+
var hasUuidPatterns = ((_d = incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.patterns) === null || _d === void 0 ? void 0 : _d.uuids) !== undefined;
5838+
var hasChannelPatterns = ((_e = incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.patterns) === null || _e === void 0 ? void 0 : _e.channels) !== undefined;
5839+
var hasGroupPatterns = ((_f = incomingParams === null || incomingParams === void 0 ? void 0 : incomingParams.patterns) === null || _f === void 0 ? void 0 : _f.groups) !== undefined;
5840+
var hasLegacyTerms = hasAuthorizedUuid ||
5841+
hasUuidResources ||
5842+
hasUuidPatterns ||
5843+
hasChannelResources ||
5844+
hasChannelPatterns ||
5845+
hasGroupResources ||
5846+
hasGroupPatterns;
5847+
if (hasVspTerms(incomingParams) && hasLegacyTerms) {
5848+
return ('Cannot mix `users`, `spaces` and `authorizedUserId` ' +
5849+
'with `uuids`, `channels`, `groups` and `authorized_uuid`');
5850+
}
57335851
if ((incomingParams.resources &&
57345852
(!incomingParams.resources.uuids || Object.keys(incomingParams.resources.uuids).length === 0) &&
57355853
(!incomingParams.resources.channels || Object.keys(incomingParams.resources.channels).length === 0) &&
5736-
(!incomingParams.resources.groups || Object.keys(incomingParams.resources.groups).length === 0)) ||
5854+
(!incomingParams.resources.groups || Object.keys(incomingParams.resources.groups).length === 0) &&
5855+
(!incomingParams.resources.users || Object.keys(incomingParams.resources.users).length === 0) &&
5856+
(!incomingParams.resources.spaces || Object.keys(incomingParams.resources.spaces).length === 0)) ||
57375857
(incomingParams.patterns &&
57385858
(!incomingParams.patterns.uuids || Object.keys(incomingParams.patterns.uuids).length === 0) &&
57395859
(!incomingParams.patterns.channels || Object.keys(incomingParams.patterns.channels).length === 0) &&
5740-
(!incomingParams.patterns.groups || Object.keys(incomingParams.patterns.groups).length === 0))) {
5860+
(!incomingParams.patterns.groups || Object.keys(incomingParams.patterns.groups).length === 0) &&
5861+
(!incomingParams.patterns.users || Object.keys(incomingParams.patterns.users).length === 0) &&
5862+
(!incomingParams.patterns.spaces || Object.keys(incomingParams.patterns.spaces).length === 0))) {
57415863
return 'Missing values for either Resources or Patterns.';
57425864
}
57435865
}

0 commit comments

Comments
 (0)