Class: LiveKit::AccessToken

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

Constant Summary collapse

DEFAULT_TTL =

6 hours in seconds; how long the access token to the server is good for

14_400
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) ⇒ AccessToken

Returns a new instance of AccessToken.



15
16
17
18
19
20
21
22
23
# 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)
  @api_key = api_key || ENV["LIVEKIT_API_KEY"]
  @api_secret = api_secret || ENV["LIVEKIT_API_SECRET"]
  @grants = ClaimGrant.new
  @grants.name = name
  @grants. = 
  @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



25
26
27
28
29
30
# File 'lib/livekit/access_token.rb', line 25

def add_grant(video_grant)
  if video_grant.is_a?(Hash)
    video_grant = VideoGrant.from_hash(video_grant)
  end
  @grants.video = video_grant
end

#metadata=(participant_md) ⇒ Object



32
33
34
# File 'lib/livekit/access_token.rb', line 32

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

#name=(participant_name) ⇒ Object



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

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

#sha256Object



40
41
42
# File 'lib/livekit/access_token.rb', line 40

def sha256
  @grants.sha256
end

#sha256=(sha_string) ⇒ Object



44
45
46
# File 'lib/livekit/access_token.rb', line 44

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

#to_jwtObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/livekit/access_token.rb', line 48

def to_jwt
  if @grants.video.nil?
    raise ArgumentError, "VideoGrant 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,
  })

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