adsk_oauth_service 1.0.50

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 91657716a45dc9f9525e2edbd92fe2e18759e932
4
+ data.tar.gz: 625676545a72935b19203529fcb40b245801b72f
5
+ SHA512:
6
+ metadata.gz: 5d41541fa29034e99521c031ede726eff248b8928a2aa7029c76a1543a8b7bbff17c212256e8c0bfc10df3560578cee6392dd1caa1c19174f24d85a1e098bdf7
7
+ data.tar.gz: 055daeb18fd1e505803339373147bd35cfa4c96c6757c8828c124a1f496c58b12e323fa5056c7c705d7857cf4a828972c1e439c805a5537245673f71b1eca351
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rspec'
4
+ gem 'configger_service'
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'adsk_oauth_service'
3
+ s.version = '1.0.50'
4
+ s.summary = "Autodesk second phase token retrieval"
5
+ s.description = "A gem for Autodesk 2-phase authentication service."
6
+ s.authors = ['Linh Chau', 'John Thomas']
7
+ s.email = 'thomas07vt@gmail.com'
8
+ s.files = [
9
+ './Gemfile', './adsk_oauth_service.gemspec',
10
+ 'lib/adsk_oauth_service.rb',
11
+ 'lib/services/auth_service.rb',
12
+ 'lib/utils/net_util.rb',
13
+ ]
14
+ s.homepage = 'https://github.com/thomas07vt/adsk_oauth_service'
15
+ s.license = 'MIT'
16
+ s.add_runtime_dependency 'configger_service'
17
+
18
+ s.add_development_dependency 'rspec', '~> 3.1'
19
+ end
@@ -0,0 +1,17 @@
1
+ require 'yaml'
2
+ require 'configger_service'
3
+
4
+ def load_gem_lib(sub_path)
5
+ spec = Gem::Specification.find_by_name('adsk_auth_service')
6
+ rb_files = Dir.glob("#{spec.gem_dir}/lib/#{sub_path}/*.rb")
7
+ rb_files.each { |rb_file| require rb_file }
8
+ rescue Exception => error
9
+ # Who cares?
10
+ end
11
+
12
+
13
+ ['services', 'utils'].each do |sub_path|
14
+ load_gem_lib(sub_path)
15
+ rb_files = Dir.glob("#{File.expand_path('.')}/lib/#{sub_path}/*.rb")
16
+ rb_files.each { |rb_file| require rb_file }
17
+ end
@@ -0,0 +1,36 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+ require_relative '../utils/net_util'
4
+
5
+ class AuthService
6
+ class << self
7
+ def load_config
8
+ @@config ||= (ConfigService.load_config('auth_keys.yml')[ConfigService.environment] rescue {})
9
+ @@config
10
+ end
11
+
12
+ def set_config(options = {})
13
+ @@config['url'] = options['url']
14
+ @@config['consumer_key'] = options['consumer_key']
15
+ @@config['consumer_secret'] = options['consumer_secret']
16
+
17
+ @@config
18
+ end
19
+
20
+ def oauth_token(options = {})
21
+ url = options['url'] || @@config['url']
22
+ key = options['consumer_key'] || @@config['consumer_key']
23
+ secret = options['consumer_secret'] || @@config['consumer_secret']
24
+
25
+ ActiveSupport::JSON.decode(
26
+ NetUtil.call_webservices(url,
27
+ 'post',
28
+ "client_id=#{key}&client_secret=#{secret}&grant_type=client_credentials",
29
+ { headers: {'Content-Type' => 'application/x-www-form-urlencoded'} }).body
30
+ )
31
+ end
32
+
33
+ end
34
+
35
+ load_config
36
+ end
@@ -0,0 +1,92 @@
1
+ require 'net/https'
2
+ require 'active_support'
3
+
4
+ class NetUtil
5
+ READ_TIMEOUT = 600 # 10 minutes
6
+ RETRY_TIMES = 3
7
+ WAIT_TIME = 5 # Wait for 5 seconds before retry
8
+
9
+
10
+ # This method performs GET, PUT and POST requests to web services
11
+ # Call it like this:
12
+ # response = NetUtil.call_web_services(url) <= This will perform a GET, with url provided by the caller
13
+ # response = NetUtil.call_web_services(url, 'post', doc) <= This will perform a POST, doc is the data to post, can be REXML::Document or XML String
14
+ # In the case of GET, the returned XML data is 'response'
15
+ # In the case of POST and PUT, the returned XML data is 'response.body'
16
+ def NetUtil.call_webservices(url, method_name = 'get', data = '', options = { headers: {'Content-Type' => 'application/json'} })
17
+ method_name = method_name.to_s.downcase
18
+ try_time = 0
19
+ begin
20
+ NetUtil.send("do_#{method_name}", {url: url, data: data}.merge(options))
21
+ rescue StandardError => error
22
+ try_time += 1
23
+ if try_time > RETRY_TIMES
24
+ puts ("\n#{Time.now} Unrecoverable error in NetUtil.call_webservices: "\
25
+ "#{error}\n#{error.backtrace.join("\n")}\n")
26
+ # It is an unrecoverable error, throw the exception back, don't suppress it.
27
+ raise "Unrecoverable error calling web services.\nURL: #{url}.\nError message: #{error.message}."
28
+ end
29
+
30
+ puts ("NetUtil.call_webservices #{url}:\nError happens: #{error}. Try #{try_time} time(s).")
31
+ sleep(WAIT_TIME)
32
+ retry
33
+ end
34
+ end
35
+
36
+ def NetUtil.do_get(options)
37
+ # headers = {'Content-Type' => 'text/xml'}
38
+ headers = build_header(options)
39
+ puts "headers = #{headers.inspect}"
40
+ url = options[:url]
41
+ uri = URI.parse(url)
42
+ req = Net::HTTP.new(uri.host, uri.port)
43
+ req = set_ssl(req, url)
44
+
45
+ response = req.get(uri.path)
46
+ return response.body
47
+ end
48
+
49
+ def NetUtil.do_post(options)
50
+ run_p(options, 'post')
51
+ end
52
+
53
+ def NetUtil.do_put(options)
54
+ run_p(options, 'put')
55
+ end
56
+
57
+
58
+ def NetUtil.run_p(options, method_name)
59
+ data = options[:data].to_s
60
+
61
+ headers = build_header(options)
62
+ url = options[:url]
63
+ uri = URI.parse(url)
64
+ req = Net::HTTP.new(uri.host, uri.port)
65
+ req = set_ssl(req, url)
66
+
67
+ req.read_timeout = READ_TIMEOUT
68
+ req.open_timeout = READ_TIMEOUT
69
+
70
+ response, body = req.send(method_name, uri.path, data, headers)
71
+
72
+ return response
73
+ end
74
+
75
+ def NetUtil.build_header(options)
76
+ headers = options[:headers] || { 'Content-Type' => 'application/json' }
77
+ conntent_type = ( headers['Content-Type'].nil? )? 'application/json' : headers['Content-Type']
78
+ headers.delete('Content-Type')
79
+
80
+ {'Content-Type' => conntent_type}.merge(headers)
81
+ end
82
+
83
+ def NetUtil.set_ssl(request, url)
84
+ if url.start_with? 'https'
85
+ request.use_ssl = true
86
+ request.verify_mode = OpenSSL::SSL::VERIFY_NONE
87
+ end
88
+ request
89
+ end
90
+ end
91
+
92
+
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adsk_oauth_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.50
5
+ platform: ruby
6
+ authors:
7
+ - Linh Chau
8
+ - John Thomas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-08-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ name: configger_service
21
+ prerelease: false
22
+ type: :runtime
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ name: rspec
35
+ prerelease: false
36
+ type: :development
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '3.1'
42
+ description: A gem for Autodesk 2-phase authentication service.
43
+ email: thomas07vt@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ./Gemfile
49
+ - ./adsk_oauth_service.gemspec
50
+ - lib/adsk_oauth_service.rb
51
+ - lib/services/auth_service.rb
52
+ - lib/utils/net_util.rb
53
+ homepage: https://github.com/thomas07vt/adsk_oauth_service
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.4.5
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Autodesk second phase token retrieval
77
+ test_files: []