Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for updating participant attributes #60

Merged
merged 6 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ jobs:
strategy:
matrix:
include:
- { os: ubuntu-latest, ruby_version: 2.6 }
- { os: ubuntu-latest, ruby_version: 2.7 }
- { os: ubuntu-latest, ruby_version: '3.0' }
- { os: ubuntu-latest, ruby_version: "3.0" }
- { os: ubuntu-latest, ruby_version: "3.3" }
- { os: ubuntu-latest, ruby_version: "jruby-9.4" }
steps:
- name: Setup Ruby, JRuby and TruffleRuby
uses: ruby/setup-ruby@v1.75.0
with:
bundler: 1
ruby-version: ${{ matrix.ruby_version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Run tests
run: |
gem install bundler:2.2.25
bundle install --jobs 4 --retry 3
bundle exec rspec
- name: Setup Ruby, JRuby and TruffleRuby
uses: ruby/setup-ruby@v1.190.0
with:
bundler: 1
ruby-version: ${{ matrix.ruby_version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Run tests
run: |
gem install bundler:2.3.7
bundle install --jobs 4 --retry 3
bundle exec rspec
25 changes: 16 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
livekit-server-sdk (0.6.5)
livekit-server-sdk (0.7.0)
google-protobuf (>= 3.21.0, < 4.0)
jwt (>= 2.2.3, < 3.0)
rack (>= 2.2.3)
Expand All @@ -10,22 +10,29 @@ PATH
GEM
remote: https://rubygems.org/
specs:
base64 (0.2.0)
coderay (1.1.3)
diff-lcs (1.5.0)
faraday (2.7.4)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
google-protobuf (3.22.2)
jwt (2.7.0)
faraday (2.10.1)
faraday-net_http (>= 2.0, < 3.2)
logger
faraday-net_http (3.1.1)
net-http
google-protobuf (3.25.4)
google-protobuf (3.25.4-x86_64-linux)
jwt (2.8.2)
base64
logger (1.6.0)
method_source (1.0.0)
net-http (0.4.1)
uri
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
pry-doc (1.4.0)
pry (~> 0.11)
yard (~> 0.9.11)
rack (2.2.6.4)
rack (3.1.7)
rake (13.0.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
Expand All @@ -40,10 +47,10 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
ruby2_keywords (0.0.5)
twirp (1.10.0)
faraday (< 3)
google-protobuf (~> 3.0, >= 3.7.0)
uri (0.13.0)
webrick (1.7.0)
yard (0.9.28)
webrick (~> 1.7.0)
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ task :proto do
"--ruby_out=lib/livekit/proto",
"--twirp_ruby_out=lib/livekit/proto",
"-Iprotocol",
"./protocol/protobufs/livekit_agent.proto",
"./protocol/protobufs/livekit_agent_dispatch.proto",
"./protocol/protobufs/livekit_egress.proto",
"./protocol/protobufs/livekit_ingress.proto",
"./protocol/protobufs/livekit_sip.proto",
Expand Down
27 changes: 22 additions & 5 deletions lib/livekit/access_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@

module LiveKit
class AccessToken
# 6 hours in seconds; how long the access token to the server is good for
DEFAULT_TTL = 14_400
# 10 minutes in seconds; how long the access token to the server is good for
DEFAULT_TTL = 600

# The signing algorithm used by the {jwt} gem internals
SIGNING_ALGORITHM = "HS256"

attr_accessor :grants, :identity

def initialize(api_key: nil, api_secret: nil, identity: nil, ttl: DEFAULT_TTL, name: nil, metadata: nil)
def initialize(
api_key: nil,
api_secret: nil,
identity: nil,
ttl: DEFAULT_TTL,
name: nil,
metadata: nil,
attributes: nil
)
@api_key = api_key || ENV["LIVEKIT_API_KEY"]
@api_secret = api_secret || ENV["LIVEKIT_API_SECRET"]
@grants = ClaimGrant.new
@grants.name = name
@grants.metadata = metadata
@grants.attributes = attributes
@identity = identity
@ttl = ttl
end
Expand All @@ -29,6 +38,13 @@ def add_grant(video_grant)
@grants.video = video_grant
end

def add_sip_grant(sip_grant)
if sip_grant.is_a?(Hash)
sip_grant = SIPGrant.from_hash(sip_grant)
end
@grants.sip = sip_grant
end

def metadata=(participant_md)
@grants.metadata = participant_md
end
Expand All @@ -46,8 +62,8 @@ def sha256=(sha_string)
end

def to_jwt
if @grants.video.nil?
raise ArgumentError, "VideoGrant is required"
if @grants.video.nil? && @grants.sip.nil?
raise ArgumentError, "VideoGrant or SIPGrant is required"
end

jwt_timestamp = Time.now.to_i
Expand All @@ -59,6 +75,7 @@ def to_jwt
iss: @api_key,
sub: @identity,
})
payload.compact!

return JWT.encode(payload, @api_secret, SIGNING_ALGORITHM)
end
Expand Down
55 changes: 52 additions & 3 deletions lib/livekit/grants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module LiveKit
class ClaimGrant
attr_accessor :identity, :name, :metadata, :sha256, :video
attr_accessor :identity, :name, :metadata, :sha256, :video, :sip, :attributes

def self.from_hash(hash)
return nil if hash.nil?
Expand All @@ -11,8 +11,10 @@ def self.from_hash(hash)
claim_grant.identity = hash["sub"]
claim_grant.name = hash["name"]
claim_grant.metadata = hash["metadata"]
claim_grant.attributes = hash["attributes"]
claim_grant.sha256 = hash["sha256"]
claim_grant.video = VideoGrant.from_hash(hash["video"])
claim_grant.sip = SIPGrant.from_hash(hash["sip"])
return claim_grant
end

Expand All @@ -22,15 +24,24 @@ def initialize
@metadata = nil
@sha256 = nil
@video = nil
@sip = nil
@attributes = nil
end

def to_hash
{
val = {
name: @name,
metadata: @metadata,
attributes: @attributes,
sha256: @sha256,
video: @video.to_hash,
}
if @video
val[:video] = @video.to_hash
end
if @sip
val[:sip] = @sip.to_hash
end
return val
end
end

Expand Down Expand Up @@ -121,4 +132,42 @@ def to_hash
hash
end
end

class SIPGrant
using LiveKit::Utils::StringifyKeysRefinement

attr_accessor :admin, :call

def initialize(
# true if can access SIP features
admin: nil,
# true if can make outgoing call
call: nil
)
@admin = admin
@call = call
end

def self.from_hash(hash)
return nil if hash.nil?

hash = hash.stringify_keys

SIPGrant.new(
admin: hash["admin"],
call: hash["call"]
)
end

def to_hash
hash = {}
instance_variables.each { |var|
val = instance_variable_get(var)
if val != nil
hash[var.to_s.delete("@")] = val
end
}
hash
end
end
end
25 changes: 25 additions & 0 deletions lib/livekit/proto/livekit_agent_dispatch_pb.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions lib/livekit/proto/livekit_agent_dispatch_twirp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Code generated by protoc-gen-twirp_ruby 1.10.0, DO NOT EDIT.
require 'twirp'
require_relative 'livekit_agent_dispatch_pb.rb'

module LiveKit
module Proto
class AgentDispatchServiceService < ::Twirp::Service
package 'livekit'
service 'AgentDispatchService'
rpc :CreateDispatch, CreateAgentDispatchRequest, AgentDispatch, :ruby_method => :create_dispatch
rpc :DeleteDispatch, DeleteAgentDispatchRequest, AgentDispatch, :ruby_method => :delete_dispatch
rpc :ListDispatch, ListAgentDispatchRequesst, ListAgentDispatchResponse, :ruby_method => :list_dispatch
end

class AgentDispatchServiceClient < ::Twirp::Client
client_for AgentDispatchServiceService
end
end
end
37 changes: 37 additions & 0 deletions lib/livekit/proto/livekit_agent_pb.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/livekit/proto/livekit_agent_twirp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Code generated by protoc-gen-twirp_ruby 1.10.0, DO NOT EDIT.
require 'twirp'
require_relative 'livekit_agent_pb.rb'

module LiveKit
module Proto
end
end
Loading
Loading