hydra-keycloak-client 0.1.17 → 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: 61046cc3cd3ab5bc987ab0769799054193795ad82fca2ca46450b3cd6a1d1c44
4
- data.tar.gz: 408a84fdaa9a9d769e54a6bfb57fba5bcfdc9c74fe158e3062128b53a29e9a37
3
+ metadata.gz: 0a2e948b3a20ed2ba3bbbfa11c0d8095d1d2668ab7b84ab9c84ee141b4ffd858
4
+ data.tar.gz: 470d447468b40bb7f22ba97126702a7c99952cf9038005097b44651ce900a0c3
5
5
  SHA512:
6
- metadata.gz: bd7d6888768745cc897b554ec7f8342e12ec879258eafe967a947a2c902af32ba0e7ba4b46b3dc1c7b826b2f99962f8112698fbb50484951100ab6ff4ed64f0c
7
- data.tar.gz: e82f992be649e2d1923df2fdff0292a5b906e82c61082671f5fecb038c4dd3cd40d567e6f05b413d35b5ab8df99bf0d031c3e56b32ae2c0b8f435822a8734d86
6
+ metadata.gz: ce821ea5c0d68d23c1e57c5696013b4a3cdec84e4bd70e1ad484c2c394e1fa5699f7b685311732c4ed8d176406faeece9b78e722cb53fae7666c69e77cb9dfa5
7
+ data.tar.gz: fdb9094dd2861753b689f2a83b367116460b49f0a3817d2ba01b1c2fcf5ea705d2e4c7f903b6131c6d137c2785a8e5572ed9c80f74959447a37e9238a2eadecb
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.18)
5
5
  dry-auto_inject
6
6
  dry-container
7
7
  dry-monads
@@ -176,17 +176,17 @@ 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')
180
+ .or(Failure(code: :not_authenticated,
181
+ context: { args: { session_state: session_state } }))
182
182
  end
183
183
 
184
184
  def authorize!(session_state)
185
- return Failure(status: 400, code: :not_authenticated) unless authenticated?(session_state)
186
-
187
185
  access_token = yield fetch_token(session_state, 'access_token')
186
+ .or(Failure(code: :not_authenticated,
187
+ context: { args: { session_state: session_state } }))
188
188
  if token_expired?(access_token)
189
- refresh_tokens(session_state)
189
+ yield refresh_tokens(session_state)
190
190
 
191
191
  access_token = yield fetch_token(session_state, 'access_token')
192
192
  end
@@ -195,9 +195,9 @@ module Hydra
195
195
  end
196
196
 
197
197
  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)
198
+ fetch_token(session_state, 'access_token')
199
+ .either(->(token) { Success(token[:jti]) },
200
+ ->(_) { Failure(code: :not_authenticated, context: { args: { session_state: session_state } }) })
201
201
  end
202
202
 
203
203
  def logout!(session_state)
@@ -223,7 +223,9 @@ module Hydra
223
223
  if value
224
224
  Success(::Hydra::Keycloak::Token.new(value))
225
225
  else
226
- Failure(status: 400, code: :token_not_found)
226
+ Failure(code: :token_not_found,
227
+ context: { args: { session_state: session_state,
228
+ token_name: token_name } })
227
229
  end
228
230
  end
229
231
  end
@@ -250,6 +252,8 @@ module Hydra
250
252
  yield save_token(session_state, 'access_token', new_tokens[:access_token])
251
253
  yield save_token(session_state, 'id_token', new_tokens[:id_token])
252
254
  yield save_token(session_state, 'refresh_token', new_tokens[:refresh_token])
255
+
256
+ Success()
253
257
  end
254
258
  end
255
259
  end
@@ -15,22 +15,22 @@ 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, context: { args: { key: key }, action: :set, error: e.message })
20
20
  end
21
21
 
22
22
  def get(key)
23
23
  Success(dalli.get(key))
24
- rescue Dalli::DalliError
25
- Failure(status: 400, code: :memcached_unavailable)
24
+ rescue Dalli::DalliError => e
25
+ Failure(code: :memcached_unavailable, context: { args: { key: key }, action: :get, error: e.message })
26
26
  end
27
27
 
28
28
  def delete(key)
29
29
  dalli.delete(key)
30
30
 
31
31
  Success(:ok)
32
- rescue Dalli::DalliError
33
- Failure(status: 400, code: :memcached_unavailable)
32
+ rescue Dalli::DalliError => e
33
+ Failure(code: :memcached_unavailable, context: { args: { key: key }, action: :delete, error: e.message })
34
34
  end
35
35
  end
36
36
  end
@@ -15,22 +15,22 @@ 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, context: { args: { key: key }, action: :set, error: e.message })
20
20
  end
21
21
 
22
22
  def get(key)
23
23
  Success(redis.get(key))
24
- rescue Redis::BaseError
25
- Failure(status: 400, code: :redis_unavailable)
24
+ rescue Redis::BaseError => e
25
+ Failure(code: :redis_unavailable, context: { args: { key: key }, action: :get, error: e.message })
26
26
  end
27
27
 
28
28
  def delete(key)
29
29
  redis.del(key)
30
30
 
31
31
  Success(:ok)
32
- rescue Redis::BaseError
33
- Failure(status: 400, code: :redis_unavailable)
32
+ rescue Redis::BaseError => e
33
+ Failure(code: :redis_unavailable, context: { args: { key: key }, action: :delete, error: e.message })
34
34
  end
35
35
  end
36
36
  end
@@ -25,19 +25,45 @@ 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
+ method: :post,
44
+ response: { code: response.code,
45
+ body: response.body } })
36
46
  end
37
- rescue *NETWORK_ERRORS
38
- Failure(status: 400, code: :keycloak_unavailable)
39
- rescue JSON::ParserError
40
- Failure(status: 400, code: :json_parser_error)
47
+ rescue *NETWORK_ERRORS => e
48
+ Failure(code: :keycloak_unavailable, context: { error: e.message })
49
+ end
50
+
51
+ def parse_response(body)
52
+ Success(JSON.parse(body))
53
+ rescue JSON::ParserError => e
54
+ Failure(code: :json_parser_error, context: { args: { body: body }, error: e.message })
55
+ end
56
+
57
+ def hide_secrets(body)
58
+ secret_fields = %i[client_secret]
59
+
60
+ body.map do |k, v|
61
+ if secret_fields.include?(k)
62
+ [k, '**hidden**']
63
+ else
64
+ [k, v]
65
+ end
66
+ end.to_h
41
67
  end
42
68
  end
43
69
  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) unless auth_code
18
18
 
19
19
  result = tokens_gateway.post(
20
20
  urls.token_endpoint,
@@ -31,7 +31,7 @@ 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
+ return Failure(code: :username_or_password_is_empty) if username.nil? || password.nil?
35
35
 
36
36
  result = tokens_gateway.post(
37
37
  urls.token_endpoint,
@@ -55,7 +55,7 @@ module Hydra
55
55
  if result['active']
56
56
  Success(result)
57
57
  else
58
- Failure(status: 400, code: :token_not_active)
58
+ Failure(code: :token_not_active)
59
59
  end
60
60
  end
61
61
  end
@@ -65,15 +65,9 @@ module Hydra
65
65
  urls.token_endpoint,
66
66
  urls.refresh_request_body(refresh_token)
67
67
  ).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
68
+ Success({ access_token: ::Hydra::Keycloak::Token.new(result['access_token']),
69
+ id_token: ::Hydra::Keycloak::Token.new(result['id_token']),
70
+ refresh_token: ::Hydra::Keycloak::Token.new(result['refresh_token']) })
77
71
  end
78
72
  end
79
73
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Hydra
4
4
  module Keycloak
5
- VERSION = '0.1.17'
5
+ VERSION = '0.1.18'
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.18
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-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt