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 groups are accessed as methods on the client. Each group returns an ApiProxy that forwards method calls to the corresponding OpenAPI API class, stripping the redundant API group prefix for brevity.

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, Railtie

Constant Summary collapse

VERSION =
"0.1.1"

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/authentik/client.rb', line 89

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) ⇒ ApiProxy

Provides access to an API group by name. Returns an ApiProxy that wraps the corresponding OpenAPI API class instance.

The method name is the API class name (without the “Api” suffix) in lowercase. For example, :core maps to CoreApi, :admin to AdminApi, and :oauth2 to OAuth2Api.

Returns:

  • (ApiProxy)

    A proxy wrapping the underlying API class instance

Raises:

  • (NoMethodError)

    if the name does not match any known API group



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

def method_missing(name, ...)
  api_info = self.class.api_map[name]
  return super unless api_info

  @api_instances[name] ||= ApiProxy.new(api_info[:klass].new(@api_client), api_info[:prefix])
end

Class Method Details

.api_mapHash{Symbol => Hash}

Returns a hash mapping API group name symbols to their API class and prefix. Automatically discovers all classes in Authentik::Api that end with “Api”.

Returns:

  • (Hash{Symbol => Hash})


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/authentik/client.rb', line 133

def self.api_map
  @api_map ||= Authentik::Api.constants
    .select { |c| c.to_s.end_with?("Api") }
    .filter_map do |c|
      klass = Authentik::Api.const_get(c)
      next unless klass.is_a?(Class)

      base = c.to_s.delete_suffix("Api").downcase
      [base.to_sym, {klass: klass, prefix: "#{base}_"}]
  end
    .to_h
end

.configurationConfiguration

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

Returns:



63
64
65
# File 'lib/authentik/client.rb', line 63

def self.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:



56
57
58
# File 'lib/authentik/client.rb', line 56

def self.configure
  yield(configuration)
end

.reset_configuration!Configuration

Resets the global configuration to a fresh Configuration instance.

Returns:



70
71
72
# File 'lib/authentik/client.rb', line 70

def self.reset_configuration!
  @configuration = Configuration.new
end

Instance Method Details

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

Returns:

  • (Boolean)


124
125
126
# File 'lib/authentik/client.rb', line 124

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