Class: Wowza::SecureToken::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/wowza/secure_token.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Params

Create the wrapper required symbol keys for:

  • stream

  • secret

  • client_ip

  • prefix

  • starttime (Integer)

  • endtime (Integer)

  • playstart (Integer)

  • playduration (Integer)

Parameters:

  • opts (Hash)

    options used to generate a token url



24
25
26
27
28
29
30
# 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

Instance Method Details

#to_tokenObject

Returns the sorted token string for the wrapped params.

Returns:

  • the sorted token string for the wrapped params



33
34
35
36
# 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) ⇒ Object

Returns a URL-safe B64 encoded hash.

Parameters:

  • digest_alg (defaults to: Digest::SHA256)

    the digest algorithm to be used to hash the params

Returns:

  • a URL-safe B64 encoded hash



40
41
42
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# 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