Sha256: 4b868620537916c69f0ee97da0e7ab21ef8ba45d178314d7d820496580d5315d

Contents?: true

Size: 1.96 KB

Versions: 50

Compression:

Stored size: 1.96 KB

Contents

module Twilio
  module Util
    class Capability

      include Twilio::Util

      def initialize(account_sid = nil, auth_token = nil)
        @account_sid = account_sid || Twilio.account_sid
        @auth_token = auth_token || Twilio.auth_token
        if @account_sid.nil? || @auth_token.nil?
          raise ArgumentError, 'Account SID and auth token are required'
        end
        @capabilities = []
      end

      def allow_client_incoming(client_name)
        @client_name = client_name # stash for use in outgoing
        scope_params = { 'clientName' => client_name }
        @capabilities << scope_uri_for('client', 'incoming', scope_params)
      end

      def allow_client_outgoing(app_sid, params = {})
        @allow_client_outgoing = true
        @outgoing_scope_params = { 'appSid' => app_sid }
        unless params.empty?
          @outgoing_scope_params['appParams'] = url_encode params
        end
      end

      def allow_event_stream(filters = {})
        scope_params = { 'path' => '/2010-04-01/Events' }
        scope_params['params'] = filters unless filters.empty?
        @capabilities << scope_uri_for('stream', 'subscribe', scope_params)
      end

      def scope_uri_for(service, privilege, params = {})
        scope_uri = "scope:#{service}:#{privilege}"
        scope_uri << "?#{url_encode(params)}" unless params.empty?
      end

      def generate(ttl = 3600)

        capabilities = @capabilities.clone # we need a local copy to work on

        # build the outgoing scope lazily so that we can use @client_name
        if @allow_client_outgoing
          params = @outgoing_scope_params
          params.merge!('clientName' => @client_name) if @client_name
          capabilities << scope_uri_for('client', 'outgoing', params)
        end

        payload = {
          'scope' => capabilities.join(' '),
          'iss' => @account_sid,
          'exp' => (Time.now.to_i + ttl),
        }

        JWT.encode payload, @auth_token

      end

    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
twilio-ruby-4.13.0 lib/twilio-ruby/util/capability.rb
twilio-ruby-4.12.1 lib/twilio-ruby/util/capability.rb
twilio-ruby-5.0.0.rc10 lib/twilio-ruby/util/capability.rb
twilio-ruby-5.0.0.rc9 lib/twilio-ruby/util/capability.rb
twilio-ruby-5.0.0.rc8 lib/twilio-ruby/util/capability.rb
twilio-ruby-5.0.0.rc7 lib/twilio-ruby/util/capability.rb
twilio-ruby-5.0.0.rc5 lib/twilio-ruby/util/capability.rb
twilio-ruby-5.0.0.rc4 lib/twilio-ruby/util/capability.rb
twilio-ruby-5.0.0.rc3 lib/twilio-ruby/util/capability.rb
twilio-ruby-4.11.1 lib/twilio-ruby/util/capability.rb
twilio-ruby-4.11.0 lib/twilio-ruby/util/capability.rb
twilio-ruby-5.0.0.rc2 lib/twilio-ruby/util/capability.rb
twilio-ruby-4.10.0.edge lib/twilio-ruby/util/capability.rb
twilio-ruby-4.10.0 lib/twilio-ruby/util/capability.rb
twilio-ruby-4.9.1 lib/twilio-ruby/util/capability.rb
twilio-ruby-4.9.1.edge lib/twilio-ruby/util/capability.rb
twilio-ruby-4.9.0 lib/twilio-ruby/util/capability.rb
twilio-ruby-4.9.0.edge lib/twilio-ruby/util/capability.rb
twilio-ruby-4.8.0 lib/twilio-ruby/util/capability.rb
twilio-ruby-4.7.0 lib/twilio-ruby/util/capability.rb