Module: Ask::Auth
- Defined in:
- lib/ask/auth.rb,
lib/ask/auth/version.rb,
lib/ask/auth/oauth/http.rb,
lib/ask/auth/providers/env.rb,
lib/ask/auth/providers/xai.rb,
lib/ask/auth/providers/file.rb,
lib/ask/auth/providers/oauth.rb,
lib/ask/auth/providers/database.rb,
lib/ask/auth/providers/device_oauth.rb,
lib/ask/auth/providers/openai_codex.rb,
lib/ask/auth/providers/github_copilot.rb,
lib/ask/auth/providers/rails_credentials.rb
Overview
Defined Under Namespace
Modules: OAuth, Providers Classes: Configuration, InvalidCredential, MissingCredential, OAuthError
Constant Summary collapse
- VERSION =
"0.3.1"
Class Method Summary collapse
- .configuration ⇒ Object
- .configure {|config| ... } ⇒ Object
- .reset_configuration! ⇒ Object
-
.resolve(*names, user: nil) ⇒ Object
Walk providers in order and return the first non-nil credential.
Class Method Details
.configuration ⇒ Object
55 56 57 |
# File 'lib/ask/auth.rb', line 55 def configuration @configuration ||= default_configuration end |
.configure {|config| ... } ⇒ Object
48 49 50 51 52 53 |
# File 'lib/ask/auth.rb', line 48 def configure config = Configuration.new yield config @configuration = config @configuration.freeze end |
.reset_configuration! ⇒ Object
59 60 61 |
# File 'lib/ask/auth.rb', line 59 def reset_configuration! @configuration = nil end |
.resolve(*names, user: nil) ⇒ Object
Walk providers in order and return the first non-nil credential. Tries each name in order and returns the first match.
Each name can be:
Symbol/String → flat key, tried literally by all providers
Array → path segments, used for nested lookups (e.g., Rails credentials)
Ask::Auth.resolve(:openai_api_key) # flat key
Ask::Auth.resolve(:opencode_api_key, :opencode_go_api_key) # fallbacks
Ask::Auth.resolve([:opencode, :api_key]) # nested path: credentials.opencode.api_key
Ask::Auth.resolve(:opencode_go_api_key, [:opencode, :api_key], user: current_user)
namesOne or more Symbols, Strings, or Arrays identifying the credential
userOptional user record for per-user providers (Database, OAuth)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ask/auth.rb', line 77 def resolve(*names, user: nil) return nil if names.empty? names.each do |name| # Validate the name parts = Array(name).map { |p| p.to_s.strip } next if parts.empty? || parts.any?(&:empty?) configuration.providers.each do |provider| # Pass the original form (Symbol/Array) so providers can interpret it value = provider.call(name, user: user) next if value.nil? normalized = normalize(value) return normalized unless normalized.nil? end end raise MissingCredential, names end |