Sha256: a76856547020809d01150089781a4d58f6532c7ee3d7c94cf613d5763dcec066

Contents?: true

Size: 1.53 KB

Versions: 18

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require "jwt"

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

    # 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)
      @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
      @identity = identity
      @ttl = ttl
    end

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

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

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

    def sha256
      @grants.sha256
    end

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

    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
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
livekit-server-sdk-0.6.5 lib/livekit/access_token.rb
livekit-server-sdk-0.6.4 lib/livekit/access_token.rb
livekit-server-sdk-0.6.3 lib/livekit/access_token.rb
livekit-server-sdk-0.6.2 lib/livekit/access_token.rb
livekit-server-sdk-0.6.1 lib/livekit/access_token.rb
livekit-server-sdk-0.5.6 lib/livekit/access_token.rb
livekit-server-sdk-0.5.5 lib/livekit/access_token.rb
livekit-server-sdk-0.5.4 lib/livekit/access_token.rb
livekit-server-sdk-0.5.3 lib/livekit/access_token.rb
livekit-server-sdk-0.5.2 lib/livekit/access_token.rb
livekit-server-sdk-0.5.1 lib/livekit/access_token.rb
livekit-server-sdk-0.5.0 lib/livekit/access_token.rb
livekit-server-sdk-0.4.0 lib/livekit/access_token.rb
livekit-server-sdk-0.3.1 lib/livekit/access_token.rb
livekit-server-sdk-0.3.0 lib/livekit/access_token.rb
livekit-server-sdk-0.2.2 lib/livekit/access_token.rb
livekit-server-sdk-0.2.1 lib/livekit/access_token.rb
livekit-server-sdk-0.2.0 lib/livekit/access_token.rb