auth0_machine_to_machine 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/auth0_machine_to_machine.rb +74 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ce2e3d608e1913f625c628d20a7bc2903a08e19485c062476539b4369df71f8d
4
+ data.tar.gz: 8a66c88347cbca9723b245e5ececb5174dce3ec4aef1acb01309137191b86edf
5
+ SHA512:
6
+ metadata.gz: 711414c2ae98016d9f27f228f77e8eb3c661eeac367744930de60c9f3b813841d444ca875cd7dda8a638440d2c05941571d261d03d449221367cf876e8bc3c20
7
+ data.tar.gz: cb7ea22443385c370c512a3003bbb26cf0dbce056c4f7f578205256d354237187c495eaaaefbb678196e3c9a3067c965f6cf47d63f4c6bb6b186a4d4089751b3
@@ -0,0 +1,74 @@
1
+ # Based on https://auth0.com/docs/quickstart/backend/rails/02-using.
2
+
3
+ require 'uri'
4
+ require 'net/http'
5
+ require 'openssl'
6
+ require 'json'
7
+ require 'date'
8
+
9
+
10
+
11
+ module Auth0MachineToMachine
12
+ class Auth0BearerTokenError < StandardError; end
13
+
14
+ class GenericError < Auth0BearerTokenError; end
15
+
16
+ class Client
17
+ def getM2M!(config)
18
+ tenant_name = config['tenant_name']
19
+ client_id = config['client_id']
20
+ client_secret = config['client_secret']
21
+ audience = config['audience']
22
+ grant_type = config['grant_type']
23
+
24
+ url = URI("https://#{tenant_name}.eu.auth0.com/oauth/token")
25
+
26
+ http = Net::HTTP.new(url.host, url.port)
27
+ use_ssl = url.scheme == 'https'
28
+ http.use_ssl = use_ssl
29
+ if use_ssl
30
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
31
+ else
32
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
33
+ end
34
+
35
+ request = Net::HTTP::Post.new(url)
36
+ request["content-type"] = 'application/json'
37
+ request["accept"] = 'application/json'
38
+
39
+ body = {
40
+ client_id: client_id,
41
+ client_secret: client_secret,
42
+ audience: audience,
43
+ grant_type: grant_type
44
+ }
45
+
46
+ request.body = body.to_json
47
+
48
+ response = http.request(request)
49
+
50
+ if response.is_a?(Net::HTTPSuccess)
51
+ body = JSON.parse(response.body)
52
+ expires_in = body['expires_in']
53
+ expire_date = Time.now + expires_in
54
+ result = {
55
+ access_token: body['access_token'],
56
+ scope: body['scope'],
57
+ expires_in: expires_in,
58
+ expire_date: expire_date,
59
+ token_type: body['token_type']
60
+ }
61
+ else
62
+ raise GenericError.new(response.code + response.body + response.message)
63
+ end
64
+ end
65
+
66
+ def getM2M(config)
67
+ begin
68
+ getM2M!(config)
69
+ rescue Exception => e
70
+ false
71
+ end
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auth0_machine_to_machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - playfulcorgi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - unrulybeardedweekend@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/auth0_machine_to_machine.rb
21
+ homepage: https://github.com/playfulcorgi/auth0_machine_to_machine
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.7.0
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.1.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Retrieves JWT bearer token from Auth0 for authenticating and authorizing
44
+ requests.
45
+ test_files: []