Skip to content

Commit

Permalink
Merge pull request #224 from twilio/prv-grant
Browse files Browse the repository at this point in the history
Add programmable voice grant
  • Loading branch information
dougblack committed Sep 1, 2016
2 parents 8c64510 + fec880d commit ac2680c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/twilio-ruby/util/access_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def to_jwt(algorithm='HS256')
now = Time.now.to_i - 1
headers = {
'cty' => 'twilio-fpa;v=1',
'typ' => 'JWT'
}

grants = {}
Expand Down Expand Up @@ -101,6 +100,41 @@ def payload

end

class VoiceGrant
attr_accessor :outgoing_application_sid,
:outgoing_application_params,
:push_credential_sid,
:endpoint_id

def key
'voice'
end

def payload
payload = {}

if outgoing_application_sid
outgoing = {}
outgoing[:application_sid] = outgoing_application_sid
if outgoing_application_params
outgoing[:params] = outgoing_application_params
end

payload[:outgoing] = outgoing
end

if push_credential_sid
payload[:push_credential_sid] = push_credential_sid
end

if endpoint_id
payload[:endpoint_id] = endpoint_id
end

payload
end
end

end
end
end
25 changes: 25 additions & 0 deletions spec/util/access_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,29 @@
expect(payload['grants']['ip_messaging']).not_to be_nil
end

it 'should add programmable voice grant' do
scat = Twilio::Util::AccessToken.new 'AC123', 'SK123','secret'
pvg = Twilio::Util::AccessToken::VoiceGrant.new
pvg.outgoing_application_sid = 'AP123'
pvg.outgoing_application_params = { :foo => 'bar' }

scat.add_grant(pvg)

token = scat.to_s
expect(token).not_to be_nil
payload, header = JWT.decode token, 'secret'

expect(payload['iss']).to eq('SK123')
expect(payload['sub']).to eq('AC123')
expect(payload['exp']).not_to be_nil
expect(payload['exp']).to be >= Time.now.to_i
expect(payload['jti']).not_to be_nil
expect(payload['jti']).to start_with payload['iss']
expect(payload['grants']).not_to be_nil
expect(payload['grants'].count).to eq(1)
expect(payload['grants']['voice']).not_to be_nil
expect(payload['grants']['voice']['outgoing']['application_sid']).to eq('AP123')
expect(payload['grants']['voice']['outgoing']['params']['foo']).to eq('bar')
end

end

0 comments on commit ac2680c

Please sign in to comment.