Class: KeycloakAdmin::ClientAuthzResourceClient

Inherits:
Client
  • Object
show all
Defined in:
lib/keycloak-admin/client/client_authz_resource_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) ⇒ ClientAuthzResourceClient

Returns a new instance of ClientAuthzResourceClient.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 3

def initialize(configuration, realm_client, client_id)
  super(configuration)
  raise ArgumentError.new("realm must be defined") unless realm_client.name_defined?
  @realm_client = realm_client
  @client_id = client_id
end

Instance Method Details

#authz_resources_url(client_id, id = nil) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 70

def authz_resources_url(client_id, id = nil)
  if id
    "#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/resource/#{id}"
  else
    "#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/resource"
  end
end

#create!(name, type, uris, owner_managed_access, display_name, scopes, attributes = {}) ⇒ Object



44
45
46
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 44

def create!(name, type, uris, owner_managed_access, display_name, scopes, attributes = {})
  save(build(name, type, uris, owner_managed_access, display_name, scopes, attributes))
end

#delete(resource_id) ⇒ Object



63
64
65
66
67
68
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 63

def delete(resource_id)
  execute_http do
    RestClient::Resource.new(authz_resources_url(@client_id, resource_id), @configuration.rest_client_options).delete(headers)
  end
  true
end

#find_by(name, type, uris, owner, scope) ⇒ Object



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

def find_by(name, type, uris, owner, scope)
  response = execute_http do
    url = "#{authz_resources_url(@client_id)}?name=#{name}&type=#{type}&uris=#{uris}&owner=#{owner}&scope=#{scope}&deep=true&first=0&max=100"
    RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |role_as_hash| ClientAuthzResourceRepresentation.from_hash(role_as_hash) }
end

#get(resource_id) ⇒ Object



17
18
19
20
21
22
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 17

def get(resource_id)
  response = execute_http do
    RestClient::Resource.new(authz_resources_url(@client_id, resource_id), @configuration.rest_client_options).get(headers)
  end
  ClientAuthzResourceRepresentation.from_hash(JSON.parse(response))
end

#listObject



10
11
12
13
14
15
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 10

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

#save(client_authz_resource_representation) ⇒ Object



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

def save(client_authz_resource_representation)
  response = execute_http do
    RestClient::Resource.new(authz_resources_url(@client_id), @configuration.rest_client_options).post(client_authz_resource_representation.to_json, headers)
  end
  ClientAuthzResourceRepresentation.from_hash(JSON.parse(response))
end

#update(resource_id, client_authz_resource_representation) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 24

def update(resource_id, client_authz_resource_representation)
  raise "scope[:name] is mandatory and the only necessary attribute to add scope to resource" if client_authz_resource_representation[:scopes] && client_authz_resource_representation[:scopes].any?{|a| !a[:name]}

  existing_resource = get(resource_id)
  new_resource = build(
    client_authz_resource_representation[:name] || existing_resource.name,
    client_authz_resource_representation[:type] || existing_resource.type,
    (client_authz_resource_representation[:uris] || [] ) + existing_resource.uris,
    client_authz_resource_representation[:owner_managed_access] || existing_resource.owner_managed_access,
    client_authz_resource_representation[:display_name] || existing_resource.display_name,
    (client_authz_resource_representation[:scopes] || []) + existing_resource.scopes.map{|s| {name: s.name}},
    client_authz_resource_representation[:attributes] || existing_resource.attributes
  )

  execute_http do
    RestClient::Resource.new(authz_resources_url(@client_id, resource_id), @configuration.rest_client_options).put(new_resource.to_json, headers)
  end
  get(resource_id)
end