Skip to content

Commit

Permalink
Simpler setter for participant attributes (#65)
Browse files Browse the repository at this point in the history
* Simpler setter for participant attributes

* updating usage to be explicit

* add attribute test

* fix typo
  • Loading branch information
davidzhao authored Oct 29, 2024
1 parent 4f3e128 commit 8753424
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
# LiveKit Server API for Ruby

<!--BEGIN_DESCRIPTION-->

Use this SDK to interact with <a href="https://livekit.io/">LiveKit</a> server APIs and create access tokens from your Ruby backend.

<!--END_DESCRIPTION-->

This library is designed to work with Ruby 2.6.0 and above.
Expand Down Expand Up @@ -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
```

Expand Down Expand Up @@ -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.

<!--BEGIN_REPO_NAV-->

<br/><table>

<thead><tr><th colspan="2">LiveKit Ecosystem</th></tr></thead>
<tbody>
<tr><td>Realtime SDKs</td><td><a href="https://github.com/livekit/components-js">React Components</a> · <a href="https://github.com/livekit/client-sdk-js">Browser</a> · <a href="https://github.com/livekit/components-swift">Swift Components</a> · <a href="https://github.com/livekit/client-sdk-swift">iOS/macOS/visionOS</a> · <a href="https://github.com/livekit/client-sdk-android">Android</a> · <a href="https://github.com/livekit/client-sdk-flutter">Flutter</a> · <a href="https://github.com/livekit/client-sdk-react-native">React Native</a> · <a href="https://github.com/livekit/rust-sdks">Rust</a> · <a href="https://github.com/livekit/node-sdks">Node.js</a> · <a href="https://github.com/livekit/python-sdks">Python</a> · <a href="https://github.com/livekit/client-sdk-unity-web">Unity (web)</a> · <a href="https://github.com/livekit/client-sdk-unity">Unity (beta)</a></td></tr><tr></tr>
Expand All @@ -147,4 +151,3 @@ The gem is available as open source under the terms of Apache 2.0 License.
</tbody>
</table>
<!--END_REPO_NAV-->

14 changes: 14 additions & 0 deletions lib/livekit/access_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,38 @@ 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

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
Expand Down
4 changes: 2 additions & 2 deletions lib/livekit/auth_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions spec/livekit/access_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
10 changes: 6 additions & 4 deletions spec/livekit/token_verifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
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)
expect(grant.video.roomJoin).to eq(true)
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)
Expand All @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit 8753424

Please sign in to comment.