Class: Plivo::Token::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/plivo/jwt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_id = nil, auth_token = nil, username = nil, uid = nil) ⇒ AccessToken

Returns a new instance of AccessToken.



32
33
34
35
36
37
38
39
40
41
# 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

Instance Attribute Details

#grantsObject (readonly)

Returns the value of attribute grants.



31
32
33
# File 'lib/plivo/jwt.rb', line 31

def grants
  @grants
end

#lifetimeObject (readonly)

Returns the value of attribute lifetime.



31
32
33
# File 'lib/plivo/jwt.rb', line 31

def lifetime
  @lifetime
end

#uidObject (readonly)

Returns the value of attribute uid.



31
32
33
# File 'lib/plivo/jwt.rb', line 31

def uid
  @uid
end

#usernameObject (readonly)

Returns the value of attribute username.



31
32
33
# File 'lib/plivo/jwt.rb', line 31

def username
  @username
end

#valid_fromObject (readonly)

Returns the value of attribute valid_from.



31
32
33
# File 'lib/plivo/jwt.rb', line 31

def valid_from
  @valid_from
end

Instance Method Details

#add_voice_grants(grants) ⇒ Object



77
78
79
80
# File 'lib/plivo/jwt.rb', line 77

def add_voice_grants(grants)
  Utils.valid_param?(:grants, grants, [VoiceGrants], true)
  @grants = grants
end

#auth_idObject



73
74
75
# File 'lib/plivo/jwt.rb', line 73

def auth_id
  @auth_credentials[:auth_id]
end

#to_jwtObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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