telesign 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/telesign.rb +305 -0
  3. data/lib/telesign/rest.rb +181 -0
  4. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c5e4faab130d077bd865c8b11d76e3873a04646b
4
+ data.tar.gz: fd647261f558b13ce9908f59bc2afdc2e2ce94e5
5
+ SHA512:
6
+ metadata.gz: 19d28c08bc21a28bd27f33599e71946545f57b46b8139383fbeafbef20b5b75ae8e254fcdffcf55dcb19c6aac7c145c17efb11fc8029d18743964d1bebc3dd29
7
+ data.tar.gz: 8c14d197c6e50361813cf5f087df058df90f4199d9298cfc31c19308783bbdc85b502254353e24b67dcf525d4d7ff07a4fb832209ee466df5af874fa20620e8e
@@ -0,0 +1,305 @@
1
+ #
2
+ # Copyright (c) 2016 TeleSign
3
+ #
4
+ # TeleSign Ruby SDK REST API endpoints.
5
+ #
6
+ # The api module contains Python classes and methods that allow you to
7
+ # use the Ruby programming language to programmatically access the
8
+ # Verify and PhoneId TeleSign web services.
9
+ #
10
+
11
+ require 'telesign/rest'
12
+
13
+ module Telesign
14
+
15
+ module API
16
+
17
+ # The PhoneId class exposes services that each provide detailed
18
+ # information about a specified phone number.
19
+ class PhoneId < Telesign::API::Rest
20
+
21
+ def initialize(customer_id,
22
+ secret_key,
23
+ ssl=true,
24
+ api_host='rest.telesign.com',
25
+ timeout=nil)
26
+
27
+ super(customer_id,
28
+ secret_key,
29
+ ssl,
30
+ api_host,
31
+ timeout)
32
+ end
33
+
34
+ # Retrieves the standard set of details about the specified phone number.
35
+ # This includes the type of phone (e.g., land line or mobile), and it's
36
+ # approximate geographic location.
37
+ def standard(phone_number,
38
+ use_case_code=nil,
39
+ extra=nil,
40
+ timeout=nil)
41
+
42
+ params = {}
43
+
44
+ unless use_case_code.nil?
45
+ params[:ucid] = use_case_code
46
+ end
47
+
48
+ unless extra.nil?
49
+ params.merge!(extra)
50
+ end
51
+
52
+ execute(Net::HTTP::Get,
53
+ "/v1/phoneid/standard/#{phone_number}",
54
+ params,
55
+ nil,
56
+ timeout)
57
+ end
58
+
59
+ # Retrieves a score for the specified phone number. This ranks the phone number's
60
+ # "risk level" on a scale from 0 to 1000, so you can code your web application to
61
+ # handle particular use cases (e.g., to stop things like chargebacks, identity
62
+ # theft, fraud, and spam).
63
+ def score(phone_number,
64
+ use_case_code,
65
+ extra=nil,
66
+ timeout=nil)
67
+
68
+ params = {:ucid => use_case_code}
69
+
70
+ unless extra.nil?
71
+ params.merge!(extra)
72
+ end
73
+
74
+ execute(Net::HTTP::Get,
75
+ "/v1/phoneid/score/#{phone_number}",
76
+ params,
77
+ nil,
78
+ timeout)
79
+ end
80
+
81
+ # In addition to the information retrieved by standard, this service provides the
82
+ # Name & Address associated with the specified phone number.
83
+ def contact(phone_number,
84
+ use_case_code,
85
+ extra=nil,
86
+ timeout=nil)
87
+
88
+ params = {:ucid => use_case_code}
89
+
90
+ unless extra.nil?
91
+ params.merge!(extra)
92
+ end
93
+
94
+ execute(Net::HTTP::Get,
95
+ "/v1/phoneid/contact/#{phone_number}",
96
+ params,
97
+ nil,
98
+ timeout)
99
+ end
100
+
101
+ # In addition to the information retrieved by standard, this service provides
102
+ # actionable data associated with the specified phone number.
103
+ def live(phone_number,
104
+ use_case_code,
105
+ extra=nil,
106
+ timeout=nil)
107
+
108
+ params = {:ucid => use_case_code}
109
+
110
+ unless extra.nil?
111
+ params.merge!(extra)
112
+ end
113
+
114
+ execute(Net::HTTP::Get,
115
+ "/v1/phoneid/live/#{phone_number}",
116
+ params,
117
+ nil,
118
+ timeout)
119
+ end
120
+
121
+ # In addition to the information retrieved by standard, this service provides
122
+ # data about potential sim_swaps associated with the specified phone number.
123
+ def sim_swap(phone_number,
124
+ use_case_code,
125
+ extra=nil,
126
+ timeout=nil)
127
+
128
+ params = {:ucid => use_case_code}
129
+
130
+ unless extra.nil?
131
+ params.merge!(extra)
132
+ end
133
+
134
+ execute(Net::HTTP::Get,
135
+ "/v1/phoneid/sim_swap/check/#{phone_number}",
136
+ params,
137
+ nil,
138
+ timeout)
139
+ end
140
+
141
+ # In addition to the information retrieved by standard, this service provides
142
+ # information on call forwarding for the phone number provided.
143
+ def call_forward(phone_number,
144
+ use_case_code,
145
+ extra=nil,
146
+ timeout=nil)
147
+
148
+ params = {:ucid => use_case_code}
149
+
150
+ unless extra.nil?
151
+ params.merge!(extra)
152
+ end
153
+
154
+ execute(Net::HTTP::Get,
155
+ "/v1/phoneid/call_forward/#{phone_number}",
156
+ params,
157
+ nil,
158
+ timeout)
159
+ end
160
+
161
+ end
162
+
163
+ # The Verify class exposes several services for sending users a verification
164
+ # token. You can use this mechanism to simply test whether you can reach users
165
+ # at the phone number they supplied, or you can have them use the token to
166
+ # authenticate themselves with your web application.
167
+ #
168
+ # This class also exposes a service that is used in conjunction with the first
169
+ # two services, in that it allows you to confirm the result of the authentication.
170
+ #
171
+ # You can use this verification factor in combination with username & password to
172
+ # provide two-factor authentication for higher security.
173
+ class Verify < Telesign::API::Rest
174
+
175
+ def initialize(customer_id,
176
+ secret_key,
177
+ ssl=true,
178
+ api_host='rest.telesign.com',
179
+ timeout=nil)
180
+
181
+ super(customer_id,
182
+ secret_key,
183
+ ssl,
184
+ api_host,
185
+ timeout)
186
+ end
187
+
188
+ # Sends a text message containing the verification code, to the specified
189
+ # phone number (supported for mobile phones only).
190
+ def sms(phone_number,
191
+ use_case_code=nil,
192
+ extra=nil,
193
+ timeout=nil)
194
+
195
+ params = {:phone_number => phone_number}
196
+
197
+ unless use_case_code.nil?
198
+ params[:use_case_code] = use_case_code
199
+ end
200
+
201
+ unless extra.nil?
202
+ params.merge!(extra)
203
+ end
204
+
205
+ execute(Net::HTTP::Post,
206
+ "/v1/verify/sms",
207
+ nil,
208
+ params,
209
+ timeout)
210
+ end
211
+
212
+ # Calls the specified phone number, and using speech synthesis, speaks the
213
+ # verification code to the user.
214
+ def call(phone_number,
215
+ use_case_code=nil,
216
+ extra=nil,
217
+ timeout=nil)
218
+
219
+ params = {:phone_number => phone_number}
220
+
221
+ unless use_case_code.nil?
222
+ params[:use_case_code] = use_case_code
223
+ end
224
+
225
+ unless extra.nil?
226
+ params.merge!(extra)
227
+ end
228
+
229
+ execute(Net::HTTP::Post,
230
+ "/v1/verify/call",
231
+ nil,
232
+ params,
233
+ timeout)
234
+ end
235
+
236
+ # Calls the specified phone number, and using speech synthesis, speaks the
237
+ # verification code to the user.
238
+ def smart(phone_number,
239
+ use_case_code,
240
+ extra=nil,
241
+ timeout=nil)
242
+
243
+ params = {:phone_number => phone_number,
244
+ :ucid => use_case_code}
245
+
246
+ unless extra.nil?
247
+ params.merge!(extra)
248
+ end
249
+
250
+ execute(Net::HTTP::Post,
251
+ "/v1/verify/smart",
252
+ nil,
253
+ params,
254
+ timeout)
255
+
256
+ end
257
+
258
+ # The **push** method sends a push notification containing the verification
259
+ # code to the specified phone number (supported for mobile phones only).
260
+ def push(phone_number,
261
+ use_case_code,
262
+ extra=nil,
263
+ timeout=nil)
264
+
265
+ params = {:phone_number => phone_number,
266
+ :ucid => use_case_code}
267
+
268
+ unless extra.nil?
269
+ params.merge!(extra)
270
+ end
271
+
272
+ execute(Net::HTTP::Post,
273
+ "/v1/verify/push",
274
+ nil,
275
+ params,
276
+ timeout)
277
+
278
+ end
279
+
280
+ # Retrieves the verification result. You make this call in your web application
281
+ # after users complete the authentication transaction (using either a call or sms).
282
+ def status(reference_id,
283
+ verify_code=nil,
284
+ extra=nil,
285
+ timeout=nil)
286
+
287
+ params = {}
288
+
289
+ unless verify_code.nil?
290
+ params[:verify_code] = verify_code
291
+ end
292
+
293
+ unless extra.nil?
294
+ params.merge!(extra)
295
+ end
296
+
297
+ execute(Net::HTTP::Get,
298
+ "/v1/verify/#{reference_id}",
299
+ params,
300
+ nil,
301
+ timeout)
302
+ end
303
+ end
304
+ end
305
+ end
@@ -0,0 +1,181 @@
1
+ #
2
+ # Copyright (c) 2016 TeleSign
3
+ #
4
+ # TeleSign Ruby SDK HMAC REST Auth.
5
+ #
6
+
7
+ require 'pp'
8
+ require 'json'
9
+ require 'time'
10
+ require 'base64'
11
+ require 'openssl'
12
+ require 'net/http'
13
+
14
+ module Telesign
15
+
16
+ module API
17
+
18
+ # == TeleSign Ruby SDK REST API Helper
19
+ #
20
+ # Telesign::API::Rest provides helper classes and functions
21
+ # to handle the HMAC REST authentication.
22
+ #
23
+ # You can use these helper functions directly or via the Telesign::API::PhoneId
24
+ # and Telesign::API::Verify classes. Please see the TeleSign REST API docs at
25
+ # http://docs.telesign.com/rest/index.html for implementation details.
26
+ #
27
+ class Rest
28
+
29
+ # Creates a new Telesign::API::Rest object with the specified credentials
30
+ # and HTTP configuration.
31
+ # The +api_host+ should be a DNS hostname or IP address.
32
+ def initialize(customer_id,
33
+ secret_key,
34
+ ssl,
35
+ api_host,
36
+ timeout=nil)
37
+
38
+ @customer_id = customer_id
39
+ @secret_key = secret_key
40
+ @ssl = ssl
41
+ @base_uri = URI("http#{ssl ? 's' : ''}://#{api_host}")
42
+ @timeout = timeout
43
+ @user_agent = 'Net::HTTP TeleSignSDK/ruby-1.0.0'
44
+ end
45
+
46
+ # Executes the REST API request based on the given configuration.
47
+ # See Telesign::API::PhoneId and Telesign::API::Verify for specific
48
+ # usage.
49
+ def execute(verb,
50
+ resource,
51
+ params=nil,
52
+ form_data=nil,
53
+ timeout=nil)
54
+
55
+ # generate the headers
56
+ headers = generate_auth_headers(
57
+ @customer_id,
58
+ @secret_key,
59
+ resource,
60
+ verb,
61
+ form_data.nil? ? nil : URI.encode_www_form(form_data))
62
+
63
+ uri = URI.join(@base_uri, resource)
64
+
65
+ # set query params
66
+ uri.query = URI.encode_www_form(params) unless params.nil?
67
+
68
+ # configure HTTP object
69
+ http = Net::HTTP.new(uri.host, uri.port)
70
+ http.use_ssl = @ssl
71
+
72
+ http.open_timeout = timeout.nil? ? @timeout : timeout
73
+ http.read_timeout = http.open_timeout
74
+ http.ssl_timeout = http.open_timeout
75
+ http.continue_timeout = http.open_timeout
76
+
77
+ #set headers
78
+ request = verb.new uri.request_uri
79
+ headers.each do |k, v|
80
+ request[k] = v
81
+ end
82
+
83
+ # set post data
84
+ request.set_form_data(form_data) unless form_data.nil?
85
+
86
+ # do the request
87
+ http_response = http.request(request)
88
+
89
+ # check response
90
+ unless http_response.is_a? Net::HTTPSuccess
91
+ if http_response.is_a? Net::HTTPUnauthorized
92
+ raise Telesign::API::AuthError.new(http_response)
93
+ else
94
+ raise Telesign::API::APIError.new(http_response)
95
+ end
96
+ end
97
+
98
+ Telesign::API::APIResponse.new(http_response)
99
+ end
100
+
101
+ # Function to generate the REST API authentication headers. A signature is
102
+ # computed based on the contents of the request and the client's secret key.
103
+ def generate_auth_headers (customer_id,
104
+ secret_key,
105
+ resource,
106
+ verb,
107
+ form_data=nil,
108
+ content_type='')
109
+
110
+ datetime_stamp = Time.now.utc.to_datetime.rfc822
111
+ nonce = rand.to_s
112
+
113
+ content_type = 'application/x-www-form-urlencoded' if verb == Net::HTTP::Post or verb == Net::HTTP::Put
114
+
115
+ string_to_sign = "#{verb.name.split('::').last.upcase}\n" +
116
+ "#{content_type}\n\n" +
117
+ "x-ts-auth-method:#{'HMAC-SHA256'}\n" +
118
+ "x-ts-date:#{datetime_stamp}\n" +
119
+ "x-ts-nonce:#{nonce}"
120
+
121
+ string_to_sign = "#{string_to_sign}\n#{form_data}" unless form_data.nil?
122
+
123
+ string_to_sign = string_to_sign + "\n#{resource}"
124
+
125
+ signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'),
126
+ Base64.decode64(secret_key), string_to_sign)).chomp
127
+
128
+ {
129
+ 'Authorization' => "TSA #{customer_id}:#{signature}",
130
+ 'x-ts-date' => datetime_stamp,
131
+ 'x-ts-auth-method' => 'HMAC-SHA256',
132
+ 'x-ts-nonce' => nonce,
133
+ 'User-Agent' => @user_agent
134
+ }
135
+ end
136
+ end
137
+
138
+ class APIResponse
139
+
140
+ attr_accessor :body, :headers, :status, :verify_code
141
+
142
+ def initialize(http_response,
143
+ verify_code=nil)
144
+
145
+ @body = JSON.parse(http_response.body)
146
+ @headers = http_response.to_hash
147
+ @status = http_response.code
148
+ @verify_code = verify_code
149
+ end
150
+ end
151
+
152
+ class APIError < StandardError
153
+
154
+ attr_accessor :errors, :headers, :status, :body
155
+
156
+ def initialize(http_response)
157
+
158
+ @errors = JSON.parse(http_response.body)['errors']
159
+ @headers = http_response.to_hash
160
+ @status = http_response.code
161
+ @body = http_response.body
162
+ end
163
+
164
+ def to_s
165
+ result = ''
166
+ @errors.each do |error|
167
+ result = "#{result}#{error['description']}\n"
168
+ end
169
+
170
+ result
171
+ end
172
+ end
173
+
174
+ class AuthError < Telesign::API::APIError
175
+
176
+ def initialize(http_response)
177
+ super(http_response)
178
+ end
179
+ end
180
+ end
181
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telesign
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jarrad Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: TeleSign Ruby SDK
14
+ email: jarrad@telesign.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/telesign.rb
20
+ - lib/telesign/rest.rb
21
+ homepage: http://rubygems.org/gems/telesign
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.5.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: TeleSign Ruby SDK
44
+ test_files: []