hydra-keycloak-client 0.1.17 → 0.1.19

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: 61046cc3cd3ab5bc987ab0769799054193795ad82fca2ca46450b3cd6a1d1c44
4
- data.tar.gz: 408a84fdaa9a9d769e54a6bfb57fba5bcfdc9c74fe158e3062128b53a29e9a37
3
+ metadata.gz: 53ac1a0505551350a5f1374aa7bd3fb390c68047b8be65e87ec00f6f36e6394a
4
+ data.tar.gz: 050da3e0481a21c09aa00410dcaf19b514bf23d6dfc868261d8205120898cd2e
5
5
  SHA512:
6
- metadata.gz: bd7d6888768745cc897b554ec7f8342e12ec879258eafe967a947a2c902af32ba0e7ba4b46b3dc1c7b826b2f99962f8112698fbb50484951100ab6ff4ed64f0c
7
- data.tar.gz: e82f992be649e2d1923df2fdff0292a5b906e82c61082671f5fecb038c4dd3cd40d567e6f05b413d35b5ab8df99bf0d031c3e56b32ae2c0b8f435822a8734d86
6
+ metadata.gz: c4ae1d7d6ce9deb7c39c7a4b82d3e3a2caf8fd9a05d9d4a089929e62a98081020699808c21745045461c30a70fa690a6706232126fa100137fe9e37a2bd183d5
7
+ data.tar.gz: e7761077fd40bb285b57136fa6b800c5f393746a9e02aab44969f81bd19a9eb5ca3fd4aaefe31718680e987999688e217ec7f36fc14d2dd7c9f839f374522b83
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hydra-keycloak-client (0.1.17)
4
+ hydra-keycloak-client (0.1.19)
5
5
  dry-auto_inject
6
6
  dry-container
7
7
  dry-monads
@@ -176,17 +176,13 @@ module Hydra
176
176
  end
177
177
 
178
178
  def access_token(session_state)
179
- return Failure(status: 400, code: :not_authenticated) unless authenticated?(session_state)
180
-
181
179
  fetch_token(session_state, 'access_token')
182
180
  end
183
181
 
184
182
  def authorize!(session_state)
185
- return Failure(status: 400, code: :not_authenticated) unless authenticated?(session_state)
186
-
187
183
  access_token = yield fetch_token(session_state, 'access_token')
188
184
  if token_expired?(access_token)
189
- refresh_tokens(session_state)
185
+ yield refresh_tokens(session_state)
190
186
 
191
187
  access_token = yield fetch_token(session_state, 'access_token')
192
188
  end
@@ -195,9 +191,7 @@ module Hydra
195
191
  end
196
192
 
197
193
  def access_token_jti(session_state)
198
- return Failure(status: 400, code: :not_authenticated) unless authenticated?(session_state)
199
-
200
- fetch_token(session_state, 'access_token').fmap(&:jti)
194
+ fetch_token(session_state, 'access_token').fmap { |token| token[:jti] }
201
195
  end
202
196
 
203
197
  def logout!(session_state)
@@ -223,7 +217,10 @@ module Hydra
223
217
  if value
224
218
  Success(::Hydra::Keycloak::Token.new(value))
225
219
  else
226
- Failure(status: 400, code: :token_not_found)
220
+ Failure(code: :token_not_found,
221
+ context: { args: { session_state: session_state,
222
+ token_name: token_name },
223
+ caller: caller })
227
224
  end
228
225
  end
229
226
  end
@@ -250,6 +247,8 @@ module Hydra
250
247
  yield save_token(session_state, 'access_token', new_tokens[:access_token])
251
248
  yield save_token(session_state, 'id_token', new_tokens[:id_token])
252
249
  yield save_token(session_state, 'refresh_token', new_tokens[:refresh_token])
250
+
251
+ Success()
253
252
  end
254
253
  end
255
254
  end
@@ -15,22 +15,25 @@ module Hydra
15
15
  dalli.set(key, value)
16
16
 
17
17
  Success(:ok)
18
- rescue Dalli::DalliError
19
- Failure(status: 400, code: :memcached_unavailable)
18
+ rescue Dalli::DalliError => e
19
+ Failure(code: :memcached_unavailable,
20
+ context: { args: { key: key }, action: :set, error: e.message, caller: caller })
20
21
  end
21
22
 
22
23
  def get(key)
23
24
  Success(dalli.get(key))
24
- rescue Dalli::DalliError
25
- Failure(status: 400, code: :memcached_unavailable)
25
+ rescue Dalli::DalliError => e
26
+ Failure(code: :memcached_unavailable,
27
+ context: { args: { key: key }, action: :get, error: e.message, caller: caller })
26
28
  end
27
29
 
28
30
  def delete(key)
29
31
  dalli.delete(key)
30
32
 
31
33
  Success(:ok)
32
- rescue Dalli::DalliError
33
- Failure(status: 400, code: :memcached_unavailable)
34
+ rescue Dalli::DalliError => e
35
+ Failure(code: :memcached_unavailable,
36
+ context: { args: { key: key }, action: :delete, error: e.message, caller: caller })
34
37
  end
35
38
  end
36
39
  end
@@ -15,22 +15,25 @@ module Hydra
15
15
  redis.set(key, value)
16
16
 
17
17
  Success(:ok)
18
- rescue Redis::BaseError
19
- Failure(status: 400, code: :redis_unavailable)
18
+ rescue Redis::BaseError => e
19
+ Failure(code: :redis_unavailable,
20
+ context: { args: { key: key }, action: :set, error: e.message, caller: caller })
20
21
  end
21
22
 
22
23
  def get(key)
23
24
  Success(redis.get(key))
24
- rescue Redis::BaseError
25
- Failure(status: 400, code: :redis_unavailable)
25
+ rescue Redis::BaseError => e
26
+ Failure(code: :redis_unavailable,
27
+ context: { args: { key: key }, action: :get, error: e.message, caller: caller })
26
28
  end
27
29
 
28
30
  def delete(key)
29
31
  redis.del(key)
30
32
 
31
33
  Success(:ok)
32
- rescue Redis::BaseError
33
- Failure(status: 400, code: :redis_unavailable)
34
+ rescue Redis::BaseError => e
35
+ Failure(code: :redis_unavailable,
36
+ context: { args: { key: key }, action: :delete, error: e.message, caller: caller })
34
37
  end
35
38
  end
36
39
  end
@@ -25,19 +25,46 @@ module Hydra
25
25
  Net::ProtocolError].freeze
26
26
 
27
27
  def post(path, body)
28
- response = http.post_form(URI(path), body)
28
+ _post(URI(path), body)
29
+ .bind { |resp| parse_response(resp.body) }
30
+ end
29
31
 
30
- if response.code == '200'
31
- json = JSON.parse(response.body)
32
+ private
32
33
 
33
- Success(json)
34
+ def _post(path, body)
35
+ response = http.post_form(URI(path), body)
36
+
37
+ if %w[200 201].include?(response.code)
38
+ Success(response)
34
39
  else
35
- Failure(status: response.code, code: :bad_keycloak_response)
40
+ Failure(code: :bad_keycloak_response,
41
+ context: { args: { path: path,
42
+ body: hide_secrets(body) },
43
+ caller: caller,
44
+ method: :post,
45
+ response: { code: response.code,
46
+ body: response.body } })
36
47
  end
37
- rescue *NETWORK_ERRORS
38
- Failure(status: 400, code: :keycloak_unavailable)
39
- rescue JSON::ParserError
40
- Failure(status: 400, code: :json_parser_error)
48
+ rescue *NETWORK_ERRORS => e
49
+ Failure(code: :keycloak_unavailable, context: { error: e.message, caller: caller })
50
+ end
51
+
52
+ def parse_response(body)
53
+ Success(JSON.parse(body))
54
+ rescue JSON::ParserError => e
55
+ Failure(code: :json_parser_error, context: { args: { body: body }, error: e.message, caller: caller })
56
+ end
57
+
58
+ def hide_secrets(body)
59
+ secret_fields = %i[client_secret]
60
+
61
+ body.map do |k, v|
62
+ if secret_fields.include?(k)
63
+ [k, '**hidden**']
64
+ else
65
+ [k, v]
66
+ end
67
+ end.to_h
41
68
  end
42
69
  end
43
70
  end
@@ -14,7 +14,7 @@ module Hydra
14
14
  inject['tokens_gateway', 'urls']
15
15
 
16
16
  def get_tokens(auth_code, code_verifier)
17
- return Failure(status: 400, code: :auth_code_was_not_received) unless auth_code
17
+ return Failure(code: :auth_code_was_not_received, context: { caller: caller }) unless auth_code
18
18
 
19
19
  result = tokens_gateway.post(
20
20
  urls.token_endpoint,
@@ -31,7 +31,10 @@ module Hydra
31
31
  end
32
32
 
33
33
  def get_tokens_by_password(username, password)
34
- return Failure(status: 400, code: :username_or_password_is_empty) if username.nil? || password.nil?
34
+ if username.nil? || password.nil?
35
+ return Failure(code: :username_or_password_is_empty,
36
+ context: { caller: caller })
37
+ end
35
38
 
36
39
  result = tokens_gateway.post(
37
40
  urls.token_endpoint,
@@ -55,7 +58,7 @@ module Hydra
55
58
  if result['active']
56
59
  Success(result)
57
60
  else
58
- Failure(status: 400, code: :token_not_active)
61
+ Failure(code: :token_not_active, caller: caller)
59
62
  end
60
63
  end
61
64
  end
@@ -65,15 +68,9 @@ module Hydra
65
68
  urls.token_endpoint,
66
69
  urls.refresh_request_body(refresh_token)
67
70
  ).bind do |result|
68
- if result['error']
69
- Failure(status: 400, code: :token_refreshing_error)
70
- else
71
- Success({
72
- access_token: ::Hydra::Keycloak::Token.new(result['access_token']),
73
- id_token: ::Hydra::Keycloak::Token.new(result['id_token']),
74
- refresh_token: ::Hydra::Keycloak::Token.new(result['refresh_token'])
75
- })
76
- end
71
+ Success({ access_token: ::Hydra::Keycloak::Token.new(result['access_token']),
72
+ id_token: ::Hydra::Keycloak::Token.new(result['id_token']),
73
+ refresh_token: ::Hydra::Keycloak::Token.new(result['refresh_token']) })
77
74
  end
78
75
  end
79
76
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Hydra
4
4
  module Keycloak
5
- VERSION = '0.1.17'
5
+ VERSION = '0.1.19'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hydra-keycloak-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fedor Kosolapov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-20 00:00:00.000000000 Z
11
+ date: 2023-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt