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)


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

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



74
75
76
77
78
79
80
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 74

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

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



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

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



67
68
69
70
71
72
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 67

def delete(resource_id)
  execute_http do
    resource(authz_resources_url(@client_id, resource_id)).delete(headers)
  end
  true
end

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



52
53
54
55
56
57
58
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 52

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

#get(resource_id) ⇒ Object



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

def get(resource_id)
  response = execute_http do
    resource(authz_resources_url(@client_id, resource_id)).get(headers)
  end
  ClientAuthzResourceRepresentation.from_hash(JSON.parse(response))
end

#listObject



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

def list
  response = execute_http do
    resource(authz_resources_url(@client_id)).get(headers)
  end
  JSON.parse(response).map { |role_as_hash| ClientAuthzResourceRepresentation.from_hash(role_as_hash) }
end

#save(client_authz_resource_representation) ⇒ Object



60
61
62
63
64
65
# File 'lib/keycloak-admin/client/client_authz_resource_client.rb', line 60

def save(client_authz_resource_representation)
  response = execute_http do
    resource(authz_resources_url(@client_id)).post(client_authz_resource_representation.to_json, headers)
  end
  ClientAuthzResourceRepresentation.from_hash(JSON.parse(response))
end

#update(resource_id, client_authz_resource_representation) ⇒ Object



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

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

  existing_resource = get(resource_id)
  new_resource = build(
    .fetch(:name, existing_resource.name),
    .fetch(:type, existing_resource.type),
    .fetch(:uris, existing_resource.uris),
    .fetch(:owner_managed_access, existing_resource.owner_managed_access),
    .fetch(:display_name, existing_resource.display_name),
    .fetch(:scopes, existing_resource.scopes.map { |scope| {name: scope.name} }),
    .fetch(:attributes, existing_resource.attributes)
  )

  execute_http do
    resource(authz_resources_url(@client_id, resource_id)).put(new_resource.to_json, headers)
  end
  get(resource_id)
end