Module: Rubino::OAuth::Registry

Defined in:
lib/rubino/oauth/registry.rb

Overview

Process-wide registry of configured OAuth providers. Mutex-protected so register / reset! are safe under concurrent boot or reload.

Hydrated from oauth.providers.* in Rubino.configuration; only ids listed in BUILTINS are considered, and any section missing both client_id and client_secret is silently skipped so a partial config never raises at boot (it just hides the provider from /v1/oauth/providers).

Constant Summary collapse

BUILTINS =
{
  github: "Rubino::OAuth::Provider::Github",
  google: "Rubino::OAuth::Provider::Google"
}.freeze

Class Method Summary collapse

Class Method Details

.allObject



36
37
38
# File 'lib/rubino/oauth/registry.rb', line 36

def all
  providers.values
end

.fetch(id) ⇒ Provider

Parameters:

  • id (String, Symbol)

Returns:

Raises:



28
29
30
# File 'lib/rubino/oauth/registry.rb', line 28

def fetch(id)
  providers[id.to_sym] or raise NotFoundError.new("oauth_provider", id)
end

.fetch_or_nil(id) ⇒ Object



32
33
34
# File 'lib/rubino/oauth/registry.rb', line 32

def fetch_or_nil(id)
  providers[id.to_sym]
end

.idsObject



40
41
42
# File 'lib/rubino/oauth/registry.rb', line 40

def ids
  providers.keys
end

.load_from_config!(configuration = Rubino.configuration) ⇒ Array<Provider>

Hydrate from the loaded Rubino configuration. Reads oauth.providers.* sections; for each id matching a BUILTIN, instantiates and registers using its declared client_id/client_secret/scopes. Replaces any previously registered providers (#reset! runs first).

Parameters:

  • configuration (#dig) (defaults to: Rubino.configuration)

    anything responding to dig(“oauth”, “providers”)

Returns:

  • (Array<Provider>)

    providers registered by this call



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubino/oauth/registry.rb', line 55

def load_from_config!(configuration = Rubino.configuration)
  reset!
  oauth_cfg = configuration.dig("oauth", "providers") || {}
  oauth_cfg.each do |id, cfg|
    klass_name = BUILTINS[id.to_sym]
    next unless klass_name
    next unless cfg["client_id"] && cfg["client_secret"]

    klass = Object.const_get(klass_name)
    register(id, klass.new(
                   client_id: cfg["client_id"],
                   client_secret: cfg["client_secret"],
                   scopes: cfg["scopes"],
                   metadata: cfg.reject { |k, _| %w[client_id client_secret scopes].include?(k) }
                 ))
  end
  all
end

.register(id, instance) ⇒ Object



20
21
22
23
# File 'lib/rubino/oauth/registry.rb', line 20

def register(id, instance)
  mutex.synchronize { providers[id.to_sym] = instance }
  instance
end

.reset!Object



44
45
46
# File 'lib/rubino/oauth/registry.rb', line 44

def reset!
  mutex.synchronize { providers.clear }
end