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 +4 -4
- data/lib/rail-locator-api/api_request.rb +13 -10
- data/lib/rail-locator-api/request.rb +5 -3
- data/lib/rail-locator-api/version.rb +1 -1
- data/lib/rail-locator-api.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52167f016f230777e3d7f8e262ae868434d5010e3ef07952559ebda36eae10a9
|
4
|
+
data.tar.gz: e6fe8e8f21e9f044948f63ab0b8c8b41d7435d2bdaed26497d95ff8f77f9b1dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 [:
|
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 [:
|
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,
|
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,
|
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,
|
data/lib/rail-locator-api.rb
CHANGED
@@ -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.
|
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|
|