Skip to content

Commit f80d58a

Browse files
authored
Add spaces_permissions and users_permissions to grant_token (#131)
1 parent 17617e1 commit f80d58a

File tree

12 files changed

+180
-22
lines changed

12 files changed

+180
-22
lines changed

.pubnub.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
---
2-
version: "5.0.0"
2+
version: "5.1.0"
33
changelog:
4+
- date: 2022-07-26
5+
version: v5.1.0
6+
changes:
7+
- type: feature
8+
text: "Add support for spaces and users permissions in grant_token."
9+
- type: feature
10+
text: "Add user_id and deprecate uuid when creating new pubnub instance."
411
- date: 2022-01-13
512
version: v5.0.0
613
changes:
@@ -625,7 +632,7 @@ sdks:
625632
- x86-64
626633
- distribution-type: package
627634
distribution-repository: RubyGems
628-
package-name: pubnub-5.0.0.gem
635+
package-name: pubnub-5.1.0.gem
629636
location: https://rubygems.org/gems/pubnub
630637
requires:
631638
- name: addressable
@@ -730,8 +737,8 @@ sdks:
730737
- x86-64
731738
- distribution-type: library
732739
distribution-repository: GitHub release
733-
package-name: pubnub-5.0.0.gem
734-
location: https://github.com/pubnub/ruby/releases/download/v5.0.0/pubnub-5.0.0.gem
740+
package-name: pubnub-5.1.0.gem
741+
location: https://github.com/pubnub/ruby/releases/download/v5.1.0/pubnub-5.1.0.gem
735742
requires:
736743
- name: addressable
737744
min-version: 2.0.0

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v5.1.0
2+
July 26 2022
3+
4+
#### Added
5+
- Add support for spaces and users permissions in grant_token.
6+
- Add user_id and deprecate uuid when creating new pubnub instance.
7+
18
## v5.0.0
29
January 13 2022
310

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
pubnub (5.0.0)
4+
pubnub (5.1.0)
55
addressable (>= 2.0.0)
66
concurrent-ruby (~> 1.1.5)
77
concurrent-ruby-edge (~> 0.5.0)

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.0.0
1+
5.1.0

fixtures/vcr_cassettes/examples/grant_token/1.yml

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/pubnub/event.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def create_variables_from_options(options)
175175
state channel_group channel_groups compressed meta customs include_token
176176
replicate with_presence cipher_key_selector include_meta join update get
177177
add remove push_token push_gateway environment topic authorized_uuid
178-
token
178+
authorized_user_id token
179179
]
180180

181181
options = options.each_with_object({}) { |option, obj| obj[option.first.to_sym] = option.last }

lib/pubnub/events/grant_token.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,28 @@ def initialize(options, app)
1010
@uuids = options[:uuids] || {}
1111
options[:channels] = options[:channels] || {}
1212
options[:channel_groups] = options[:channel_groups] || {}
13+
@spaces_permissions = options[:spaces_permissions] || {}
14+
@users_permissions = options[:users_permissions] || {}
1315
super
1416
end
1517

1618
def fire
1719
Pubnub.logger.debug('Pubnub::GrantToken') { "Fired event #{self.class}" }
20+
if @authorized_user_id != nil
21+
uuid = @authorized_user_id
22+
elsif @authorized_uuid != nil
23+
uuid = @authorized_uuid
24+
else
25+
uuid = nil
26+
end
1827

1928
raw_body = {
2029
ttl: @ttl,
2130
permissions: {
2231
meta: @meta,
23-
uuid: @authorized_uuid,
24-
resources: prepare_permissions(:resource, @channels, @channel_groups, @uuids),
25-
patterns: prepare_permissions(:pattern, @channels, @channel_groups, @uuids)
32+
uuid: uuid,
33+
resources: prepare_permissions(:resource, @channels, @channel_groups, @uuids, @spaces_permissions, @users_permissions),
34+
patterns: prepare_permissions(:pattern, @channels, @channel_groups, @uuids, @spaces_permissions, @users_permissions)
2635
}.select { |_, v| v }
2736
}
2837
body = Formatter.format_message(raw_body, "", false, false)
@@ -47,11 +56,11 @@ def current_operation
4756
Pubnub::Constants::OPERATION_GRANT_TOKEN
4857
end
4958

50-
def prepare_permissions(type, channels, groups, uuids)
59+
def prepare_permissions(type, channels, groups, uuids, spaces_permissions, users_permissions)
5160
{
52-
channels: prepare_single_permissions(type, channels),
61+
channels: prepare_single_permissions(type, channels).merge!(prepare_single_permissions(type, spaces_permissions)),
5362
groups: prepare_single_permissions(type, groups),
54-
uuids: prepare_single_permissions(type, uuids)
63+
uuids: prepare_single_permissions(type, uuids).merge!(prepare_single_permissions(type, users_permissions))
5564
}
5665
end
5766

lib/pubnub/validators/grant_token.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def validate!
1212
validate_permissions!(@uuids, ":uuids")
1313
validate_permissions!(@channels, ":channels")
1414
validate_permissions!(@channel_groups, ":uuids")
15+
validate_objects_entities_separation!
1516
end
1617

1718
private
@@ -45,6 +46,19 @@ def validate_permissions!(arg, name)
4546
":#{name} has to be kind of Hash for grant token event."
4647
) unless arg.is_a?(Hash)
4748
end
49+
50+
def validate_objects_entities_separation!
51+
entities_set = !@spaces_permissions.empty? ||
52+
!@users_permissions.empty?
53+
objects_set = !@channels.empty? ||
54+
!@channel_groups.empty? ||
55+
!@uuids.empty?
56+
57+
raise(
58+
ArgumentError.new(object: self, message: "Can't mix entities and objects"),
59+
"Can't mix entities and objects"
60+
) if (entities_set && objects_set)
61+
end
4862
end
4963
end
5064
end

lib/pubnub/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Toplevel Pubnub module.
22
module Pubnub
3-
VERSION = '5.0.0'.freeze
3+
VERSION = '5.1.0'.freeze
44
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require "spec_helper"
2+
3+
describe Pubnub::GrantToken do
4+
around :each do |example|
5+
@fired = false
6+
7+
@callback = -> (_envelope) do
8+
@fired = true
9+
end
10+
11+
@pubnub = Pubnub.new(
12+
publish_key: "pub-a-mock-key",
13+
subscribe_key: "sub-a-mock-key",
14+
secret_key: "sec-a-mock-key",
15+
user_id: "ruby-test-user-id-client-one",
16+
auth_key: "ruby-test-auth-client-one",
17+
)
18+
19+
example.run_with_retry retry: 10
20+
end
21+
22+
it "__channel___demo____group__nil___read__false___write__false___manage__false___ttl__300___auth_key__nil___http_sync__true___callback___block_" do
23+
VCR.use_cassette("examples/grant_token/1", record: :none) do
24+
Pubnub::Grant.any_instance.stub(:current_time).and_return "1657795717"
25+
Pubnub::Grant.any_instance.stub(:signature).and_return "v2.dOOoWVBHwrRw3kXQ37gWR1De6TlufWW_ccWaLFMhaSw"
26+
envelope = @pubnub.grant_token(
27+
ttl: 60,
28+
authorized_user_id: "authorized_user_id",
29+
spaces_permissions: {
30+
"id": Pubnub::Permissions.res(read: true),
31+
"whatever.*": Pubnub::Permissions.pat(read: true, write: true)
32+
},
33+
users_permissions: {
34+
"user1": Pubnub::Permissions.res(update: true),
35+
"users.*": Pubnub::Permissions.pat(create: true, delete: true),
36+
},
37+
http_sync: true,
38+
&@callback
39+
)
40+
expect(envelope.is_a?(Pubnub::Envelope)).to eq true
41+
expect(envelope.error?).to eq false
42+
43+
expect(envelope.status[:code]).to eq(200)
44+
expect(envelope.status[:category]).to eq(:ack)
45+
expect(envelope.status[:config]).to eq({:tls => false, :uuid => "ruby-test-user-id-client-one", :auth_key => "ruby-test-auth-client-one", :origin => "ps.pndsn.com"})
46+
47+
expect(envelope.result[:code]).to eq(200)
48+
expect(envelope.result[:operation]).to eq(:grant_token)
49+
end
50+
end
51+
52+
end

0 commit comments

Comments
 (0)