Skip to content

Commit

Permalink
twilio client capability token generation now included
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmbenton committed Jul 26, 2011
1 parent 74e1f08 commit f9da109
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/twilio-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

require "#{TWILIO_RUBY_ROOT}/twilio-ruby/util"
require "#{TWILIO_RUBY_ROOT}/twilio-ruby/util/request_validator"
require "#{TWILIO_RUBY_ROOT}/twilio-ruby/util/capability"
require "#{TWILIO_RUBY_ROOT}/twilio-ruby/rest/errors"
require "#{TWILIO_RUBY_ROOT}/twilio-ruby/rest/utils"
require "#{TWILIO_RUBY_ROOT}/twilio-ruby/rest/list_resource"
Expand Down
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Client

HTTP_HEADERS = {
'Accept' => 'application/json',
'User-Agent' => 'twilio-ruby/3.1.1',
'User-Agent' => 'twilio-ruby/3.2.0',
}

attr_reader :account_sid, :account, :accounts
Expand Down
61 changes: 61 additions & 0 deletions lib/twilio-ruby/util/capability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module Twilio
module Util
class Capability

include Twilio::Util

def initialize(account_sid, auth_token)
@account_sid = account_sid
@auth_token = auth_token
@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
6 changes: 3 additions & 3 deletions twilio-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Gem::Specification.new do |s|
s.name = "twilio-ruby"
s.version = "3.1.1"
s.version = "3.2.0"
s.author = "Andrew Benton"
s.email = "andrew@twilio.com"
s.description = "A simple library for communicating with the Twilio REST API"
s.summary = "A simple library for communicating with the Twilio REST API"
s.description = "A simple library for communicating with the Twilio REST API, building TwiML, and generating Twilio Client Capability Tokens"
s.summary = "A simple library for communicating with the Twilio REST API, building TwiML, and generating Twilio Client Capability Tokens"
s.homepage = "http://github.com/twilio/twilio-ruby"
s.platform = Gem::Platform::RUBY
s.files = Dir['lib/**/*.rb'] + Dir['test/**/*.rb'] + ['examples.rb', 'Rakefile', 'LICENSE', 'README.md', 'twilio-ruby.gemspec']
Expand Down

0 comments on commit f9da109

Please sign in to comment.