rail-locator-api 0.1.16 → 0.1.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 954fca62238bc7b33b0b86999e017649c5338f01b0fd4d684651abcf75b062a4
4
- data.tar.gz: e1e0fb3102e08e62195644fd8a479c6172eea3220c7b62bfd049f8e85dc026cf
3
+ metadata.gz: 52167f016f230777e3d7f8e262ae868434d5010e3ef07952559ebda36eae10a9
4
+ data.tar.gz: e6fe8e8f21e9f044948f63ab0b8c8b41d7435d2bdaed26497d95ff8f77f9b1dd
5
5
  SHA512:
6
- metadata.gz: be314fe74d7ec16776b2c528de676d44d067d38792f4eb671942e2e8c91c3e3fe66292492abdb7fa9bd11dd44db2065fe6fb712912148bd633bf363fc78c4396
7
- data.tar.gz: 855004fb2e73a23d5086ba1bad484fd7509364fa9325820fd3792d4bc30e02fad838a9b28ba918bfa9fbbc7d0e1705088bf854da18cedf444043f78be3bd3611
6
+ metadata.gz: ab2ae71377ed90c81742e6fb852e6dfbc6d6a3e2e768a2609ac77f8b6256f75cfae469e52285c3c2160b13b1a3ef295848bd0bca6bc3064ceefccc7ed38ad955
7
+ data.tar.gz: 42c4526305e430d5e15aea498b26de8de279e64c00547cfae93fc77951ddbbacdfce627f8c6c8383dbc98c5c20b68f9754bc47b056a62fcbaacf59e9c0c2b143
@@ -121,21 +121,24 @@ module RailLocatorApi
121
121
  if request
122
122
  request.params.merge!(params) if params
123
123
  request.headers['Content-Type'] = 'application/json'
124
- unless RailLocatorApi::Request.token_alive?(RailLocatorApi::Request.access_token)
125
- if RailLocatorApi::Request.token_alive?(RailLocatorApi::Request.refresh_token)
126
- response = RailLocatorApi.refresh_access_token
127
- RailLocatorApi::Request.access_token = response.try(:dig, "access_token")
128
- RailLocatorApi::Request.refresh_token = response.try(:dig, "refresh_token")
129
- end
130
- end
131
-
132
124
  if self.api_auth_method == :base64
133
125
  request.headers['Authorization'] = "Basic " + Base64::encode64("#{self.api_user_email}:#{self.api_user_password}")
134
126
  end
135
- if [:api_key, :keycloak].include?(self.api_auth_method)
127
+ if [:keycloak].include?(self.api_auth_method)
128
+ unless RailLocatorApi::Request.token_alive?(RailLocatorApi::Request.access_token)
129
+ if RailLocatorApi::Request.token_alive?(RailLocatorApi::Request.refresh_token)
130
+ response = RailLocatorApi.refresh_access_token
131
+ RailLocatorApi::Request.access_token = response.try(:dig, "access_token")
132
+ RailLocatorApi::Request.refresh_token = response.try(:dig, "refresh_token")
133
+ else
134
+ error_params = { title: "UNAUTHORIZED", status_code: 401 }
135
+ error = RailLocatorApiError.new("Token is unavailable", error_params)
136
+ raise error
137
+ end
138
+ end
136
139
  request.headers['Authorization'] = "Bearer #{RailLocatorApi::Request.access_token}"
137
140
  end
138
- if [:keycloak].include?(self.api_auth_method)
141
+ if [:api_key].include?(self.api_auth_method)
139
142
  request.headers['X-API-KEY'] = "#{RailLocatorApi::Request.api_key}"
140
143
  end
141
144
  request.headers['User-Agent'] = "RailLocatorApi/#{RailLocatorApi::VERSION} Ruby gem"
@@ -22,6 +22,7 @@ module RailLocatorApi
22
22
  @access_token = @access_token.strip if @access_token
23
23
  @refresh_token = refresh_token || self.class.refresh_token
24
24
  @refresh_token = @refresh_token.strip if @refresh_token
25
+
25
26
  @api_user_email = api_user_email || ENV['API_USER_EMAIL'] || ""
26
27
  @api_user_password = api_user_password || ENV['API_USER_PASSWORD'] || ""
27
28
  @api_endpoint = api_endpoint || self.class.api_endpoint
@@ -86,10 +87,10 @@ module RailLocatorApi
86
87
  reset
87
88
  end
88
89
 
89
- def token_alive?(token)
90
+ def token_alive?(token, jwt_secret_code=RailLocatorApi.keycloak_client_secret)
90
91
  begin
91
92
  return false if token.nil?
92
- exp = JWT.decode(token, JWT_SECTET_CODE, false).try(:first).try(:dig, "exp")
93
+ exp = JWT.decode(token, jwt_secret_code, false).try(:first).try(:dig, "exp")
93
94
  return false if exp.nil?
94
95
  Time.at(exp) > Time.now + 30.second
95
96
  rescue => e
@@ -110,7 +111,8 @@ module RailLocatorApi
110
111
  :debug, :without_ratelimit, :logger, :test
111
112
 
112
113
  def method_missing(sym, *args, &block)
113
- new(access_token: self.access_token, refresh_token: self.refresh_token, api_key: self.api_key,
114
+ new(access_token: self.access_token, refresh_token: self.refresh_token,
115
+ api_key: self.api_key,
114
116
  api_user_email: self.api_user_email, api_user_password: self.api_user_email,
115
117
  api_auth_method: self.api_auth_method, api_endpoint: self.api_endpoint,
116
118
  timeout: self.timeout, open_timeout: self.open_timeout, faraday_adapter: self.faraday_adapter,
@@ -1,3 +1,3 @@
1
1
  module RailLocatorApi
2
- VERSION = "0.1.16"
2
+ VERSION = "0.1.18"
3
3
  end
@@ -13,7 +13,7 @@ module RailLocatorApi
13
13
  if RailLocatorApi.keycloak_grant_type == "password"
14
14
  payload = "grant_type=#{RailLocatorApi.keycloak_grant_type}&client_id=#{RailLocatorApi.keycloak_client_id}&username=#{RailLocatorApi.api_user_email}&password=#{RailLocatorApi.api_user_password}"
15
15
  else
16
- payload = "grant_type=#{RailLocatorApi.keycloak_grant_type}&client_id=#{RailLocatorApi.keycloak_client_id}&client_secret=#{RailLocatorApi.keycloak_client_secret}&code=#{RailLocatorApi::Request.api_key}&redirect_uri=#{RailLocatorApi.keycloak_redirect_uri}"
16
+ payload = "grant_type=#{RailLocatorApi.keycloak_grant_type}&client_id=#{RailLocatorApi.keycloak_client_id}&client_secret=#{RailLocatorApi.keycloak_client_secret}&code=#{RailLocatorApi::Request.access_token}&redirect_uri=#{RailLocatorApi.keycloak_redirect_uri}"
17
17
  end
18
18
  connection = Faraday.new(RailLocatorApi.keycloak_token_url, proxy: nil,
19
19
  ssl: RailLocatorApi::Request.ssl_options || { version: "TLSv1_2" }) do |faraday|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rail-locator-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Osetrov