Module: Keycloak::Admin

Defined in:
lib/keycloak/admin.rb,
lib/keycloak/admin/agent.rb,
lib/keycloak/admin/error.rb,
lib/keycloak/admin/users.rb,
lib/keycloak/admin/groups.rb,
lib/keycloak/admin/realms.rb,
lib/keycloak/admin/clients.rb,
lib/keycloak/admin/version.rb,
lib/keycloak/admin/resource.rb,
lib/keycloak/admin/resource/pagination.rb

Overview

Keycloak::Admin is a Ruby client for the Keycloak administration API.

This is the main module. It provides methods to configure the connection to any Keycloak service, as well as methods to manage and control the several Keycloak resources.

Defined Under Namespace

Modules: Clients, Groups, Realms, Resource, Users Classes: Agent, BadGatewayError, BadRequestError, ConfigurationError, ConflictError, ConnectionFailedError, ForbiddenError, InternalServerError, MethodNotAllowedError, NotFoundError, ServiceUnavailableError, UnauthorizedError

Constant Summary collapse

VERSION =
'26.0.8'

Class Method Summary collapse

Class Method Details

.clientsObject

Manage realm clients. See Keycloak::Admin::Clients for usage.



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

def clients
  configured!
  Keycloak::Admin::Clients.agent = @agent

  Keycloak::Admin::Clients
end

.configure {|@agent| ... } ⇒ Object

Configure Keycloak::Admin, the handler for all administration tasks.

Provide Keycloak server base url, master realm admin user credentials and id of realm to be managed.

Keycloak::Admin.configure do |config|
  config.username   = 'admin',
  config.password   = 'admin',
  config.realm      = 'zone',                         # default: master
  config.base_url   = 'https://keycloak.example.com'  # default: http://localhost:8080
end

Yields:

  • (@agent)


33
34
35
36
37
38
39
# File 'lib/keycloak/admin.rb', line 33

def configure
  @agent ||= Keycloak::Admin::Agent.new
  yield @agent
  @configured = true

  true
end

.configured!Object

Raise error if Keycloak::Admin is not configured.



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

def configured!
  raise Keycloak::Admin::ConfigurationError, 'Keycloak::Admin is not configured' if !configured?

  true
end

.configured?Boolean

Verify if Keycloak::Admin is configured.

Returns:

  • (Boolean)


43
44
45
# File 'lib/keycloak/admin.rb', line 43

def configured?
  @configured.nil? ? false : @configured
end

.groupsObject

Manage realm groups. See Keycloak::Admin::Groups for usage.



92
93
94
95
96
97
# File 'lib/keycloak/admin.rb', line 92

def groups
  configured!
  Keycloak::Admin::Groups.agent = @agent

  Keycloak::Admin::Groups
end

.raise_error(response) ⇒ Object

Raises:

  • (error_class)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/keycloak/admin/error.rb', line 18

def raise_error(response)
  error_classes = {
    400 => BadRequestError,
    401 => UnauthorizedError,
    403 => ForbiddenError,
    404 => NotFoundError,
    405 => MethodNotAllowedError,
    409 => ConflictError,
    500 => InternalServerError,
    502 => BadGatewayError,
    503 => ServiceUnavailableError
  }
  error_class = error_classes[response.code] || StandardError

  raise error_class, error_message(response)
end

.ready?Boolean

Verify if the Keycloak server is ready.

Beware, method returns nil if the server does not respond (404 not found) to health checks.

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/keycloak/admin.rb', line 104

def ready?
  configured!

  begin
    @agent.head('health/ready')
    true
  rescue Keycloak::Admin::NotFoundError
    nil
  rescue Keycloak::Admin::ServiceUnavailableError
    false
  end
end

.realmsObject

Manage server realms. See Keycloak::Admin::Realms for usage.



65
66
67
68
69
70
# File 'lib/keycloak/admin.rb', line 65

def realms
  configured!
  Keycloak::Admin::Realms.agent = @agent

  Keycloak::Admin::Realms
end

.reset!Object

Reset Keycloak::Admin configuration.



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

def reset!
  @agent&.logout
  @agent = nil
  @configured = nil
end

.usersObject

Manage realm users. See Keycloak::Admin::Users for usage.



83
84
85
86
87
88
# File 'lib/keycloak/admin.rb', line 83

def users
  configured!
  Keycloak::Admin::Users.agent = @agent

  Keycloak::Admin::Users
end