@voxgig/sdkgen 4.48.0 → 4.49.0

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.
Files changed (174) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/dist/tsconfig.tsbuildinfo +1 -1
  3. package/dist/utility.js +9 -1
  4. package/dist/utility.js.map +1 -1
  5. package/package.json +1 -1
  6. package/project/.sdk/model/feature/secrets.aontu +3 -0
  7. package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
  8. package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
  9. package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
  10. package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
  11. package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
  12. package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
  13. package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
  14. package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
  15. package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
  16. package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
  17. package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
  18. package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
  19. package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
  20. package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
  21. package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
  22. package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
  23. package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
  24. package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
  25. package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
  26. package/project/.sdk/tm/c/feature/cache.c +16 -1
  27. package/project/.sdk/tm/c/feature/secrets.c +149 -18
  28. package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
  29. package/project/.sdk/tm/c/tests/feature_test.c +31 -0
  30. package/project/.sdk/tm/c/utility/make_spec.c +1 -1
  31. package/project/.sdk/tm/c/utility/prepare_body.c +64 -1
  32. package/project/.sdk/tm/c/utility/prepare_query.c +11 -6
  33. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
  34. package/project/.sdk/tm/clojure/src/sdk/core.clj +48 -10
  35. package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
  36. package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
  37. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
  38. package/project/.sdk/tm/cpp/core/types.hpp +1 -11
  39. package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
  40. package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
  41. package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
  42. package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
  43. package/project/.sdk/tm/cpp/utility/pipeline.hpp +52 -4
  44. package/project/.sdk/tm/csharp/core/Control.cs +0 -5
  45. package/project/.sdk/tm/csharp/core/Point.cs +6 -0
  46. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
  47. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
  48. package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
  49. package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
  50. package/project/.sdk/tm/csharp/utility/MakeSpec.cs +2 -2
  51. package/project/.sdk/tm/csharp/utility/PrepareBody.cs +42 -0
  52. package/project/.sdk/tm/csharp/utility/PrepareQuery.cs +13 -2
  53. package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
  54. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
  55. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
  56. package/project/.sdk/tm/elixir/lib/projectname/utility.ex +62 -6
  57. package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
  58. package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
  59. package/project/.sdk/tm/go/core/target.go +8 -0
  60. package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
  61. package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
  62. package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
  63. package/project/.sdk/tm/go/test/feature_test.go +32 -0
  64. package/project/.sdk/tm/go/utility/media.go +2 -2
  65. package/project/.sdk/tm/go/utility/prepare_body.go +37 -1
  66. package/project/.sdk/tm/go/utility/prepare_query.go +10 -1
  67. package/project/.sdk/tm/java/core/Control.java +0 -5
  68. package/project/.sdk/tm/java/core/Point.java +7 -0
  69. package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
  70. package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
  71. package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
  72. package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
  73. package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
  74. package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
  75. package/project/.sdk/tm/java/utility/MakeSpec.java +1 -1
  76. package/project/.sdk/tm/java/utility/Media.java +2 -2
  77. package/project/.sdk/tm/java/utility/PrepareBody.java +50 -0
  78. package/project/.sdk/tm/java/utility/PrepareQuery.java +11 -1
  79. package/project/.sdk/tm/js/src/Point.js +2 -0
  80. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
  81. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
  82. package/project/.sdk/tm/js/src/utility/MakeSpecUtility.js +1 -1
  83. package/project/.sdk/tm/js/src/utility/PrepareBodyUtility.js +29 -0
  84. package/project/.sdk/tm/js/src/utility/PrepareQueryUtility.js +5 -3
  85. package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
  86. package/project/.sdk/tm/js/test/feature.test.js +17 -0
  87. package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
  88. package/project/.sdk/tm/kotlin/core/Point.kt +7 -0
  89. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
  90. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
  91. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
  92. package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
  93. package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
  94. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
  95. package/project/.sdk/tm/kotlin/utility/MakeSpecUrl.kt +1 -1
  96. package/project/.sdk/tm/kotlin/utility/Prepare.kt +40 -10
  97. package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
  98. package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
  99. package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
  100. package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
  101. package/project/.sdk/tm/lua/utility/clean.lua +1 -0
  102. package/project/.sdk/tm/lua/utility/make_spec.lua +1 -1
  103. package/project/.sdk/tm/lua/utility/prepare_body.lua +40 -0
  104. package/project/.sdk/tm/lua/utility/prepare_query.lua +11 -1
  105. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
  106. package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
  107. package/project/.sdk/tm/ocaml/sdk_runtime.ml +40 -5
  108. package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
  109. package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
  110. package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
  111. package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
  112. package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
  113. package/project/.sdk/tm/perl/t/feature.t +24 -0
  114. package/project/.sdk/tm/perl/utility/make_spec.pm +1 -1
  115. package/project/.sdk/tm/perl/utility/prepare_body.pm +38 -4
  116. package/project/.sdk/tm/perl/utility/prepare_query.pm +7 -1
  117. package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
  118. package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
  119. package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
  120. package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
  121. package/project/.sdk/tm/php/utility/MakeSpec.php +2 -2
  122. package/project/.sdk/tm/php/utility/PrepareBody.php +31 -0
  123. package/project/.sdk/tm/php/utility/PrepareQuery.php +9 -2
  124. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
  125. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
  126. package/project/.sdk/tm/py/pkg/utility/make_spec.py +1 -1
  127. package/project/.sdk/tm/py/pkg/utility/prepare_body.py +26 -0
  128. package/project/.sdk/tm/py/pkg/utility/prepare_query.py +7 -2
  129. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
  130. package/project/.sdk/tm/py/test/test_feature.py +18 -0
  131. package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
  132. package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
  133. package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
  134. package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
  135. package/project/.sdk/tm/rb/utility/make_spec.rb +1 -1
  136. package/project/.sdk/tm/rb/utility/prepare_body.rb +31 -3
  137. package/project/.sdk/tm/rb/utility/prepare_query.rb +7 -1
  138. package/project/.sdk/tm/rust/feature/cache.rs +15 -1
  139. package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
  140. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
  141. package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
  142. package/project/.sdk/tm/rust/utility/prepare_body.rs +51 -0
  143. package/project/.sdk/tm/rust/utility/prepare_query.rs +12 -4
  144. package/project/.sdk/tm/scala/core/Control.scala +0 -5
  145. package/project/.sdk/tm/scala/core/Point.scala +3 -0
  146. package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
  147. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
  148. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
  149. package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
  150. package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
  151. package/project/.sdk/tm/scala/utility/MakeSpec.scala +1 -1
  152. package/project/.sdk/tm/scala/utility/Prepare.scala +53 -12
  153. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
  154. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Point.swift +3 -0
  155. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
  156. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
  157. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Make.swift +1 -1
  158. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Prepare.swift +36 -1
  159. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
  160. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
  161. package/project/.sdk/tm/ts/src/Point.ts +3 -0
  162. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
  163. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
  164. package/project/.sdk/tm/ts/src/utility/PrepareBodyUtility.ts +29 -0
  165. package/project/.sdk/tm/ts/src/utility/PrepareQueryUtility.ts +5 -3
  166. package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
  167. package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
  168. package/project/.sdk/tm/zig/core/utility.zig +47 -4
  169. package/project/.sdk/tm/zig/feature/cache.zig +15 -1
  170. package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
  171. package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
  172. package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
  173. package/project/sdkgen-package.json +1 -1
  174. package/src/utility.ts +12 -1
@@ -55,6 +55,23 @@ require_relative '../utility/struct/voxgig_struct'
55
55
  require_relative 'secrets/voxgig_sekreto'
56
56
 
57
57
  class ProjectNameSecretsFeature < ProjectNameBaseFeature
58
+ REDIRECT_MAX = 5
59
+
60
+ # Where a method-keeping redirect points, when that is on the exchange's
61
+ # own origin. Any other redirect would drop the body or leave the origin.
62
+ def self.moved(url, at, res)
63
+ return nil unless res.is_a?(Hash) && [307, 308].include?(res["status"].to_i)
64
+ headers = res["headers"]
65
+ location = headers.is_a?(Hash) ? headers["location"] : nil
66
+ return nil unless location.is_a?(String) && !location.empty?
67
+ nxt = URI.join(at, location)
68
+ here = URI.parse(url)
69
+ origin = ->(u) { [u.scheme.to_s.downcase, u.host.to_s.downcase, u.port] }
70
+ origin.call(nxt) == origin.call(here) ? nxt.to_s : nil
71
+ rescue URI::Error, ArgumentError
72
+ nil
73
+ end
74
+
58
75
  def initialize
59
76
  super
60
77
  @version = "0.1.0"
@@ -65,6 +82,8 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
65
82
  @client = nil
66
83
  @options = {}
67
84
  @liveopts = {}
85
+ @declared = false
86
+ @basic = false
68
87
 
69
88
  @secretname = "apikey"
70
89
  @cache = true
@@ -102,6 +121,12 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
102
121
  @liveopts = ctx.options.is_a?(Hash) ? ctx.options : {}
103
122
  @active = @options["active"] == true
104
123
 
124
+ # What prepare_auth was generated from: the config declares auth only
125
+ # where it places a credential, and Basic only where it composes one.
126
+ declared = VoxgigStruct.getpath(ctx.config, "options.auth")
127
+ @declared = declared.is_a?(Hash)
128
+ @basic = @declared && declared["basic"] == true
129
+
105
130
  return unless @active
106
131
 
107
132
  @secretname = _str(@options["name"], "apikey")
@@ -127,6 +152,7 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
127
152
  "response" => _str(xopts["response"], "access_token"),
128
153
  "statuses" => statuses,
129
154
  "retries" => xopts["retries"].is_a?(Numeric) ? xopts["retries"].to_i : 1,
155
+ "redirect" => "same-origin" == xopts["redirect"] ? "same-origin" : "refuse",
130
156
  }
131
157
  end
132
158
 
@@ -261,6 +287,12 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
261
287
 
262
288
  private
263
289
 
290
+ # prepare_auth's decision: auth the config declares, not suppressed by
291
+ # `auth: nil`.
292
+ def _authon
293
+ @declared && !@liveopts["auth"].nil?
294
+ end
295
+
264
296
  # Every value this feature resolves or buys is a secret the SDK handles,
265
297
  # and none arrives under an option key the intake registration saw.
266
298
  def _register(value)
@@ -274,7 +306,7 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
274
306
  # boolean is the whole of what resolve needs to tell a cacheable HIT from
275
307
  # a miss it must not keep.
276
308
  def _resolve_once
277
- return false if @sekreto.nil?
309
+ return false if @sekreto.nil? || !_authon
278
310
 
279
311
  # Miss-vs-error: `try` answers nil for "no store has it" and RAISES for
280
312
  # "a store could not answer" - only the miss falls through.
@@ -311,15 +343,6 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
311
343
  # is the same path expiry takes anyway.
312
344
  return true unless apikey.nil? || apikey.empty?
313
345
 
314
- # `auth: nil` is the documented way to send NO credential, and a
315
- # purchase is a credential-bearing call: the refresh token goes to the
316
- # token endpoint in the request body. _with_refresh honours suppression
317
- # for the RETRY, but it runs after this - by then the refresh token has
318
- # already left the process, and no later check can call it back. The
319
- # suppression has to be honoured here, before the first purchase, or it
320
- # only ever half-held.
321
- return false if @liveopts["auth"].nil?
322
-
323
346
  _buy
324
347
 
325
348
  true
@@ -332,12 +355,9 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
332
355
  # because the header was built by the synchronous prepare_auth before
333
356
  # this request left, and it carries the token that just failed.
334
357
  def _with_refresh(ctx, url, fetchdef, inner)
335
- # `auth: nil` is the documented way to send NO credential, and
336
- # prepare_auth honours it by removing the header. A refusal of a
337
- # deliberately unauthenticated request is not an expired token and
338
- # cannot be fixed by buying one - retrying would transmit exactly the
339
- # credential the caller suppressed.
340
- return inner.call(ctx, url, fetchdef) if @liveopts["auth"].nil?
358
+ # A refusal of an unauthenticated request is not an expired token, and
359
+ # retrying would transmit the credential that auth off withholds.
360
+ return inner.call(ctx, url, fetchdef) unless _authon
341
361
 
342
362
  max = @exchange["retries"]
343
363
  attempt = 0
@@ -449,13 +469,26 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
449
469
  # feature wraps, and sending the token request back through it would
450
470
  # recurse on the first expiry - and would route the exchange through
451
471
  # the test mock, which knows nothing about it.
452
- res, err = fetch.call(url, {
472
+ fetchdef = {
453
473
  "method" => x["method"],
454
474
  "headers" => { "content-type" => "application/json" },
455
475
  "body" => JSON.generate({ x["request"] => @refresh }),
456
- # Never followed: a redirect would carry the refresh token on.
476
+ # Never followed by the client: a redirect would carry the refresh token on.
457
477
  "redirect" => "manual",
458
- })
478
+ }
479
+
480
+ res, err = fetch.call(url, fetchdef)
481
+
482
+ # By option alone, and only while the exchange stays on its own origin.
483
+ at = url
484
+ hops = 0
485
+ while err.nil? && "same-origin" == x["redirect"] && hops < REDIRECT_MAX
486
+ nxt = ProjectNameSecretsFeature.moved(url, at, res)
487
+ break if nxt.nil?
488
+ at = nxt
489
+ hops += 1
490
+ res, err = fetch.call(at, fetchdef)
491
+ end
459
492
 
460
493
  unless err.nil?
461
494
  raise VoxgigSekreto::SekretoError,
@@ -525,16 +558,26 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
525
558
  name = name.is_a?(String) && !name.empty? ? name.downcase : "authorization"
526
559
 
527
560
  # Suppressed auth means NO header, the same answer prepare_auth gives.
528
- if @liveopts["auth"].nil? || token.nil? || token.empty?
561
+ if !_authon || token.nil? || token.empty?
529
562
  headers.delete(name)
530
563
  return
531
564
  end
532
565
 
566
+ value = @basic && VoxgigStruct.getpath(@liveopts, "auth.basic") == true ? _pair(token) : token
567
+
533
568
  prefix = VoxgigStruct.getpath(@liveopts, "auth.prefix")
534
569
  prefix = "" unless prefix.is_a?(String)
535
570
 
536
571
  # Empty prefix (raw apiKey credential) must not add a leading space.
537
- headers[name] = prefix.empty? ? token : "#{prefix} #{token}"
572
+ headers[name] = prefix.empty? ? value : "#{prefix} #{value}"
573
+ end
574
+
575
+ # Under Basic the token is the key half, as apikey is: prepare_auth's pair.
576
+ def _pair(token)
577
+ secret = @liveopts["secret"]
578
+ pair = ["#{token}:#{secret.is_a?(String) ? secret : ""}"].pack("m0")
579
+ _register(pair)
580
+ pair
538
581
  end
539
582
 
540
583
  # The plugin DEFINITIONS the model selected for this feature, emitted by
@@ -591,8 +634,12 @@ class ProjectNameSecretsFeature < ProjectNameBaseFeature
591
634
  # A non-2xx answer IS a response: hand back its status so the caller
592
635
  # reports "token exchange failed: <status>" rather than a transport
593
636
  # error.
637
+ resp_headers = {}
638
+ resp.each_header { |k, v| resp_headers[k.downcase] = v }
639
+
594
640
  return {
595
641
  "status" => resp.code.to_i,
642
+ "headers" => resp_headers,
596
643
  "json" => -> {
597
644
  begin
598
645
  raw.nil? || raw.empty? ? nil : JSON.parse(raw)
@@ -43,6 +43,16 @@ class SecretsFeatureTest < Minitest::Test
43
43
  name.is_a?(String) && !name.empty? ? name.downcase : "authorization"
44
44
  end
45
45
 
46
+ # The form prepare_auth writes, from the DEFAULT client's auth: under Basic
47
+ # the token is the key half of the encoded pair, and a prefix is
48
+ # space-joined.
49
+ CREDFORM = begin
50
+ auth = ProjectNameSDK.test(nil, nil).options_map["auth"]
51
+ auth = {} unless auth.is_a?(Hash)
52
+ prefix = auth["prefix"].is_a?(String) ? auth["prefix"] : ""
53
+ { "prefix" => prefix, "basic" => auth["basic"] == true }
54
+ end
55
+
46
56
  # ---------------------------------------------------------------------
47
57
  # The recording transport: system.fetch for a LIVE client, scripting one
48
58
  # status per API call (the last repeating) and a token endpoint for the
@@ -75,6 +85,7 @@ class SecretsFeatureTest < Minitest::Test
75
85
  "url" => fullurl,
76
86
  "auth" => headers[CREDHEADER],
77
87
  "has" => headers.key?(CREDHEADER),
88
+ "values" => headers.values.map(&:to_s).join("\n"),
78
89
  "body" => fetchdef["body"],
79
90
  "redirect" => fetchdef["redirect"],
80
91
  }
@@ -217,12 +228,15 @@ class SecretsFeatureTest < Minitest::Test
217
228
  # -------------------------------------------------------------------
218
229
  # Support.
219
230
 
220
- # The credential header carries the SPEC's credential prefix (if any),
221
- # which a TEMPLATE cannot know - so assert on the CREDENTIAL and let the
222
- # prefix be whatever this SDK's API declares.
223
- def assert_credential(header, token)
224
- ok = header == token || header.to_s.end_with?(" " + token)
225
- assert ok, "expected the #{CREDHEADER} header to carry #{token}, got: #{header.inspect}"
231
+ # The header value prepare_auth writes for a token.
232
+ def credential(token, secret = "")
233
+ value = CREDFORM["basic"] ? ["#{token}:#{secret}"].pack("m0") : token
234
+ CREDFORM["prefix"].empty? ? value : "#{CREDFORM["prefix"]} #{value}"
235
+ end
236
+
237
+ def assert_credential(header, token, secret = "")
238
+ assert_equal credential(token, secret), header,
239
+ "expected the #{CREDHEADER} header to carry #{token}"
226
240
  end
227
241
 
228
242
  def secrets_feature_of(client)
@@ -351,7 +365,9 @@ class SecretsFeatureTest < Minitest::Test
351
365
 
352
366
  # Read from the config, never from a probe: an SDK whose config declares no
353
367
  # auth places no credential, and these tests assert on the one it places.
368
+ # The auth-off test asserts it places none.
354
369
  def setup
370
+ return if name.start_with?("test_auth_off_")
355
371
  unless ProjectNameConfig.shared_config["options"].key?("auth")
356
372
  skip("this SDK's config declares no auth, so it places no credential")
357
373
  end
@@ -417,6 +433,17 @@ class SecretsFeatureTest < Minitest::Test
417
433
  clearenv("APIKEY")
418
434
  end
419
435
 
436
+ def test_a_resolved_key_is_placed_as_prepare_auth_places_it_the_secret_kept
437
+ setenv("APIKEY", "ENVKEY04")
438
+ wire = Wire.new
439
+ client = secrets_client(wire, { "secret" => "PASS01", "feature" => secrets_opts })
440
+
441
+ drive_op(client, wire)
442
+ assert_credential(wire.api[0]["auth"], "ENVKEY04", "PASS01")
443
+ ensure
444
+ clearenv("APIKEY")
445
+ end
446
+
420
447
  def test_custom_provider_objects_are_accepted_verbatim
421
448
  asked = []
422
449
  wire = Wire.new
@@ -780,6 +807,29 @@ class SecretsFeatureTest < Minitest::Test
780
807
  clearenv("REFRESH_TOKEN")
781
808
  end
782
809
 
810
+ def test_a_token_bought_on_a_refusal_is_placed_as_prepare_auth_places_it
811
+ setenv("REFRESH_TOKEN", "REFRESH01")
812
+ wire = Wire.new
813
+ wire.apistatus = [401, 200]
814
+
815
+ client = secrets_client(wire, {
816
+ "secret" => "PASS01",
817
+ "feature" => secrets_opts({
818
+ "name" => "refresh_token",
819
+ "exchange" => { "active" => true },
820
+ }),
821
+ })
822
+
823
+ drive_op(client, wire)
824
+
825
+ api = wire.api
826
+ assert_equal 2, api.length, "expected the request to be retried exactly once"
827
+ assert_credential(api[0]["auth"], "ACCESS01", "PASS01")
828
+ assert_credential(api[1]["auth"], "ACCESS02", "PASS01")
829
+ ensure
830
+ clearenv("REFRESH_TOKEN")
831
+ end
832
+
783
833
  # `auth: nil` SUPPRESSES THE PURCHASE, not just the retry.
784
834
  #
785
835
  # resolve runs before _with_refresh's suppression check, so the refresh
@@ -925,12 +975,12 @@ class SecretsFeatureTest < Minitest::Test
925
975
  assert_credential(server.auth, "RAWTOK01")
926
976
  end
927
977
 
928
- # Real listeners and the raw fallback: a stub follows nothing.
929
- def test_a_redirect_answering_the_exchange_is_a_failure_and_nothing_follows_it
930
- elsewhere = []
931
- followed = []
932
- answer = [307, ""]
978
+ # Real listeners and the raw fallback: a stub follows nothing. The
979
+ # origin answers the exchange as o["answer"] says and records what else it gets.
980
+ def with_origins
981
+ o = { "elsewhere" => [], "followed" => [], "exchanged" => 0, "answer" => [307, ""] }
933
982
  lock = Mutex.new
983
+ o["lock"] = lock
934
984
 
935
985
  listen = lambda do |&handle|
936
986
  server = TCPServer.new("127.0.0.1", 0)
@@ -964,50 +1014,74 @@ class SecretsFeatureTest < Minitest::Test
964
1014
  end
965
1015
 
966
1016
  other, otherthread = listen.call do |method, path, body|
967
- lock.synchronize { elsewhere << { "method" => method, "path" => path, "body" => body } }
1017
+ lock.synchronize { o["elsewhere"] << { "method" => method, "path" => path, "body" => body } }
968
1018
  ok.call('{"access_token":"ELSEWHERE01"}')
969
1019
  end
970
- origin, originthread = listen.call do |method, path, _body|
1020
+ origin, originthread = listen.call do |method, path, body|
971
1021
  if "/api/auth/token" == path
972
- status, location = lock.synchronize { answer }
1022
+ status, location = lock.synchronize do
1023
+ o["exchanged"] += 1
1024
+ o["answer"]
1025
+ end
973
1026
  "HTTP/1.1 #{status} Redirect\r\nlocation: #{location}\r\n" \
974
1027
  "content-length: 0\r\nconnection: close\r\n\r\n"
975
1028
  else
976
- lock.synchronize { followed << "#{method} #{path}" }
1029
+ lock.synchronize { o["followed"] << "#{method} #{path} #{body}".strip }
977
1030
  ok.call('{"access_token":"MOVED01"}')
978
1031
  end
979
1032
  end
980
1033
 
981
- base = "http://127.0.0.1:#{origin.addr[1]}/api"
982
- away = "http://127.0.0.1:#{other.addr[1]}/api/auth/token"
1034
+ o["origin"] = "http://127.0.0.1:#{origin.addr[1]}"
1035
+ o["base"] = o["origin"] + "/api"
1036
+ o["away"] = "http://127.0.0.1:#{other.addr[1]}/api/auth/token"
983
1037
 
984
1038
  begin
1039
+ yield o
1040
+ ensure
1041
+ origin.close rescue nil
1042
+ other.close rescue nil
1043
+ originthread.join
1044
+ otherthread.join
1045
+ end
1046
+ end
1047
+
1048
+ def redirect_client(o, exchange = {})
1049
+ with_secrets do |extend_it|
1050
+ opts = {
1051
+ "base" => o["base"],
1052
+ "allow" => { "op" => "direct" },
1053
+ "feature" => { "secrets" => {
1054
+ "active" => true,
1055
+ "name" => "refresh_token",
1056
+ "exchange" => { "active" => true, "refresh" => "REFRESH01" }.merge(exchange),
1057
+ } },
1058
+ }
1059
+ opts["extend"] = [ProjectNameSecretsFeature.new] if extend_it
1060
+ ProjectNameSDK.new(opts)
1061
+ end
1062
+ end
1063
+
1064
+ def redirect_probe(client)
1065
+ res = client.direct({ "path" => "/probe" })
1066
+ [res, res.is_a?(Hash) && !res["err"].nil? ? res["err"].to_s : res.inspect]
1067
+ rescue StandardError => e
1068
+ [nil, e.message]
1069
+ end
1070
+
1071
+ def test_a_redirect_answering_the_exchange_is_a_failure_and_nothing_follows_it
1072
+ with_origins do |o|
1073
+ base = o["base"]
1074
+ away = o["away"]
1075
+ elsewhere = o["elsewhere"]
1076
+ followed = o["followed"]
1077
+
985
1078
  answers = [[307, away], [308, away], [302, away], [307, "/api/moved/token"]]
986
1079
  allowed = []
987
1080
  refused = 0
988
1081
 
989
1082
  answers.each do |a|
990
- lock.synchronize { answer = a }
991
- client = with_secrets do |extend_it|
992
- opts = {
993
- "base" => base,
994
- "allow" => { "op" => "direct" },
995
- "feature" => { "secrets" => {
996
- "active" => true,
997
- "name" => "refresh_token",
998
- "exchange" => { "active" => true, "refresh" => "REFRESH01" },
999
- } },
1000
- }
1001
- opts["extend"] = [ProjectNameSecretsFeature.new] if extend_it
1002
- ProjectNameSDK.new(opts)
1003
- end
1004
-
1005
- message = begin
1006
- res = client.direct({ "path" => "/probe" })
1007
- res.is_a?(Hash) && !res["err"].nil? ? res["err"].to_s : res.inspect
1008
- rescue StandardError => e
1009
- e.message
1010
- end
1083
+ o["lock"].synchronize { o["answer"] = a }
1084
+ _res, message = redirect_probe(redirect_client(o))
1011
1085
 
1012
1086
  if message.include?("token exchange failed: #{a[0]} from #{base}/auth/token")
1013
1087
  refused += 1
@@ -1023,11 +1097,65 @@ class SecretsFeatureTest < Minitest::Test
1023
1097
  assert_equal [], followed, "a request followed a refused exchange"
1024
1098
  assert_equal [], allowed, "a redirect was not a failed exchange"
1025
1099
  assert_equal answers.length, refused
1026
- ensure
1027
- origin.close rescue nil
1028
- other.close rescue nil
1029
- originthread.join
1030
- otherthread.join
1100
+ end
1101
+ end
1102
+
1103
+ def test_with_the_option_a_redirect_on_the_exchanges_own_origin_is_followed_and_no_other
1104
+ with_origins do |o|
1105
+ sent = ' {"refresh_token":"REFRESH01"}'
1106
+ # Each case: the status, the Location, and the path the exchange is
1107
+ # asked again at, or nil where the redirect is refused.
1108
+ cases = [
1109
+ [307, "/api/moved/token", "/api/moved/token"],
1110
+ [308, o["origin"] + "/api/moved/token", "/api/moved/token"],
1111
+ # A Location of a query alone keeps the path that answered.
1112
+ [307, "?step=2", "/api/auth/token?step=2"],
1113
+ [307, o["away"], nil],
1114
+ [302, "/api/moved/token", nil],
1115
+ [307, "/api/auth/token", nil],
1116
+ ]
1117
+
1118
+ wrong = []
1119
+ followed = 0
1120
+ refused = 0
1121
+ looped = 0
1122
+
1123
+ cases.each do |status, location, path|
1124
+ o["lock"].synchronize do
1125
+ o["answer"] = [status, location]
1126
+ o["followed"].clear
1127
+ o["exchanged"] = 0
1128
+ end
1129
+
1130
+ res, message = redirect_probe(redirect_client(o, { "redirect" => "same-origin" }))
1131
+ seen = "#{status} to #{location}: #{message} #{o['followed'].inspect}"
1132
+
1133
+ if path
1134
+ if res.is_a?(Hash) && true == res["ok"] &&
1135
+ "POST #{path}#{sent}" == o["followed"][0] && 2 == o["followed"].length
1136
+ followed += 1
1137
+ else
1138
+ wrong << seen
1139
+ end
1140
+ elsif o["followed"].empty? &&
1141
+ message.include?("token exchange failed: #{status} from #{o['base']}/auth/token")
1142
+ refused += 1
1143
+ else
1144
+ wrong << seen
1145
+ end
1146
+
1147
+ looped = o["exchanged"] if "/api/auth/token" == location
1148
+ end
1149
+
1150
+ puts "secrets: exchange opt-in followed #{followed} redirect(s) on its own origin, " \
1151
+ "refused #{refused}, #{o['elsewhere'].length} elsewhere"
1152
+
1153
+ assert_equal [], o["elsewhere"], "the exchange reached another origin"
1154
+ assert_equal [], wrong
1155
+ assert_equal 3, followed
1156
+ assert_equal 3, refused
1157
+ # A redirect to itself is followed up to the hop limit and no more.
1158
+ assert_equal 6, looped
1031
1159
  end
1032
1160
  end
1033
1161
 
@@ -1058,4 +1186,47 @@ class SecretsFeatureTest < Minitest::Test
1058
1186
  assert_equal 0, wire.calls.length,
1059
1187
  "a refused op must never yield a request: #{wire.calls.inspect}"
1060
1188
  end
1189
+
1190
+ # -------------------------------------------------------------------
1191
+ # Off by the config where it declares no auth, else by `auth: nil`: either
1192
+ # way the feature places no credential, buys no token and retries nothing.
1193
+
1194
+ def test_auth_off_places_no_credential_and_buys_no_token
1195
+ declared = ProjectNameConfig.shared_config["options"].key?("auth")
1196
+ by = declared ? "option" : "config"
1197
+ off = declared ? { "auth" => nil } : {}
1198
+ secrets = ["OFFKEY01", "OFFKEY02", "OFFREFRESH01", "ACCESS01"]
1199
+ carries = ->(call) { call["has"] || secrets.any? { |s| call["values"].include?(s) } }
1200
+ sent = []
1201
+ bought = 0
1202
+
1203
+ scenarios = [
1204
+ ["a key the chain resolves", { "APIKEY" => "OFFKEY01" }, {}, {}, [200]],
1205
+ ["an explicit apikey", {}, { "apikey" => "OFFKEY02" }, {}, [200]],
1206
+ ["a refusal", { "REFRESH_TOKEN" => "OFFREFRESH01" }, {},
1207
+ { "name" => "refresh_token", "exchange" => { "active" => true } }, [401]],
1208
+ ]
1209
+
1210
+ scenarios.each do |what, env, sdkopts, fopts, statuses|
1211
+ env.each { |k, v| setenv(k, v) }
1212
+ wire = Wire.new
1213
+ wire.apistatus = statuses
1214
+ client = secrets_client(wire, sdkopts.merge(off).merge("feature" => secrets_opts(fopts)))
1215
+ drive_op(client, wire)
1216
+
1217
+ assert_equal 1, wire.api.length, "#{what}: a request was retried with auth off"
1218
+ assert_equal 0, wire.token.length, "#{what}: a token was bought with auth off"
1219
+ refute carries.call(wire.api[0]), "#{what}: a credential was sent with auth off"
1220
+ assert_equal "", secrets_feature_of(client).credential,
1221
+ "#{what}: the feature resolved a credential with auth off"
1222
+
1223
+ sent.concat(wire.api)
1224
+ bought += wire.token.length
1225
+ ensure
1226
+ env.each_key { |k| clearenv(k) }
1227
+ end
1228
+
1229
+ puts "secrets: auth off by #{by}: #{sent.length} request(s), " \
1230
+ "#{sent.count { |c| carries.call(c) }} credential(s), #{bought} token(s)"
1231
+ end
1061
1232
  end
@@ -569,6 +569,26 @@ class FeatureTest < Minitest::Test
569
569
  assert_equal 2, h.track(:@_cache)["bypass"]
570
570
  end
571
571
 
572
+ def test_cache_a_body_that_declares_another_type_than_json_passes_by_unread
573
+ skip_unless_feature("cache")
574
+ sent = []
575
+ server, calls = H.recording_server do |n, _fd|
576
+ res = H.make_response(200, nil, { "content-type" => "audio/wav" })
577
+ res["body"] = +"RIFF#{n}"
578
+ sent << res
579
+ [res, nil]
580
+ end
581
+ h = harness([fspec("cache")], server: server)
582
+ a = h.op(path: "/w/1/audio")["ctx"].response
583
+ b = h.op(path: "/w/1/audio")["ctx"].response
584
+ assert_equal 2, calls.length, "the body was stored"
585
+ assert_same sent[0]["body"], a.body, "the cache handed on a copy it had read"
586
+ assert_same sent[0]["json"], a.json_func
587
+ assert_same sent[1]["body"], b.body
588
+ assert_same sent[1]["json"], b.json_func
589
+ assert_equal 2, h.track(:@_cache)["bypass"]
590
+ end
591
+
572
592
  def test_cache_refetches_after_the_ttl
573
593
  skip_unless_feature("cache")
574
594
  clock = H::Clock.new
@@ -45,7 +45,7 @@ module ProjectNameUtilities
45
45
  if 'graphql' == VoxgigStruct.getprop(point, 'kind')
46
46
  # GraphQL addresses one endpoint: no path parts, no query string, and
47
47
  # the body carries the operation. prepare_body is skipped
48
- # deliberately — it only emits a body for data-input ops, whereas
48
+ # deliberately — it builds only a REST body, whereas
49
49
  # every GraphQL op posts one, including load/list/remove.
50
50
  ctx.spec.body = utility.graphql_body.call(ctx)
51
51
  ctx.spec.path = ''
@@ -1,9 +1,37 @@
1
1
  # ProjectName SDK utility: prepare_body
2
+ require_relative 'struct/voxgig_struct'
2
3
  require_relative 'media'
3
4
  module ProjectNameUtilities
4
5
  PrepareBody = ->(ctx) {
5
- return nil unless ctx.op.input == "data"
6
- return ProjectNameUtilities.raw_body(ctx.reqdata) if ProjectNameUtilities.raw_request?(ctx.point)
7
- ctx.utility.transform_request.call(ctx)
6
+ if ctx.op.input == "data"
7
+ return ProjectNameUtilities.raw_body(ctx.reqdata) if ProjectNameUtilities.raw_request?(ctx.point)
8
+ return ctx.utility.transform_request.call(ctx)
9
+ end
10
+ fields = ctx.point ? VoxgigStruct.getprop(ctx.point, "fields") : nil
11
+ return nil unless fields.is_a?(Array) && !fields.empty?
12
+ ProjectNameUtilities.match_body(ctx, fields)
8
13
  }
14
+
15
+ # A list, load or remove routed to a method with a body sends the match
16
+ # fields that body declares, never a path parameter, and never a header,
17
+ # cookie or query argument the body does not also declare as a field.
18
+ def self.match_body(ctx, fields)
19
+ point = ctx.point
20
+ params = VoxgigStruct.getprop(point, "params")
21
+ routed = params.is_a?(Array) ? params.dup : []
22
+ ["params", "header", "cookie", "query"].each do |kind|
23
+ defs = VoxgigStruct.getpath(point, "args.#{kind}")
24
+ next unless defs.is_a?(Array)
25
+ defs.each do |ad|
26
+ next unless kind == "params" || true != VoxgigStruct.getprop(ad, "field")
27
+ routed << VoxgigStruct.getprop(ad, "name")
28
+ end
29
+ end
30
+ body = {}
31
+ fields.each do |name|
32
+ val = VoxgigStruct.getprop(ctx.reqmatch || {}, name)
33
+ body[name] = val if !val.nil? && name != "$action" && !routed.include?(name)
34
+ end
35
+ body
36
+ end
9
37
  end
@@ -19,7 +19,9 @@ module ProjectNameUtilities
19
19
  end
20
20
  end
21
21
  # A header or cookie parameter travels in the headers, which prepare_headers
22
- # fills, unless a query parameter shares its name: then both are sent.
22
+ # fills, and a field the point's body declares travels in the body, which
23
+ # prepare_body fills, unless a query parameter shares its name: then both
24
+ # are sent.
23
25
  ql = VoxgigStruct.getpath(point, "args.query")
24
26
  declared = ql.is_a?(Array) ? ql.map { |qd| VoxgigStruct.getprop(qd, "name") } : []
25
27
  [VoxgigStruct.getpath(point, "args.header"), VoxgigStruct.getpath(point, "args.cookie")].each do |hl|
@@ -29,6 +31,10 @@ module ProjectNameUtilities
29
31
  params << name if name.is_a?(String) && !declared.include?(name)
30
32
  end
31
33
  end
34
+ fields = VoxgigStruct.getprop(point, "fields")
35
+ if fields.is_a?(Array)
36
+ fields.each { |name| params << name if name.is_a?(String) && !declared.include?(name) }
37
+ end
32
38
  end
33
39
  # A query parameter travels under the name the definition gives it, its
34
40
  # orig, which the model may have renamed for the caller.
@@ -8,6 +8,7 @@ use crate::core::error::ProjectNameError;
8
8
  use crate::core::helpers::{call_json, get_str, getp, jo, json_thunk, setp};
9
9
  use crate::core::types::{Feature, FetcherFn};
10
10
  use crate::feature::support::*;
11
+ use crate::utility::media::is_json_media;
11
12
  use crate::utility::voxgigstruct::Value;
12
13
 
13
14
  pub struct CacheSnapshot {
@@ -119,7 +120,7 @@ fn through(
119
120
  let out = inner(ctx, url, fetchdef);
120
121
 
121
122
  if let Ok(res) = &out {
122
- if storable(res) {
123
+ if storable(res) && json(res) {
123
124
  let snap = Rc::new(snapshot(res));
124
125
  let ttl = fopt_int(options, "ttl", 5000);
125
126
  let mut t = track.borrow_mut();
@@ -147,6 +148,19 @@ fn storable(res: &Value) -> bool {
147
148
  && !matches!(getp(res, "unreadable"), Value::Bool(true))
148
149
  }
149
150
 
151
+ // A body that declares another type than JSON passes by, unread.
152
+ fn json(res: &Value) -> bool {
153
+ let mut declared = String::new();
154
+ if let Value::Map(hm) = getp(res, "headers") {
155
+ for (k, v) in hm.borrow().iter() {
156
+ if let (true, Value::Str(t)) = (k.eq_ignore_ascii_case("content-type"), v) {
157
+ declared = t.clone();
158
+ }
159
+ }
160
+ }
161
+ declared.trim().is_empty() || is_json_media(&declared)
162
+ }
163
+
150
164
  fn snapshot(res: &Value) -> CacheSnapshot {
151
165
  let headers = Value::empty_map();
152
166
  if let Value::Map(hm) = getp(res, "headers") {