Class: LiveKit::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/livekit/access_token.rb

Constant Summary collapse

DEFAULT_TTL =

10 minutes in seconds; how long the access token to the server is good for

600
SIGNING_ALGORITHM =

The signing algorithm used by the jwt gem internals

"HS256"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, api_secret: nil, identity: nil, ttl: DEFAULT_TTL, name: nil, metadata: nil, attributes: nil) ⇒ AccessToken

Returns a new instance of AccessToken.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/livekit/access_token.rb', line 15

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. = 
  @grants.attributes = attributes
  @identity = identity
  @ttl = ttl
end

Instance Attribute Details

#grantsObject

Returns the value of attribute grants.



13
14
15
# File 'lib/livekit/access_token.rb', line 13

def grants
  @grants
end

#identityObject

Returns the value of attribute identity.



13
14
15
# File 'lib/livekit/access_token.rb', line 13

def identity
  @identity
end

Instance Method Details

#add_grant(video_grant) ⇒ Object

Deprecated, use video_grant = instead



35
36
37
38
39
40
# File 'lib/livekit/access_token.rb', line 35

def add_grant(video_grant)
  if video_grant.is_a?(Hash)
    video_grant = VideoGrant.from_hash(video_grant)
  end
  self.video_grant = video_grant
end

#add_sip_grant(sip_grant) ⇒ Object

Deprecated, use sip_grant = instead



47
48
49
50
51
52
# File 'lib/livekit/access_token.rb', line 47

def add_sip_grant(sip_grant)
  if sip_grant.is_a?(Hash)
    sip_grant = SIPGrant.from_hash(sip_grant)
  end
  self.sip_grant = sip_grant
end

#attributes=(participant_attributes) ⇒ Object



62
63
64
# File 'lib/livekit/access_token.rb', line 62

def attributes=(participant_attributes)
  @grants.attributes = participant_attributes
end

#metadata=(participant_md) ⇒ Object



58
59
60
# File 'lib/livekit/access_token.rb', line 58

def metadata=(participant_md)
  @grants. = participant_md
end

#name=(participant_name) ⇒ Object



66
67
68
# File 'lib/livekit/access_token.rb', line 66

def name=(participant_name)
  @grants.name = participant_name
end

#room_config=(room_config) ⇒ Object



78
79
80
# File 'lib/livekit/access_token.rb', line 78

def room_config=(room_config)
  @grants.room_config = room_config
end

#room_preset=(room_preset) ⇒ Object



82
83
84
# File 'lib/livekit/access_token.rb', line 82

def room_preset=(room_preset)
  @grants.room_preset = room_preset
end

#sha256Object



70
71
72
# File 'lib/livekit/access_token.rb', line 70

def sha256
  @grants.sha256
end

#sha256=(sha_string) ⇒ Object



74
75
76
# File 'lib/livekit/access_token.rb', line 74

def sha256=(sha_string)
  @grants.sha256 = sha_string
end

#sip_grant=(sip_grant) ⇒ Object



54
55
56
# File 'lib/livekit/access_token.rb', line 54

def sip_grant=(sip_grant)
  @grants.sip = sip_grant
end

#to_jwtObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/livekit/access_token.rb', line 86

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

  jwt_timestamp = Time.now.to_i
  payload = {}
  payload.merge!(@grants.to_hash)
  payload.merge!({
    exp: jwt_timestamp + @ttl,
    nbf: jwt_timestamp - 5,
    iss: @api_key,
    sub: @identity,
  })
  payload.compact!

  return JWT.encode(payload, @api_secret, SIGNING_ALGORITHM)
end

#video_grant=(video_grant) ⇒ Object



42
43
44
# File 'lib/livekit/access_token.rb', line 42

def video_grant=(video_grant)
  @grants.video = video_grant
end