plivo 4.7.1 → 4.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27e78b1d3b0da475d5b7bc48985460db5a7fee0b
4
- data.tar.gz: efd5113cd127bb099449f365c30406fdf35ead96
3
+ metadata.gz: dbbe456a4f4f19cb4d520c470c32a12fbf1d886a
4
+ data.tar.gz: 017ae48d6b340d1f71267a8c726b1ccacab11bde
5
5
  SHA512:
6
- metadata.gz: 99d38e6766b25aa3d0be69122f287df3760b06b177ad06e1efa3d6bf52673154aa39dd901c4463e5bfc68384366cf67c52246dacae56b28a92036697fe8c0f1a
7
- data.tar.gz: 8b9a013b00f5aae92c63947b2c4f75be3093357e3e2029ea7f971019d2484ccb8dce71d90d1d2cc695d7ce67155b0b0c8b1599aa81353c462027eb5c120b6974
6
+ metadata.gz: c2b4252b08ad48e9302fb37ec1f8dc1362ea4c2af5b95334841eada03f1e88865453dea532e5fe6d15a829dd868ebabc67cf75a41905ba8276b6d158edda4d23
7
+ data.tar.gz: 578e0a1b358eb14dba2ae2b03b6925975586a262122628bf84257c63f50cc3d59bfc9ea486c4fc71590ecbbdd037c7ddab863c4ebe7451f4308330a64fbd6498
@@ -1,5 +1,8 @@
1
1
  # Change Log
2
2
 
3
+ ## [4.8.0](https://github.com/plivo/plivo-ruby/releases/tag/v4.8.0) (2020-05-28)
4
+ - Add JWT helper functions.
5
+
3
6
  ## [4.7.1](https://github.com/plivo/plivo-ruby/releases/tag/v4.7.1) (2020-05-06)
4
7
  - Fix Send MMS with existing media_ids.
5
8
 
data/README.md CHANGED
@@ -8,7 +8,7 @@ The Plivo Ruby SDK makes it simpler to integrate communications into your Ruby a
8
8
  Add this line to your application's Gemfile:
9
9
 
10
10
  ```ruby
11
- gem 'plivo', '>= 4.7.1'
11
+ gem 'plivo', '>= 4.8.0'
12
12
  ```
13
13
 
14
14
  And then execute:
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'plivo'
3
+
4
+ include Plivo
5
+
6
+ AUTH_ID = 'MADADADADADADADADADA'
7
+ AUTH_TOKEN = 'AUTH_TOKEN'
8
+
9
+ begin
10
+ acctkn = Plivo::Token::AccessToken.new(
11
+ AUTH_ID,
12
+ AUTH_TOKEN,
13
+ '{username}',
14
+ '{uid}'
15
+ )
16
+ # update token validity (from, lifetime, till)
17
+ acctkn.update_validity(Time.now, 300)
18
+ # add voice grants (incoming, outgoing)
19
+ acctkn.add_voice_grants(Plivo::Token::VoiceGrants.new(true, true))
20
+ puts acctkn.to_jwt
21
+ rescue ValidationError => e
22
+ puts 'Exception: ' + e.message
23
+ end
@@ -5,6 +5,7 @@ require_relative 'plivo/base'
5
5
  require_relative 'plivo/resources'
6
6
  require_relative 'plivo/rest_client'
7
7
  require_relative 'plivo/xml'
8
+ require_relative 'plivo/jwt'
8
9
  require_relative 'plivo/phlo_client'
9
10
  require_relative 'plivo/base_client'
10
11
 
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
4
+ require 'uri'
5
+ require 'base64'
6
+ require 'date'
7
+ require 'jwt'
8
+
9
+ module Plivo
10
+ module Token
11
+ include Utils
12
+
13
+ class VoiceGrants
14
+ attr_reader :incoming_allow, :outgoing_allow
15
+
16
+ def initialize(incoming = nil, outgoing = nil)
17
+ Utils.valid_param?(:incoming, incoming, [TrueClass, FalseClass], false)
18
+ Utils.valid_param?(:outgoing, outgoing, [TrueClass, FalseClass], false)
19
+ @incoming_allow = incoming
20
+ @outgoing_allow = outgoing
21
+ end
22
+
23
+ def to_hash
24
+ hash = {}
25
+ instance_variables.each { |var| hash[var.to_s.delete('@')] = instance_variable_get(var) }
26
+ hash
27
+ end
28
+ end
29
+
30
+ class AccessToken
31
+ attr_reader :uid, :username, :valid_from, :lifetime, :grants
32
+ def initialize(auth_id = nil, auth_token = nil, username = nil, uid = nil)
33
+ configure_credentials(auth_id, auth_token)
34
+ Utils.valid_param?(:username, username, [String, Symbol], true)
35
+ @username = username
36
+
37
+ Utils.valid_param?(:uid, uid, [String, Symbol], false)
38
+ uid ||= username + '-' + Time.now.to_i
39
+ @uid = uid
40
+ update_validity
41
+ end
42
+
43
+ def update_validity(valid_from = nil, lifetime = nil, valid_till = nil)
44
+ Utils.valid_param?(:valid_from, valid_from, [Time, Integer], false)
45
+ Utils.valid_param?(:lifetime, lifetime, [Integer], false)
46
+ Utils.valid_param?(:valid_till, valid_till, [Time, Integer], false)
47
+
48
+ if valid_from.nil?
49
+ @lifetime = lifetime || 84_600
50
+ @valid_from = if valid_till.nil?
51
+ Time.now
52
+ else
53
+ Time.at(valid_till.to_i - @lifetime).utc
54
+ end
55
+
56
+ elsif valid_till.nil?
57
+ @lifetime = lifetime || 84_600
58
+ @valid_from = valid_from
59
+ else
60
+ unless lifetime.nil?
61
+ raise Exceptions::ValidationError, 'use any 2 of valid_from, lifetime and valid_till'
62
+ end
63
+
64
+ @valid_from = valid_from
65
+ @lifetime = valid_till.to_i - valid_from.to_i
66
+ end
67
+
68
+ return unless @lifetime < 180 || @lifetime > 84_600
69
+
70
+ raise Exceptions::ValidationError, 'validity out of [180, 84600] seconds'
71
+ end
72
+
73
+ def auth_id
74
+ @auth_credentials[:auth_id]
75
+ end
76
+
77
+ def add_voice_grants(grants)
78
+ Utils.valid_param?(:grants, grants, [VoiceGrants], true)
79
+ @grants = grants
80
+ end
81
+
82
+ def to_jwt
83
+ payload = {
84
+ jti: uid,
85
+ sub: username,
86
+ iss: auth_id,
87
+ nbf: valid_from.to_i,
88
+ exp: valid_from.to_i + lifetime,
89
+ grants: {
90
+ voice: grants.to_hash || {}
91
+ }
92
+ }
93
+ JWT.encode payload, key, 'HS256', {typ: 'JWT', cty: 'plivo;v=1'}
94
+ end
95
+
96
+ private
97
+
98
+ def key
99
+ @auth_credentials[:auth_token]
100
+ end
101
+
102
+ def configure_credentials(auth_id, auth_token)
103
+ # Fetches and sets the right credentials
104
+ auth_id ||= ENV['PLIVO_AUTH_ID']
105
+ auth_token ||= ENV['PLIVO_AUTH_TOKEN']
106
+
107
+ raise Exceptions::AuthenticationError, 'Couldn\'t find auth credentials' unless
108
+ auth_id && auth_token
109
+
110
+ raise Exceptions::AuthenticationError, "Invalid auth_id: '#{auth_id}'" unless
111
+ Utils.valid_account?(auth_id)
112
+
113
+ @auth_credentials = {
114
+ auth_id: auth_id,
115
+ auth_token: auth_token
116
+ }
117
+ end
118
+ end
119
+ end
120
+ end
@@ -1,3 +1,3 @@
1
1
  module Plivo
2
- VERSION = '4.7.1'.freeze
2
+ VERSION = '4.8.0'.freeze
3
3
  end
@@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
34
34
  spec.add_dependency 'faraday', '~> 0.9'
35
35
  spec.add_dependency 'faraday_middleware', '~> 0.12.2'
36
36
  spec.add_dependency 'htmlentities'
37
+ spec.add_dependency 'jwt'
37
38
 
38
39
  spec.add_development_dependency 'bundler', '>= 1.14', '<3.0'
39
40
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plivo
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.7.1
4
+ version: 4.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Plivo SDKs Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-06 00:00:00.000000000 Z
11
+ date: 2020-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jwt
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +164,7 @@ files:
150
164
  - Rakefile
151
165
  - ci/config.yml
152
166
  - examples/conference_bridge.rb
167
+ - examples/jwt.rb
153
168
  - examples/multi_party_call.rb
154
169
  - examples/phlos.rb
155
170
  - lib/plivo.rb
@@ -159,6 +174,7 @@ files:
159
174
  - lib/plivo/base/response.rb
160
175
  - lib/plivo/base_client.rb
161
176
  - lib/plivo/exceptions.rb
177
+ - lib/plivo/jwt.rb
162
178
  - lib/plivo/phlo_client.rb
163
179
  - lib/plivo/resources.rb
164
180
  - lib/plivo/resources/accounts.rb