hydra-keycloak-client 0.1.8 → 0.1.12

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: 4e7087568f245ec3f7b88cf24e7006da3e74bda757177d44ca109e914d4e7814
4
- data.tar.gz: 2643f7fe37e38a43f9052d006a3693e335b4721d051dd66169a7c8dc99d3a1fc
3
+ metadata.gz: 90747753193ef584afd8c70fdb15dd9f38fc097500e6ab748a8690869d02ee9e
4
+ data.tar.gz: 8ffbb7be26674cd909e70115a9954a1956197cde46eba192792e6353038ab985
5
5
  SHA512:
6
- metadata.gz: 847680ed2c9520cabbd720929d264fd901138aca6069b319a7a577a098a1991fe54d34714a4e1a64e1ead16ebe4b53e15c2c496c5c556930335386282792bf75
7
- data.tar.gz: 4a6baa901c8d4fae1b2658849a459d844b585368fb587ac02e673c9442e798f8161dde3bb53b2a1e5457a8f07090150637db76a71ea6df8ada3e43530b222c31
6
+ metadata.gz: 7e2ecf0a2b3cb6edfb676a3bd9944ead397640c2811a7fa5c9e743e6f15eed60aafa1460ae2973b4d01682472f57ca57b3b7715557e2a93966022e9e62b93a63
7
+ data.tar.gz: ede2dd403ed0049bcd2cc6dd536d99a0b8823299d9387c12bbc9f2eb31c6955ef001f47a5106793aefe52e4943a7ca3a988a5b8b985acb50f7cda60f9da88522
@@ -10,7 +10,23 @@ jobs:
10
10
  - uses: actions/checkout@v2
11
11
 
12
12
  - name: Build image
13
- run: docker build -f Dockerfile.test -t hkc_test_image .
13
+ run: docker build -f Dockerfile.test -t hkc_image:testing .
14
14
 
15
- - name: Run tests
16
- run: docker run hkc_test_image rspec
15
+ - name: "Run tests"
16
+ run: |
17
+ docker run --name hkc_image \
18
+ -e SIMPLE_COV_ENABLED=true \
19
+ hkc_image:testing \
20
+ ./run_tests.sh
21
+
22
+ - name: "Save coverage report"
23
+ run: docker cp hkc_image:/app/coverage/coverage.xml coverage.xml
24
+
25
+ - name: "Upload coverage report"
26
+ run: pip install --user codecov==2.0.22 && python -m codecov --required --url="http://coverage.dev.latera.ru/api" --branch="${{ github.ref_name }}" --token="17989959" --slug="hydra-billing/hydra-keycloak-client"
27
+
28
+ - name: Clean up container
29
+ if: always()
30
+ run: |
31
+ docker kill hkc_image 2>/dev/null || true
32
+ docker rm hkc_image 2>/dev/null || true
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5.8
3
+ Exclude:
4
+ - 'spec/**/**/*'
5
+
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ Metrics/AbcSize:
10
+ Max: 50
11
+
12
+ Metrics/MethodLength:
13
+ Max: 30
data/Dockerfile.test CHANGED
@@ -1,7 +1,9 @@
1
1
  FROM ruby:2.7.4
2
2
 
3
3
  COPY . /app
4
+
4
5
  WORKDIR /app
5
6
 
7
+ RUN chmod +x ./run_tests.sh
8
+
6
9
  RUN bundle install
7
- CMD bundle exec rspec
data/Gemfile CHANGED
@@ -1,9 +1,15 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in hydra-keycloak-client.gemspec
4
6
  gemspec
5
7
 
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
10
+ gem 'rubocop', '~> 1.26'
8
11
 
9
12
  gem 'pry'
13
+
14
+ gem 'simplecov', require: false, group: :test
15
+ gem 'simplecov-cobertura', require: false, group: :test
data/Gemfile.lock CHANGED
@@ -1,26 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hydra-keycloak-client (0.1.8)
5
- dalli
4
+ hydra-keycloak-client (0.1.11)
6
5
  dry-auto_inject
7
6
  dry-container
8
7
  dry-monads
9
8
  dry-schema
10
9
  dry-struct
11
10
  jwt
12
- redis
13
11
 
14
12
  GEM
15
13
  remote: https://rubygems.org/
16
14
  specs:
15
+ ast (2.4.2)
17
16
  coderay (1.1.3)
18
- concurrent-ruby (1.1.9)
19
- dalli (3.2.1)
17
+ concurrent-ruby (1.1.10)
20
18
  diff-lcs (1.4.4)
19
+ docile (1.4.0)
21
20
  dry-auto_inject (0.9.0)
22
21
  dry-container (>= 0.3.4)
23
- dry-configurable (0.14.0)
22
+ dry-configurable (0.15.0)
24
23
  concurrent-ruby (~> 1.0)
25
24
  dry-core (~> 0.6)
26
25
  dry-container (0.9.0)
@@ -36,7 +35,7 @@ GEM
36
35
  dry-monads (1.4.0)
37
36
  concurrent-ruby (~> 1.0)
38
37
  dry-core (~> 0.7)
39
- dry-schema (1.9.1)
38
+ dry-schema (1.9.3)
40
39
  concurrent-ruby (~> 1.0)
41
40
  dry-configurable (~> 0.13, >= 0.13.0)
42
41
  dry-core (~> 0.5, >= 0.5)
@@ -54,13 +53,18 @@ GEM
54
53
  dry-inflector (~> 0.1, >= 0.1.2)
55
54
  dry-logic (~> 1.0, >= 1.0.2)
56
55
  ice_nine (0.11.2)
57
- jwt (2.3.0)
56
+ jwt (2.4.1)
58
57
  method_source (1.0.0)
58
+ parallel (1.21.0)
59
+ parser (3.1.1.0)
60
+ ast (~> 2.4.1)
59
61
  pry (0.14.1)
60
62
  coderay (~> 1.1)
61
63
  method_source (~> 1.0)
64
+ rainbow (3.1.1)
62
65
  rake (12.3.3)
63
- redis (4.6.0)
66
+ regexp_parser (2.2.1)
67
+ rexml (3.2.5)
64
68
  rspec (3.10.0)
65
69
  rspec-core (~> 3.10.0)
66
70
  rspec-expectations (~> 3.10.0)
@@ -74,6 +78,28 @@ GEM
74
78
  diff-lcs (>= 1.2.0, < 2.0)
75
79
  rspec-support (~> 3.10.0)
76
80
  rspec-support (3.10.3)
81
+ rubocop (1.26.0)
82
+ parallel (~> 1.10)
83
+ parser (>= 3.1.0.0)
84
+ rainbow (>= 2.2.2, < 4.0)
85
+ regexp_parser (>= 1.8, < 3.0)
86
+ rexml
87
+ rubocop-ast (>= 1.16.0, < 2.0)
88
+ ruby-progressbar (~> 1.7)
89
+ unicode-display_width (>= 1.4.0, < 3.0)
90
+ rubocop-ast (1.16.0)
91
+ parser (>= 3.1.1.0)
92
+ ruby-progressbar (1.11.0)
93
+ simplecov (0.21.2)
94
+ docile (~> 1.1)
95
+ simplecov-html (~> 0.11)
96
+ simplecov_json_formatter (~> 0.1)
97
+ simplecov-cobertura (2.1.0)
98
+ rexml
99
+ simplecov (~> 0.19)
100
+ simplecov-html (0.12.3)
101
+ simplecov_json_formatter (0.1.4)
102
+ unicode-display_width (2.1.0)
77
103
 
78
104
  PLATFORMS
79
105
  ruby
@@ -83,6 +109,9 @@ DEPENDENCIES
83
109
  pry
84
110
  rake (~> 12.0)
85
111
  rspec (~> 3.0)
112
+ rubocop (~> 1.26)
113
+ simplecov
114
+ simplecov-cobertura
86
115
 
87
116
  BUNDLED WITH
88
117
  2.2.25
data/README.md CHANGED
@@ -32,7 +32,8 @@ keycloack_client = Hydra::Keycloak::ClientCreator.call(
32
32
  secret: keycloak_config.fetch(:secret),
33
33
  logout_redirect: keycloak_config.fetch(:logout_redirect),
34
34
  store_client: 'redis',
35
- store_client_options: {redis_host: ENV['REDIS_HOST'], redis_port: ENV['REDIS_PORT']}
35
+ store_client_options: {redis_host: ENV['REDIS_HOST'], redis_port: ENV['REDIS_PORT']},
36
+ scope: 'hoper_scope homs_scope'
36
37
  })
37
38
  ```
38
39
 
@@ -40,7 +41,7 @@ or for memcached:
40
41
 
41
42
  ```
42
43
  ...
43
- store_client: 'redis',
44
+ store_client: 'memcached',
44
45
  store_client_options: {
45
46
  memcached_host: "localhost",
46
47
  memcached_port: "11211",
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "hydra/keycloak/client"
4
+ require 'bundler/setup'
5
+ require 'hydra/keycloak/client'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "hydra/keycloak/client"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
@@ -1,38 +1,38 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require './lib/hydra/keycloak/version'
2
4
 
3
5
  Gem::Specification.new do |spec|
4
- spec.require_paths = ["lib"]
5
- spec.name = "hydra-keycloak-client"
6
+ spec.require_paths = ['lib']
7
+ spec.name = 'hydra-keycloak-client'
6
8
  spec.version = Hydra::Keycloak::VERSION
7
- spec.authors = ["Fedor Kosolapov"]
8
- spec.email = ["f.kosolapov@latera.ru"]
9
+ spec.authors = ['Fedor Kosolapov']
10
+ spec.email = ['f.kosolapov@latera.ru']
9
11
 
10
- spec.summary = "Keycloak client for SSO"
11
- spec.description = "Keycloak client for SSO"
12
- spec.homepage = "https://github.com/latera/hydra-keycloak-client"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
12
+ spec.summary = 'Keycloak client for SSO'
13
+ spec.description = 'Keycloak client for SSO'
14
+ spec.homepage = 'https://github.com/hydra-billing/hydra-keycloak-client'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.8')
14
16
 
15
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
16
18
 
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = spec.homepage
19
- spec.metadata["changelog_uri"] = spec.homepage
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = spec.homepage
21
+ spec.metadata['changelog_uri'] = spec.homepage
20
22
 
21
23
  # Specify which files should be added to the gem when it is released.
22
24
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
26
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
27
  end
26
- spec.bindir = "bin"
28
+ spec.bindir = 'bin'
27
29
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
28
30
 
29
31
  spec.add_dependency('jwt')
30
- spec.add_dependency('dalli')
31
- spec.add_dependency('redis')
32
32
 
33
- spec.add_dependency('dry-monads')
34
33
  spec.add_dependency('dry-auto_inject')
35
34
  spec.add_dependency('dry-container')
35
+ spec.add_dependency('dry-monads')
36
36
  spec.add_dependency('dry-schema')
37
37
  spec.add_dependency('dry-struct')
38
38
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'dry/monads'
3
4
  require 'dry/auto_inject'
4
5
  require 'dry/container'
@@ -14,85 +15,120 @@ module Hydra
14
15
  class ClientCreator
15
16
  extend ::Hydra::Keycloak::Mixin
16
17
 
17
- def self.call(config:)
18
- memcached_schema = Dry::Schema.JSON do
19
- required(:memcached_host).filled(:string)
20
- required(:memcached_port).filled(:string)
21
- required(:memcached_namespace).filled(:string)
18
+ class << self
19
+ def call(config:)
20
+ register_containers(validate_config(config))
22
21
  end
23
22
 
24
- redis_schema = Dry::Schema.JSON do
25
- required(:redis_host).filled(:string)
26
- required(:redis_port).filled(:string)
27
- end
23
+ def validate_config(config)
24
+ validated_config = config_schema.call(config)
25
+
26
+ if validated_config.failure?
27
+ raise ConfigurationError, "Wrong configuration params: #{validated_config.errors(full: true).to_h}"
28
+ end
28
29
 
29
- config_schema = Dry::Schema.JSON do
30
- required(:auth_server_url).filled(:string)
31
- required(:realm).filled(:string)
32
- required(:client_id).filled(:string)
33
- required(:redirect_uri).filled(:string)
34
- required(:secret).filled(:string)
35
- required(:logout_redirect).filled(:string)
36
- required(:store_client).value(included_in?: ['redis', 'memcached'])
37
- required(:store_client_options).hash(memcached_schema | redis_schema)
30
+ validated_config
31
+ end
32
+
33
+ def config_schema
34
+ memcached_schema = ::Hydra::Keycloak::ClientCreator.memcached_schema
35
+ redis_schema = ::Hydra::Keycloak::ClientCreator.redis_schema
36
+ Dry::Schema.JSON do
37
+ required(:auth_server_url).filled(:string)
38
+ required(:realm).filled(:string)
39
+ required(:client_id).filled(:string)
40
+ required(:redirect_uri).filled(:string)
41
+ required(:secret).filled(:string)
42
+ required(:logout_redirect).filled(:string)
43
+ required(:store_client).value(included_in?: %w[redis memcached])
44
+ required(:store_client_options).hash(memcached_schema | redis_schema)
45
+ optional(:scope).array(:str?)
46
+ end
38
47
  end
39
48
 
40
- validated_config = config_schema.call(config)
49
+ def memcached_schema
50
+ Dry::Schema.JSON do
51
+ required(:memcached_host).filled(:string)
52
+ required(:memcached_port).filled(:string)
53
+ required(:memcached_namespace).filled(:string)
54
+ end
55
+ end
41
56
 
42
- if validated_config.failure?
43
- raise ConfigurationError, "Wrong configuration params: #{validated_config.errors(full: true).to_h}"
57
+ def redis_schema
58
+ Dry::Schema.JSON do
59
+ required(:redis_host).filled(:string)
60
+ required(:redis_port).filled(:string)
61
+ end
44
62
  end
45
63
 
46
- container.register :urls do
47
- require 'hydra/keycloak/urls'
64
+ def register_containers(validated_config)
65
+ register_urls(validated_config)
66
+ register_queries
67
+ register_store_client(validated_config)
68
+ register_store
69
+ register_code_verifier
48
70
 
49
- ::Hydra::Keycloak::Urls.new(validated_config)
71
+ ::Hydra::Keycloak::Client.new
50
72
  end
51
73
 
52
- container.register :queries do
53
- require 'hydra/keycloak/queries/gateway'
74
+ def register_urls(config)
75
+ container.register :urls do
76
+ require 'hydra/keycloak/urls'
54
77
 
55
- ::Hydra::Keycloak::Queries::Gateway.new
78
+ ::Hydra::Keycloak::Urls.new(config)
79
+ end
56
80
  end
57
81
 
58
- if validated_config[:store_client] == 'redis'
59
- container.register :redis do
60
- require 'redis'
61
-
62
- ::Redis.new(host: validated_config[:redis_host], port: validated_config[:redis_port])
63
- end
64
-
65
- container.register :store_client do
66
- require 'hydra/keycloak/store/redis_client'
67
-
68
- ::Hydra::Keycloak::Store::RedisClient.new
69
- end
70
- elsif validated_config[:store_client] == 'memcached'
71
- container.register :dalli do
72
- require 'dalli'
73
-
74
- ::Dalli::Client.new(
75
- "#{validated_config[:store_client_options][:memcached_host]}:#{validated_config[:store_client_options][:memcached_port]}",
76
- namespace: validated_config[:store_client_options][:memcached_namespace]
77
- )
78
- end
79
-
80
- container.register :store_client do
81
- require 'hydra/keycloak/store/memcached_client'
82
-
83
- ::Hydra::Keycloak::Store::MemcachedClient.new
82
+ def register_queries
83
+ container.register :queries do
84
+ require 'hydra/keycloak/queries/gateway'
85
+
86
+ ::Hydra::Keycloak::Queries::Gateway.new
84
87
  end
85
- end
88
+ end
86
89
 
87
- container.register :store do
88
- require 'hydra/keycloak/store/gateway'
90
+ def register_store_client(config)
91
+ case config[:store_client]
92
+ when 'redis'
93
+ require 'hydra/keycloak/store/adapters/redis'
89
94
 
90
- ::Hydra::Keycloak::Store::Gateway.new
95
+ container.register :redis do
96
+ ::Redis.new(host: config[:redis_host], port: config[:redis_port])
97
+ end
98
+
99
+ container.register :store_client do
100
+ require 'hydra/keycloak/store/redis_client'
101
+
102
+ ::Hydra::Keycloak::Store::RedisClient.new
103
+ end
104
+ when 'memcached'
105
+ require 'hydra/keycloak/store/adapters/memcached'
106
+ container.register :dalli do
107
+ ::Dalli::Client.new(
108
+ "#{config[:store_client_options][:memcached_host]}:#{config[:store_client_options][:memcached_port]}",
109
+ namespace: config[:store_client_options][:memcached_namespace]
110
+ )
111
+ end
112
+
113
+ container.register :store_client do
114
+ require 'hydra/keycloak/store/memcached_client'
115
+
116
+ ::Hydra::Keycloak::Store::MemcachedClient.new
117
+ end
118
+ end
91
119
  end
92
120
 
93
- container.register(:code_verifier, ::Hydra::Keycloak::CodeVerifier.new)
121
+ def register_store
122
+ container.register :store do
123
+ require 'hydra/keycloak/store/gateway'
124
+
125
+ ::Hydra::Keycloak::Store::Gateway.new
126
+ end
127
+ end
94
128
 
95
- ::Hydra::Keycloak::Client.new
129
+ def register_code_verifier
130
+ container.register(:code_verifier, ::Hydra::Keycloak::CodeVerifier.new)
131
+ end
96
132
  end
97
133
  end
98
134
 
@@ -107,9 +143,7 @@ module Hydra
107
143
  end
108
144
 
109
145
  def authenticate!(auth_code)
110
- unless auth_code
111
- return Failure(status: 400, code: :auth_code_was_not_received)
112
- end
146
+ return Failure(status: 400, code: :auth_code_was_not_received) unless auth_code
113
147
 
114
148
  queries.get_tokens(auth_code, code_verifier.value).fmap do |tokens|
115
149
  access_token = tokens[:access_token]
@@ -131,17 +165,13 @@ module Hydra
131
165
  end
132
166
 
133
167
  def access_token(session_state)
134
- unless authenticated?(session_state)
135
- return Failure(status: 400, code: :not_authenticated)
136
- end
168
+ return Failure(status: 400, code: :not_authenticated) unless authenticated?(session_state)
137
169
 
138
170
  fetch_token(session_state, 'access_token')
139
171
  end
140
172
 
141
173
  def authorize!(session_state)
142
- unless authenticated?(session_state)
143
- return Failure(status: 400, code: :not_authenticated)
144
- end
174
+ return Failure(status: 400, code: :not_authenticated) unless authenticated?(session_state)
145
175
 
146
176
  access_token = yield fetch_token(session_state, 'access_token')
147
177
  if token_expired?(access_token)
@@ -154,13 +184,9 @@ module Hydra
154
184
  end
155
185
 
156
186
  def access_token_jti(session_state)
157
- unless authenticated?(session_state)
158
- return Failure(status: 400, code: :not_authenticated)
159
- end
187
+ return Failure(status: 400, code: :not_authenticated) unless authenticated?(session_state)
160
188
 
161
- fetch_token(session_state, 'access_token').fmap do |access_token|
162
- access_token.jti
163
- end
189
+ fetch_token(session_state, 'access_token').fmap(&:jti)
164
190
  end
165
191
 
166
192
  def logout!(session_state)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "digest"
4
- require "securerandom"
3
+ require 'digest'
4
+ require 'securerandom'
5
5
 
6
6
  require 'hydra/keycloak/container'
7
7
 
@@ -9,24 +9,24 @@ module Hydra
9
9
  module Keycloak
10
10
  class CodeVerifier
11
11
  attr_reader :value, :code_challenge
12
-
12
+
13
13
  def generate
14
14
  @value = _generate
15
15
  @code_challenge = _generate_pkce(@value)
16
16
  end
17
-
17
+
18
18
  private
19
-
19
+
20
20
  def _generate
21
21
  # https://datatracker.ietf.org/doc/html/rfc7636#section-4.1
22
22
  charset = Array('A'..'Z') + Array('a'..'z') + Array(0..9)
23
- charset.push("-").push(".").push("_").push("~")
23
+ charset.push('-').push('.').push('_').push('~')
24
24
  Array.new(128) { charset.sample }.join
25
25
  end
26
26
 
27
27
  def _generate_pkce(code_verifier)
28
28
  # https://datatracker.ietf.org/doc/html/rfc7636#section-4.6
29
- Digest::SHA256.base64digest(code_verifier).tr("+/", "-_").tr("=", "")
29
+ Digest::SHA256.base64digest(code_verifier).tr('+/', '-_').tr('=', '')
30
30
  end
31
31
  end
32
32
  end
@@ -19,11 +19,11 @@ module Hydra
19
19
 
20
20
  class << self
21
21
  def inject(target)
22
- -> *values { target.send(:include, Import[*values]) }
22
+ ->(*values) { target.send(:include, Import[*values]) }
23
23
  end
24
-
24
+
25
25
  def args_inject(target)
26
- -> *values { target.send(:include, Import.args[*values]) }
26
+ ->(*values) { target.send(:include, Import.args[*values]) }
27
27
  end
28
28
  end
29
29
 
@@ -31,11 +31,11 @@ module Hydra
31
31
  def container
32
32
  ::Hydra::Keycloak::Container
33
33
  end
34
-
34
+
35
35
  def inject
36
36
  ::Hydra::Keycloak.inject(self)
37
37
  end
38
-
38
+
39
39
  def args_inject
40
40
  ::Hydra::Keycloak.args_inject(self)
41
41
  end
@@ -19,8 +19,8 @@ module Hydra
19
19
 
20
20
  result.fmap do |tokens|
21
21
  {
22
- access_token: ::Hydra::Keycloak::Token.new(tokens['access_token']),
23
- id_token: ::Hydra::Keycloak::Token.new(tokens['id_token']),
22
+ access_token: ::Hydra::Keycloak::Token.new(tokens['access_token']),
23
+ id_token: ::Hydra::Keycloak::Token.new(tokens['id_token']),
24
24
  refresh_token: ::Hydra::Keycloak::Token.new(tokens['refresh_token'])
25
25
  }
26
26
  end
@@ -44,10 +44,10 @@ module Hydra
44
44
  Failure(status: 400, code: :token_refreshing_error)
45
45
  else
46
46
  Success({
47
- access_token: ::Hydra::Keycloak::Token.new(result['access_token']),
48
- id_token: ::Hydra::Keycloak::Token.new(result['id_token']),
49
- refresh_token: ::Hydra::Keycloak::Token.new(result['refresh_token'])
50
- })
47
+ access_token: ::Hydra::Keycloak::Token.new(result['access_token']),
48
+ id_token: ::Hydra::Keycloak::Token.new(result['id_token']),
49
+ refresh_token: ::Hydra::Keycloak::Token.new(result['refresh_token'])
50
+ })
51
51
  end
52
52
  end
53
53
  end
@@ -10,14 +10,14 @@ module Hydra
10
10
  class HttpClient
11
11
  include ::Dry::Monads[:result]
12
12
 
13
- NetworkErrors = [Timeout::Error,
14
- Errno::EINVAL,
15
- Errno::ECONNRESET,
16
- EOFError,
17
- Errno::ECONNREFUSED,
18
- Net::HTTPBadResponse,
19
- Net::HTTPHeaderSyntaxError,
20
- Net::ProtocolError]
13
+ NETWORK_ERRORS = [Timeout::Error,
14
+ Errno::EINVAL,
15
+ Errno::ECONNRESET,
16
+ EOFError,
17
+ Errno::ECONNREFUSED,
18
+ Net::HTTPBadResponse,
19
+ Net::HTTPHeaderSyntaxError,
20
+ Net::ProtocolError].freeze
21
21
 
22
22
  def do_post_request(path, body)
23
23
  response = Net::HTTP.post_form(URI(path), **body)
@@ -29,7 +29,7 @@ module Hydra
29
29
  else
30
30
  Failure(status: response.code, code: :bad_keycloak_response)
31
31
  end
32
- rescue *NetworkErrors
32
+ rescue *NETWORK_ERRORS
33
33
  Failure(status: 400, code: :keycloak_unavailable)
34
34
  rescue JSON::ParserError
35
35
  Failure(status: 400, code: :json_parser_error)
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'dalli'
5
+ rescue LoadError
6
+ raise LoadError, 'You must add `dalli` into your project`s Gemfile and launch bundle install'
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'redis'
5
+ rescue LoadError
6
+ raise LoadError, 'You must add `redis` into your project`s Gemfile and launch bundle install'
7
+ end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dalli'
4
3
  require 'dry/monads'
5
4
  require 'dry/auto_inject'
6
5
 
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'redis'
4
3
  require 'dry/monads'
5
4
  require 'dry/auto_inject'
6
5
 
@@ -19,7 +19,7 @@ module Hydra
19
19
  @data = ::JWT.decode(source, nil, false).first.transform_keys(&:to_sym)
20
20
  end
21
21
 
22
- %i(exp iat auth_time iss session_state scope jti).each do |field|
22
+ %i[exp iat auth_time iss session_state scope jti].each do |field|
23
23
  define_method(field) do
24
24
  @data.fetch(field)
25
25
  end
@@ -3,6 +3,8 @@
3
3
  module Hydra
4
4
  module Keycloak
5
5
  class Urls
6
+ DEFAULT_SCOPE = ['openid'].freeze
7
+
6
8
  def initialize(config)
7
9
  @config = config
8
10
  end
@@ -14,9 +16,9 @@ module Hydra
14
16
  "client_id=#{@config[:client_id]}&" \
15
17
  "redirect_uri=#{@config[:redirect_uri]}&" \
16
18
  "nonce=#{@config[:secret]}&" \
17
- 'scope=openid&' \
19
+ "scope=#{scope}&" \
18
20
  "code_challenge=#{code_challenge}&" \
19
- "code_challenge_method=S256"
21
+ 'code_challenge_method=S256'
20
22
  end
21
23
 
22
24
  def token_endpoint
@@ -59,9 +61,15 @@ module Hydra
59
61
  client_secret: @config[:secret],
60
62
  grant_type: 'refresh_token',
61
63
  refresh_token: refresh_token,
62
- scope: 'openid'
64
+ scope: scope
63
65
  }
64
66
  end
67
+
68
+ private
69
+
70
+ def scope
71
+ (DEFAULT_SCOPE + @config[:scope]).join('%20')
72
+ end
65
73
  end
66
74
  end
67
75
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Hydra
4
4
  module Keycloak
5
- VERSION = '0.1.8'
5
+ VERSION = '0.1.12'
6
6
  end
7
7
  end
data/run_tests.sh ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+
3
+ bundle exec rubocop --display-only-failed
4
+
5
+ bundle exec rspec
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.8
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fedor Kosolapov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-14 00:00:00.000000000 Z
11
+ date: 2022-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: dalli
28
+ name: dry-auto_inject
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: redis
42
+ name: dry-container
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -66,34 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: dry-auto_inject
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: dry-container
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :runtime
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
69
  - !ruby/object:Gem::Dependency
98
70
  name: dry-schema
99
71
  requirement: !ruby/object:Gem::Requirement
@@ -132,6 +104,9 @@ extensions: []
132
104
  extra_rdoc_files: []
133
105
  files:
134
106
  - ".github/workflows/main.yml"
107
+ - ".gitignore"
108
+ - ".rspec"
109
+ - ".rubocop.yml"
135
110
  - Dockerfile.test
136
111
  - Gemfile
137
112
  - Gemfile.lock
@@ -145,19 +120,22 @@ files:
145
120
  - lib/hydra/keycloak/container.rb
146
121
  - lib/hydra/keycloak/queries/gateway.rb
147
122
  - lib/hydra/keycloak/queries/http_client.rb
123
+ - lib/hydra/keycloak/store/adapters/memcached.rb
124
+ - lib/hydra/keycloak/store/adapters/redis.rb
148
125
  - lib/hydra/keycloak/store/gateway.rb
149
126
  - lib/hydra/keycloak/store/memcached_client.rb
150
127
  - lib/hydra/keycloak/store/redis_client.rb
151
128
  - lib/hydra/keycloak/token.rb
152
129
  - lib/hydra/keycloak/urls.rb
153
130
  - lib/hydra/keycloak/version.rb
154
- homepage: https://github.com/latera/hydra-keycloak-client
131
+ - run_tests.sh
132
+ homepage: https://github.com/hydra-billing/hydra-keycloak-client
155
133
  licenses: []
156
134
  metadata:
157
135
  allowed_push_host: https://rubygems.org
158
- homepage_uri: https://github.com/latera/hydra-keycloak-client
159
- source_code_uri: https://github.com/latera/hydra-keycloak-client
160
- changelog_uri: https://github.com/latera/hydra-keycloak-client
136
+ homepage_uri: https://github.com/hydra-billing/hydra-keycloak-client
137
+ source_code_uri: https://github.com/hydra-billing/hydra-keycloak-client
138
+ changelog_uri: https://github.com/hydra-billing/hydra-keycloak-client
161
139
  post_install_message:
162
140
  rdoc_options: []
163
141
  require_paths:
@@ -166,14 +144,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
166
144
  requirements:
167
145
  - - ">="
168
146
  - !ruby/object:Gem::Version
169
- version: 2.3.0
147
+ version: 2.5.8
170
148
  required_rubygems_version: !ruby/object:Gem::Requirement
171
149
  requirements:
172
150
  - - ">="
173
151
  - !ruby/object:Gem::Version
174
152
  version: '0'
175
153
  requirements: []
176
- rubygems_version: 3.2.3
154
+ rubygems_version: 3.1.6
177
155
  signing_key:
178
156
  specification_version: 4
179
157
  summary: Keycloak client for SSO