Skip to content

Commit

Permalink
Merge pull request #185 from plivo/kowshik_tokencreation
Browse files Browse the repository at this point in the history
Kowshik tokencreation
  • Loading branch information
abhishekGupta-Plivo authored Aug 12, 2022
2 parents aadc799 + 2b233be commit 308cd99
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [4.29.0](https://github.com/plivo/plivo-go/tree/v4.29.0) (2022-08-01)
**Feature - Token Creation**
- `JWT Token Creation API` added functionality to create a new JWT token.

## [4.28.0](https://github.com/plivo/plivo-go/tree/v4.28.0) (2022-07-11)
**Feature - STIR Attestation**
- Add stir attestation param as part of Get CDR and Get live call APIs Response
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The Plivo Ruby SDK makes it simpler to integrate communications into your Ruby a
Add this line to your application's Gemfile:

```ruby
gem 'plivo', '>= 4.28.0'
gem 'plivo', '>= 4.29.0'
```

And then execute:
Expand Down
1 change: 1 addition & 0 deletions lib/plivo/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative 'resources/numbers'
require_relative 'resources/conferences'
require_relative 'resources/calls'
require_relative 'resources/token'
require_relative 'resources/endpoints'
require_relative 'resources/addresses'
require_relative 'resources/identities'
Expand Down
66 changes: 66 additions & 0 deletions lib/plivo/resources/token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module Plivo
module Resources
include Plivo
include Plivo::Utils
class Token < Base::Resource
def initialize(client, options = nil)
@_name = 'JWT/Token'
super
@_is_voice_request = true
end
def to_s
{
api_id: @api_id,
token: @token
}.to_s
end
end

class TokenInterface < Base::ResourceInterface
def initialize(client, resource_list_json = nil)
@_name = 'JWT/Token'
@_resource_type = Token
super
@_is_voice_request = true
end


def create(iss , options = nil)
valid_param?(:iss, iss, [String, Symbol, Hash], true)
params = {}
params[:iss] = iss

return perform_create(params, false) if options.nil?
# return perform_action('Record', 'POST', nil, true) if options.nil?
valid_param?(:options, options, [Hash], false)


if options.key?("sub") && valid_param?("sub", options["sub"], [String, Symbol], false )
params[:sub] = options["sub"]
end
if options.key("nbf") && valid_param?("nbf", options["nbf"], [Integer, Symbol], false )
params[:nbf] = options["nbf"]
end
if options.key("exp") && valid_param?("exp", options["exp"], [Integer, Symbol], false )
params[:exp] = options["exp"]
end
if options.key?("incoming_allow") || options.key?("outgoing_allow")
params[:per] = {}
params[:per][:voice] = {}
if options.key?("incoming_allow") && valid_param?("incoming_allow", options["incoming_allow"], [TrueClass, FalseClass, String,Symbol], false)
params[:per][:voice][:incoming_allow] = options["incoming_allow"]
end
if options.key?("outgoing_allow") && valid_param?("outgoing_allow", options["outgoing_allow"], [TrueClass, FalseClass, String, Symbol], false)
params[:per][:voice][:outgoing_allow] = options["outgoing_allow"]
end
end
if options.key?("app") && valid_param?("app", options["app"], [String, Symbol], false)
params[:app] = options["app"]
end

perform_create(params.merge(options), false)
end
end
end
end

2 changes: 2 additions & 0 deletions lib/plivo/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class RestClient < BaseClient
# Resources
attr_reader :messages, :account, :subaccounts, :recordings
attr_reader :pricings, :numbers, :calls, :conferences
attr_reader :token
attr_reader :phone_numbers, :applications, :endpoints, :multipartycalls
attr_reader :addresses, :identities
attr_reader :call_feedback
Expand Down Expand Up @@ -49,6 +50,7 @@ def configure_interfaces
@phone_numbers = Resources::PhoneNumberInterface.new(self)
@conferences = Resources::ConferenceInterface.new(self)
@calls = Resources::CallInterface.new(self)
@token = Resources::TokenInterface.new(self)
@endpoints = Resources::EndpointInterface.new(self)
@applications = Resources::ApplicationInterface.new(self)
@addresses = Resources::AddressInterface.new(self)
Expand Down
2 changes: 1 addition & 1 deletion lib/plivo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Plivo
VERSION = "4.28.0".freeze
VERSION = "4.29.0".freeze
end
4 changes: 4 additions & 0 deletions spec/mocks/tokenCreateResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"api_id": "c3a4d6c0-0b9a-11ed-9fa2-0242ac110004",
"token": "eyJhbGciOiJIUzI1NiIsImN0eSI6InBsaXZvO3Y9MSIsInR5cCI6IkpXVCJ9.eyJhcHAiOiIiLCJleHAiOjE2NTg3ODU4ODUsImlzcyI6Ik1BTURWTFpKWTJaR1k1TVdVMVpKIiwibmJmIjoxNjU4Njk5NDg1LCJwZXIiOnsidm9pY2UiOnsiaW5jb21pbmdfYWxsb3ciOmZhbHNlLCJvdXRnb2luZ19hbGxvdyI6ZmFsc2V9fSwic3ViIjoiS293c2hpayJ9.iWwtfH9QNO7nIE_HK0GSJ3U81oRQR9gcUScrPixBK_s"
}
30 changes: 30 additions & 0 deletions spec/resource_token_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'rspec'

describe 'Token test' do
def to_json(token)
{
iss:token.iss
}.reject { |_, v| v.nil? }.to_json
end
def to_json_create(token)
{
api_id: token.api_id,
token: token.token

}.reject { |_, v| v.nil? }.to_json
end


it 'creates a token' do
contents = File.read(Dir.pwd + '/spec/mocks/tokenCreateResponse.json')
mock(200, JSON.parse(contents))
expect(JSON.parse(to_json_create(@api.token
.create('MAXXXXXXXXXXXXXXXXXX'))))
.to eql(JSON.parse(contents).reject { |_, v| v.nil? })
compare_requests(uri: '/v1/Account/MAXXXXXXXXXXXXXXXXXX/JWT/Token/',
method: 'POST',
data: {
iss:'MAXXXXXXXXXXXXXXXXXX',
})
end
end

0 comments on commit 308cd99

Please sign in to comment.