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.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/Dockerfile +24 -0
  3. data/.github/workflows/ci.yml +80 -0
  4. data/.gitignore +9 -9
  5. data/.rspec +2 -2
  6. data/CHANGELOG.md +194 -183
  7. data/Dockerfile +12 -12
  8. data/Gemfile +3 -3
  9. data/Gemfile.lock +51 -49
  10. data/MIT-LICENSE +20 -20
  11. data/README.md +748 -482
  12. data/bin/console +9 -9
  13. data/keycloak-admin.gemspec +24 -24
  14. data/lib/keycloak-admin/client/attack_detection_client.rb +41 -41
  15. data/lib/keycloak-admin/client/client.rb +56 -56
  16. data/lib/keycloak-admin/client/client_authz_permission_client.rb +81 -0
  17. data/lib/keycloak-admin/client/client_authz_policy_client.rb +76 -0
  18. data/lib/keycloak-admin/client/client_authz_resource_client.rb +93 -0
  19. data/lib/keycloak-admin/client/client_authz_scope_client.rb +71 -0
  20. data/lib/keycloak-admin/client/client_client.rb +71 -71
  21. data/lib/keycloak-admin/client/client_role_client.rb +20 -20
  22. data/lib/keycloak-admin/client/client_role_mappings_client.rb +32 -32
  23. data/lib/keycloak-admin/client/configurable_token_client.rb +35 -35
  24. data/lib/keycloak-admin/client/group_client.rb +148 -131
  25. data/lib/keycloak-admin/client/identity_provider_client.rb +51 -51
  26. data/lib/keycloak-admin/client/realm_client.rb +122 -106
  27. data/lib/keycloak-admin/client/role_client.rb +59 -59
  28. data/lib/keycloak-admin/client/role_mapper_client.rb +47 -45
  29. data/lib/keycloak-admin/client/token_client.rb +29 -29
  30. data/lib/keycloak-admin/client/user_client.rb +266 -266
  31. data/lib/keycloak-admin/configuration.rb +52 -52
  32. data/lib/keycloak-admin/representation/attack_detection_representation.rb +17 -17
  33. data/lib/keycloak-admin/representation/camel_json.rb +12 -12
  34. data/lib/keycloak-admin/representation/client_authz_permission_representation.rb +34 -0
  35. data/lib/keycloak-admin/representation/client_authz_policy_config_representation.rb +15 -0
  36. data/lib/keycloak-admin/representation/client_authz_policy_representation.rb +27 -0
  37. data/lib/keycloak-admin/representation/client_authz_resource_representation.rb +26 -0
  38. data/lib/keycloak-admin/representation/client_authz_scope_representation.rb +17 -0
  39. data/lib/keycloak-admin/representation/client_representation.rb +71 -71
  40. data/lib/keycloak-admin/representation/credential_representation.rb +38 -38
  41. data/lib/keycloak-admin/representation/federated_identity_representation.rb +15 -15
  42. data/lib/keycloak-admin/representation/group_representation.rb +21 -21
  43. data/lib/keycloak-admin/representation/identity_provider_mapper_representation.rb +19 -19
  44. data/lib/keycloak-admin/representation/identity_provider_representation.rb +67 -67
  45. data/lib/keycloak-admin/representation/impersonation_redirection_representation.rb +16 -16
  46. data/lib/keycloak-admin/representation/impersonation_representation.rb +43 -43
  47. data/lib/keycloak-admin/representation/protocol_mapper_representation.rb +19 -19
  48. data/lib/keycloak-admin/representation/realm_representation.rb +14 -14
  49. data/lib/keycloak-admin/representation/representation.rb +23 -23
  50. data/lib/keycloak-admin/representation/role_representation.rb +19 -19
  51. data/lib/keycloak-admin/representation/session_representation.rb +22 -22
  52. data/lib/keycloak-admin/representation/token_representation.rb +39 -39
  53. data/lib/keycloak-admin/representation/user_representation.rb +47 -47
  54. data/lib/keycloak-admin/resource/base_role_containing_resource.rb +28 -28
  55. data/lib/keycloak-admin/resource/group_resource.rb +11 -11
  56. data/lib/keycloak-admin/resource/user_resource.rb +7 -7
  57. data/lib/keycloak-admin/version.rb +3 -3
  58. data/lib/keycloak-admin.rb +84 -75
  59. data/spec/client/attack_detection_client_spec.rb +102 -102
  60. data/spec/client/client_authz_permission_client_spec.rb +170 -0
  61. data/spec/client/client_authz_policy_client_spec.rb +170 -0
  62. data/spec/client/client_authz_resource_client_spec.rb +150 -0
  63. data/spec/client/client_authz_scope_client_spec.rb +134 -0
  64. data/spec/client/client_client_spec.rb +133 -133
  65. data/spec/client/client_role_mappings_client_spec.rb +82 -82
  66. data/spec/client/client_spec.rb +28 -28
  67. data/spec/client/configurable_token_client_spec.rb +34 -34
  68. data/spec/client/group_client_spec.rb +328 -258
  69. data/spec/client/identity_provider_client_spec.rb +92 -92
  70. data/spec/client/realm_client_spec.rb +155 -155
  71. data/spec/client/role_client_spec.rb +79 -79
  72. data/spec/client/role_mapper_client_spec.rb +113 -68
  73. data/spec/client/token_client_spec.rb +68 -68
  74. data/spec/client/user_client_spec.rb +373 -373
  75. data/spec/configuration_spec.rb +113 -113
  76. data/spec/integration/client_authorization_spec.rb +93 -0
  77. data/spec/representation/attack_detection_representation_spec.rb +15 -15
  78. data/spec/representation/client_authz_permission_representation_spec.rb +52 -0
  79. data/spec/representation/client_authz_policy_representation_spec.rb +47 -0
  80. data/spec/representation/client_authz_resource_representation_spec.rb +33 -0
  81. data/spec/representation/client_authz_scope_representation_spec.rb +19 -0
  82. data/spec/representation/client_representation_spec.rb +119 -119
  83. data/spec/representation/group_representation_spec.rb +22 -22
  84. data/spec/representation/identity_provider_mapper_representation_spec.rb +24 -24
  85. data/spec/representation/identity_provider_representation_spec.rb +113 -113
  86. data/spec/representation/impersonation_representation_spec.rb +163 -163
  87. data/spec/representation/protocol_mapper_representation_spec.rb +57 -57
  88. data/spec/representation/role_representation_spec.rb +37 -37
  89. data/spec/representation/session_representation_spec.rb +15 -15
  90. data/spec/representation/user_representation_spec.rb +15 -15
  91. data/spec/resource/group_resource_spec.rb +14 -14
  92. data/spec/resource/user_resource_spec.rb +14 -14
  93. data/spec/spec_helper.rb +37 -37
  94. metadata +29 -9
@@ -1,113 +1,113 @@
1
- RSpec.describe KeycloakAdmin::RealmClient do
2
-
3
- let(:client_id) { "admin-cli" }
4
- let(:client_secret) { "aaaaaaaa" }
5
- let(:client_realm_name) { "master2" }
6
- let(:use_service_account) { true }
7
- let(:username) { "a" }
8
- let(:password) { "b" }
9
- let(:rest_client_options) { {timeout: 10 } }
10
-
11
- before(:each) do
12
- @configuration = KeycloakAdmin::Configuration.new
13
- @configuration.server_url = "http://auth.service.io/auth"
14
- @configuration.server_domain = "auth.service.io"
15
- @configuration.client_id = client_id
16
- @configuration.client_secret = client_secret
17
- @configuration.client_realm_name = client_realm_name
18
- @configuration.use_service_account = use_service_account
19
- @configuration.username = username
20
- @configuration.password = password
21
- @configuration.rest_client_options = rest_client_options
22
- end
23
-
24
- describe "#headers_for_token_retrieval" do
25
- before(:each) do
26
- @headers = @configuration.headers_for_token_retrieval
27
- end
28
-
29
- context "when use_service_account is false" do
30
- let(:use_service_account) { false }
31
- it "returns an empty hash" do
32
- expect(@headers).to be_empty
33
- end
34
- end
35
-
36
- context "when use_service_account is true" do
37
- let(:use_service_account) { true }
38
- it "returns a single element" do
39
- expect(@headers.size).to eq 1
40
- end
41
-
42
- it "returns the Authorization Key" do
43
- expect(@headers.has_key?(:Authorization)).to be true
44
- end
45
-
46
- it "returns a Basic Authorization Key" do
47
- expect(@headers[:Authorization]).to start_with "Basic"
48
- end
49
-
50
- context "client_id='a' and client_secret='b'" do
51
- let(:client_id) { "a" }
52
- let(:client_secret) { "b" }
53
-
54
- it "returns a Basic Authorization = 'Basic YTpi'" do
55
- expect(@headers[:Authorization]).to eq "Basic YTpi"
56
- end
57
- end
58
-
59
- context "client_id='365e3c66-fd0f-11e7-8be5-0ed5f89f718b' and client_secret='411e6f9a-fd0f-11e7-8be5-0ed5f89f718b'" do
60
- let(:client_id) { "365e3c66-fd0f-11e7-8be5-0ed5f89f718b" }
61
- let(:client_secret) { "411e6f9a-fd0f-11e7-8be5-0ed5f89f718b" }
62
-
63
- it "returns a Basic Authorization = 'Basic MzY1ZTNjNjYtZmQwZi0xMWU3LThiZTUtMGVkNWY4OWY3MThiOjQxMWU2ZjlhLWZkMGYtMTFlNy04YmU1LTBlZDVmODlmNzE4Yg=='" do
64
- expect(@headers[:Authorization]).to eq "Basic MzY1ZTNjNjYtZmQwZi0xMWU3LThiZTUtMGVkNWY4OWY3MThiOjQxMWU2ZjlhLWZkMGYtMTFlNy04YmU1LTBlZDVmODlmNzE4Yg=="
65
- end
66
- end
67
-
68
- end
69
- end
70
-
71
- describe "#body_for_token_retrieval" do
72
- before(:each) do
73
- @body = @configuration.body_for_token_retrieval
74
- end
75
- context "when use_service_account is false" do
76
- let(:use_service_account) { false }
77
- it "returns a hash of 5 elements" do
78
- expect(@body.size).to eq 5
79
- end
80
-
81
- it "returns a hash containing the username" do
82
- expect(@body[:username]).to eq username
83
- end
84
-
85
- it "returns a hash containing the password" do
86
- expect(@body[:password]).to eq password
87
- end
88
-
89
- it "returns a hash containing the grant_type 'password'" do
90
- expect(@body[:grant_type]).to eq "password"
91
- end
92
-
93
- it "returns a hash containing the client_id" do
94
- expect(@body[:client_id]).to eq client_id
95
- end
96
-
97
- it "returns a hash containing the client_secret" do
98
- expect(@body[:client_secret]).to eq client_secret
99
- end
100
- end
101
-
102
- context "when use_service_account is true" do
103
- let(:use_service_account) { true }
104
- it "returns a hash of 1 element" do
105
- expect(@body.size).to eq 1
106
- end
107
-
108
- it "returns a hash containing the grant_type" do
109
- expect(@body[:grant_type]).to eq "client_credentials"
110
- end
111
- end
112
- end
113
- end
1
+ RSpec.describe KeycloakAdmin::RealmClient do
2
+
3
+ let(:client_id) { "admin-cli" }
4
+ let(:client_secret) { "aaaaaaaa" }
5
+ let(:client_realm_name) { "master2" }
6
+ let(:use_service_account) { true }
7
+ let(:username) { "a" }
8
+ let(:password) { "b" }
9
+ let(:rest_client_options) { {timeout: 10 } }
10
+
11
+ before(:each) do
12
+ @configuration = KeycloakAdmin::Configuration.new
13
+ @configuration.server_url = "http://auth.service.io/auth"
14
+ @configuration.server_domain = "auth.service.io"
15
+ @configuration.client_id = client_id
16
+ @configuration.client_secret = client_secret
17
+ @configuration.client_realm_name = client_realm_name
18
+ @configuration.use_service_account = use_service_account
19
+ @configuration.username = username
20
+ @configuration.password = password
21
+ @configuration.rest_client_options = rest_client_options
22
+ end
23
+
24
+ describe "#headers_for_token_retrieval" do
25
+ before(:each) do
26
+ @headers = @configuration.headers_for_token_retrieval
27
+ end
28
+
29
+ context "when use_service_account is false" do
30
+ let(:use_service_account) { false }
31
+ it "returns an empty hash" do
32
+ expect(@headers).to be_empty
33
+ end
34
+ end
35
+
36
+ context "when use_service_account is true" do
37
+ let(:use_service_account) { true }
38
+ it "returns a single element" do
39
+ expect(@headers.size).to eq 1
40
+ end
41
+
42
+ it "returns the Authorization Key" do
43
+ expect(@headers.has_key?(:Authorization)).to be true
44
+ end
45
+
46
+ it "returns a Basic Authorization Key" do
47
+ expect(@headers[:Authorization]).to start_with "Basic"
48
+ end
49
+
50
+ context "client_id='a' and client_secret='b'" do
51
+ let(:client_id) { "a" }
52
+ let(:client_secret) { "b" }
53
+
54
+ it "returns a Basic Authorization = 'Basic YTpi'" do
55
+ expect(@headers[:Authorization]).to eq "Basic YTpi"
56
+ end
57
+ end
58
+
59
+ context "client_id='365e3c66-fd0f-11e7-8be5-0ed5f89f718b' and client_secret='411e6f9a-fd0f-11e7-8be5-0ed5f89f718b'" do
60
+ let(:client_id) { "365e3c66-fd0f-11e7-8be5-0ed5f89f718b" }
61
+ let(:client_secret) { "411e6f9a-fd0f-11e7-8be5-0ed5f89f718b" }
62
+
63
+ it "returns a Basic Authorization = 'Basic MzY1ZTNjNjYtZmQwZi0xMWU3LThiZTUtMGVkNWY4OWY3MThiOjQxMWU2ZjlhLWZkMGYtMTFlNy04YmU1LTBlZDVmODlmNzE4Yg=='" do
64
+ expect(@headers[:Authorization]).to eq "Basic MzY1ZTNjNjYtZmQwZi0xMWU3LThiZTUtMGVkNWY4OWY3MThiOjQxMWU2ZjlhLWZkMGYtMTFlNy04YmU1LTBlZDVmODlmNzE4Yg=="
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+
71
+ describe "#body_for_token_retrieval" do
72
+ before(:each) do
73
+ @body = @configuration.body_for_token_retrieval
74
+ end
75
+ context "when use_service_account is false" do
76
+ let(:use_service_account) { false }
77
+ it "returns a hash of 5 elements" do
78
+ expect(@body.size).to eq 5
79
+ end
80
+
81
+ it "returns a hash containing the username" do
82
+ expect(@body[:username]).to eq username
83
+ end
84
+
85
+ it "returns a hash containing the password" do
86
+ expect(@body[:password]).to eq password
87
+ end
88
+
89
+ it "returns a hash containing the grant_type 'password'" do
90
+ expect(@body[:grant_type]).to eq "password"
91
+ end
92
+
93
+ it "returns a hash containing the client_id" do
94
+ expect(@body[:client_id]).to eq client_id
95
+ end
96
+
97
+ it "returns a hash containing the client_secret" do
98
+ expect(@body[:client_secret]).to eq client_secret
99
+ end
100
+ end
101
+
102
+ context "when use_service_account is true" do
103
+ let(:use_service_account) { true }
104
+ it "returns a hash of 1 element" do
105
+ expect(@body.size).to eq 1
106
+ end
107
+
108
+ it "returns a hash containing the grant_type" do
109
+ expect(@body[:grant_type]).to eq "client_credentials"
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,93 @@
1
+ RSpec.describe 'ClientAuthorization' do
2
+
3
+ before(:each) do
4
+ skip("This test requires to be run in a Github action.") unless ENV["GITHUB_ACTIONS"]
5
+
6
+ KeycloakAdmin.configure do |config|
7
+ config.use_service_account = false
8
+ config.server_url = "http://localhost:8080/"
9
+ config.client_id = "admin-cli"
10
+ config.client_realm_name = "master"
11
+ config.username = "admin"
12
+ config.password = "admin"
13
+ config.rest_client_options = { timeout: 5, verify_ssl: false }
14
+ end
15
+ end
16
+
17
+ after(:each) do
18
+ configure
19
+ end
20
+
21
+ describe "ClientAuthorization Suite" do
22
+ it do
23
+ realm_name = "dummy"
24
+
25
+ client = KeycloakAdmin.realm(realm_name).clients.find_by_client_id("dummy-client")
26
+ client.authorization_services_enabled = true
27
+ KeycloakAdmin.realm(realm_name).clients.update(client)
28
+
29
+ expect(KeycloakAdmin.realm(realm_name).authz_scopes(client.id).list.size).to eql(0)
30
+ expect(KeycloakAdmin.realm(realm_name).authz_resources(client.id).list.size).to eql(1)
31
+ expect(KeycloakAdmin.realm(realm_name).authz_policies(client.id, 'role').list.size).to eql(0)
32
+
33
+ realm_role = KeycloakAdmin.realm(realm_name).roles.get("default-roles-dummy")
34
+
35
+ scope_1 = KeycloakAdmin.realm(realm_name).authz_scopes(client.id).create!("POST_1", "POST 1 scope", "http://asdas")
36
+ scope_2 = KeycloakAdmin.realm(realm_name).authz_scopes(client.id).create!("POST_2", "POST 2 scope", "http://asdas")
37
+ expect(KeycloakAdmin.realm(realm_name).authz_scopes(client.id).search("POST").first.name).to eql("POST_1")
38
+ expect(KeycloakAdmin.realm(realm_name).authz_scopes(client.id).get(scope_1.id).name).to eql("POST_1")
39
+
40
+ resource = KeycloakAdmin.realm(realm_name).authz_resources(client.id).create!("Dummy Resource", "type", ["/asdf/*", "/tmp/"], true, "display_name", [], {"a": ["b", "c"]})
41
+
42
+ expect(KeycloakAdmin.realm(realm_name).authz_resources(client.id).find_by("Dummy Resource", "", "", "", "").first.name).to eql("Dummy Resource")
43
+ expect(KeycloakAdmin.realm(realm_name).authz_resources(client.id).find_by("", "type", "", "", "").first.name).to eql("Dummy Resource")
44
+
45
+ expect(KeycloakAdmin.realm(realm_name).authz_resources(client.id).get(resource.id).scopes.count).to eql(0)
46
+ expect(KeycloakAdmin.realm(realm_name).authz_resources(client.id).get(resource.id).uris.count).to eql(2)
47
+ KeycloakAdmin.realm(realm_name).authz_resources(client.id).update(resource.id,
48
+ {
49
+ "name": "Dummy Resource",
50
+ "type": "type",
51
+ "owner_managed_access": true,
52
+ "display_name": "display_name",
53
+ "attributes": {"a":["b","c"]},
54
+ "uris": [ "/asdf/*" , "/tmp/45" ],
55
+ "scopes":[
56
+ {name: scope_1.name},{name: scope_2.name}
57
+ ],
58
+ "icon_uri": "https://icon.ico"
59
+ }
60
+ )
61
+
62
+ expect(KeycloakAdmin.realm(realm_name).authz_resources(client.id).get(resource.id).scopes.count).to eql(2)
63
+
64
+ policy = KeycloakAdmin.realm(realm_name).authz_policies(client.id, 'role').create!("Policy 1", "description", "role", "POSITIVE", "UNANIMOUS", true, [{id: realm_role.id, required: true}])
65
+ expect(KeycloakAdmin.realm(realm_name).authz_policies(client.id, 'role').find_by("Policy 1", "role").first.name).to eql("Policy 1")
66
+ expect(KeycloakAdmin.realm(realm_name).authz_policies(client.id, 'role').get(policy.id).name).to eql("Policy 1")
67
+ scope_permission = KeycloakAdmin.realm(realm_name).authz_permissions(client.id, :scope).create!("Dummy Scope Permission", "scope description", "UNANIMOUS", "POSITIVE", [resource.id], [policy.id], [scope_1.id, scope_2.id], "")
68
+ resource_permission = KeycloakAdmin.realm(realm_name).authz_permissions(client.id, :resource).create!("Dummy Resource Permission", "resource description", "UNANIMOUS", "POSITIVE", [resource.id], [policy.id], nil, "")
69
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, "", resource.id).list.size).to eql(2)
70
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, "resource").get(resource_permission.id).name).to eql("Dummy Resource Permission")
71
+ expect(KeycloakAdmin.realm(realm_name).authz_scopes(client.id, resource.id).list.size).to eql(2)
72
+
73
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, 'scope').list.size).to eql(3)
74
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, 'resource').list.size).to eql(3)
75
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, "resource").find_by(resource_permission.name, nil).first.name).to eql("Dummy Resource Permission")
76
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, "resource").find_by(resource_permission.name, resource.id).first.name).to eql("Dummy Resource Permission")
77
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, "scope").find_by(scope_permission.name, resource.id).first.name).to eql("Dummy Scope Permission")
78
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, "scope").find_by(scope_permission.name, resource.id, "POST_1").first.name).to eql("Dummy Scope Permission")
79
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, "resource").find_by(nil, resource.id).first.name).to eql("Dummy Resource Permission")
80
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, "scope").find_by(nil, resource.id).first.name).to eql("Dummy Scope Permission")
81
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, "scope").find_by(nil, resource.id, "POST_1").first.name).to eql("Dummy Scope Permission")
82
+ expect(KeycloakAdmin.realm(realm_name).authz_permissions(client.id, "scope").find_by(scope_permission.name, nil).first.name).to eql("Dummy Scope Permission")
83
+
84
+ KeycloakAdmin.realm(realm_name).authz_permissions(client.id, 'scope').delete(scope_permission.id)
85
+ KeycloakAdmin.realm(realm_name).authz_permissions(client.id, 'resource').delete(resource_permission.id)
86
+ KeycloakAdmin.realm(realm_name).authz_policies(client.id, 'role').delete(policy.id)
87
+ KeycloakAdmin.realm(realm_name).authz_resources(client.id).delete(resource.id)
88
+ KeycloakAdmin.realm(realm_name).authz_scopes(client.id).delete(scope_1.id)
89
+ KeycloakAdmin.realm(realm_name).authz_scopes(client.id).delete(scope_2.id)
90
+
91
+ end
92
+ end
93
+ end
@@ -1,16 +1,16 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe KeycloakAdmin::AttackDetectionRepresentation do
4
- describe '.from_hash' do
5
- it 'converts json response to class structure' do
6
- rep = described_class.from_hash({
7
- 'numFailures' => 2,
8
- 'disabled' => true,
9
- 'lastIPFailure' => 12345,
10
- 'last_failure' => 12345678
11
- })
12
- expect(rep.num_failures).to eq 2
13
- expect(rep).to be_a described_class
14
- end
15
- end
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe KeycloakAdmin::AttackDetectionRepresentation do
4
+ describe '.from_hash' do
5
+ it 'converts json response to class structure' do
6
+ rep = described_class.from_hash({
7
+ 'numFailures' => 2,
8
+ 'disabled' => true,
9
+ 'lastIPFailure' => 12345,
10
+ 'last_failure' => 12345678
11
+ })
12
+ expect(rep.num_failures).to eq 2
13
+ expect(rep).to be_a described_class
14
+ end
15
+ end
16
16
  end
@@ -0,0 +1,52 @@
1
+ RSpec.describe KeycloakAdmin::ClientAuthzPermissionRepresentation do
2
+ describe '.from_hash, #resource based permission' do
3
+ it 'converts json response to class structure' do
4
+ rep = described_class.from_hash({
5
+ "id" => "e9e3bc49-fe11-4287-b6fc-fa8be4930ffa",
6
+ "resources" => ["4f55e984-d1ec-405c-a25c-1387f88acd5c"],
7
+ "policies" => ["e9e3bc49-fe11-4287-b6fc-fa8be4930ffa"],
8
+ "name" => "delme policy",
9
+ "description" => "Delme policy description",
10
+ "decisionStrategy" => "UNANIMOUS",
11
+ "resourceType" => ""
12
+ })
13
+ expect(rep.id).to eq "e9e3bc49-fe11-4287-b6fc-fa8be4930ffa"
14
+ expect(rep.resources).to eq ["4f55e984-d1ec-405c-a25c-1387f88acd5c"]
15
+ expect(rep.policies).to eq ["e9e3bc49-fe11-4287-b6fc-fa8be4930ffa"]
16
+ expect(rep.name).to eq "delme policy"
17
+ expect(rep.description).to eq "Delme policy description"
18
+ expect(rep.decision_strategy).to eq "UNANIMOUS"
19
+ expect(rep.resource_type).to eq ""
20
+ expect(rep).to be_a described_class
21
+ end
22
+ end
23
+
24
+ describe '.from_hash, #scope based permission' do
25
+ it 'converts json response to class structure' do
26
+ rep = described_class.from_hash(
27
+
28
+ { "id" => "4d762e5d-bf3d-4641-8f94-97e8a1869d1d",
29
+ "name" => "permission name",
30
+ "description" => "permission description",
31
+ "type" => "scope",
32
+ "policies" => ["e9e3bc49-fe11-4287-b6fc-fa8be4930ffa"],
33
+ "resources" => ["4f55e984-d1ec-405c-a25c-1387f88acd5c"],
34
+ "scopes" => ["7c4809c5-33b6-4668-a318-19b302214d20"],
35
+ "logic" => "POSITIVE",
36
+ "decisionStrategy" => "UNANIMOUS"
37
+ })
38
+ expect(rep.id).to eq "4d762e5d-bf3d-4641-8f94-97e8a1869d1d"
39
+ expect(rep.resources).to eq ["4f55e984-d1ec-405c-a25c-1387f88acd5c"]
40
+ expect(rep.policies).to eq ["e9e3bc49-fe11-4287-b6fc-fa8be4930ffa"]
41
+ expect(rep.scopes).to eq ["7c4809c5-33b6-4668-a318-19b302214d20"]
42
+ expect(rep.name).to eq "permission name"
43
+ expect(rep.description).to eq "permission description"
44
+ expect(rep.decision_strategy).to eq "UNANIMOUS"
45
+ expect(rep.logic).to eq "POSITIVE"
46
+ expect(rep.type).to eq "scope"
47
+ expect(rep.resource_type).to eq nil
48
+ expect(rep).to be_a described_class
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe KeycloakAdmin::ClientAuthzPolicyRepresentation do
4
+ let(:realm_name) { "valid-realm" }
5
+ let(:client_id) { "valid-client-id" }
6
+ let(:policy_id) { "valid-policy-id" }
7
+ let(:role_id) { "valid-role-id" }
8
+ let(:role_name) { "valid-role-name" }
9
+ let(:policy_name) { "valid-policy-name" }
10
+ let(:policy_description) { "valid-policy-description" }
11
+ let(:policy_type) { "role" }
12
+ let(:policy_logic) { "POSITIVE" }
13
+ let(:policy_decision_strategy) { "UNANIMOUS" }
14
+ let(:policy) do
15
+ {
16
+ "id": policy_id,
17
+ "name": policy_name,
18
+ "description": policy_description,
19
+ "type": policy_type,
20
+ "logic": policy_logic,
21
+ "decisionStrategy": policy_decision_strategy,
22
+ "roles": [{ "id": role_id, "required": true }]
23
+ }
24
+ end
25
+ let(:client_authz_policy) { KeycloakAdmin.realm(realm_name).authz_policies(client_id, 'role') }
26
+
27
+ before(:each) do
28
+ stub_token_client
29
+ end
30
+
31
+ describe "#create!" do
32
+ before(:each) do
33
+ allow_any_instance_of(RestClient::Resource).to receive(:post).and_return policy.to_json
34
+ end
35
+
36
+ it "returns created authz policy" do
37
+ response = client_authz_policy.create!(policy_name, policy_description, policy_type, policy_logic, policy_decision_strategy, true, [{ id: role_id, required: true }])
38
+ expect(response.id).to eq policy_id
39
+ expect(response.name).to eq policy_name
40
+ expect(response.description).to eq policy_description
41
+ expect(response.type).to eq policy_type
42
+ expect(response.logic).to eq policy_logic
43
+ expect(response.decision_strategy).to eq policy_decision_strategy
44
+ expect(response.roles).to eq [{ "id" => role_id, "required" => true }]
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,33 @@
1
+ RSpec.describe KeycloakAdmin::ClientAuthzResourceRepresentation do
2
+ describe '.from_hash' do
3
+ it 'converts json response to class structure' do
4
+ rep = described_class.from_hash({
5
+ "name" => "Default Resource",
6
+ "type" => "urn:delme-client-id:resources:default",
7
+ "owner" => {
8
+ "id" => "d259b451-371b-432a-a526-3508f3a36f3b",
9
+ "name" => "delme-client-id"
10
+ },
11
+ "ownerManagedAccess" => true,
12
+ "displayName" => "Display Name",
13
+ "attributes" => { "a" => ["b"]},
14
+ "_id" => "385966a2-14b9-4cc4-9539-5f2fe1008222",
15
+ "uris" => ["/*"],
16
+ "scopes" => [{"id"=>"c0779ce3-0900-4ea3-b1d6-b23e1f19c662",
17
+ "name" => "GET",
18
+ "iconUri" => "http=>//asdfasdf"}],
19
+ "icon_uri" => "http://icon"
20
+ })
21
+ expect(rep.id).to eq "385966a2-14b9-4cc4-9539-5f2fe1008222"
22
+ expect(rep.name).to eq "Default Resource"
23
+ expect(rep.type).to eq "urn:delme-client-id:resources:default"
24
+ expect(rep.uris).to eq ["/*"]
25
+ expect(rep.owner_managed_access).to eq true
26
+ expect(rep.attributes).to eq({ :"a" => ["b"]})
27
+ expect(rep.display_name).to eq "Display Name"
28
+ expect(rep.scopes[0].id).to eq "c0779ce3-0900-4ea3-b1d6-b23e1f19c662"
29
+ expect(rep.scopes[0].name).to eq "GET"
30
+ expect(rep).to be_a described_class
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe KeycloakAdmin::ClientAuthzScopeRepresentation do
4
+ describe '.from_hash' do
5
+ it 'converts json response to class structure' do
6
+ rep = described_class.from_hash({
7
+ "id" =>"c0779ce3-0900-4ea3-b1d6-b23e1f19c662",
8
+ "name" => "GET",
9
+ "iconUri" => "http://asdfasdf/image.png",
10
+ "displayName" => "GET authz scope"
11
+ })
12
+ expect(rep.id).to eq "c0779ce3-0900-4ea3-b1d6-b23e1f19c662"
13
+ expect(rep.name).to eq "GET"
14
+ expect(rep.icon_uri).to eq "http://asdfasdf/image.png"
15
+ expect(rep.display_name).to eq "GET authz scope"
16
+ expect(rep).to be_a described_class
17
+ end
18
+ end
19
+ end