From 8753424abfa33df29343fb24c79631f783f09797 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Tue, 29 Oct 2024 15:54:19 -0700 Subject: [PATCH] Simpler setter for participant attributes (#65) * Simpler setter for participant attributes * updating usage to be explicit * add attribute test * fix typo --- README.md | 9 ++++++--- lib/livekit/access_token.rb | 14 ++++++++++++++ lib/livekit/auth_mixin.rb | 4 ++-- spec/livekit/access_token_spec.rb | 4 ++-- spec/livekit/token_verifier_spec.rb | 10 ++++++---- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 083ab6d..2f33c0a 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,9 @@ # LiveKit Server API for Ruby + Use this SDK to interact with LiveKit server APIs and create access tokens from your Ruby backend. + This library is designed to work with Ruby 2.6.0 and above. @@ -46,8 +48,8 @@ require 'livekit' token = LiveKit::AccessToken.new(api_key: 'yourkey', api_secret: 'yoursecret') token.identity = 'participant-identity' token.name = 'participant-name' -token.add_grant(roomJoin: true, room: 'room-name') - +token.set_video_grant(LiveKit::VideoGrant.new(roomJoin: true, room: 'room-name')) +token.attributes = { "mykey" => "myvalue" } puts token.to_jwt ``` @@ -136,7 +138,9 @@ You may store credentials in environment variables. If api-key or api-secret is The gem is available as open source under the terms of Apache 2.0 License. +
+ @@ -147,4 +151,3 @@ The gem is available as open source under the terms of Apache 2.0 License.
LiveKit Ecosystem
Realtime SDKsReact Components · Browser · Swift Components · iOS/macOS/visionOS · Android · Flutter · React Native · Rust · Node.js · Python · Unity (web) · Unity (beta)
- diff --git a/lib/livekit/access_token.rb b/lib/livekit/access_token.rb index bd9ddd0..f3eb7b5 100644 --- a/lib/livekit/access_token.rb +++ b/lib/livekit/access_token.rb @@ -31,17 +31,27 @@ def initialize( @ttl = ttl end + # Deprecated, use set_video_grant instead def add_grant(video_grant) if video_grant.is_a?(Hash) video_grant = VideoGrant.from_hash(video_grant) end + self.set_video_grant(video_grant) + end + + def set_video_grant(video_grant) @grants.video = video_grant end + # Deprecated, use set_sip_grant instead def add_sip_grant(sip_grant) if sip_grant.is_a?(Hash) sip_grant = SIPGrant.from_hash(sip_grant) end + self.set_sip_grant(sip_grant) + end + + def set_sip_grant(sip_grant) @grants.sip = sip_grant end @@ -49,6 +59,10 @@ def metadata=(participant_md) @grants.metadata = participant_md end + def attributes=(participant_attributes) + @grants.attributes = participant_attributes + end + def name=(participant_name) @grants.name = participant_name end diff --git a/lib/livekit/auth_mixin.rb b/lib/livekit/auth_mixin.rb index 42e2349..b733df7 100644 --- a/lib/livekit/auth_mixin.rb +++ b/lib/livekit/auth_mixin.rb @@ -10,10 +10,10 @@ def auth_header( headers = {} t = ::LiveKit::AccessToken.new(api_key: @api_key, api_secret: @api_secret) if video_grant != nil - t.add_grant(video_grant) + t.set_video_grant(video_grant) end if sip_grant != nil - t.add_sip_grant(sip_grant) + t.set_sip_grant(sip_grant) end headers["Authorization"] = "Bearer #{t.to_jwt}" headers["User-Agent"] = "LiveKit Ruby SDK" diff --git a/spec/livekit/access_token_spec.rb b/spec/livekit/access_token_spec.rb index 5e53260..19dced8 100644 --- a/spec/livekit/access_token_spec.rb +++ b/spec/livekit/access_token_spec.rb @@ -6,7 +6,7 @@ RSpec.describe LiveKit::AccessToken do it "generates a valid JWT with defaults" do token = described_class.new(api_key: TEST_KEY, api_secret: TEST_SECRET) - token.add_grant LiveKit::VideoGrant.new + token.set_video_grant LiveKit::VideoGrant.new jwt = token.to_jwt decoded = JWT.decode(jwt, TEST_SECRET, true, algorithm: "HS256") expect(decoded.first["iss"]).to eq(TEST_KEY) @@ -24,7 +24,7 @@ token = described_class.new(api_key: TEST_KEY, api_secret: TEST_SECRET, identity: "test_identity", ttl: 60) token.name = "myname" - token.add_grant(LiveKit::VideoGrant.new(roomJoin: true, room: "myroom", canPublish: false)) + token.set_video_grant(LiveKit::VideoGrant.new(roomJoin: true, room: "myroom", canPublish: false)) jwt = token.to_jwt decoded = JWT.decode(jwt, TEST_SECRET, true, algorithm: "HS256") diff --git a/spec/livekit/token_verifier_spec.rb b/spec/livekit/token_verifier_spec.rb index 148dc76..d6a1e0f 100644 --- a/spec/livekit/token_verifier_spec.rb +++ b/spec/livekit/token_verifier_spec.rb @@ -5,7 +5,8 @@ token = LiveKit::AccessToken.new(api_key: TEST_KEY, api_secret: TEST_SECRET, identity: "user") token.name = "name" - token.add_grant LiveKit::VideoGrant.new(roomJoin: true, room: "testroom") + token.set_video_grant LiveKit::VideoGrant.new(roomJoin: true, room: "testroom") + token.attributes = { "mykey" => "myvalue" } jwt = token.to_jwt v = described_class.new(api_key: TEST_KEY, api_secret: TEST_SECRET) grant = v.verify(jwt) @@ -13,12 +14,13 @@ expect(grant.video.room).to eq("testroom") expect(grant.name).to eq("name") expect(grant.identity).to eq("user") + expect(grant.attributes["mykey"]).to eq("myvalue") end it "fails on expired tokens" do token = LiveKit::AccessToken.new(api_key: TEST_KEY, api_secret: TEST_SECRET, identity: "test_identity", ttl: -10) - token.add_grant(LiveKit::VideoGrant.new(roomJoin: true)) + token.set_video_grant(LiveKit::VideoGrant.new(roomJoin: true)) jwt = token.to_jwt v = described_class.new(api_key: TEST_KEY, api_secret: TEST_SECRET) @@ -28,7 +30,7 @@ it "fails on invalid secret" do token = LiveKit::AccessToken.new(api_key: TEST_KEY, api_secret: TEST_SECRET, identity: "test_identity") - token.add_grant(LiveKit::VideoGrant.new(roomJoin: true)) + token.set_video_grant(LiveKit::VideoGrant.new(roomJoin: true)) jwt = token.to_jwt v = described_class.new(api_key: TEST_KEY, api_secret: "wrong-secret") @@ -38,7 +40,7 @@ it "fails on invalid api-key" do token = LiveKit::AccessToken.new(api_key: TEST_KEY, api_secret: TEST_SECRET, identity: "test_identity") - token.add_grant(LiveKit::VideoGrant.new(roomJoin: true)) + token.set_video_grant(LiveKit::VideoGrant.new(roomJoin: true)) jwt = token.to_jwt v = described_class.new(api_key: "wrong key", api_secret: TEST_SECRET)