ask-auth 0.1.2 → 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: d839820898bcd001809176b196900faf071717a66b53d70ac4a9605b31b3aa1f
4
- data.tar.gz: dc3b21d1e383b5c7ee08d9b3798bb966d828b5f8e245c5cd5be036931092c823
3
+ metadata.gz: 52438e8c475956073bd3f61dcba1e5f6f6ff0619cf83532ba6429603786373e6
4
+ data.tar.gz: 199345260588e9813e0feddd9881fbcf7290e67dae2ed5fd259c380085927dad
5
5
  SHA512:
6
- metadata.gz: a092a91d436da5623755ca25116c28510921fad524fc7a562305fba76b94e2ec1d273d65264a847454e9e60813c742c930b2710545315c0b8e8790d303a10689
7
- data.tar.gz: a0e8feee12cadfb082abac95349febe768b8f67e0697a9287dceee9b4a4feacf1d6fcbe141969196afd23055c47675e4cadacc17a285212700f1fb6c55a732f7
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,29 +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
41
  # 1. Full name as a single top-level key
34
- return creds.public_send(s) if creds.respond_to?(s)
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("_")
35
56
 
36
- # 2. First segment as namespace, joined rest as sub-key
37
- # e.g., opencode_api_key credentials.opencode.api_key
38
- if parts.length >= 2
39
- first = parts.first
40
- rest = parts.drop(1).join("_")
41
- if creds.respond_to?(first)
42
- obj = creds.public_send(first)
43
- return obj.public_send(rest) if obj.respond_to?(rest)
57
+ obj = prefix_parts.reduce(creds) do |o, p|
58
+ break nil unless o.respond_to?(p)
59
+ o.public_send(p)
44
60
  end
45
- end
61
+ next unless obj
46
62
 
47
- # 3. Fully split path (original behavior)
48
- # e.g., opencode.go.api_key → credentials.opencode.go.api_key
49
- value = parts.reduce(creds) do |obj, part|
50
- break nil unless obj.respond_to?(part)
51
- 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
52
69
  end
53
70
 
54
- value
71
+ nil
55
72
  end
56
73
  end
57
74
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ask
4
4
  module Auth
5
- VERSION = "0.1.2"
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.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaka Ruto