class Plivo::Token::AccessToken

Attributes

grants[R]
lifetime[R]
uid[R]
username[R]
valid_from[R]

Public Class Methods

new(auth_id = nil, auth_token = nil, username = nil, uid = nil) click to toggle source
# File lib/plivo/jwt.rb, line 32
def initialize(auth_id = nil, auth_token = nil, username = nil, uid = nil)
  configure_credentials(auth_id, auth_token)
  Utils.valid_param?(:username, username, [String, Symbol], true)
  @username = username

  Utils.valid_param?(:uid, uid, [String, Symbol], false)
  uid ||= username + '-' + Time.now.to_i
  @uid = uid
  update_validity
end

Public Instance Methods

add_voice_grants(grants) click to toggle source
# File lib/plivo/jwt.rb, line 77
def add_voice_grants(grants)
  Utils.valid_param?(:grants, grants, [VoiceGrants], true)
  @grants = grants
end
auth_id() click to toggle source
# File lib/plivo/jwt.rb, line 73
def auth_id
  @auth_credentials[:auth_id]
end
to_jwt() click to toggle source
# File lib/plivo/jwt.rb, line 82
def to_jwt
  payload = {
    jti: uid,
    sub: username,
    iss: auth_id,
    nbf: valid_from.to_i,
    exp: valid_from.to_i + lifetime,
    grants: {
      voice: grants.to_hash || {}
    }
  }
  JWT.encode payload, key, 'HS256', {typ: 'JWT', cty: 'plivo;v=1'}
end
update_validity(valid_from = nil, lifetime = nil, valid_till = nil) click to toggle source
# File lib/plivo/jwt.rb, line 43
def update_validity(valid_from = nil, lifetime = nil, valid_till = nil)
  Utils.valid_param?(:valid_from, valid_from, [Time, Integer], false)
  Utils.valid_param?(:lifetime, lifetime, [Integer], false)
  Utils.valid_param?(:valid_till, valid_till, [Time, Integer], false)

  if valid_from.nil?
    @lifetime = lifetime || 84_600
    @valid_from = if valid_till.nil?
                    Time.now
                  else
                    Time.at(valid_till.to_i - @lifetime).utc
                  end

  elsif valid_till.nil?
    @lifetime = lifetime || 84_600
    @valid_from = valid_from
  else
    unless lifetime.nil?
      raise Exceptions::ValidationError, 'use any 2 of valid_from, lifetime and valid_till'
    end

    @valid_from = valid_from
    @lifetime = valid_till.to_i - valid_from.to_i
  end

  return unless @lifetime < 180 || @lifetime > 84_600

  raise Exceptions::ValidationError, 'validity out of [180, 84600] seconds'
end

Private Instance Methods

configure_credentials(auth_id, auth_token) click to toggle source
# File lib/plivo/jwt.rb, line 102
def configure_credentials(auth_id, auth_token)
  # Fetches and sets the right credentials
  auth_id ||= ENV['PLIVO_AUTH_ID']
  auth_token ||= ENV['PLIVO_AUTH_TOKEN']

  raise Exceptions::AuthenticationError, 'Couldn\'t find auth credentials' unless
    auth_id && auth_token

  raise Exceptions::AuthenticationError, "Invalid auth_id: '#{auth_id}'" unless
    Utils.valid_account?(auth_id)

  @auth_credentials = {
    auth_id: auth_id,
    auth_token: auth_token
  }
end
key() click to toggle source
# File lib/plivo/jwt.rb, line 98
def key
  @auth_credentials[:auth_token]
end