class Wowza::SecureToken::Params

Wraps a params hash in the sorting and hashing logic necessary to generate a Wowza secure token

Public Class Methods

new(opts) click to toggle source

Create the wrapper required symbol keys for:

  • stream

  • secret

  • client_ip

  • prefix

  • starttime (Integer)

  • endtime (Integer)

  • playstart (Integer)

  • playduration (Integer)

@param opts [Hash] options used to generate a token url

# File lib/wowza/secure_token.rb, line 24
def initialize(opts)
  @prefix = opts.delete(:prefix)
  @stream = opts.delete(:stream)
  @client_ip = opts.delete(:client_ip)
  @secret = opts.delete(:secret)
  @params = opts
end

Public Instance Methods

to_token() click to toggle source

@return the sorted token string for the wrapped params

# File lib/wowza/secure_token.rb, line 33
def to_token
  query_string = params_to_sorted_query_string(prefix_params(@params).merge(@client_ip => nil, @secret => nil))
  "#{@stream}?#{query_string}"
end
to_token_hash(digest_alg = Digest::SHA256) click to toggle source

@param digest_alg the digest algorithm to be used to hash the params @return a URL-safe B64 encoded hash

# File lib/wowza/secure_token.rb, line 40
def to_token_hash(digest_alg = Digest::SHA256)
  Base64.urlsafe_encode64(digest_alg.digest(to_token))
end
to_url_with_token_hash(host, port, stream_type) click to toggle source
# File lib/wowza/secure_token.rb, line 44
def to_url_with_token_hash(host, port, stream_type)
  query_string = params_to_sorted_query_string(prefix_params(@params))
  case stream_type
  when 'hls', 'hls-ssl'
    (stream_type == 'hls' ? 'http' : 'https') + "://#{host}:#{port}/#{@stream}/playlist.m3u8?#{query_string}&#{@prefix}hash=#{self.to_token_hash}"
  when 'mpeg-dash', 'mpeg-dash-ssl'
    (stream_type == 'mpeg-dash' ? 'http' : 'https') + "://#{host}:#{port}/#{@stream}/manifest.mpd?#{query_string}&#{@prefix}hash=#{self.to_token_hash}"
  when 'rtmp', 'rtmps'
    "#{stream_type}://#{host}:#{port}/#{@stream}?#{query_string}&#{@prefix}hash=#{self.to_token_hash}"
  else
    raise "Unsupported stream_type: #{stream_type}"
  end
end

Private Instance Methods

params_to_sorted_query_string(prms) click to toggle source
# File lib/wowza/secure_token.rb, line 60
def params_to_sorted_query_string(prms)
  prms.sort.map { |p| p[1].to_s == '' ? p[0] : "#{p[0]}=#{p[1]}" }.join('&')
end
prefix_params(hsh) click to toggle source
# File lib/wowza/secure_token.rb, line 64
def prefix_params(hsh)
  hsh = hsh.clone
  hsh.map { |p| ["#{@prefix}#{p[0]}", p[1]] }.to_h
end