ask-auth 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 899769786d686dad5733fb428f4947d077af5e4913573558890ac309dc246d7e
4
- data.tar.gz: 8dad782619a917d310858ec90136b86638fdffe64e71060c72ba8af42f91a41f
3
+ metadata.gz: d25e993c78fcede6b8d50972bfc8b99b5a30a03d85cf9c725eb5d5f3ae728725
4
+ data.tar.gz: d9792c086a2d3f84ecdbd5760bfd5ec02bb0074d3cb57670d8df5c7b3280015d
5
5
  SHA512:
6
- metadata.gz: 69064653971ef660f7c2aa2f297f5a1119888a18b5521ebee9c4bbf1058f474ffb70275d9cf60dec044ee94565520d9e0fc9defc782ee4b74505e17b01d4a6c4
7
- data.tar.gz: e819dd4a9a9bac4a5d9cca0064d750610b16c8d1baf3de33cd4bb792ec3292279091a271e0c34ccfa85738ff228cd798384e503ef4cd909aae96b4e66b118fca
6
+ metadata.gz: 29a7da04a8d494a59ace1c34840d7b57dead0277f19f02b6e824e30eb9511df5cd6453cfad4ce829a95666f4b9cd5d9f5ae3e43d6b67d549dcbb345c77f51c92
7
+ data.tar.gz: 94757f55995fac37460342c50ccbcac891096dc9f5a4b6c429d5f431d181a13556364301c065f3c2c15208a20209de48d33149ba6987bfdd11b8476f4c2cc819
@@ -24,6 +24,7 @@ module Ask
24
24
  data = load_file
25
25
  return nil unless data
26
26
 
27
+ return nil unless name.respond_to?(:to_s)
27
28
  value = data[name.to_s] || data[name.to_sym]
28
29
  value
29
30
  end
@@ -5,28 +5,17 @@ module Ask
5
5
  module Providers
6
6
  # Resolves credentials from Rails encrypted credentials.
7
7
  #
8
- # Tries multiple strategies in order, from least to most nested:
8
+ # Supports two lookup modes determined by the +name+ argument type:
9
9
  #
10
- # 1. Full name as a single top-level key (flat layout)
11
- # +resolve(:openai_api_key)+ +Rails.application.credentials.openai_api_key+
10
+ # Symbol/String → flat key, looked up literally
11
+ # resolve(:opencode_api_key) → credentials.opencode_api_key
12
+ # resolve("opencode_api_key") → credentials.opencode_api_key
12
13
  #
13
- # 2. Progressively split into prefix + joined suffix
14
- # +resolve(:opencode_api_key)+ tries:
15
- # a. +Rails.application.credentials.opencode.api_key+ ← most common
16
- # b. +Rails.application.credentials.opencode.api.key+ ← fully split
14
+ # Array → path segments, navigated in order
15
+ # resolve([:opencode, :api_key]) credentials.opencode.api_key
16
+ # resolve([:opencode, :go, :api_key]) → credentials.opencode.go.api_key
17
17
  #
18
- # 3. For compound provider slugs, +resolve(:opencode_go_api_key)+ tries:
19
- # a. +Rails.application.credentials.opencode.go_api_key+ ← slug as single key
20
- # b. +Rails.application.credentials.opencode.go.api_key+ ← slug + api_key
21
- # c. +Rails.application.credentials.opencode.go.api.key+ ← fully split
22
- #
23
- # 4. For deeply nested, +resolve(:nvidia_nim_api_key)+ tries:
24
- # a. +Rails.application.credentials.nvidia.nim_api_key+ ← nvidia + nim_api_key
25
- # b. +Rails.application.credentials.nvidia.nim.api_key+ ← nvidia.nim + api_key
26
- # c. +Rails.application.credentials.nvidia.nim.api.key+ ← fully split
27
- #
28
- # Strategy 2b/3b/4b keep +api_key+ intact as a single credential key
29
- # instead of breaking it into +api+ and +key+.
18
+ # No splitting, no guessing. The caller determines the lookup path.
30
19
  #
31
20
  # Safely returns nil when Rails is not loaded.
32
21
  class RailsCredentials
@@ -34,41 +23,23 @@ module Ask
34
23
  return nil unless defined?(::Rails) && ::Rails.application.respond_to?(:credentials)
35
24
 
36
25
  creds = ::Rails.application.credentials
37
- s = name.to_s
38
- parts = s.split("_")
39
- return nil if parts.empty?
40
-
41
- # 1. Full name as a single top-level key
42
- # Must check the value, not just respond_to?, because
43
- # ActiveSupport::OrderedOptions#respond_to? returns true
44
- # for any method name, even when the key doesn't exist.
45
- val = creds.public_send(s) rescue nil
46
- return val if val
47
-
48
- # 2. Try progressively longer prefixes, keeping the remainder
49
- # joined as a single key name at each level.
50
- # split_at=1: parts[0] as prefix, parts[1..] joined as key
51
- # split_at=2: parts[0..1] as prefix, parts[2..] joined as key
52
- # etc.
53
- (1...parts.length).each do |split_at|
54
- prefix_parts = parts.take(split_at)
55
- key = parts.drop(split_at).join("_")
56
-
57
- obj = prefix_parts.reduce(creds) do |o, p|
58
- break nil unless o.respond_to?(p)
59
- o.public_send(p)
60
- end
61
- next unless obj
26
+ parts = Array(name).map { |p| p.to_s.strip }
27
+ return nil if parts.empty? || parts.any?(&:empty?)
62
28
 
29
+ # Navigate the path segments. Must check the VALUE, not just
30
+ # respond_to?, because ActiveSupport::OrderedOptions#respond_to?
31
+ # returns true for any method name, even when the key doesn't exist.
32
+ value = parts.reduce(creds) do |obj, part|
63
33
  begin
64
- val = obj.public_send(key)
65
- return val if val
34
+ val = obj.public_send(part)
35
+ break nil if val.nil?
36
+ val
66
37
  rescue NoMethodError
67
- # respond_to? can lie for some credential objects
38
+ break nil
68
39
  end
69
40
  end
70
41
 
71
- nil
42
+ value
72
43
  end
73
44
  end
74
45
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ask
4
4
  module Auth
5
- VERSION = "0.2.0"
5
+ VERSION = "0.2.2"
6
6
  end
7
7
  end
data/lib/ask/auth.rb CHANGED
@@ -51,20 +51,29 @@ module Ask
51
51
  end
52
52
 
53
53
  # Walk providers in order and return the first non-nil credential.
54
- # Accepts one or more credential names tries each in order,
55
- # returning the first match across all providers.
54
+ # Tries each name in order and returns the first match.
56
55
  #
57
- # Ask::Auth.resolve(:openai_api_key)
58
- # Ask::Auth.resolve(:opencode_go_api_key, :opencode_api_key, user: current_user)
56
+ # Each +name+ can be:
57
+ # Symbol/String → flat key, tried literally by all providers
58
+ # Array → path segments, used for nested lookups (e.g., Rails credentials)
59
59
  #
60
- # +names+:: One or more Symbols or Strings identifying the credential
60
+ # Ask::Auth.resolve(:openai_api_key) # flat key
61
+ # Ask::Auth.resolve(:opencode_api_key, :opencode_go_api_key) # fallbacks
62
+ # Ask::Auth.resolve([:opencode, :api_key]) # nested path: credentials.opencode.api_key
63
+ # Ask::Auth.resolve(:opencode_go_api_key, [:opencode, :api_key], user: current_user)
64
+ #
65
+ # +names+:: One or more Symbols, Strings, or Arrays identifying the credential
61
66
  # +user+:: Optional user record for per-user providers (Database, OAuth)
62
67
  def resolve(*names, user: nil)
63
- names = names.flatten.map { |n| n.to_s.strip }.reject(&:empty?)
64
68
  return nil if names.empty?
65
69
 
66
70
  names.each do |name|
71
+ # Validate the name
72
+ parts = Array(name).map { |p| p.to_s.strip }
73
+ next if parts.empty? || parts.any?(&:empty?)
74
+
67
75
  configuration.providers.each do |provider|
76
+ # Pass the original form (Symbol/Array) so providers can interpret it
68
77
  value = provider.call(name, user: user)
69
78
  next if value.nil?
70
79
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ask-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaka Ruto