Class: Vidyo::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/vidyo/token.rb

Instance Method Summary collapse

Constructor Details

#initialize(key:, application_id:, user_name:, expires_in:) ⇒ Token

Returns a new instance of Token.



12
13
14
15
16
17
# File 'lib/vidyo/token.rb', line 12

def initialize(key:, application_id:, user_name:, expires_in:)
  @key = key
  @application_id = application_id
  @user_name = user_name
  @expires_in = expires_in
end

Instance Method Details

#bodyObject



40
41
42
# File 'lib/vidyo/token.rb', line 40

def body
  to_bytes(request_type) + separator + to_bytes(jid) + separator + to_bytes(expires_at.to_s) + separator
end

#epoch_secondsObject



19
20
21
22
# File 'lib/vidyo/token.rb', line 19

def epoch_seconds
  # Converts 1970-01-01 to seconds since 0 AD.
  62167219200
end

#expires_atObject



24
25
26
# File 'lib/vidyo/token.rb', line 24

def expires_at
  epoch_seconds + Time.now.gmtime.to_i + @expires_in
end

#jidObject



28
29
30
# File 'lib/vidyo/token.rb', line 28

def jid
  "#{@user_name}@#{@application_id}"
end

#macObject



44
45
46
47
# File 'lib/vidyo/token.rb', line 44

def mac
  sha384 = OpenSSL::HMAC.new(@key, OpenSSL::Digest.new('sha384'))
  sha384.update(body.pack("C*"))
end

#request_typeObject



36
37
38
# File 'lib/vidyo/token.rb', line 36

def request_type
  "provision"
end

#separatorObject



32
33
34
# File 'lib/vidyo/token.rb', line 32

def separator
  [0]
end

#serializeObject



53
54
55
# File 'lib/vidyo/token.rb', line 53

def serialize
  Base64.strict_encode64(unserialized)
end

#unserializedObject



49
50
51
# File 'lib/vidyo/token.rb', line 49

def unserialized
  (body + separator).pack("C*") + mac.hexdigest
end