Class: KeycloakAdmin::ClientAuthzPolicyClient

Inherits:
Client
  • Object
show all
Defined in:
lib/keycloak-admin/client/client_authz_policy_client.rb

Instance Method Summary collapse

Methods inherited from Client

#create_payload, #created_id, #current_token, #execute_http, #headers, #server_url

Constructor Details

#initialize(configuration, realm_client, client_id, type) ⇒ ClientAuthzPolicyClient

Returns a new instance of ClientAuthzPolicyClient.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
# File 'lib/keycloak-admin/client/client_authz_policy_client.rb', line 3

def initialize(configuration, realm_client, client_id, type)
  super(configuration)
  raise ArgumentError.new("realm must be defined") unless realm_client.name_defined?
  raise ArgumentError.new("type must be defined") unless type
  raise ArgumentError.new("only 'role' policies supported") unless type.to_sym == :role

  @realm_client = realm_client
  @client_id = client_id
  @type = type
end

Instance Method Details

#authz_policy_url(client_id, type, id = nil) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/keycloak-admin/client/client_authz_policy_client.rb', line 56

def authz_policy_url(client_id, type, id = nil)
  if id
    "#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/policy/#{type}/#{id}"
  else
    "#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/policy/#{type}?permission=false"
  end
end

#build(name, description, type, logic, decision_strategy, fetch_roles, roles = []) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/keycloak-admin/client/client_authz_policy_client.rb', line 64

def build(name, description, type, logic, decision_strategy, fetch_roles, roles=[])
  policy                   = ClientAuthzPolicyRepresentation.new
  policy.name              = name
  policy.description       = description
  policy.type              = type
  policy.logic             = logic
  policy.decision_strategy = decision_strategy
  policy.fetch_roles       = fetch_roles
  policy.roles             = roles
  policy
end

#create!(name, description, type, logic, decision_strategy, fetch_roles, roles) ⇒ Object



14
15
16
17
# File 'lib/keycloak-admin/client/client_authz_policy_client.rb', line 14

def create!(name, description, type, logic, decision_strategy, fetch_roles, roles)
  response = save(build(name, description, type, logic, decision_strategy, fetch_roles, roles))
  ClientAuthzPolicyRepresentation.from_hash(JSON.parse(response))
end

#delete(policy_id) ⇒ Object



42
43
44
45
46
47
# File 'lib/keycloak-admin/client/client_authz_policy_client.rb', line 42

def delete(policy_id)
  execute_http do
    RestClient::Resource.new(authz_policy_url(@client_id, @type, policy_id), @configuration.rest_client_options).delete(headers)
  end
  true
end

#find_by(name, type) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/keycloak-admin/client/client_authz_policy_client.rb', line 34

def find_by(name, type)
  response = execute_http do
    url = "#{authz_policy_url(@client_id, @type)}?permission=false&name=#{name}&type=#{type}&first=0&max=11"
    RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |role_as_hash| ClientAuthzPolicyRepresentation.from_hash(role_as_hash) }
end

#get(policy_id) ⇒ Object



27
28
29
30
31
32
# File 'lib/keycloak-admin/client/client_authz_policy_client.rb', line 27

def get(policy_id)
  response = execute_http do
    RestClient::Resource.new(authz_policy_url(@client_id, @type, policy_id), @configuration.rest_client_options).get(headers)
  end
  ClientAuthzPolicyRepresentation.from_hash(JSON.parse(response))
end

#listObject



49
50
51
52
53
54
# File 'lib/keycloak-admin/client/client_authz_policy_client.rb', line 49

def list
  response = execute_http do
    RestClient::Resource.new(authz_policy_url(@client_id, @type), @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |role_as_hash| ClientAuthzPolicyRepresentation.from_hash(role_as_hash) }
end

#save(policy_representation) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/keycloak-admin/client/client_authz_policy_client.rb', line 19

def save(policy_representation)
  execute_http do
    RestClient::Resource.new(authz_policy_url(@client_id, @type), @configuration.rest_client_options).post(
      create_payload(policy_representation), headers
    )
  end
end