Class: Authentik::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/authentik/client.rb,
lib/authentik/client/railtie.rb,
lib/authentik/client/version.rb,
lib/authentik/client/configuration.rb

Overview

Client provides a Ruby interface to the authentik API. It wraps the auto-generated OpenAPI client and organizes API calls into groups that correspond to the underlying API classes.

API endpoints are accessed directly as methods on the client. The client resolves the endpoint prefix (for example, core_ or admin_) to the corresponding auto-generated OpenAPI API class.

New API groups added to the underlying OpenAPI client are automatically discovered and available without changes to this class.

Examples:

Configure at startup and create a client

Authentik::Client.configure do |config|
  config.host = "authentik.example.com"
  config.token = "your-api-token"
end

client = Authentik::Client.new

Create a client with inline options

client = Authentik::Client.new(
  host: "authentik.example.com",
  token: "your-api-token"
)

List all applications

client.core_applications_list

Get the admin version

client.admin_version_retrieve

List OAuth2 access tokens

client.oauth2_access_tokens_list

Defined Under Namespace

Classes: Configuration, NoEndpointError, Railtie

Constant Summary collapse

RESOURCE_ACTIONS =
%w[list retrieve create update partial_update destroy].freeze
VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: nil, token: nil, scheme: nil, **options) ⇒ Client

Returns a new instance of Client.

Parameters:

  • host (String, nil) (defaults to: nil)

    The authentik server hostname (e.g., “authentik.example.com”). Falls back to the value set in configuration when not provided.

  • token (String, nil) (defaults to: nil)

    The API bearer token for authentication. Falls back to the value set in configuration when not provided.

  • scheme (String, nil) (defaults to: nil)

    The URL scheme (+“https”+ or “http”). Falls back to the value set in configuration, which defaults to “https”.

  • options (Hash)

    Additional per-instance options forwarded to Authentik::Api::Configuration (e.g., verify_ssl: false, timeout: 60). Take precedence over values set in configuration.

Raises:

  • (ArgumentError)

    if host is not provided here or in the global configuration

  • (ArgumentError)

    if token is not provided here or in the global configuration



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/authentik/client.rb', line 195

def initialize(host: nil, token: nil, scheme: nil, **options)
  cfg = self.class.configuration
  resolved_host = host || cfg.host
  resolved_token = token || cfg.access_token
  resolved_scheme = scheme || cfg.scheme

  raise ArgumentError, "host is required" if resolved_host.nil?
  raise ArgumentError, "token is required" if resolved_token.nil?

  @api_client = build_api_client(
    cfg,
    host: resolved_host,
    token: resolved_token,
    scheme: resolved_scheme,
    **options
  )
  @api_instances = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object

Dispatches endpoint calls directly to the corresponding generated API group instance, resolved by the endpoint prefix.



216
217
218
219
220
221
222
223
224
225
# File 'lib/authentik/client.rb', line 216

def method_missing(name, ...)
  if self.class.endpoint?(name)
    group = self.class.group_by_endpoint(name)
    api_group_instance(group).public_send(name, ...)
  elsif self.class.group?(name)
    api_group_instance(name)
  else
    super
  end
end

Class Method Details

.configurationConfiguration

Returns the global Configuration instance, creating it on the first call.

Returns:



68
69
70
# File 'lib/authentik/client.rb', line 68

def configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields the global Configuration object so that settings can be applied at startup (e.g., in a Rails initializer).

Examples:

Authentik::Client.configure do |config|
  config.host  = "authentik.example.com"
  config.token = "your-api-token"
end

Yield Parameters:



61
62
63
# File 'lib/authentik/client.rb', line 61

def configure
  yield(configuration)
end

.endpoint?(name) ⇒ Boolean

Returns true if ‘name` is a known endpoint.

Parameters:

  • name (String, Symbol)

    Endpoint name identifier, e.g., ‘core_users_list`.

Returns:

  • (Boolean)

    True if ‘name` is a known endpoint.



120
121
122
# File 'lib/authentik/client.rb', line 120

def endpoint?(name)
  endpoints.include?(name.to_sym)
end

.endpointsArray<Symbol>

Returns all known API endpoints.

Returns:

  • (Array<Symbol>)


127
128
129
# File 'lib/authentik/client.rb', line 127

def endpoints
  endpoint_group_map.keys
end

.fetch_api_class_by_group(name) ⇒ Class

Returns the API class for a given API group.

Parameters:

  • name (String, Symbol)

    API group name, e.g., ‘core`.

Returns:

  • (Class)

    The API class for the group.

Raises:

  • (KeyError)

    if the group is not known.



144
145
146
# File 'lib/authentik/client.rb', line 144

def fetch_api_class_by_group(name)
  group_api_class_map.fetch(name.to_sym)
end

.group?(name) ⇒ Boolean

Returns true if ‘name` is an API group.

Parameters:

  • name (String, Symbol)

    Group name identifier, e.g., ‘core`.

Returns:

  • (Boolean)

    True if an API group of ‘name` exists.



83
84
85
# File 'lib/authentik/client.rb', line 83

def group?(name)
  groups.include?(name.downcase.to_sym)
end

.group_by_endpoint(name) ⇒ Symbol?

Returns the API group for a given endpoint.

Parameters:

  • name (String, Symbol)

    Endpoint name identifier, e.g., ‘core_users_list`.

Returns:

  • (Symbol, nil)

    The API group for the endpoint, or nil if not found.



135
136
137
# File 'lib/authentik/client.rb', line 135

def group_by_endpoint(name)
  endpoint_group_map[name.to_sym]
end

.groupsArray<Symbol>

Returns all known API groups.

Returns:

  • (Array<Symbol>)


90
91
92
# File 'lib/authentik/client.rb', line 90

def groups
  group_api_class_map.keys
end

.reset_configuration!Configuration

Resets the global configuration to a fresh Configuration instance.

Returns:



75
76
77
# File 'lib/authentik/client.rb', line 75

def reset_configuration!
  @configuration = Configuration.new
end

.resource?(name) ⇒ Boolean

Returns true if ‘name` is a known resource.

A resource is a collection of API endpoints that share a common prefix. For example, ‘:core_users` is the resource for `:core_users_list`, `:core_users_retrieve`, etc.

Parameters:

  • name (String, Symbol)

    Resource name identifier, e.g., ‘core_users`

Returns:

  • (Boolean)

    True if a resource of ‘name` exists.



102
103
104
# File 'lib/authentik/client.rb', line 102

def resource?(name)
  resources.include?(name.to_sym)
end

.resourcesObject

Returns all known API resources.



107
108
109
110
111
112
113
114
# File 'lib/authentik/client.rb', line 107

def resources
  @resources ||= begin
    remove_action_regexp = /(.*?)(_(#{RESOURCE_ACTIONS.join("|")}))?(_with_http_info)?$/
    endpoints
      .map { |endpoint| endpoint.to_s.gsub(remove_action_regexp, '\1').to_sym }
      .uniq
  end
end

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/authentik/client.rb', line 227

def respond_to_missing?(name, include_private = false)
  self.class.group?(name) || self.class.endpoint?(name) || super
end