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 +4 -4
- data/lib/ask/auth/providers/rails_credentials.rb +44 -27
- data/lib/ask/auth/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 52438e8c475956073bd3f61dcba1e5f6f6ff0619cf83532ba6429603786373e6
|
|
4
|
+
data.tar.gz: 199345260588e9813e0feddd9881fbcf7290e67dae2ed5fd259c380085927dad
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
#
|
|
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.
|
|
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.
|
|
14
|
-
# +
|
|
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.
|
|
17
|
-
# +Rails.application.credentials.
|
|
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
|
-
#
|
|
20
|
-
#
|
|
21
|
-
#
|
|
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+.
|
|
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
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
61
|
+
next unless obj
|
|
46
62
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
71
|
+
nil
|
|
55
72
|
end
|
|
56
73
|
end
|
|
57
74
|
end
|
data/lib/ask/auth/version.rb
CHANGED