keycloak-admin 1.1.3 → 1.1.4
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 +4 -4
- data/.github/workflows/Dockerfile +24 -24
- data/.github/workflows/ci.yml +80 -83
- data/.gitignore +9 -9
- data/.rspec +2 -2
- data/CHANGELOG.md +194 -188
- data/Dockerfile +12 -12
- data/Gemfile +3 -3
- data/Gemfile.lock +51 -49
- data/MIT-LICENSE +20 -20
- data/README.md +748 -748
- data/bin/console +9 -9
- data/keycloak-admin.gemspec +24 -24
- data/lib/keycloak-admin/client/attack_detection_client.rb +41 -41
- data/lib/keycloak-admin/client/client.rb +56 -56
- data/lib/keycloak-admin/client/client_authz_permission_client.rb +80 -80
- data/lib/keycloak-admin/client/client_authz_policy_client.rb +75 -75
- data/lib/keycloak-admin/client/client_authz_resource_client.rb +92 -92
- data/lib/keycloak-admin/client/client_authz_scope_client.rb +70 -70
- data/lib/keycloak-admin/client/client_client.rb +71 -71
- data/lib/keycloak-admin/client/client_role_client.rb +20 -20
- data/lib/keycloak-admin/client/client_role_mappings_client.rb +32 -32
- data/lib/keycloak-admin/client/configurable_token_client.rb +35 -35
- data/lib/keycloak-admin/client/group_client.rb +148 -131
- data/lib/keycloak-admin/client/identity_provider_client.rb +51 -51
- data/lib/keycloak-admin/client/realm_client.rb +122 -122
- data/lib/keycloak-admin/client/role_client.rb +59 -59
- data/lib/keycloak-admin/client/role_mapper_client.rb +47 -45
- data/lib/keycloak-admin/client/token_client.rb +29 -29
- data/lib/keycloak-admin/client/user_client.rb +266 -266
- data/lib/keycloak-admin/configuration.rb +52 -52
- data/lib/keycloak-admin/representation/attack_detection_representation.rb +17 -17
- data/lib/keycloak-admin/representation/camel_json.rb +12 -12
- data/lib/keycloak-admin/representation/client_authz_permission_representation.rb +33 -33
- data/lib/keycloak-admin/representation/client_authz_policy_config_representation.rb +14 -14
- data/lib/keycloak-admin/representation/client_authz_policy_representation.rb +26 -26
- data/lib/keycloak-admin/representation/client_authz_resource_representation.rb +25 -25
- data/lib/keycloak-admin/representation/client_authz_scope_representation.rb +16 -16
- data/lib/keycloak-admin/representation/client_representation.rb +71 -71
- data/lib/keycloak-admin/representation/credential_representation.rb +38 -38
- data/lib/keycloak-admin/representation/federated_identity_representation.rb +15 -15
- data/lib/keycloak-admin/representation/group_representation.rb +21 -21
- data/lib/keycloak-admin/representation/identity_provider_mapper_representation.rb +19 -19
- data/lib/keycloak-admin/representation/identity_provider_representation.rb +67 -67
- data/lib/keycloak-admin/representation/impersonation_redirection_representation.rb +16 -16
- data/lib/keycloak-admin/representation/impersonation_representation.rb +43 -43
- data/lib/keycloak-admin/representation/protocol_mapper_representation.rb +19 -19
- data/lib/keycloak-admin/representation/realm_representation.rb +14 -14
- data/lib/keycloak-admin/representation/representation.rb +23 -23
- data/lib/keycloak-admin/representation/role_representation.rb +19 -19
- data/lib/keycloak-admin/representation/session_representation.rb +22 -22
- data/lib/keycloak-admin/representation/token_representation.rb +39 -39
- data/lib/keycloak-admin/representation/user_representation.rb +47 -47
- data/lib/keycloak-admin/resource/base_role_containing_resource.rb +28 -28
- data/lib/keycloak-admin/resource/group_resource.rb +11 -11
- data/lib/keycloak-admin/resource/user_resource.rb +7 -7
- data/lib/keycloak-admin/version.rb +3 -3
- data/lib/keycloak-admin.rb +84 -84
- data/spec/client/attack_detection_client_spec.rb +102 -102
- data/spec/client/client_authz_permission_client_spec.rb +170 -170
- data/spec/client/client_authz_policy_client_spec.rb +169 -169
- data/spec/client/client_authz_resource_client_spec.rb +150 -150
- data/spec/client/client_authz_scope_client_spec.rb +133 -133
- data/spec/client/client_client_spec.rb +133 -133
- data/spec/client/client_role_mappings_client_spec.rb +82 -82
- data/spec/client/client_spec.rb +28 -28
- data/spec/client/configurable_token_client_spec.rb +34 -34
- data/spec/client/group_client_spec.rb +328 -258
- data/spec/client/identity_provider_client_spec.rb +92 -92
- data/spec/client/realm_client_spec.rb +155 -155
- data/spec/client/role_client_spec.rb +79 -79
- data/spec/client/role_mapper_client_spec.rb +113 -68
- data/spec/client/token_client_spec.rb +68 -68
- data/spec/client/user_client_spec.rb +373 -373
- data/spec/configuration_spec.rb +113 -113
- data/spec/integration/client_authorization_spec.rb +93 -95
- data/spec/representation/attack_detection_representation_spec.rb +15 -15
- data/spec/representation/client_authz_permission_representation_spec.rb +52 -52
- data/spec/representation/client_authz_policy_representation_spec.rb +46 -46
- data/spec/representation/client_authz_resource_representation_spec.rb +33 -33
- data/spec/representation/client_authz_scope_representation_spec.rb +18 -18
- data/spec/representation/client_representation_spec.rb +119 -119
- data/spec/representation/group_representation_spec.rb +22 -22
- data/spec/representation/identity_provider_mapper_representation_spec.rb +24 -24
- data/spec/representation/identity_provider_representation_spec.rb +113 -113
- data/spec/representation/impersonation_representation_spec.rb +163 -163
- data/spec/representation/protocol_mapper_representation_spec.rb +57 -57
- data/spec/representation/role_representation_spec.rb +37 -37
- data/spec/representation/session_representation_spec.rb +15 -15
- data/spec/representation/user_representation_spec.rb +15 -15
- data/spec/resource/group_resource_spec.rb +14 -14
- data/spec/resource/user_resource_spec.rb +14 -14
- data/spec/spec_helper.rb +37 -37
- metadata +10 -10
data/bin/console
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
require "bundler/setup"
|
|
5
|
-
require "keycloak-admin"
|
|
6
|
-
require "byebug"
|
|
7
|
-
|
|
8
|
-
require "irb"
|
|
9
|
-
IRB.start
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bundler/setup"
|
|
5
|
+
require "keycloak-admin"
|
|
6
|
+
require "byebug"
|
|
7
|
+
|
|
8
|
+
require "irb"
|
|
9
|
+
IRB.start
|
data/keycloak-admin.gemspec
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
$:.push File.expand_path("../lib", __FILE__)
|
|
2
|
-
|
|
3
|
-
require "keycloak-admin/version"
|
|
4
|
-
|
|
5
|
-
Gem::Specification.new do |spec|
|
|
6
|
-
spec.name = "keycloak-admin"
|
|
7
|
-
spec.version = KeycloakAdmin::VERSION
|
|
8
|
-
spec.authors = ["Lorent Lempereur"]
|
|
9
|
-
spec.email = ["lorent.lempereur.dev@gmail.com"]
|
|
10
|
-
spec.homepage = "https://github.com/looorent/keycloak-admin-ruby"
|
|
11
|
-
spec.summary = "Keycloak Admin REST API client written in Ruby"
|
|
12
|
-
spec.description = "Keycloak Admin REST API client written in Ruby"
|
|
13
|
-
spec.license = "MIT"
|
|
14
|
-
|
|
15
|
-
spec.files = `git ls-files -z`.split("\x0")
|
|
16
|
-
spec.require_paths = ["lib"]
|
|
17
|
-
|
|
18
|
-
spec.required_ruby_version = '>= 2.3'
|
|
19
|
-
|
|
20
|
-
spec.add_dependency "http-cookie", "~> 1.0", ">= 1.0.3"
|
|
21
|
-
spec.add_dependency "rest-client", "~> 2.0"
|
|
22
|
-
spec.add_development_dependency "rspec", "3.
|
|
23
|
-
spec.add_development_dependency "byebug", "
|
|
24
|
-
end
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
|
2
|
+
|
|
3
|
+
require "keycloak-admin/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "keycloak-admin"
|
|
7
|
+
spec.version = KeycloakAdmin::VERSION
|
|
8
|
+
spec.authors = ["Lorent Lempereur"]
|
|
9
|
+
spec.email = ["lorent.lempereur.dev@gmail.com"]
|
|
10
|
+
spec.homepage = "https://github.com/looorent/keycloak-admin-ruby"
|
|
11
|
+
spec.summary = "Keycloak Admin REST API client written in Ruby"
|
|
12
|
+
spec.description = "Keycloak Admin REST API client written in Ruby"
|
|
13
|
+
spec.license = "MIT"
|
|
14
|
+
|
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
16
|
+
spec.require_paths = ["lib"]
|
|
17
|
+
|
|
18
|
+
spec.required_ruby_version = '>= 2.3'
|
|
19
|
+
|
|
20
|
+
spec.add_dependency "http-cookie", "~> 1.0", ">= 1.0.3"
|
|
21
|
+
spec.add_dependency "rest-client", "~> 2.0"
|
|
22
|
+
spec.add_development_dependency "rspec", "3.13.2"
|
|
23
|
+
spec.add_development_dependency "byebug", "12.0.0"
|
|
24
|
+
end
|
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
module KeycloakAdmin
|
|
2
|
-
class AttackDetectionClient < Client
|
|
3
|
-
def initialize(configuration, realm_client)
|
|
4
|
-
super(configuration)
|
|
5
|
-
raise ArgumentError.new("realm must be defined") unless realm_client.name_defined?
|
|
6
|
-
@realm_client = realm_client
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def lock_status(user_id)
|
|
10
|
-
raise ArgumentError.new("user_id must be defined") if user_id.nil?
|
|
11
|
-
|
|
12
|
-
response = execute_http do
|
|
13
|
-
RestClient::Resource.new(brute_force_url(user_id), @configuration.rest_client_options).get(headers)
|
|
14
|
-
end
|
|
15
|
-
AttackDetectionRepresentation.from_hash(JSON.parse(response))
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def unlock_user(user_id)
|
|
19
|
-
raise ArgumentError.new("user_id must be defined") if user_id.nil?
|
|
20
|
-
|
|
21
|
-
execute_http do
|
|
22
|
-
RestClient::Resource.new(brute_force_url(user_id), @configuration.rest_client_options).delete(headers)
|
|
23
|
-
end
|
|
24
|
-
true
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def unlock_users
|
|
28
|
-
execute_http do
|
|
29
|
-
RestClient::Resource.new(brute_force_url, @configuration.rest_client_options).delete(headers)
|
|
30
|
-
end
|
|
31
|
-
true
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def brute_force_url(user_id = nil)
|
|
35
|
-
if user_id
|
|
36
|
-
"#{@realm_client.realm_admin_url}/attack-detection/brute-force/users/#{user_id}"
|
|
37
|
-
else
|
|
38
|
-
"#{@realm_client.realm_admin_url}/attack-detection/brute-force/users"
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
1
|
+
module KeycloakAdmin
|
|
2
|
+
class AttackDetectionClient < Client
|
|
3
|
+
def initialize(configuration, realm_client)
|
|
4
|
+
super(configuration)
|
|
5
|
+
raise ArgumentError.new("realm must be defined") unless realm_client.name_defined?
|
|
6
|
+
@realm_client = realm_client
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def lock_status(user_id)
|
|
10
|
+
raise ArgumentError.new("user_id must be defined") if user_id.nil?
|
|
11
|
+
|
|
12
|
+
response = execute_http do
|
|
13
|
+
RestClient::Resource.new(brute_force_url(user_id), @configuration.rest_client_options).get(headers)
|
|
14
|
+
end
|
|
15
|
+
AttackDetectionRepresentation.from_hash(JSON.parse(response))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def unlock_user(user_id)
|
|
19
|
+
raise ArgumentError.new("user_id must be defined") if user_id.nil?
|
|
20
|
+
|
|
21
|
+
execute_http do
|
|
22
|
+
RestClient::Resource.new(brute_force_url(user_id), @configuration.rest_client_options).delete(headers)
|
|
23
|
+
end
|
|
24
|
+
true
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def unlock_users
|
|
28
|
+
execute_http do
|
|
29
|
+
RestClient::Resource.new(brute_force_url, @configuration.rest_client_options).delete(headers)
|
|
30
|
+
end
|
|
31
|
+
true
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def brute_force_url(user_id = nil)
|
|
35
|
+
if user_id
|
|
36
|
+
"#{@realm_client.realm_admin_url}/attack-detection/brute-force/users/#{user_id}"
|
|
37
|
+
else
|
|
38
|
+
"#{@realm_client.realm_admin_url}/attack-detection/brute-force/users"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
42
|
end
|
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
module KeycloakAdmin
|
|
2
|
-
class Client
|
|
3
|
-
|
|
4
|
-
def initialize(configuration)
|
|
5
|
-
@configuration = configuration
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
def server_url
|
|
9
|
-
@configuration.server_url
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def current_token
|
|
13
|
-
@current_token ||= KeycloakAdmin.create_client(@configuration, @configuration.client_realm_name).token.get
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def headers
|
|
17
|
-
{
|
|
18
|
-
Authorization: "Bearer #{current_token.access_token}",
|
|
19
|
-
content_type: :json,
|
|
20
|
-
accept: :json
|
|
21
|
-
}
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def execute_http
|
|
25
|
-
yield
|
|
26
|
-
rescue RestClient::Exceptions::Timeout => e
|
|
27
|
-
raise
|
|
28
|
-
rescue RestClient::ExceptionWithResponse => e
|
|
29
|
-
http_error(e.response)
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def created_id(response)
|
|
33
|
-
unless response.net_http_res.is_a? Net::HTTPCreated
|
|
34
|
-
raise "Create method returned status #{response.net_http_res.message} (Code: #{response.net_http_res.code}); expected status: Created (201)"
|
|
35
|
-
end
|
|
36
|
-
(_head, _separator, id) = response.headers[:location].rpartition("/")
|
|
37
|
-
id
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def create_payload(value)
|
|
41
|
-
if value.nil?
|
|
42
|
-
""
|
|
43
|
-
elsif value.kind_of?(Array)
|
|
44
|
-
"[#{value.map(&:to_json) * ","}]"
|
|
45
|
-
else
|
|
46
|
-
value.to_json
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
private
|
|
51
|
-
|
|
52
|
-
def http_error(response)
|
|
53
|
-
raise "Keycloak: The request failed with response code #{response.code} and message: #{response.body}"
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|
|
1
|
+
module KeycloakAdmin
|
|
2
|
+
class Client
|
|
3
|
+
|
|
4
|
+
def initialize(configuration)
|
|
5
|
+
@configuration = configuration
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def server_url
|
|
9
|
+
@configuration.server_url
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def current_token
|
|
13
|
+
@current_token ||= KeycloakAdmin.create_client(@configuration, @configuration.client_realm_name).token.get
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def headers
|
|
17
|
+
{
|
|
18
|
+
Authorization: "Bearer #{current_token.access_token}",
|
|
19
|
+
content_type: :json,
|
|
20
|
+
accept: :json
|
|
21
|
+
}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def execute_http
|
|
25
|
+
yield
|
|
26
|
+
rescue RestClient::Exceptions::Timeout => e
|
|
27
|
+
raise
|
|
28
|
+
rescue RestClient::ExceptionWithResponse => e
|
|
29
|
+
http_error(e.response)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def created_id(response)
|
|
33
|
+
unless response.net_http_res.is_a? Net::HTTPCreated
|
|
34
|
+
raise "Create method returned status #{response.net_http_res.message} (Code: #{response.net_http_res.code}); expected status: Created (201)"
|
|
35
|
+
end
|
|
36
|
+
(_head, _separator, id) = response.headers[:location].rpartition("/")
|
|
37
|
+
id
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def create_payload(value)
|
|
41
|
+
if value.nil?
|
|
42
|
+
""
|
|
43
|
+
elsif value.kind_of?(Array)
|
|
44
|
+
"[#{value.map(&:to_json) * ","}]"
|
|
45
|
+
else
|
|
46
|
+
value.to_json
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def http_error(response)
|
|
53
|
+
raise "Keycloak: The request failed with response code #{response.code} and message: #{response.body}"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
module KeycloakAdmin
|
|
2
|
-
class ClientAuthzPermissionClient < Client
|
|
3
|
-
def initialize(configuration, realm_client, client_id, type, resource_id = nil)
|
|
4
|
-
super(configuration)
|
|
5
|
-
raise ArgumentError.new("realm must be defined") unless realm_client.name_defined?
|
|
6
|
-
raise ArgumentError.new("bad permission type") if !resource_id && !%i[resource scope].include?(type.to_sym)
|
|
7
|
-
|
|
8
|
-
@realm_client = realm_client
|
|
9
|
-
@client_id = client_id
|
|
10
|
-
@type = type
|
|
11
|
-
@resource_id = resource_id
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def delete(permission_id)
|
|
15
|
-
execute_http do
|
|
16
|
-
RestClient::Resource.new(authz_permission_url(@client_id, nil, nil, permission_id), @configuration.rest_client_options).delete(headers)
|
|
17
|
-
end
|
|
18
|
-
true
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def find_by(name, resource, scope = nil)
|
|
22
|
-
response = execute_http do
|
|
23
|
-
url = "#{authz_permission_url(@client_id)}?name=#{name}&resource=#{resource}&type=#{@type}&scope=#{scope}&deep=true&first=0&max=100"
|
|
24
|
-
RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
|
|
25
|
-
end
|
|
26
|
-
JSON.parse(response).map { |role_as_hash| ClientAuthzPermissionRepresentation.from_hash(role_as_hash) }
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def create!(name, description, decision_strategy,logic = "POSITIVE", resources = [], policies = [], scopes = [], resource_type = nil)
|
|
30
|
-
response = save(build(name, description, decision_strategy, logic, resources, policies, scopes, resource_type))
|
|
31
|
-
ClientAuthzPermissionRepresentation.from_hash(JSON.parse(response))
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def save(permission_representation)
|
|
35
|
-
execute_http do
|
|
36
|
-
RestClient::Resource.new(authz_permission_url(@client_id, nil, permission_representation.type), @configuration.rest_client_options).post(
|
|
37
|
-
create_payload(permission_representation), headers
|
|
38
|
-
)
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def list
|
|
43
|
-
response = execute_http do
|
|
44
|
-
RestClient::Resource.new(authz_permission_url(@client_id, @resource_id), @configuration.rest_client_options).get(headers)
|
|
45
|
-
end
|
|
46
|
-
JSON.parse(response).map { |role_as_hash| ClientAuthzPermissionRepresentation.from_hash(role_as_hash) }
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def get(permission_id)
|
|
50
|
-
response = execute_http do
|
|
51
|
-
RestClient::Resource.new(authz_permission_url(@client_id, nil, @type, permission_id), @configuration.rest_client_options).get(headers)
|
|
52
|
-
end
|
|
53
|
-
ClientAuthzPermissionRepresentation.from_hash(JSON.parse(response))
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def authz_permission_url(client_id, resource_id = nil, type = nil, id = nil)
|
|
57
|
-
if resource_id
|
|
58
|
-
"#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/resource/#{resource_id}/permissions"
|
|
59
|
-
elsif id
|
|
60
|
-
"#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/permission/#{type}/#{id}"
|
|
61
|
-
else
|
|
62
|
-
"#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/permission/#{type}"
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def build(name, description, decision_strategy, logic, resources, policies, scopes, resource_type)
|
|
67
|
-
policy = ClientAuthzPermissionRepresentation.new
|
|
68
|
-
policy.name = name
|
|
69
|
-
policy.description = description
|
|
70
|
-
policy.type = @type
|
|
71
|
-
policy.decision_strategy = decision_strategy
|
|
72
|
-
policy.resource_type = resource_type
|
|
73
|
-
policy.resources = resources
|
|
74
|
-
policy.policies = policies
|
|
75
|
-
policy.scopes = scopes
|
|
76
|
-
policy.logic = logic
|
|
77
|
-
policy
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
end
|
|
1
|
+
module KeycloakAdmin
|
|
2
|
+
class ClientAuthzPermissionClient < Client
|
|
3
|
+
def initialize(configuration, realm_client, client_id, type, resource_id = nil)
|
|
4
|
+
super(configuration)
|
|
5
|
+
raise ArgumentError.new("realm must be defined") unless realm_client.name_defined?
|
|
6
|
+
raise ArgumentError.new("bad permission type") if !resource_id && !%i[resource scope].include?(type.to_sym)
|
|
7
|
+
|
|
8
|
+
@realm_client = realm_client
|
|
9
|
+
@client_id = client_id
|
|
10
|
+
@type = type
|
|
11
|
+
@resource_id = resource_id
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def delete(permission_id)
|
|
15
|
+
execute_http do
|
|
16
|
+
RestClient::Resource.new(authz_permission_url(@client_id, nil, nil, permission_id), @configuration.rest_client_options).delete(headers)
|
|
17
|
+
end
|
|
18
|
+
true
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def find_by(name, resource, scope = nil)
|
|
22
|
+
response = execute_http do
|
|
23
|
+
url = "#{authz_permission_url(@client_id)}?name=#{name}&resource=#{resource}&type=#{@type}&scope=#{scope}&deep=true&first=0&max=100"
|
|
24
|
+
RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
|
|
25
|
+
end
|
|
26
|
+
JSON.parse(response).map { |role_as_hash| ClientAuthzPermissionRepresentation.from_hash(role_as_hash) }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def create!(name, description, decision_strategy,logic = "POSITIVE", resources = [], policies = [], scopes = [], resource_type = nil)
|
|
30
|
+
response = save(build(name, description, decision_strategy, logic, resources, policies, scopes, resource_type))
|
|
31
|
+
ClientAuthzPermissionRepresentation.from_hash(JSON.parse(response))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def save(permission_representation)
|
|
35
|
+
execute_http do
|
|
36
|
+
RestClient::Resource.new(authz_permission_url(@client_id, nil, permission_representation.type), @configuration.rest_client_options).post(
|
|
37
|
+
create_payload(permission_representation), headers
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def list
|
|
43
|
+
response = execute_http do
|
|
44
|
+
RestClient::Resource.new(authz_permission_url(@client_id, @resource_id), @configuration.rest_client_options).get(headers)
|
|
45
|
+
end
|
|
46
|
+
JSON.parse(response).map { |role_as_hash| ClientAuthzPermissionRepresentation.from_hash(role_as_hash) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def get(permission_id)
|
|
50
|
+
response = execute_http do
|
|
51
|
+
RestClient::Resource.new(authz_permission_url(@client_id, nil, @type, permission_id), @configuration.rest_client_options).get(headers)
|
|
52
|
+
end
|
|
53
|
+
ClientAuthzPermissionRepresentation.from_hash(JSON.parse(response))
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def authz_permission_url(client_id, resource_id = nil, type = nil, id = nil)
|
|
57
|
+
if resource_id
|
|
58
|
+
"#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/resource/#{resource_id}/permissions"
|
|
59
|
+
elsif id
|
|
60
|
+
"#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/permission/#{type}/#{id}"
|
|
61
|
+
else
|
|
62
|
+
"#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/permission/#{type}"
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def build(name, description, decision_strategy, logic, resources, policies, scopes, resource_type)
|
|
67
|
+
policy = ClientAuthzPermissionRepresentation.new
|
|
68
|
+
policy.name = name
|
|
69
|
+
policy.description = description
|
|
70
|
+
policy.type = @type
|
|
71
|
+
policy.decision_strategy = decision_strategy
|
|
72
|
+
policy.resource_type = resource_type
|
|
73
|
+
policy.resources = resources
|
|
74
|
+
policy.policies = policies
|
|
75
|
+
policy.scopes = scopes
|
|
76
|
+
policy.logic = logic
|
|
77
|
+
policy
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
|
81
81
|
end
|
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
module KeycloakAdmin
|
|
2
|
-
class ClientAuthzPolicyClient < Client
|
|
3
|
-
def initialize(configuration, realm_client, client_id, type)
|
|
4
|
-
super(configuration)
|
|
5
|
-
raise ArgumentError.new("realm must be defined") unless realm_client.name_defined?
|
|
6
|
-
raise ArgumentError.new("type must be defined") unless type
|
|
7
|
-
raise ArgumentError.new("only 'role' policies supported") unless type.to_sym == :role
|
|
8
|
-
|
|
9
|
-
@realm_client = realm_client
|
|
10
|
-
@client_id = client_id
|
|
11
|
-
@type = type
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def create!(name, description, type, logic, decision_strategy, fetch_roles, roles)
|
|
15
|
-
response = save(build(name, description, type, logic, decision_strategy, fetch_roles, roles))
|
|
16
|
-
ClientAuthzPolicyRepresentation.from_hash(JSON.parse(response))
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def save(policy_representation)
|
|
20
|
-
execute_http do
|
|
21
|
-
RestClient::Resource.new(authz_policy_url(@client_id, @type), @configuration.rest_client_options).post(
|
|
22
|
-
create_payload(policy_representation), headers
|
|
23
|
-
)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def get(policy_id)
|
|
28
|
-
response = execute_http do
|
|
29
|
-
RestClient::Resource.new(authz_policy_url(@client_id, @type, policy_id), @configuration.rest_client_options).get(headers)
|
|
30
|
-
end
|
|
31
|
-
ClientAuthzPolicyRepresentation.from_hash(JSON.parse(response))
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def find_by(name, type)
|
|
35
|
-
response = execute_http do
|
|
36
|
-
url = "#{authz_policy_url(@client_id, @type)}?permission=false&name=#{name}&type=#{type}&first=0&max=11"
|
|
37
|
-
RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
|
|
38
|
-
end
|
|
39
|
-
JSON.parse(response).map { |role_as_hash| ClientAuthzPolicyRepresentation.from_hash(role_as_hash) }
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def delete(policy_id)
|
|
43
|
-
execute_http do
|
|
44
|
-
RestClient::Resource.new(authz_policy_url(@client_id, @type, policy_id), @configuration.rest_client_options).delete(headers)
|
|
45
|
-
end
|
|
46
|
-
true
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def list
|
|
50
|
-
response = execute_http do
|
|
51
|
-
RestClient::Resource.new(authz_policy_url(@client_id, @type), @configuration.rest_client_options).get(headers)
|
|
52
|
-
end
|
|
53
|
-
JSON.parse(response).map { |role_as_hash| ClientAuthzPolicyRepresentation.from_hash(role_as_hash) }
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def authz_policy_url(client_id, type, id = nil)
|
|
57
|
-
if id
|
|
58
|
-
"#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/policy/#{type}/#{id}"
|
|
59
|
-
else
|
|
60
|
-
"#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/policy/#{type}?permission=false"
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def build(name, description, type, logic, decision_strategy, fetch_roles, roles=[])
|
|
65
|
-
policy = ClientAuthzPolicyRepresentation.new
|
|
66
|
-
policy.name = name
|
|
67
|
-
policy.description = description
|
|
68
|
-
policy.type = type
|
|
69
|
-
policy.logic = logic
|
|
70
|
-
policy.decision_strategy = decision_strategy
|
|
71
|
-
policy.fetch_roles = fetch_roles
|
|
72
|
-
policy.roles = roles
|
|
73
|
-
policy
|
|
74
|
-
end
|
|
75
|
-
end
|
|
1
|
+
module KeycloakAdmin
|
|
2
|
+
class ClientAuthzPolicyClient < Client
|
|
3
|
+
def initialize(configuration, realm_client, client_id, type)
|
|
4
|
+
super(configuration)
|
|
5
|
+
raise ArgumentError.new("realm must be defined") unless realm_client.name_defined?
|
|
6
|
+
raise ArgumentError.new("type must be defined") unless type
|
|
7
|
+
raise ArgumentError.new("only 'role' policies supported") unless type.to_sym == :role
|
|
8
|
+
|
|
9
|
+
@realm_client = realm_client
|
|
10
|
+
@client_id = client_id
|
|
11
|
+
@type = type
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def create!(name, description, type, logic, decision_strategy, fetch_roles, roles)
|
|
15
|
+
response = save(build(name, description, type, logic, decision_strategy, fetch_roles, roles))
|
|
16
|
+
ClientAuthzPolicyRepresentation.from_hash(JSON.parse(response))
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def save(policy_representation)
|
|
20
|
+
execute_http do
|
|
21
|
+
RestClient::Resource.new(authz_policy_url(@client_id, @type), @configuration.rest_client_options).post(
|
|
22
|
+
create_payload(policy_representation), headers
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def get(policy_id)
|
|
28
|
+
response = execute_http do
|
|
29
|
+
RestClient::Resource.new(authz_policy_url(@client_id, @type, policy_id), @configuration.rest_client_options).get(headers)
|
|
30
|
+
end
|
|
31
|
+
ClientAuthzPolicyRepresentation.from_hash(JSON.parse(response))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def find_by(name, type)
|
|
35
|
+
response = execute_http do
|
|
36
|
+
url = "#{authz_policy_url(@client_id, @type)}?permission=false&name=#{name}&type=#{type}&first=0&max=11"
|
|
37
|
+
RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
|
|
38
|
+
end
|
|
39
|
+
JSON.parse(response).map { |role_as_hash| ClientAuthzPolicyRepresentation.from_hash(role_as_hash) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def delete(policy_id)
|
|
43
|
+
execute_http do
|
|
44
|
+
RestClient::Resource.new(authz_policy_url(@client_id, @type, policy_id), @configuration.rest_client_options).delete(headers)
|
|
45
|
+
end
|
|
46
|
+
true
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def list
|
|
50
|
+
response = execute_http do
|
|
51
|
+
RestClient::Resource.new(authz_policy_url(@client_id, @type), @configuration.rest_client_options).get(headers)
|
|
52
|
+
end
|
|
53
|
+
JSON.parse(response).map { |role_as_hash| ClientAuthzPolicyRepresentation.from_hash(role_as_hash) }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def authz_policy_url(client_id, type, id = nil)
|
|
57
|
+
if id
|
|
58
|
+
"#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/policy/#{type}/#{id}"
|
|
59
|
+
else
|
|
60
|
+
"#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/policy/#{type}?permission=false"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def build(name, description, type, logic, decision_strategy, fetch_roles, roles=[])
|
|
65
|
+
policy = ClientAuthzPolicyRepresentation.new
|
|
66
|
+
policy.name = name
|
|
67
|
+
policy.description = description
|
|
68
|
+
policy.type = type
|
|
69
|
+
policy.logic = logic
|
|
70
|
+
policy.decision_strategy = decision_strategy
|
|
71
|
+
policy.fetch_roles = fetch_roles
|
|
72
|
+
policy.roles = roles
|
|
73
|
+
policy
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
76
|
end
|