adsk_auth_service 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 24ceb16b5c1979203cb72ca2a45f8b68d6d8cafe
4
+ data.tar.gz: 7db9253c7ff03217a9a8a46d276640ae3646a9c2
5
+ SHA512:
6
+ metadata.gz: b50837937a6502f42deb1090442ac4f2ca06c4ad37e3fa458494fd8a9c2e1e94eeaa7c7364d84d6618bf0477887d0d735d7b3a890f2edc61c134e2574dedfb1b
7
+ data.tar.gz: f991be89d33df13020e753573d0e3eafc88c01e1415b78e817750750eecccf7df6973693834bc11957755508715ff104cc57022e0978873acc7101c69f09e3bf
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rspec'
4
+ gem 'logging'
5
+ gem 'activesupport'
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'adsk_auth_service'
3
+ s.version = '1.0.0'
4
+ s.summary = "Autodesk second phase token retrieval"
5
+ s.description = "A gem for Autodesk 2-phase authentication service."
6
+ s.authors = ['Linh Chau']
7
+ s.email = 'chauhonglinh@gmail.com'
8
+ s.files = [
9
+ './Gemfile', './adsk_auth_service.gemspec', 'lib/adsk_auth_service.rb',
10
+ 'lib/services/auth_service.rb', 'lib/services/sdk_logger.rb', 'lib/utils/net_util.rb',
11
+ 'spec/services/auth_service_spec.rb', 'spec/rspec_helper.rb', 'conf/auth_keys.example.yml'
12
+ ]
13
+ s.homepage = 'https://git.autodesk.com/t-chaul/adsk_auth_service.git'
14
+ s.license = 'MIT'
15
+ end
@@ -0,0 +1,25 @@
1
+ local:
2
+ consumer_key: YOUR_CONSUMER_KEY
3
+ consumer_secret: YOUR_CONSUMER_SECRET
4
+ url: https://developer-dev.api.autodesk.com/authentication/v1/authenticate
5
+
6
+ development:
7
+ consumer_key: YOUR_CONSUMER_KEY
8
+ consumer_secret: YOUR_CONSUMER_SECRET
9
+ url: https://developer-dev.api.autodesk.com/authentication/v1/authenticate
10
+
11
+ test:
12
+ consumer_key: YOUR_CONSUMER_KEY
13
+ consumer_secret: YOUR_CONSUMER_SECRET
14
+ url: https://developer-dev.api.autodesk.com/authentication/v1/authenticate
15
+
16
+ stage:
17
+ consumer_key: YOUR_CONSUMER_KEY
18
+ consumer_secret: YOUR_CONSUMER_SECRET
19
+ url: https://developer-stg.api.autodesk.com/authentication/v1/authenticate
20
+
21
+ production:
22
+ consumer_key: YOUR_CONSUMER_KEY
23
+ consumer_secret: YOUR_CONSUMER_SECRET
24
+ url: https://developer.api.autodesk.com/authentication/v1/authenticate
25
+
@@ -0,0 +1,5 @@
1
+ require 'yaml'
2
+
3
+ ['services/auth_service', 'services/sdk_logger', 'utils/net_util'].each do |rb_file|
4
+ require rb_file
5
+ end
@@ -0,0 +1,24 @@
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 ||= YAML.load(File.open("#{File.expand_path('.')}/conf/auth_keys.yml"))[ENV['RACK_ENV'] || 'local']
9
+ end
10
+
11
+ def oauth_token
12
+ @@token_data ||= ActiveSupport::JSON.decode(
13
+ NetUtil.call_webservices(@@config['url'],
14
+ 'post',
15
+ "client_id=#{@@config['consumer_key']}&client_secret=#{@@config['consumer_secret']}&grant_type=client_credentials",
16
+ { headers: {'Content-Type' => 'application/x-www-form-urlencoded'} }).body
17
+ )
18
+
19
+ end
20
+
21
+ end
22
+
23
+ load_config
24
+ end
@@ -0,0 +1,6 @@
1
+ class SdkLogger
2
+ LOGGER = Logger.new(STDOUT)
3
+ def self.logger
4
+ LOGGER
5
+ end
6
+ end
@@ -0,0 +1,95 @@
1
+ require 'net/https'
2
+ require 'active_support'
3
+ require_relative '../services/sdk_logger'
4
+
5
+ class NetUtil
6
+ READ_TIMEOUT = 600 # 10 minutes
7
+ RETRY_TIMES = 3
8
+ WAIT_TIME = 5 # Wait for 5 seconds before retry
9
+
10
+
11
+ # This method performs GET, PUT and POST requests to web services
12
+ # Call it like this:
13
+ # response = NetUtil.call_web_services(url) <= This will perform a GET, with url provided by the caller
14
+ # 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
15
+ # In the case of GET, the returned XML data is 'response'
16
+ # In the case of POST and PUT, the returned XML data is 'response.body'
17
+ def NetUtil.call_webservices(url, method_name = 'get', data = '', options = { headers: {'Content-Type' => 'application/json'} })
18
+ method_name = method_name.to_s.downcase
19
+ try_time = 0
20
+ begin
21
+ NetUtil.send("do_#{method_name}", {url: url, data: data}.merge(options))
22
+ rescue StandardError => error
23
+ try_time += 1
24
+ if try_time > RETRY_TIMES
25
+ SdkLogger.logger.error("\n#{Time.now} Unrecoverable error in NetUtil.call_webservices: "\
26
+ "#{error}\n#{error.backtrace.join("\n")}\n")
27
+ # It is an unrecoverable error, throw the exception back, don't suppress it.
28
+ raise "Unrecoverable error calling web services.\nURL: #{url}.\nError message: #{error.message}."
29
+ end
30
+
31
+ SdkLogger.logger.error("NetUtil.call_webservices #{url}:\nError happens: #{error}. Try #{try_time} time(s).")
32
+ sleep(WAIT_TIME)
33
+ retry
34
+ end
35
+ end
36
+
37
+ def NetUtil.do_get(options)
38
+ # headers = {'Content-Type' => 'text/xml'}
39
+ headers = build_header(options)
40
+ puts "headers = #{headers.inspect}"
41
+ url = options[:url]
42
+ uri = URI.parse(url)
43
+ req = Net::HTTP.new(uri.host, uri.port)
44
+ req = set_ssl(req, url)
45
+
46
+ response = req.get(uri.path)
47
+ return response.body
48
+ end
49
+
50
+ def NetUtil.do_post(options)
51
+ run_p(options, 'post')
52
+ end
53
+
54
+ def NetUtil.do_put(options)
55
+ run_p(options, 'put')
56
+ end
57
+
58
+
59
+ def NetUtil.run_p(options, method_name)
60
+ data = options[:data].to_s
61
+
62
+ headers = build_header(options)
63
+ url = options[:url]
64
+ uri = URI.parse(url)
65
+ req = Net::HTTP.new(uri.host, uri.port)
66
+ req = set_ssl(req, url)
67
+
68
+ req.read_timeout = READ_TIMEOUT
69
+ req.open_timeout = READ_TIMEOUT
70
+
71
+ response, body = req.send(method_name, uri.path, data, headers)
72
+
73
+ return response
74
+ end
75
+
76
+ def NetUtil.build_header(options)
77
+ headers = options[:headers] || { 'Content-Type' => 'application/json' }
78
+ conntent_type = ( headers['Content-Type'].nil? )? 'application/json' : headers['Content-Type']
79
+ headers.delete('Content-Type')
80
+
81
+ {'Content-Type' => conntent_type}.merge(headers)
82
+ end
83
+
84
+ def NetUtil.set_ssl(request, url)
85
+ SdkLogger.logger.debug("URL is #{url}")
86
+ if url.start_with? 'https'
87
+ SdkLogger.logger.debug("Use https - URL is #{url}")
88
+ request.use_ssl = true
89
+ request.verify_mode = OpenSSL::SSL::VERIFY_NONE
90
+ end
91
+ request
92
+ end
93
+ end
94
+
95
+
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'yaml'
3
+
4
+ ['lib/services', 'lib/utils'].each do |sub_path|
5
+ Dir.glob("#{File.expand_path('.')}/#{sub_path}/*.rb").each { |rb_file| require rb_file }
6
+ end
@@ -0,0 +1,26 @@
1
+ require_relative('../rspec_helper')
2
+
3
+ describe AuthService do
4
+ context 'load_config' do
5
+ it 'load config from YAML file' do
6
+ config = AuthService.load_config
7
+ expect(config).not_to be_nil
8
+ expect(config.is_a? Hash).to eql(true)
9
+ end
10
+ end
11
+
12
+ context 'oauth_token' do
13
+ it 'returns auth token' do
14
+ token_data = AuthService.oauth_token
15
+ puts("Token #{token_data}")
16
+
17
+ # token_data['access_token'] is the real token
18
+ expect(token_data).not_to be_nil
19
+ expect(token_data.is_a? Hash).to eql(true)
20
+ expect(token_data['token_type']).not_to be_nil
21
+ expect(token_data['expires_in']).not_to be_nil
22
+ expect(token_data['access_token']).not_to be_nil
23
+ end
24
+ end
25
+
26
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adsk_auth_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Linh Chau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem for Autodesk 2-phase authentication service.
14
+ email: chauhonglinh@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - "./Gemfile"
20
+ - "./adsk_auth_service.gemspec"
21
+ - conf/auth_keys.example.yml
22
+ - lib/adsk_auth_service.rb
23
+ - lib/services/auth_service.rb
24
+ - lib/services/sdk_logger.rb
25
+ - lib/utils/net_util.rb
26
+ - spec/rspec_helper.rb
27
+ - spec/services/auth_service_spec.rb
28
+ homepage: https://git.autodesk.com/t-chaul/adsk_auth_service.git
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.4.3
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Autodesk second phase token retrieval
52
+ test_files: []