@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
@@ -333,6 +333,22 @@
333
333
  (h-op h :path "/w") (h-op h :path "/w")
334
334
  (t/is-eq (count @calls) 2 "two calls")
335
335
  (t/is-eq (mget (h-track h "_cache") "bypass") 2 "bypass"))))
336
+ (check "cache-passes-a-body-that-declares-another-type-than-json-by-unread" "cache"
337
+ (fn [] (let [sent (atom [])
338
+ [server calls] (recording-server
339
+ (fn [_n _fd]
340
+ (let [res (make-response 200 nil (vs/jm "content-type" "audio/wav"))]
341
+ (swap! sent conj res)
342
+ [res nil])))
343
+ h (make-harness [(fspec "cache")] :server server)
344
+ a (h-op h :path "/w/1/audio") b (h-op h :path "/w/1/audio")
345
+ ;; The harness rebuilds the response; its json fn is carried as given.
346
+ handed (fn [r i] (identical? (:json @(core/oget (get r "ctx") :response))
347
+ (vs/getprop (nth @sent i nil) "json")))]
348
+ (t/is-eq (count @calls) 2 "the body was stored")
349
+ (t/is-true (handed a 0) "the cache handed on a copy it had read")
350
+ (t/is-true (handed b 1) "the second read was handed on as returned")
351
+ (t/is-eq (mget (h-track h "_cache") "bypass") 2 "bypass"))))
336
352
  (check "cache-refetches-after-ttl" "cache"
337
353
  (fn [] (let [c (make-clock)
338
354
  [server calls] (recording-server nil)
@@ -743,8 +743,7 @@ public:
743
743
  // materialises the whole result). This runtime is synchronous, so the
744
744
  // returned vector is a materialised cursor the caller iterates. `callopts`
745
745
  // parameterises the call: inbound yields the streaming feature's items when
746
- // active, else the materialised items; outbound attaches an iterable `body`
747
- // to the request (reqdata `body$`); `ctrl` threads pipeline control.
746
+ // active, else the materialised items; `ctrl` threads pipeline control.
748
747
  std::vector<Value> stream(const std::string& action,
749
748
  const Value& args = Value::undef(),
750
749
  const Value& callopts = Value::undef());
@@ -1431,15 +1430,6 @@ inline std::vector<Value> EntityBase::stream(const std::string& action,
1431
1430
  cs.reqmatch = args.is_map() ? args : vmap();
1432
1431
  CtxPtr ctx = u->makeContext(cs, entctx);
1433
1432
 
1434
- // Outbound: attach a caller iterable `body` so the transport can stream a
1435
- // request payload.
1436
- Value body = getp(streamOpts, "body");
1437
- if (!is_nullish(body)) {
1438
- Value reqdata = ctx->reqdata.is_map() ? ctx->reqdata : vmap();
1439
- map_put(reqdata, "body$", body);
1440
- ctx->reqdata = reqdata;
1441
- }
1442
-
1443
1433
  try {
1444
1434
  u->featureHook(ctx, "PrePoint");
1445
1435
  Value point = u->makePoint(ctx);
@@ -16,6 +16,7 @@
16
16
  #include <vector>
17
17
 
18
18
  #include "../core/types.hpp"
19
+ #include "../utility/pipeline.hpp"
19
20
  #include "base.hpp"
20
21
  #include "options.hpp"
21
22
 
@@ -110,7 +111,7 @@ private:
110
111
  throw err;
111
112
  }
112
113
 
113
- if (storable(res)) {
114
+ if (storable(res) && json(res)) {
114
115
  CacheSnapshot snap = snapshot(res);
115
116
  int ttl = fopt::foptInt(options, "ttl", 5000);
116
117
  evict();
@@ -134,6 +135,18 @@ private:
134
135
  return status >= 200 && status < 300 && !(unreadable.is_bool() && unreadable.as_bool());
135
136
  }
136
137
 
138
+ // A body that declares another type than JSON passes by, unread.
139
+ bool json(const Value& res) {
140
+ Value headers = getp(res, "headers");
141
+ if (!headers.is_map()) return true;
142
+ for (const auto& kv : *headers.as_map()) {
143
+ if (util::mediaLower(kv.first) != "content-type" || !kv.second.is_string()) continue;
144
+ return std::string::npos == kv.second.as_string().find_first_not_of(" \t") ||
145
+ util::isJsonMedia(kv.second);
146
+ }
147
+ return true;
148
+ }
149
+
137
150
  CacheSnapshot snapshot(const Value& res) {
138
151
  CacheSnapshot snap;
139
152
 
@@ -101,6 +101,7 @@
101
101
  #define SDK_FEATURE_SECRETS_HPP
102
102
 
103
103
  #include <algorithm>
104
+ #include <cctype>
104
105
  #include <cstdlib>
105
106
  #include <functional>
106
107
  #include <memory>
@@ -180,6 +181,7 @@ public:
180
181
  std::string response = "access_token";
181
182
  std::vector<int> statuses;
182
183
  int retries = 1;
184
+ bool sameorigin = false;
183
185
  };
184
186
 
185
187
  struct Track {
@@ -205,6 +207,12 @@ public:
205
207
  // read for `apikey` and `auth`, never written.
206
208
  liveopts_ = (nullptr != client && client->options.is_map()) ? client->options : ctx->options;
207
209
 
210
+ // What prepareAuth was generated from: the config declares auth only
211
+ // where it places a credential, and Basic only where it composes one.
212
+ Value declared = Struct::getpath(ctx->config, {"options", "auth"});
213
+ declared_ = declared.is_map();
214
+ basic_ = declared_ && is_true(mapget(declared, "basic"));
215
+
208
216
  // WRAP FIRST. The fail-closed gate needs the seam whatever happens
209
217
  // below - a chain that fails to build must still refuse to send - so
210
218
  // the wrapper is installed before anything here can fail, and the
@@ -231,6 +239,7 @@ public:
231
239
  x->request = fopt::foptStr(xopts, "request", "refresh_token");
232
240
  x->response = fopt::foptStr(xopts, "response", "access_token");
233
241
  x->retries = fopt::foptInt(xopts, "retries", 1);
242
+ x->sameorigin = "same-origin" == fopt::foptStr(xopts, "redirect", "refuse");
234
243
  Value sl = fopt::foptList(xopts, "statuses");
235
244
  if (sl.is_list()) {
236
245
  for (const auto& s : *sl.as_list()) {
@@ -399,6 +408,8 @@ public:
399
408
 
400
409
  private:
401
410
  Value liveopts_ = Value::undef();
411
+ bool declared_ = false;
412
+ bool basic_ = false;
402
413
  std::string secretname_ = "apikey";
403
414
  bool cache_ = true;
404
415
 
@@ -515,13 +526,39 @@ private:
515
526
 
516
527
  if (cred_.empty()) return;
517
528
 
529
+ std::string value = cred_;
530
+ if (basic_ && is_true(Struct::getpath(liveopts_, {"auth", "basic"}))) {
531
+ value = pair(cred_);
532
+ }
533
+
518
534
  std::string prefix = as_str(Struct::getpath(liveopts_, {"auth", "prefix"}));
519
- map_put(headers, name,
520
- Value(prefix.empty() ? cred_ : prefix + " " + cred_));
535
+ map_put(headers, name, Value(prefix.empty() ? value : prefix + " " + value));
521
536
  }
522
537
 
538
+ // prepareAuth's decision: suppressed where the config declares no auth,
539
+ // or where `auth` is null.
523
540
  bool authSuppressed() const {
524
- return is_nullish(mapget(liveopts_, "auth"));
541
+ return !declared_ || is_nullish(mapget(liveopts_, "auth"));
542
+ }
543
+
544
+ // Under Basic the token is the key half, as apikey is: prepareAuth's pair.
545
+ std::string pair(const std::string& token) {
546
+ static const char* ALPHABET =
547
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
548
+ std::string in = token + ":" + as_str(mapget(liveopts_, "secret"));
549
+ std::string out;
550
+ out.reserve(((in.size() + 2) / 3) * 4);
551
+ for (size_t i = 0; i < in.size(); i += 3) {
552
+ unsigned int v = static_cast<unsigned char>(in[i]) << 16;
553
+ if (i + 1 < in.size()) v |= static_cast<unsigned int>(static_cast<unsigned char>(in[i + 1])) << 8;
554
+ if (i + 2 < in.size()) v |= static_cast<unsigned int>(static_cast<unsigned char>(in[i + 2]));
555
+ out += ALPHABET[(v >> 18) & 0x3f];
556
+ out += ALPHABET[(v >> 12) & 0x3f];
557
+ out += (i + 1 < in.size()) ? ALPHABET[(v >> 6) & 0x3f] : '=';
558
+ out += (i + 2 < in.size()) ? ALPHABET[v & 0x3f] : '=';
559
+ }
560
+ registerSecret(out);
561
+ return out;
525
562
  }
526
563
 
527
564
  // ---- the exchange -----------------------------------------------------------
@@ -579,21 +616,38 @@ private:
579
616
  // bundled transport of last resort from the generated kinds.cpp.
580
617
  Value sysFetch = Struct::getpath(opts, {"system", "fetch"});
581
618
  Value res;
582
- if (sysFetch.is_injector()) {
583
- try {
584
- vs::Injection inj(Value::undef(), Value::undef());
585
- res = sysFetch.as_injector()(inj, vlist({Value(url), fetchdef}), std::string(""),
586
- Value::undef());
587
- } catch (const SdkErrorPtr& e) {
588
- return e->msg;
589
- } catch (const std::exception& e) {
590
- return std::string("secrets: token exchange transport failed: ") + e.what();
619
+ auto send = [&](const std::string& at) -> std::string {
620
+ if (sysFetch.is_injector()) {
621
+ try {
622
+ vs::Injection inj(Value::undef(), Value::undef());
623
+ res = sysFetch.as_injector()(inj, vlist({Value(at), fetchdef}), std::string(""),
624
+ Value::undef());
625
+ } catch (const SdkErrorPtr& e) {
626
+ return e->msg;
627
+ } catch (const std::exception& e) {
628
+ return std::string("secrets: token exchange transport failed: ") + e.what();
629
+ }
630
+ return "";
591
631
  }
592
- } else {
593
632
  std::vector<std::pair<std::string, std::string>> hdrs{{"content-type", "application/json"}};
594
- SecretsRawResponse raw = secrets_rawfetch(x.method, url, hdrs, bodytext);
633
+ SecretsRawResponse raw = secrets_rawfetch(x.method, at, hdrs, bodytext);
595
634
  if (!raw.ok) return raw.err;
635
+ // The vendored client reports no headers, so no Location: nothing is followed.
596
636
  res = vmap({{"status", Value(raw.status)}, {"body", Value(raw.body)}});
637
+ return "";
638
+ };
639
+
640
+ std::string failed = send(url);
641
+ if (!failed.empty()) return failed;
642
+
643
+ // By option alone, and only while the exchange stays on its own origin.
644
+ std::string at = url;
645
+ for (int hops = 0; x.sameorigin && hops < REDIRECT_MAX; hops++) {
646
+ std::string next = moved(url, at, res);
647
+ if (next.empty()) break;
648
+ at = next;
649
+ failed = send(at);
650
+ if (!failed.empty()) return failed;
597
651
  }
598
652
 
599
653
  int status = fopt::fresStatus(res);
@@ -622,6 +676,68 @@ private:
622
676
  return "";
623
677
  }
624
678
 
679
+ static constexpr int REDIRECT_MAX = 5;
680
+
681
+ // "scheme://host:port" of an absolute http(s) URL, the port defaulted, and
682
+ // where its authority ends; "" when it is not one.
683
+ static std::string origin(const std::string& url, size_t* rest = nullptr) {
684
+ size_t sep = url.find("://");
685
+ if (std::string::npos == sep || 5 < sep) return "";
686
+ std::string scheme = url.substr(0, sep);
687
+ for (auto& c : scheme) c = (char)std::tolower((unsigned char)c);
688
+ if ("http" != scheme && "https" != scheme) return "";
689
+ size_t end = std::min(url.find_first_of("/?#", sep + 3), url.size());
690
+ size_t auth = url.rfind('@', end);
691
+ auth = (std::string::npos == auth || auth < sep + 3) ? sep + 3 : auth + 1;
692
+ size_t close = '[' == url[auth] ? url.find(']', auth) : auth;
693
+ if (std::string::npos == close || end < close) return "";
694
+ size_t colon = url.find(':', close);
695
+ if (end < colon) colon = std::string::npos;
696
+ size_t hostend = std::string::npos == colon ? end : colon;
697
+ if (hostend == auth) return "";
698
+ std::string port = "https" == scheme ? "443" : "80";
699
+ if (std::string::npos != colon && colon + 1 < end) {
700
+ port = url.substr(colon + 1, end - colon - 1);
701
+ if (5 < port.size() || std::string::npos != port.find_first_not_of("0123456789") ||
702
+ 65535 < std::stol(port)) {
703
+ return "";
704
+ }
705
+ port = std::to_string(std::stol(port));
706
+ }
707
+ std::string host = url.substr(auth, hostend - auth);
708
+ for (auto& c : host) c = (char)std::tolower((unsigned char)c);
709
+ if (nullptr != rest) *rest = end;
710
+ return scheme + "://" + host + ":" + port;
711
+ }
712
+
713
+ // Where a 307 or 308 points, when that is on the exchange's own origin. Any
714
+ // other redirect would drop the body or leave the origin.
715
+ static std::string moved(const std::string& url, const std::string& at, const Value& res) {
716
+ int status = fopt::fresStatus(res);
717
+ std::string location = as_str(getp(getp(res, "headers"), "location"));
718
+ size_t rest = 0;
719
+ std::string own = origin(url);
720
+ if ((307 != status && 308 != status) || location.empty() || own.empty() ||
721
+ origin(at, &rest).empty()) {
722
+ return "";
723
+ }
724
+ std::string next;
725
+ if (std::string::npos != location.find("://")) {
726
+ next = location;
727
+ } else if (0 == location.rfind("//", 0)) {
728
+ next = at.substr(0, at.find("://") + 1) + location;
729
+ } else if ('/' == location[0]) {
730
+ next = at.substr(0, rest) + location;
731
+ } else if ('?' == location[0]) {
732
+ next = at.substr(0, at.find_first_of("?#")) + location;
733
+ } else {
734
+ size_t dir = at.find_last_of('/', std::min(at.find_first_of("?#", rest), at.size()) - 1);
735
+ next = (std::string::npos == dir || dir < rest) ? at.substr(0, rest) + "/" + location
736
+ : at.substr(0, dir + 1) + location;
737
+ }
738
+ return own == origin(next) ? next : "";
739
+ }
740
+
625
741
  bool spent(const Value& res) const {
626
742
  int status = fopt::fresStatus(res);
627
743
  if (0 > status) return false;
@@ -638,7 +754,7 @@ private:
638
754
  // Answers the provider's own message, or "". Called under mu_.
639
755
  std::string resolve() {
640
756
  if (cache_ && resolved_) return "";
641
- if (!sek_) return "";
757
+ if (!sek_ || authSuppressed()) return "";
642
758
 
643
759
  std::optional<std::string> found;
644
760
  try {
@@ -720,9 +836,8 @@ private:
720
836
 
721
837
  if (!exchange_) return inner(ctx, url, fetchdef);
722
838
 
723
- // `auth: null` is a deliberately unauthenticated request: a refusal of
724
- // it is not an expired token and cannot be fixed by buying one -
725
- // retrying would transmit exactly the credential the caller suppressed.
839
+ // A refusal of an unauthenticated request is not an expired token, and
840
+ // retrying would transmit the credential that auth off withholds.
726
841
  if (suppressed) return inner(ctx, url, fetchdef);
727
842
 
728
843
  int max = exchange_->retries;
@@ -196,10 +196,14 @@ static Value secretsOpts(const Value& providers, const Value& more = Value::unde
196
196
  }
197
197
 
198
198
  // The header prepareAuth would build for this client and token: the model
199
- // decides the prefix, so it is read off the live options rather than assumed.
200
- static std::string expectAuth(SdkClient* client, const std::string& token) {
199
+ // decides the prefix and Basic, so both are read off the live options rather
200
+ // than assumed. Under Basic the token is the key half of the encoded pair.
201
+ static std::string expectAuth(SdkClient* client, const std::string& token,
202
+ const std::string& secret = "") {
201
203
  std::string prefix = as_str(Struct::getpath(client->options, {"auth", "prefix"}));
202
- return prefix.empty() ? token : prefix + " " + token;
204
+ std::string value = is_true(Struct::getpath(client->options, {"auth", "basic"}))
205
+ ? util::cleanBase64(token + ":" + secret) : token;
206
+ return prefix.empty() ? value : prefix + " " + value;
203
207
  }
204
208
 
205
209
  static Value direct(std::shared_ptr<ProjectNameSDK> client) {
@@ -298,8 +302,17 @@ static void t_auth_null_suppresses() {
298
302
  ASSERT_EQ(rec->api, 1, "auth null: the op still proceeds");
299
303
  ASSERT_FALSE(rec->hasAuth(0), "auth null: NO header even though the chain resolved");
300
304
  auto f = SecretsFeature::of(client.get());
301
- if (f) ASSERT_EQ(f->credential(), std::string("CHAINKEY"),
302
- "auth null: the chain still resolved (suppression is at the header)");
305
+ if (f) ASSERT_EQ(f->credential(), std::string(""), "auth null: nothing is resolved");
306
+ }
307
+
308
+ static void t_resolved_key_paired() {
309
+ auto rec = std::make_shared<Rec>();
310
+ auto client = liveClient(rec, secretsOpts(vlist({memoryProvider("APIKEY", "CHAINKEY5")})),
311
+ vmap({{"secret", Value(std::string("PASS01"))}}));
312
+ Value res = direct(client);
313
+ ASSERT_TRUE(okOf(res), "paired: direct failed: " + errOf(res));
314
+ ASSERT_EQ(rec->auth(0), expectAuth(client.get(), "CHAINKEY5", "PASS01"),
315
+ "paired: a resolved key is placed as prepareAuth places it, the secret kept");
303
316
  }
304
317
 
305
318
  static void t_custom_provider_verbatim() {
@@ -653,6 +666,21 @@ static void t_exchange_401_rebuys_and_retries() {
653
666
  ASSERT_EQ(rec->auth(3), expectAuth(client.get(), "ACCESS02"), "401: the retry carries the NEW token");
654
667
  }
655
668
 
669
+ static void t_exchange_retry_paired() {
670
+ auto rec = std::make_shared<Rec>();
671
+ rec->script = {401, 200};
672
+ auto client = liveClient(rec, secretsOpts(vlist({memoryProvider("APIKEY", "REFRESH1")}),
673
+ vmap({{"exchange", exchangeOn()}})),
674
+ vmap({{"secret", Value(std::string("PASS01"))}}));
675
+ direct(client);
676
+ ASSERT_EQ(rec->api, 2, "paired 401: retried once");
677
+ // calls: token, api(401), token, api(200)
678
+ ASSERT_EQ(rec->auth(1), expectAuth(client.get(), "ACCESS01", "PASS01"),
679
+ "paired 401: the first attempt kept the secret");
680
+ ASSERT_EQ(rec->auth(3), expectAuth(client.get(), "ACCESS02", "PASS01"),
681
+ "paired 401: a token bought on a refusal is placed as prepareAuth places it");
682
+ }
683
+
656
684
  static void t_exchange_retry_once_not_loop() {
657
685
  auto rec = std::make_shared<Rec>();
658
686
  rec->script = {401, 401, 401};
@@ -758,6 +786,7 @@ static void t_exchange_auth_null_suppresses() {
758
786
  Value res = direct(client);
759
787
  ASSERT_FALSE(okOf(res), "auth null exchange: the 401 comes back");
760
788
  ASSERT_EQ(rec->api, 1, "auth null exchange: no retry for a deliberately unauthenticated request");
789
+ ASSERT_EQ(rec->token, 0, "auth null exchange: no token is bought");
761
790
  ASSERT_FALSE(rec->hasAuth(rec->calls.size() - 1), "auth null exchange: no header");
762
791
  }
763
792
 
@@ -802,13 +831,16 @@ struct Moved {
802
831
  };
803
832
  static const std::vector<Moved> MOVED = {{307, true}, {308, true}, {302, true}, {307, false}};
804
833
 
805
- static Value redirectClient(const std::string& base, const Value& fetch) {
834
+ static Value redirectClient(const std::string& base, const Value& fetch,
835
+ const std::string& redirect = "") {
836
+ Value x = exchangeOn(vmap({{"refresh", Value(std::string("REFRESH01"))}}));
837
+ if (!redirect.empty()) map_put(x, "redirect", Value(redirect));
806
838
  Value opts = vmap({
807
839
  {"base", Value(base)},
808
840
  {"feature", vmap({{"secrets", vmap({
809
841
  {"active", Value(true)},
810
842
  {"name", Value(std::string("refresh_token"))},
811
- {"exchange", exchangeOn(vmap({{"refresh", Value(std::string("REFRESH01"))}}))}})}})}});
843
+ {"exchange", x}})}})}});
812
844
  if (fetch.is_injector()) map_put(opts, "system", vmap({{"fetch", fetch}}));
813
845
  return direct(std::make_shared<ProjectNameSDK>(opts));
814
846
  }
@@ -997,12 +1029,143 @@ static void t_exchange_redirect_refused() {
997
1029
  redirectSystemFetch();
998
1030
  }
999
1031
 
1032
+ // The option through the caller's system.fetch, the one transport whose
1033
+ // answer carries a Location in every cpp build.
1034
+ static void t_exchange_redirect_same_origin() {
1035
+ const std::string base = "http://api.test/api";
1036
+ const std::string sent = " POST manual {\"refresh_token\":\"REFRESH01\"}";
1037
+ const std::string asked = base + "/auth/token" + sent;
1038
+ // The url the exchange is asked again at, empty when refused; a Location of
1039
+ // a query alone keeps the path that answered.
1040
+ struct Case {
1041
+ int status;
1042
+ std::string location;
1043
+ std::string next;
1044
+ };
1045
+ const std::vector<Case> cases = {
1046
+ {307, "/api/moved/token", base + "/moved/token"},
1047
+ {308, "http://api.test/api/moved/token", base + "/moved/token"},
1048
+ {307, "?step=2", base + "/auth/token?step=2"},
1049
+ {307, "http://other.test/api/auth/token", ""},
1050
+ {302, "/api/moved/token", ""},
1051
+ {307, "/api/auth/token", ""}};
1052
+
1053
+ int followed = 0;
1054
+ int refused = 0;
1055
+ int elsewhere = 0;
1056
+ size_t looped = 0;
1057
+ std::vector<std::string> wrong;
1058
+
1059
+ for (const Case& c : cases) {
1060
+ std::vector<std::string> calls;
1061
+ vs::Injector fn = [&](vs::Injection&, const Value& args, const std::string&,
1062
+ const Value&) -> Value {
1063
+ std::string url = as_str(vs::getelem(args, Value(int64_t(0))));
1064
+ Value fd = vs::getelem(args, Value(int64_t(1)));
1065
+ if (0 != url.rfind("http://api.test/", 0)) elsewhere++;
1066
+ if (base + "/thing" == url) return fhResponse(200, vmap({{"ok", Value(true)}}), Value::undef());
1067
+ calls.push_back(url + " " + as_str(getp(fd, "method")) + " " +
1068
+ as_str(getp(fd, "redirect")) + " " + as_str(getp(fd, "body")));
1069
+ if (base + "/auth/token" != url) {
1070
+ return fhResponse(200, vmap({{"access_token", Value(std::string("MOVED01"))}}), Value::undef());
1071
+ }
1072
+ return vmap({{"status", Value(c.status)},
1073
+ {"headers", vmap({{"location", Value(c.location)}})},
1074
+ {"body", Value(std::string(""))}});
1075
+ };
1076
+ Value res = redirectClient(base, Value(fn), "same-origin");
1077
+
1078
+ bool loop = "/api/auth/token" == c.location;
1079
+ if (loop) looped = calls.size();
1080
+ bool follows = !c.next.empty();
1081
+ std::vector<std::string> want = follows ? std::vector<std::string>{asked, c.next + sent}
1082
+ : std::vector<std::string>(loop ? std::max<size_t>(calls.size(), 1) : 1, asked);
1083
+ bool failed = !okOf(res) && contains(errOf(res), redirectWant(c.status, base));
1084
+ if (calls == want && (follows ? okOf(res) : failed)) {
1085
+ (follows ? followed : refused)++;
1086
+ } else {
1087
+ std::string seen;
1088
+ for (const auto& call : calls) seen += "[" + call + "]";
1089
+ wrong.push_back(std::to_string(c.status) + " to " + c.location + ": " + errOf(res) + " " + seen);
1090
+ }
1091
+ }
1092
+
1093
+ std::printf("secrets: exchange opt-in followed %d redirect(s) on its own origin, refused %d, "
1094
+ "%d elsewhere\n", followed, refused, elsewhere);
1095
+ for (const auto& w : wrong) ASSERT_TRUE(false, "same-origin: " + w);
1096
+ ASSERT_EQ(elsewhere, 0, "same-origin: the exchange reached another origin");
1097
+ ASSERT_EQ(followed, 3, "same-origin: every redirect on the origin followed");
1098
+ ASSERT_EQ(refused, 3, "same-origin: every other redirect refused");
1099
+ ASSERT_EQ(looped, (size_t)6, "same-origin: a redirect to itself is followed five times");
1100
+ }
1101
+
1102
+ // ---- auth off ----------------------------------------------------------------
1103
+
1104
+ // Off by the config where it declares no auth, else by `auth: null`: either
1105
+ // way the feature places no credential, buys no token and retries nothing.
1106
+ static bool carries(const Rec& rec, size_t i) {
1107
+ static const std::vector<std::string> SECRETS = {"OFFKEY01", "OFFKEY02", "OFFREFRESH01", "ACCESS01"};
1108
+ if (rec.hasAuth(i)) return true;
1109
+ Value headers = getp(getp(rec.calls[i], "fetchdef"), "headers");
1110
+ if (!headers.is_map()) return false;
1111
+ for (const auto& kv : *headers.as_map()) {
1112
+ std::string v = as_str(kv.second);
1113
+ for (const auto& secret : SECRETS) {
1114
+ if (contains(v, secret)) return true;
1115
+ }
1116
+ }
1117
+ return false;
1118
+ }
1119
+
1120
+ static void t_auth_off() {
1121
+ bool declared = getp(getp(sharedConfig(), Value("options")), Value("auth")).is_map();
1122
+ int sent = 0;
1123
+ int credentials = 0;
1124
+ int bought = 0;
1125
+
1126
+ for (int scenario = 0; scenario < 3; scenario++) {
1127
+ auto rec = std::make_shared<Rec>();
1128
+ Value fopts = secretsOpts(Value::undef());
1129
+ Value extra = vmap();
1130
+ if (0 == scenario) {
1131
+ fopts = secretsOpts(vlist({memoryProvider("APIKEY", "OFFKEY01")}));
1132
+ } else if (1 == scenario) {
1133
+ map_put(extra, "apikey", Value(std::string("OFFKEY02")));
1134
+ } else {
1135
+ rec->script = {401};
1136
+ fopts = secretsOpts(vlist({memoryProvider("APIKEY", "OFFREFRESH01")}),
1137
+ vmap({{"exchange", exchangeOn()}}));
1138
+ }
1139
+ if (declared) map_put(extra, "auth", Value(nullptr));
1140
+
1141
+ auto client = liveClient(rec, fopts, extra);
1142
+ direct(client);
1143
+
1144
+ ASSERT_EQ(rec->api, 1, "auth off: one request, not retried");
1145
+ ASSERT_EQ(rec->token, 0, "auth off: no token bought");
1146
+ ASSERT_FALSE(carries(*rec, rec->calls.size() - 1), "auth off: no credential sent");
1147
+ auto f = SecretsFeature::of(client.get());
1148
+ ASSERT_TRUE(nullptr != f, "auth off: the feature is installed");
1149
+ if (f) ASSERT_EQ(f->credential(), std::string(""), "auth off: nothing resolved");
1150
+
1151
+ for (size_t i = 0; i < rec->calls.size(); i++) {
1152
+ if (!contains(rec->url(i), rec->tokenpath) && carries(*rec, i)) credentials++;
1153
+ }
1154
+ sent += rec->api;
1155
+ bought += rec->token;
1156
+ }
1157
+
1158
+ std::printf("secrets: auth off by %s: %d request(s), %d credential(s), %d token(s)\n",
1159
+ declared ? "option" : "config", sent, credentials, bought);
1160
+ }
1161
+
1000
1162
  int main() {
1001
1163
  // Read from the config, never from a probe: an SDK whose config declares no
1002
1164
  // auth places no credential, and these tests assert on the one it places.
1003
1165
  if (!getp(getp(sharedConfig(), Value("options")), Value("auth")).is_map()) {
1004
1166
  std::cout << " SKIP [secrets]: this SDK's config declares no auth, so it places no credential\n";
1005
- return 0;
1167
+ RUN(t_auth_off);
1168
+ return sdktest::summary("secrets_test");
1006
1169
  }
1007
1170
 
1008
1171
  RUN(t_inactive_apikey_as_before);
@@ -1011,6 +1174,7 @@ int main() {
1011
1174
  RUN(t_omitted_apikey_defers);
1012
1175
  RUN(t_empty_apikey_defers);
1013
1176
  RUN(t_auth_null_suppresses);
1177
+ RUN(t_resolved_key_paired);
1014
1178
  RUN(t_custom_provider_verbatim);
1015
1179
  RUN(t_miss_everywhere_no_header);
1016
1180
  RUN(t_provider_error_fails_direct);
@@ -1030,6 +1194,7 @@ int main() {
1030
1194
  RUN(t_exchange_explicit_refresh_wins);
1031
1195
  RUN(t_exchange_one_purchase_many_requests);
1032
1196
  RUN(t_exchange_401_rebuys_and_retries);
1197
+ RUN(t_exchange_retry_paired);
1033
1198
  RUN(t_exchange_retry_once_not_loop);
1034
1199
  RUN(t_exchange_other_status_not_expiry);
1035
1200
  RUN(t_exchange_statuses_configurable);
@@ -1041,6 +1206,8 @@ int main() {
1041
1206
  RUN(t_exchange_auth_null_suppresses);
1042
1207
  RUN(t_exchange_test_mode_buys_nothing);
1043
1208
  RUN(t_exchange_redirect_refused);
1209
+ RUN(t_exchange_redirect_same_origin);
1210
+ RUN(t_auth_off);
1044
1211
 
1045
1212
  std::printf("secrets: ran %d case(s)\n", CASES);
1046
1213
  return sdktest::summary("secrets_test");
@@ -371,6 +371,32 @@ static void cache_doesNotCacheNon2xx() {
371
371
  ASSERT_EQ(f->bypass, 2, "expected 2 bypasses");
372
372
  }
373
373
 
374
+ static void cache_passesOtherThanJsonByUnread() {
375
+ if (!have({"cache"})) return;
376
+ auto rec = std::make_shared<FhRecorder>();
377
+ auto sent = std::make_shared<std::vector<Value>>();
378
+ rec->reply = [sent](int, const Value&) {
379
+ Value res = fhResponse(200, Value::undef(), vmap({{"content-type", Value(std::string("audio/wav"))}}));
380
+ sent->push_back(res);
381
+ return res;
382
+ };
383
+ FetcherFn server = [rec](CtxPtr c, const std::string& u, const Value& fd) { return rec->fetch(c, u, fd); };
384
+ auto f = std::make_shared<CacheFeature>();
385
+ auto h = fhMake(server, {FF(f, Value::undef())});
386
+ FhOpResult a = h->op(fhOp("load").setPath("/w/1/audio"));
387
+ FhOpResult b = h->op(fhOp("load").setPath("/w/1/audio"));
388
+ // The transport's own response shares its headers map; a rebuilt copy does not.
389
+ auto same = [&](const FhOpResult& r, size_t i) {
390
+ Value got = r.ctx && r.ctx->response ? r.ctx->response->headers : Value::undef();
391
+ Value own = i < sent->size() ? getp((*sent)[i], "headers") : Value::undef();
392
+ return got.is_map() && own.is_map() && got.as_map() == own.as_map();
393
+ };
394
+ ASSERT_EQ((int)rec->calls.size(), 2, "the body was stored");
395
+ ASSERT_TRUE(same(a, 0), "the cache handed on a copy it had read");
396
+ ASSERT_TRUE(same(b, 1), "the cache handed on a copy it had read");
397
+ ASSERT_EQ(f->bypass, 2, "expected 2 bypasses");
398
+ }
399
+
374
400
  static void cache_refetchesAfterTtl() {
375
401
  if (!have({"cache"})) return;
376
402
  FhClock clock;
@@ -905,6 +931,7 @@ int main() {
905
931
  T_RUN(cache_servesRepeatedReadFromCache);
906
932
  T_RUN(cache_doesNotCacheNonGet);
907
933
  T_RUN(cache_doesNotCacheNon2xx);
934
+ T_RUN(cache_passesOtherThanJsonByUnread);
908
935
  T_RUN(cache_refetchesAfterTtl);
909
936
  T_RUN(cache_evictsOldestPastMax);
910
937
  T_RUN(cache_inactiveDoesNotWrap);