ask-auth 0.1.3 → 0.1.4

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: f8535335b52d412f3c6cd8cb70de3547d5872c21e0aac5465c1bac88a5446847
4
- data.tar.gz: 1072dfab33c51e9ab57b6ccd928f97f9f55a947dd27d542696dece9330fa0326
3
+ metadata.gz: 52438e8c475956073bd3f61dcba1e5f6f6ff0619cf83532ba6429603786373e6
4
+ data.tar.gz: 199345260588e9813e0feddd9881fbcf7290e67dae2ed5fd259c380085927dad
5
5
  SHA512:
6
- metadata.gz: 349a4863d2061377b373402ea937e9fbaf15a8928662ec3af72e7754fc3e87542bf2e62e9355fbe90140e1a61c4d5e77985b1c87a706eddda8cf8abcd4599fe2
7
- data.tar.gz: b01eabafd424f1bf3348b55f05678b6ff6aeb20b805e870cf975631d400915e992d05f64799c74e9cc53a0a37e16db332b1a9bac096be25fd413df0acd8bb100
6
+ metadata.gz: 39dd2a342b2b02afb910bc1132b8e90e211a2607d0413db69808646ac04fa91653c16d9014284f5e6a841370a497621d4eb6b83c83151c46a7d6a73739f9f815
7
+ data.tar.gz: 0dc793d15763b903da6b55b85e0791ef90c4c86f6d81059abab08211767d7d980b492c774d305dea683d884d68869dd98f9d4269abd72b43980909a3b4407c3a
@@ -5,21 +5,28 @@ module Ask
5
5
  module Providers
6
6
  # Resolves credentials from Rails encrypted credentials.
7
7
  #
8
- # Convention: +resolve(:github_token)+ looks up multiple paths in order:
8
+ # Tries multiple strategies in order, from least to most nested:
9
9
  #
10
- # 1. Full name as a single top-level key (flat layout):
11
- # +Rails.application.credentials.github_token+
10
+ # 1. Full name as a single top-level key (flat layout)
11
+ # +resolve(:openai_api_key)+ → +Rails.application.credentials.openai_api_key+
12
12
  #
13
- # 2. First segment as namespace, joined rest as sub-key (nested layout):
14
- # +Rails.application.credentials.github.token+
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
15
17
  #
16
- # 3. Fully split path (deeply nested layout):
17
- # +Rails.application.credentials.github.api.key+
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
18
22
  #
19
- # Strategy 2 handles compound key names like +api_key+ correctly.
20
- # For +resolve(:opencode_api_key)+, it looks up:
21
- # +Rails.application.credentials.opencode.api_key+
22
- # instead of breaking +api_key+ into +api+ and +key+.
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+.
23
30
  #
24
31
  # Safely returns nil when Rails is not loaded.
25
32
  class RailsCredentials
@@ -29,33 +36,39 @@ module Ask
29
36
  creds = ::Rails.application.credentials
30
37
  s = name.to_s
31
38
  parts = s.split("_")
39
+ return nil if parts.empty?
32
40
 
33
- # 1. Full name as a single top-level key (e.g., credentials.opencode_api_key)
34
- # Must check the returned value, not just respond_to?, because
35
- # ActiveSupport::OrderedOptions#respond_to? returns true for any
36
- # method name even when the key doesn't exist in the hash.
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.
37
45
  val = creds.public_send(s) rescue nil
38
46
  return val if val
39
47
 
40
- # 2. First segment as namespace, joined rest as sub-key
41
- # e.g., opencode_api_key credentials.opencode.api_key
42
- if parts.length >= 2
43
- first = parts.first
44
- rest = parts.drop(1).join("_")
45
- if creds.respond_to?(first)
46
- obj = creds.public_send(first)
47
- return obj.public_send(rest) if obj.respond_to?(rest)
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)
48
60
  end
49
- end
61
+ next unless obj
50
62
 
51
- # 3. Fully split path (original behavior)
52
- # e.g., opencode.go.api_key → credentials.opencode.go.api_key
53
- value = parts.reduce(creds) do |obj, part|
54
- break nil unless obj.respond_to?(part)
55
- obj.public_send(part)
63
+ begin
64
+ val = obj.public_send(key)
65
+ return val if val
66
+ rescue NoMethodError
67
+ # respond_to? can lie for some credential objects
68
+ end
56
69
  end
57
70
 
58
- value
71
+ nil
59
72
  end
60
73
  end
61
74
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ask
4
4
  module Auth
5
- VERSION = "0.1.3"
5
+ VERSION = "0.1.4"
6
6
  end
7
7
  end
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.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaka Ruto