keycloak-admin 1.1.2 → 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 -0
- data/.github/workflows/ci.yml +80 -0
- data/.gitignore +9 -9
- data/.rspec +2 -2
- data/CHANGELOG.md +194 -183
- data/Dockerfile +12 -12
- data/Gemfile +3 -3
- data/Gemfile.lock +51 -49
- data/MIT-LICENSE +20 -20
- data/README.md +748 -482
- 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 +81 -0
- data/lib/keycloak-admin/client/client_authz_policy_client.rb +76 -0
- data/lib/keycloak-admin/client/client_authz_resource_client.rb +93 -0
- data/lib/keycloak-admin/client/client_authz_scope_client.rb +71 -0
- 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 -106
- 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 +34 -0
- data/lib/keycloak-admin/representation/client_authz_policy_config_representation.rb +15 -0
- data/lib/keycloak-admin/representation/client_authz_policy_representation.rb +27 -0
- data/lib/keycloak-admin/representation/client_authz_resource_representation.rb +26 -0
- data/lib/keycloak-admin/representation/client_authz_scope_representation.rb +17 -0
- 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 -75
- data/spec/client/attack_detection_client_spec.rb +102 -102
- data/spec/client/client_authz_permission_client_spec.rb +170 -0
- data/spec/client/client_authz_policy_client_spec.rb +170 -0
- data/spec/client/client_authz_resource_client_spec.rb +150 -0
- data/spec/client/client_authz_scope_client_spec.rb +134 -0
- 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 -0
- data/spec/representation/attack_detection_representation_spec.rb +15 -15
- data/spec/representation/client_authz_permission_representation_spec.rb +52 -0
- data/spec/representation/client_authz_policy_representation_spec.rb +47 -0
- data/spec/representation/client_authz_resource_representation_spec.rb +33 -0
- data/spec/representation/client_authz_scope_representation_spec.rb +19 -0
- 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 +29 -9
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
module KeycloakAdmin
|
|
2
|
-
class ClientClient < 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 get(id)
|
|
10
|
-
response = execute_http do
|
|
11
|
-
RestClient::Resource.new(clients_url(id), @configuration.rest_client_options).get(headers)
|
|
12
|
-
end
|
|
13
|
-
ClientRepresentation.from_hash(JSON.parse(response))
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def save(client_representation)
|
|
17
|
-
execute_http do
|
|
18
|
-
RestClient::Resource.new(clients_url, @configuration.rest_client_options).post(
|
|
19
|
-
create_payload(client_representation), headers
|
|
20
|
-
)
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def list
|
|
25
|
-
response = execute_http do
|
|
26
|
-
RestClient::Resource.new(clients_url, @configuration.rest_client_options).get(headers)
|
|
27
|
-
end
|
|
28
|
-
JSON.parse(response).map { |client_as_hash| ClientRepresentation.from_hash(client_as_hash) }
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def find_by_client_id(client_id)
|
|
32
|
-
list.find { |client| client.client_id == client_id }
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def delete(id)
|
|
36
|
-
execute_http do
|
|
37
|
-
RestClient::Resource.new(clients_url(id), @configuration.rest_client_options).delete(headers)
|
|
38
|
-
end
|
|
39
|
-
true
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def update(client_representation)
|
|
43
|
-
execute_http do
|
|
44
|
-
RestClient::Resource.new(clients_url(client_representation.id), @configuration.rest_client_options).put(
|
|
45
|
-
create_payload(client_representation), headers
|
|
46
|
-
)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
get(client_representation.id)
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
def get_service_account_user(client_id)
|
|
53
|
-
response = execute_http do
|
|
54
|
-
RestClient::Resource.new(service_account_user_url(client_id), @configuration.rest_client_options).get(headers)
|
|
55
|
-
end
|
|
56
|
-
UserRepresentation.from_hash(JSON.parse(response))
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def clients_url(id=nil)
|
|
60
|
-
if id
|
|
61
|
-
"#{@realm_client.realm_admin_url}/clients/#{id}"
|
|
62
|
-
else
|
|
63
|
-
"#{@realm_client.realm_admin_url}/clients"
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
def service_account_user_url(client_id)
|
|
68
|
-
"#{clients_url(client_id)}/service-account-user"
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
end
|
|
1
|
+
module KeycloakAdmin
|
|
2
|
+
class ClientClient < 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 get(id)
|
|
10
|
+
response = execute_http do
|
|
11
|
+
RestClient::Resource.new(clients_url(id), @configuration.rest_client_options).get(headers)
|
|
12
|
+
end
|
|
13
|
+
ClientRepresentation.from_hash(JSON.parse(response))
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def save(client_representation)
|
|
17
|
+
execute_http do
|
|
18
|
+
RestClient::Resource.new(clients_url, @configuration.rest_client_options).post(
|
|
19
|
+
create_payload(client_representation), headers
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def list
|
|
25
|
+
response = execute_http do
|
|
26
|
+
RestClient::Resource.new(clients_url, @configuration.rest_client_options).get(headers)
|
|
27
|
+
end
|
|
28
|
+
JSON.parse(response).map { |client_as_hash| ClientRepresentation.from_hash(client_as_hash) }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def find_by_client_id(client_id)
|
|
32
|
+
list.find { |client| client.client_id == client_id }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def delete(id)
|
|
36
|
+
execute_http do
|
|
37
|
+
RestClient::Resource.new(clients_url(id), @configuration.rest_client_options).delete(headers)
|
|
38
|
+
end
|
|
39
|
+
true
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def update(client_representation)
|
|
43
|
+
execute_http do
|
|
44
|
+
RestClient::Resource.new(clients_url(client_representation.id), @configuration.rest_client_options).put(
|
|
45
|
+
create_payload(client_representation), headers
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
get(client_representation.id)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def get_service_account_user(client_id)
|
|
53
|
+
response = execute_http do
|
|
54
|
+
RestClient::Resource.new(service_account_user_url(client_id), @configuration.rest_client_options).get(headers)
|
|
55
|
+
end
|
|
56
|
+
UserRepresentation.from_hash(JSON.parse(response))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def clients_url(id=nil)
|
|
60
|
+
if id
|
|
61
|
+
"#{@realm_client.realm_admin_url}/clients/#{id}"
|
|
62
|
+
else
|
|
63
|
+
"#{@realm_client.realm_admin_url}/clients"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def service_account_user_url(client_id)
|
|
68
|
+
"#{clients_url(client_id)}/service-account-user"
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
module KeycloakAdmin
|
|
2
|
-
class ClientRoleClient < 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 list(client_id)
|
|
10
|
-
response = execute_http do
|
|
11
|
-
RestClient::Resource.new(clients_url(client_id), @configuration.rest_client_options).get(headers)
|
|
12
|
-
end
|
|
13
|
-
JSON.parse(response).map { |role_as_hash| RoleRepresentation.from_hash(role_as_hash) }
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def clients_url(id)
|
|
17
|
-
"#{@realm_client.realm_admin_url}/clients/#{id}/roles"
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
1
|
+
module KeycloakAdmin
|
|
2
|
+
class ClientRoleClient < 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 list(client_id)
|
|
10
|
+
response = execute_http do
|
|
11
|
+
RestClient::Resource.new(clients_url(client_id), @configuration.rest_client_options).get(headers)
|
|
12
|
+
end
|
|
13
|
+
JSON.parse(response).map { |role_as_hash| RoleRepresentation.from_hash(role_as_hash) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def clients_url(id)
|
|
17
|
+
"#{@realm_client.realm_admin_url}/clients/#{id}/roles"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
module KeycloakAdmin
|
|
2
|
-
class ClientRoleMappingsClient < Client
|
|
3
|
-
def initialize(configuration, user_resource, client_id)
|
|
4
|
-
super(configuration)
|
|
5
|
-
@user_resource = user_resource
|
|
6
|
-
@client_id = client_id
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def list_available
|
|
10
|
-
response = execute_http do
|
|
11
|
-
RestClient::Resource.new(list_available_url, @configuration.rest_client_options).get(headers)
|
|
12
|
-
end
|
|
13
|
-
JSON.parse(response).map { |role_as_hash| RoleRepresentation.from_hash(role_as_hash) }
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def save(role_representation_list)
|
|
17
|
-
execute_http do
|
|
18
|
-
RestClient::Resource.new(base_url, @configuration.rest_client_options).post(
|
|
19
|
-
create_payload(role_representation_list), headers
|
|
20
|
-
)
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def list_available_url
|
|
25
|
-
"#{@user_resource.resource_url}/role-mappings/clients/#{@client_id}/available"
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def base_url
|
|
29
|
-
"#{@user_resource.resource_url}/role-mappings/clients/#{@client_id}"
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
end
|
|
1
|
+
module KeycloakAdmin
|
|
2
|
+
class ClientRoleMappingsClient < Client
|
|
3
|
+
def initialize(configuration, user_resource, client_id)
|
|
4
|
+
super(configuration)
|
|
5
|
+
@user_resource = user_resource
|
|
6
|
+
@client_id = client_id
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def list_available
|
|
10
|
+
response = execute_http do
|
|
11
|
+
RestClient::Resource.new(list_available_url, @configuration.rest_client_options).get(headers)
|
|
12
|
+
end
|
|
13
|
+
JSON.parse(response).map { |role_as_hash| RoleRepresentation.from_hash(role_as_hash) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def save(role_representation_list)
|
|
17
|
+
execute_http do
|
|
18
|
+
RestClient::Resource.new(base_url, @configuration.rest_client_options).post(
|
|
19
|
+
create_payload(role_representation_list), headers
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def list_available_url
|
|
25
|
+
"#{@user_resource.resource_url}/role-mappings/clients/#{@client_id}/available"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def base_url
|
|
29
|
+
"#{@user_resource.resource_url}/role-mappings/clients/#{@client_id}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
module KeycloakAdmin
|
|
2
|
-
class ConfigurableTokenClient < 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 token_url
|
|
10
|
-
"#{realm_url}/configurable-token"
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def realm_url
|
|
14
|
-
@realm_client.realm_url
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def exchange_with(user_access_token, token_lifespan_in_seconds)
|
|
18
|
-
response = execute_http do
|
|
19
|
-
RestClient::Request.execute(
|
|
20
|
-
@configuration.rest_client_options.merge(
|
|
21
|
-
method: :post,
|
|
22
|
-
url: token_url,
|
|
23
|
-
payload: { tokenLifespanInSeconds: token_lifespan_in_seconds }.to_json,
|
|
24
|
-
headers: {
|
|
25
|
-
Authorization: "Bearer #{user_access_token}",
|
|
26
|
-
content_type: :json,
|
|
27
|
-
accept: :json
|
|
28
|
-
}
|
|
29
|
-
)
|
|
30
|
-
)
|
|
31
|
-
end
|
|
32
|
-
TokenRepresentation.from_json(response.body)
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
1
|
+
module KeycloakAdmin
|
|
2
|
+
class ConfigurableTokenClient < 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 token_url
|
|
10
|
+
"#{realm_url}/configurable-token"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def realm_url
|
|
14
|
+
@realm_client.realm_url
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def exchange_with(user_access_token, token_lifespan_in_seconds)
|
|
18
|
+
response = execute_http do
|
|
19
|
+
RestClient::Request.execute(
|
|
20
|
+
@configuration.rest_client_options.merge(
|
|
21
|
+
method: :post,
|
|
22
|
+
url: token_url,
|
|
23
|
+
payload: { tokenLifespanInSeconds: token_lifespan_in_seconds }.to_json,
|
|
24
|
+
headers: {
|
|
25
|
+
Authorization: "Bearer #{user_access_token}",
|
|
26
|
+
content_type: :json,
|
|
27
|
+
accept: :json
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
TokenRepresentation.from_json(response.body)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -1,131 +1,148 @@
|
|
|
1
|
-
module KeycloakAdmin
|
|
2
|
-
class GroupClient < 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 get(group_id)
|
|
10
|
-
response = execute_http do
|
|
11
|
-
RestClient::Resource.new(groups_url(group_id), @configuration.rest_client_options).get(headers)
|
|
12
|
-
end
|
|
13
|
-
GroupRepresentation.from_hash(JSON.parse(response))
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def children(parent_id)
|
|
17
|
-
response = execute_http do
|
|
18
|
-
url = "#{groups_url(parent_id)}/children"
|
|
19
|
-
RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
|
|
20
|
-
end
|
|
21
|
-
JSON.parse(response).map { |group_as_hash| GroupRepresentation.from_hash(group_as_hash) }
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def list
|
|
25
|
-
search(nil)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def search(query)
|
|
29
|
-
derived_headers = case query
|
|
30
|
-
when String
|
|
31
|
-
headers.merge({params: { search: query }})
|
|
32
|
-
when Hash
|
|
33
|
-
headers.merge({params: query })
|
|
34
|
-
else
|
|
35
|
-
headers
|
|
36
|
-
end
|
|
37
|
-
response = execute_http do
|
|
38
|
-
RestClient::Resource.new(groups_url, @configuration.rest_client_options).get(derived_headers)
|
|
39
|
-
end
|
|
40
|
-
JSON.parse(response).map { |group_as_hash| GroupRepresentation.from_hash(group_as_hash) }
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def create!(name, path = nil, attributes = {})
|
|
44
|
-
response = save(build(name, path, attributes))
|
|
45
|
-
created_id(response)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def save(group_representation)
|
|
49
|
-
execute_http do
|
|
50
|
-
payload = create_payload(group_representation)
|
|
51
|
-
if group_representation.id
|
|
52
|
-
RestClient::Resource.new(groups_url(group_representation.id), @configuration.rest_client_options).put(payload, headers)
|
|
53
|
-
else
|
|
54
|
-
RestClient::Resource.new(groups_url, @configuration.rest_client_options).post(payload, headers)
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def create_subgroup!(parent_id, name, attributes = {})
|
|
60
|
-
url = "#{groups_url(parent_id)}/children"
|
|
61
|
-
response = execute_http do
|
|
62
|
-
RestClient::Resource.new(url, @configuration.rest_client_options).post(
|
|
63
|
-
create_payload(build(name, nil, attributes)), headers
|
|
64
|
-
)
|
|
65
|
-
end
|
|
66
|
-
created_id(response)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
def delete(group_id)
|
|
70
|
-
execute_http do
|
|
71
|
-
RestClient::Resource.new(groups_url(group_id), @configuration.rest_client_options).delete(headers)
|
|
72
|
-
end
|
|
73
|
-
true
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def members(group_id, first=0, max=100)
|
|
77
|
-
url = "#{groups_url(group_id)}/members"
|
|
78
|
-
query = {first: first.try(:to_i), max: max.try(:to_i)}.compact
|
|
79
|
-
unless query.empty?
|
|
80
|
-
query_string = query.to_a.map { |e| "#{e[0]}=#{e[1]}" }.join("&")
|
|
81
|
-
url = "#{url}?#{query_string}"
|
|
82
|
-
end
|
|
83
|
-
response = execute_http do
|
|
84
|
-
RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
|
|
85
|
-
end
|
|
86
|
-
JSON.parse(response).map { |user_as_hash| UserRepresentation.from_hash(user_as_hash) }
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
# Gets all realm-level roles for a group
|
|
90
|
-
def get_realm_level_roles(group_id)
|
|
91
|
-
url = "#{groups_url(group_id)}/role-mappings/realm"
|
|
92
|
-
response = execute_http do
|
|
93
|
-
RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
|
|
94
|
-
end
|
|
95
|
-
JSON.parse(response).map { |role_as_hash| RoleRepresentation.from_hash(role_as_hash) }
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
# Adds a realm-level role to a group via the role name
|
|
99
|
-
def add_realm_level_role_name!(group_id, role_name)
|
|
100
|
-
# creates a full role-representation object needed by the keycloak api to work
|
|
101
|
-
role_representation = RoleClient.new(@configuration, @realm_client).get(role_name)
|
|
102
|
-
url = "#{groups_url(group_id)}/role-mappings/realm"
|
|
103
|
-
response = execute_http do
|
|
104
|
-
RestClient::Resource.new(url, @configuration.rest_client_options).post(
|
|
105
|
-
create_payload([role_representation]), headers
|
|
106
|
-
)
|
|
107
|
-
end
|
|
108
|
-
role_representation
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
1
|
+
module KeycloakAdmin
|
|
2
|
+
class GroupClient < 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 get(group_id)
|
|
10
|
+
response = execute_http do
|
|
11
|
+
RestClient::Resource.new(groups_url(group_id), @configuration.rest_client_options).get(headers)
|
|
12
|
+
end
|
|
13
|
+
GroupRepresentation.from_hash(JSON.parse(response))
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def children(parent_id)
|
|
17
|
+
response = execute_http do
|
|
18
|
+
url = "#{groups_url(parent_id)}/children"
|
|
19
|
+
RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
|
|
20
|
+
end
|
|
21
|
+
JSON.parse(response).map { |group_as_hash| GroupRepresentation.from_hash(group_as_hash) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def list
|
|
25
|
+
search(nil)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def search(query)
|
|
29
|
+
derived_headers = case query
|
|
30
|
+
when String
|
|
31
|
+
headers.merge({params: { search: query }})
|
|
32
|
+
when Hash
|
|
33
|
+
headers.merge({params: query })
|
|
34
|
+
else
|
|
35
|
+
headers
|
|
36
|
+
end
|
|
37
|
+
response = execute_http do
|
|
38
|
+
RestClient::Resource.new(groups_url, @configuration.rest_client_options).get(derived_headers)
|
|
39
|
+
end
|
|
40
|
+
JSON.parse(response).map { |group_as_hash| GroupRepresentation.from_hash(group_as_hash) }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def create!(name, path = nil, attributes = {})
|
|
44
|
+
response = save(build(name, path, attributes))
|
|
45
|
+
created_id(response)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def save(group_representation)
|
|
49
|
+
execute_http do
|
|
50
|
+
payload = create_payload(group_representation)
|
|
51
|
+
if group_representation.id
|
|
52
|
+
RestClient::Resource.new(groups_url(group_representation.id), @configuration.rest_client_options).put(payload, headers)
|
|
53
|
+
else
|
|
54
|
+
RestClient::Resource.new(groups_url, @configuration.rest_client_options).post(payload, headers)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def create_subgroup!(parent_id, name, attributes = {})
|
|
60
|
+
url = "#{groups_url(parent_id)}/children"
|
|
61
|
+
response = execute_http do
|
|
62
|
+
RestClient::Resource.new(url, @configuration.rest_client_options).post(
|
|
63
|
+
create_payload(build(name, nil, attributes)), headers
|
|
64
|
+
)
|
|
65
|
+
end
|
|
66
|
+
created_id(response)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def delete(group_id)
|
|
70
|
+
execute_http do
|
|
71
|
+
RestClient::Resource.new(groups_url(group_id), @configuration.rest_client_options).delete(headers)
|
|
72
|
+
end
|
|
73
|
+
true
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def members(group_id, first=0, max=100)
|
|
77
|
+
url = "#{groups_url(group_id)}/members"
|
|
78
|
+
query = {first: first.try(:to_i), max: max.try(:to_i)}.compact
|
|
79
|
+
unless query.empty?
|
|
80
|
+
query_string = query.to_a.map { |e| "#{e[0]}=#{e[1]}" }.join("&")
|
|
81
|
+
url = "#{url}?#{query_string}"
|
|
82
|
+
end
|
|
83
|
+
response = execute_http do
|
|
84
|
+
RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
|
|
85
|
+
end
|
|
86
|
+
JSON.parse(response).map { |user_as_hash| UserRepresentation.from_hash(user_as_hash) }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Gets all realm-level roles for a group
|
|
90
|
+
def get_realm_level_roles(group_id)
|
|
91
|
+
url = "#{groups_url(group_id)}/role-mappings/realm"
|
|
92
|
+
response = execute_http do
|
|
93
|
+
RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
|
|
94
|
+
end
|
|
95
|
+
JSON.parse(response).map { |role_as_hash| RoleRepresentation.from_hash(role_as_hash) }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Adds a realm-level role to a group via the role name
|
|
99
|
+
def add_realm_level_role_name!(group_id, role_name)
|
|
100
|
+
# creates a full role-representation object needed by the keycloak api to work
|
|
101
|
+
role_representation = RoleClient.new(@configuration, @realm_client).get(role_name)
|
|
102
|
+
url = "#{groups_url(group_id)}/role-mappings/realm"
|
|
103
|
+
response = execute_http do
|
|
104
|
+
RestClient::Resource.new(url, @configuration.rest_client_options).post(
|
|
105
|
+
create_payload([role_representation]), headers
|
|
106
|
+
)
|
|
107
|
+
end
|
|
108
|
+
role_representation
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Remove a realm-level role from a group by the role name
|
|
112
|
+
def remove_realm_level_role_name!(group_id, role_name)
|
|
113
|
+
role_representation = RoleClient.new(@configuration, @realm_client).get(role_name)
|
|
114
|
+
url = "#{groups_url(group_id)}/role-mappings/realm"
|
|
115
|
+
execute_http do
|
|
116
|
+
RestClient::Request.execute(
|
|
117
|
+
@configuration.rest_client_options.merge(
|
|
118
|
+
url:,
|
|
119
|
+
method: :delete,
|
|
120
|
+
payload: create_payload([role_representation]),
|
|
121
|
+
headers: headers
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
end
|
|
125
|
+
true
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def groups_url(id=nil)
|
|
129
|
+
if id
|
|
130
|
+
"#{@realm_client.realm_admin_url}/groups/#{id}"
|
|
131
|
+
else
|
|
132
|
+
"#{@realm_client.realm_admin_url}/groups"
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
private
|
|
137
|
+
|
|
138
|
+
def build(name, path, attributes)
|
|
139
|
+
GroupRepresentation.from_hash(
|
|
140
|
+
{
|
|
141
|
+
"name" => name,
|
|
142
|
+
"path" => path,
|
|
143
|
+
"attributes" => attributes
|
|
144
|
+
}
|
|
145
|
+
)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|