leaseweb-rest-api 1.1.4 → 1.1.5

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: 7ecc51f07cf45f19053de3fd9cf08410e955cf50
4
- data.tar.gz: 44c1862d16410624b02560c2f1278d85a79ae916
3
+ metadata.gz: 6b83be5bd2fd6f52f68ffdbc06bf81eb2ceaa6cc
4
+ data.tar.gz: ca30de4fae4eb69c21be20332ef038717d9acbe7
5
5
  SHA512:
6
- metadata.gz: b0294824019a175a512b657fdc38d5b8f656a1cd9dec950d37244f858cead8082924ce1d9b8dbd74982cce5f598454535b6513447ac21b1097693bc0fafd05f9
7
- data.tar.gz: 5d9f49e10b03721c891b8de40ae8f263e919923e3e05fa936d04576abf59f8b92dd907525c103d2b4b7051280f877c83cc884b559f085fd315ce56c9b29986e0
6
+ metadata.gz: f9800eb6c4b1b66ad08873f8b0173f12005e18c93a8f3d888c61a10433826da27356d2cf327195278c64293729149397ec5196f20d2d466a8b5de0a28677eeba
7
+ data.tar.gz: 74965ed037312892f4759771496c51a7906968c56f9f7bb7eb9f7bfd0db2cede3524ee2fbb3bf07f934d522e896787e18062f30e8db3166662a748d0b6513b52
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'leaseweb-rest-api'
3
- s.version = '1.1.4'
3
+ s.version = '1.1.5'
4
4
  s.authors = 'Arnoud Vermeer'
5
5
  s.email = 'a.vermeer@tech.leaseweb.com'
6
6
  s.license = 'Apache'
@@ -13,24 +13,59 @@ class LeasewebAPI
13
13
 
14
14
  base_uri 'https://api.leaseweb.com'
15
15
 
16
+ def initialize
17
+ @tmpdir = "/tmp/lsw-rest-api"
18
+ Dir.mkdir(@tmpdir) unless Dir.exist?(@tmpdir)
19
+ end
20
+
16
21
  def apiKeyAuth(apikey)
17
22
  @options = { headers: { 'X-Lsw-Auth' => apikey, 'Content-Type' => 'application/json' } }
18
23
  end
19
24
 
20
- def getOauthToken(clientId, clientSecret)
21
- response = self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: clientId, password: clientSecret }, body: { grant_type: 'client_credentials' })
22
- access_token = response.parsed_response['access_token']
25
+ def getOauthToken(client_id, client_secret)
26
+ access_token = validate_token(client_id)
27
+
28
+ if (access_token == false)
29
+ response = self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: client_id, password: client_secret }, body: { grant_type: 'client_credentials' })
30
+ access_token = response.parsed_response['access_token']
31
+ cache_token(client_id, access_token, response.parsed_response['expires_in'])
32
+ end
23
33
 
24
34
  @options = { headers: { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' } }
25
35
  end
26
36
 
27
37
  def getPasswordToken(username, password, client_id, client_secret)
28
- response = self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: client_id, password: client_secret }, body: { grant_type: 'password', username: username, password: password })
29
- access_token = response.parsed_response['access_token']
38
+ access_token = validate_token(client_id)
39
+
40
+ if (access_token == false)
41
+ response = self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: client_id, password: client_secret }, body: { grant_type: 'password', username: username, password: password })
42
+ access_token = response.parsed_response['access_token']
43
+ cache_token(client_id, access_token, response.parsed_response['expires_in'])
44
+ end
30
45
 
31
46
  @options = { headers: { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' } }
32
47
  end
33
48
 
49
+ def validate_token(client_id)
50
+ file = "#{@tmpdir}/#{client_id}.json"
51
+ content = JSON.parse(File.read(file))
52
+ expires_at = DateTime.parse(content['expires_at'])
53
+
54
+ if expires_at > DateTime.now
55
+ return content['access_token']
56
+ else
57
+ File.delete(file)
58
+ end
59
+
60
+ return false
61
+ end
62
+
63
+ def cache_token(client_id, access_token, expires_in)
64
+ file = "#{@tmpdir}/#{client_id}.json"
65
+ content = { access_token: access_token, expires_at: Time.now.getutc + expires_in }.to_json
66
+ File.write(file, content)
67
+ end
68
+
34
69
  def readPrivateKey(privateKey, password)
35
70
  @private_key = OpenSSL::PKey::RSA.new(File.read(privateKey), password)
36
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leaseweb-rest-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arnoud Vermeer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-09 00:00:00.000000000 Z
11
+ date: 2017-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty