Class: Plivo::Token::AccessToken
- Inherits:
-
Object
- Object
- Plivo::Token::AccessToken
- Defined in:
- lib/plivo/jwt.rb
Instance Attribute Summary collapse
-
#grants ⇒ Object
readonly
Returns the value of attribute grants.
-
#lifetime ⇒ Object
readonly
Returns the value of attribute lifetime.
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
-
#valid_from ⇒ Object
readonly
Returns the value of attribute valid_from.
Instance Method Summary collapse
- #add_voice_grants(grants) ⇒ Object
- #auth_id ⇒ Object
-
#initialize(auth_id = nil, auth_token = nil, username = nil, uid = nil) ⇒ AccessToken
constructor
A new instance of AccessToken.
- #to_jwt ⇒ Object
- #update_validity(valid_from = nil, lifetime = nil, valid_till = nil) ⇒ Object
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
#grants ⇒ Object (readonly)
Returns the value of attribute grants.
31 32 33 |
# File 'lib/plivo/jwt.rb', line 31 def grants @grants end |
#lifetime ⇒ Object (readonly)
Returns the value of attribute lifetime.
31 32 33 |
# File 'lib/plivo/jwt.rb', line 31 def lifetime @lifetime end |
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
31 32 33 |
# File 'lib/plivo/jwt.rb', line 31 def uid @uid end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
31 32 33 |
# File 'lib/plivo/jwt.rb', line 31 def username @username end |
#valid_from ⇒ Object (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_id ⇒ Object
73 74 75 |
# File 'lib/plivo/jwt.rb', line 73 def auth_id @auth_credentials[:auth_id] end |
#to_jwt ⇒ Object
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 |