lib/twilio-ruby/jwt/access_token.rb in twilio-ruby-5.0.0.rc26 vs lib/twilio-ruby/jwt/access_token.rb in twilio-ruby-5.0.0

- old
+ new

@@ -1,14 +1,14 @@ module Twilio module JWT module AccessTokenGrant def _key - fail NotImplementedError + raise NotImplementedError end def _generate_payload - fail NotImplementedError + raise NotImplementedError end end class AccessToken < BaseJWT attr_accessor :account_sid, @@ -18,11 +18,20 @@ :grants, :nbf, :ttl, :valid_until - def initialize(account_sid, signing_key_sid, secret, grants=[], identity: nil, nbf: nil, ttl: 3600, valid_until: nil) + def initialize( + account_sid, + signing_key_sid, + secret, + grants = [], + identity: nil, + nbf: nil, + ttl: 3600, + valid_until: nil + ) super(secret_key: secret, issuer: signing_key_sid, subject: account_sid, nbf: nbf, ttl: ttl, @@ -44,31 +53,31 @@ def to_s to_jwt end protected + def _generate_payload now = Time.now.to_i grants = {} - if @identity - grants[:identity] = @identity - end + grants[:identity] = @identity if @identity @grants.each { |grant| grants[grant._key] = grant._generate_payload } unless @grants.empty? payload = { - jti: "#{@signing_key_sid}-#{now}", - grants: grants + jti: "#{@signing_key_sid}-#{now}", + grants: grants } payload end protected + def _generate_headers headers = { - cty: 'twilio-fpa;v=1' + cty: 'twilio-fpa;v=1' } headers end @@ -77,25 +86,20 @@ attr_accessor :service_sid, :endpoint_id, :deployment_role_sid, :push_credential_sid - def _key 'ip_messaging' end def _generate_payload payload = {} - if service_sid - payload[:service_sid] = service_sid - end + payload[:service_sid] = service_sid if service_sid - if endpoint_id - payload[:endpoint_id] = endpoint_id - end + payload[:endpoint_id] = endpoint_id if endpoint_id if deployment_role_sid payload[:deployment_role_sid] = deployment_role_sid end @@ -103,11 +107,10 @@ payload[:push_credential_sid] = push_credential_sid end payload end - end class VoiceGrant include AccessTokenGrant attr_accessor :outgoing_application_sid, @@ -134,13 +137,11 @@ if push_credential_sid payload[:push_credential_sid] = push_credential_sid end - if endpoint_id - payload[:endpoint_id] = endpoint_id - end + payload[:endpoint_id] = endpoint_id if endpoint_id payload end end @@ -149,22 +150,17 @@ attr_accessor :service_sid, :endpoint_id def _key 'data_sync' - end def _generate_payload payload = {} - if service_sid - payload['service_sid'] = service_sid - end - if endpoint_id - payload['endpoint_id'] = endpoint_id - end + payload['service_sid'] = service_sid if service_sid + payload['endpoint_id'] = endpoint_id if endpoint_id payload end end @@ -188,12 +184,10 @@ class << self extend Gem::Deprecate deprecate :new, 'VideoGrant.new', 2017, 5 end - - end class VideoGrant include AccessTokenGrant attr_accessor :room @@ -203,13 +197,11 @@ end def _generate_payload payload = {} - if room - payload[:room] = room - end + payload[:room] = room if room payload end end @@ -224,20 +216,14 @@ end def _generate_payload payload = {} - if workspace_sid - payload[:workspace_sid] = workspace_sid - end + payload[:workspace_sid] = workspace_sid if workspace_sid - if worker_sid - payload[:worker_sid] = worker_sid - end + payload[:worker_sid] = worker_sid if worker_sid - if role - payload[:role] = role - end + payload[:role] = role if role payload end end end