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



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

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

#add_sip_grant(sip_grant) ⇒ Object



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

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

#metadata=(participant_md) ⇒ Object



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

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

#name=(participant_name) ⇒ Object



52
53
54
# File 'lib/livekit/access_token.rb', line 52

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

#sha256Object



56
57
58
# File 'lib/livekit/access_token.rb', line 56

def sha256
  @grants.sha256
end

#sha256=(sha_string) ⇒ Object



60
61
62
# File 'lib/livekit/access_token.rb', line 60

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

#to_jwtObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/livekit/access_token.rb', line 64

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