rail-locator-api 0.1.17 → 0.1.18
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
|
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
|
@@ -13,8 +13,6 @@ RailLocatorApi.setup do |config|
|
|
13
13
|
config::Request.api_user_email ||= ENV['API_USER_EMAIL']
|
14
14
|
config::Request.api_user_password ||= ENV['API_USER_PASSWORD']
|
15
15
|
|
16
|
-
config::Request.jwt_secret_code ||= ENV['JWT_SECRET_CODE']
|
17
|
-
|
18
16
|
config::Request.timeout = 60
|
19
17
|
config::Request.open_timeout = 60
|
20
18
|
config::Request.symbolize_keys = true
|
@@ -33,9 +33,6 @@ module RailLocatorApi
|
|
33
33
|
protected
|
34
34
|
|
35
35
|
# Convenience accessors
|
36
|
-
def jwt_secret_code
|
37
|
-
@request_builder.jwt_secret_code
|
38
|
-
end
|
39
36
|
|
40
37
|
def access_token
|
41
38
|
@request_builder.access_token
|
@@ -124,20 +121,24 @@ module RailLocatorApi
|
|
124
121
|
if request
|
125
122
|
request.params.merge!(params) if params
|
126
123
|
request.headers['Content-Type'] = 'application/json'
|
127
|
-
unless RailLocatorApi::Request.token_alive?(RailLocatorApi::Request.access_token)
|
128
|
-
if RailLocatorApi::Request.token_alive?(RailLocatorApi::Request.refresh_token)
|
129
|
-
response = RailLocatorApi.refresh_access_token
|
130
|
-
RailLocatorApi::Request.access_token = response.try(:dig, "access_token")
|
131
|
-
RailLocatorApi::Request.refresh_token = response.try(:dig, "refresh_token")
|
132
|
-
end
|
133
|
-
end
|
134
124
|
if self.api_auth_method == :base64
|
135
125
|
request.headers['Authorization'] = "Basic " + Base64::encode64("#{self.api_user_email}:#{self.api_user_password}")
|
136
126
|
end
|
137
|
-
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
|
138
139
|
request.headers['Authorization'] = "Bearer #{RailLocatorApi::Request.access_token}"
|
139
140
|
end
|
140
|
-
if [:
|
141
|
+
if [:api_key].include?(self.api_auth_method)
|
141
142
|
request.headers['X-API-KEY'] = "#{RailLocatorApi::Request.api_key}"
|
142
143
|
end
|
143
144
|
request.headers['User-Agent'] = "RailLocatorApi/#{RailLocatorApi::VERSION} Ruby gem"
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module RailLocatorApi
|
2
2
|
class Request
|
3
|
-
attr_accessor :
|
3
|
+
attr_accessor :access_token, :refresh_token, :api_key, :api_user_email, :api_user_password, :api_auth_method, :api_endpoint,
|
4
4
|
:timeout, :open_timeout, :proxy, :ssl_options, :faraday_adapter, :symbolize_keys, :debug,
|
5
5
|
:without_ratelimit, :logger, :test
|
6
6
|
|
@@ -9,7 +9,7 @@ module RailLocatorApi
|
|
9
9
|
DEFAULT_TIMEOUT = 60
|
10
10
|
DEFAULT_OPEN_TIMEOUT = 60
|
11
11
|
|
12
|
-
def initialize(
|
12
|
+
def initialize(access_token: nil, refresh_token: nil, api_key: nil, api_user_email: nil, api_user_password: nil,
|
13
13
|
api_endpoint: nil, api_auth_method: nil, timeout: nil, open_timeout: nil, proxy: nil, ssl_options: nil,
|
14
14
|
faraday_adapter: nil, symbolize_keys: false, debug: false, without_ratelimit: false,
|
15
15
|
logger: nil, test: false)
|
@@ -23,9 +23,6 @@ module RailLocatorApi
|
|
23
23
|
@refresh_token = refresh_token || self.class.refresh_token
|
24
24
|
@refresh_token = @refresh_token.strip if @refresh_token
|
25
25
|
|
26
|
-
@jwt_secret_code = jwt_secret_code || self.class.jwt_secret_code
|
27
|
-
@jwt_secret_code = @jwt_secret_code.strip if @jwt_secret_code
|
28
|
-
|
29
26
|
@api_user_email = api_user_email || ENV['API_USER_EMAIL'] || ""
|
30
27
|
@api_user_password = api_user_password || ENV['API_USER_PASSWORD'] || ""
|
31
28
|
@api_endpoint = api_endpoint || self.class.api_endpoint
|
@@ -90,7 +87,7 @@ module RailLocatorApi
|
|
90
87
|
reset
|
91
88
|
end
|
92
89
|
|
93
|
-
def token_alive?(token, jwt_secret_code
|
90
|
+
def token_alive?(token, jwt_secret_code=RailLocatorApi.keycloak_client_secret)
|
94
91
|
begin
|
95
92
|
return false if token.nil?
|
96
93
|
exp = JWT.decode(token, jwt_secret_code, false).try(:first).try(:dig, "exp")
|
@@ -109,12 +106,12 @@ module RailLocatorApi
|
|
109
106
|
end
|
110
107
|
|
111
108
|
class << self
|
112
|
-
attr_accessor :
|
109
|
+
attr_accessor :access_token, :refresh_token, :api_key, :api_user_email, :api_user_password, :api_auth_method,
|
113
110
|
:timeout, :open_timeout, :api_endpoint, :proxy, :ssl_options, :faraday_adapter, :symbolize_keys,
|
114
111
|
:debug, :without_ratelimit, :logger, :test
|
115
112
|
|
116
113
|
def method_missing(sym, *args, &block)
|
117
|
-
new(
|
114
|
+
new(access_token: self.access_token, refresh_token: self.refresh_token,
|
118
115
|
api_key: self.api_key,
|
119
116
|
api_user_email: self.api_user_email, api_user_password: self.api_user_email,
|
120
117
|
api_auth_method: self.api_auth_method, api_endpoint: self.api_endpoint,
|
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|
|