@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
@@ -94,6 +94,8 @@ class ProjectNameSecretsMapProvider implements \Voxgig\Sekreto\Provider
94
94
  // and answers with a deterministic fake token.
95
95
  class ProjectNameSecretsFeature extends ProjectNameBaseFeature
96
96
  {
97
+ private const REDIRECT_MAX = 5;
98
+
97
99
  private mixed $client = null;
98
100
 
99
101
  /** @var array<string,mixed> */
@@ -106,6 +108,11 @@ class ProjectNameSecretsFeature extends ProjectNameBaseFeature
106
108
  /** @var array<string,mixed> */
107
109
  private array $liveopts = [];
108
110
 
111
+ // What prepare_auth was generated from: the config declares auth only
112
+ // where it places a credential, and Basic only where it composes one.
113
+ private bool $declared = false;
114
+ private bool $basic = false;
115
+
109
116
  private string $secretname = 'apikey';
110
117
  private bool $cache = true;
111
118
  private ?\Voxgig\Sekreto\Sekreto $sekreto = null;
@@ -161,6 +168,10 @@ class ProjectNameSecretsFeature extends ProjectNameBaseFeature
161
168
  $this->active = ($options['active'] ?? null) === true;
162
169
  $this->rootctx = $ctx;
163
170
 
171
+ $declared = \Voxgig\Struct\Struct::getpath($ctx->config, 'options.auth');
172
+ $this->declared = is_array($declared);
173
+ $this->basic = $this->declared && true === ($declared['basic'] ?? null);
174
+
164
175
  if (!$this->active) {
165
176
  return;
166
177
  }
@@ -191,6 +202,7 @@ class ProjectNameSecretsFeature extends ProjectNameBaseFeature
191
202
  'response' => self::str($xopts['response'] ?? null, 'access_token'),
192
203
  'statuses' => $statuses,
193
204
  'retries' => is_numeric($xopts['retries'] ?? null) ? (int)$xopts['retries'] : 1,
205
+ 'redirect' => 'same-origin' === ($xopts['redirect'] ?? null) ? 'same-origin' : 'refuse',
194
206
  ];
195
207
  }
196
208
 
@@ -362,7 +374,7 @@ class ProjectNameSecretsFeature extends ProjectNameBaseFeature
362
374
  // from a miss it must not cache.
363
375
  private function resolve_once(): bool
364
376
  {
365
- if (null === $this->sekreto) {
377
+ if (null === $this->sekreto || !$this->authon()) {
366
378
  return false;
367
379
  }
368
380
 
@@ -408,17 +420,6 @@ class ProjectNameSecretsFeature extends ProjectNameBaseFeature
408
420
  return true;
409
421
  }
410
422
 
411
- // `auth: null` is the documented way to send NO credential, and a
412
- // purchase is a credential-bearing call: the refresh token goes to
413
- // the token endpoint in the request body. with_refresh honours
414
- // suppression for the RETRY, but it runs after this - by then the
415
- // refresh token has already left the process, and no later check
416
- // can call it back. The suppression has to be honoured here, before
417
- // the first purchase, or it only ever half-held.
418
- if (null === ($this->liveopts['auth'] ?? null)) {
419
- return false;
420
- }
421
-
422
423
  $this->buy();
423
424
 
424
425
  return true;
@@ -432,12 +433,9 @@ class ProjectNameSecretsFeature extends ProjectNameBaseFeature
432
433
  // this request left, and it carries the token that just failed.
433
434
  private function with_refresh(ProjectNameContext $ctx, string $url, array $fetchdef, mixed $inner): array
434
435
  {
435
- // `auth: null` is the documented way to send NO credential, and
436
- // prepare_auth honours it by removing the header. A refusal of a
437
- // deliberately unauthenticated request is not an expired token and
438
- // cannot be fixed by buying one - retrying would transmit exactly
439
- // the credential the caller suppressed.
440
- if (null === ($this->liveopts['auth'] ?? null)) {
436
+ // A refusal of an unauthenticated request is not an expired token,
437
+ // and retrying would transmit the credential that auth off withholds.
438
+ if (!$this->authon()) {
441
439
  return $inner($ctx, $url, $fetchdef);
442
440
  }
443
441
 
@@ -561,13 +559,26 @@ class ProjectNameSecretsFeature extends ProjectNameBaseFeature
561
559
  // feature wraps, and sending the token request back through it
562
560
  // would recurse on the first expiry - and would route the exchange
563
561
  // through the test mock, which knows nothing about it.
564
- [$res, $err] = $fetch($url, [
562
+ $fetchdef = [
565
563
  'method' => $x['method'],
566
564
  'headers' => ['content-type' => 'application/json'],
567
565
  'body' => $body,
568
- // Never followed: a redirect would carry the refresh token on.
566
+ // Never followed by the client: a redirect would carry the refresh token on.
569
567
  'redirect' => 'manual',
570
- ]);
568
+ ];
569
+
570
+ [$res, $err] = $fetch($url, $fetchdef);
571
+
572
+ // By option alone, and only while the exchange stays on its own origin.
573
+ $at = $url;
574
+ for ($hops = 0; null === $err && 'same-origin' === $x['redirect'] && $hops < self::REDIRECT_MAX; $hops++) {
575
+ $next = self::moved($url, $at, $res);
576
+ if (null === $next) {
577
+ break;
578
+ }
579
+ $at = $next;
580
+ [$res, $err] = $fetch($at, $fetchdef);
581
+ }
571
582
 
572
583
  if (null !== $err) {
573
584
  throw new \Voxgig\Sekreto\SekretoError(
@@ -627,11 +638,15 @@ class ProjectNameSecretsFeature extends ProjectNameBaseFeature
627
638
 
628
639
  // Suppressed auth means NO header, the same answer prepare_auth
629
640
  // gives - and so does an empty credential.
630
- if (null === ($this->liveopts['auth'] ?? null) || '' === $token) {
641
+ if (!$this->authon() || '' === $token) {
631
642
  unset($fetchdef['headers'][$name]);
632
643
  return;
633
644
  }
634
645
 
646
+ $value = $this->basic
647
+ && true === (\Voxgig\Struct\Struct::getpath($this->liveopts, 'auth.basic') ?? null)
648
+ ? $this->pair($token) : $token;
649
+
635
650
  $prefix = \Voxgig\Struct\Struct::getpath($this->liveopts, 'auth.prefix');
636
651
  if (!is_string($prefix)) {
637
652
  $prefix = '';
@@ -639,7 +654,23 @@ class ProjectNameSecretsFeature extends ProjectNameBaseFeature
639
654
 
640
655
  // Empty prefix (raw apiKey credential) must not add a leading space.
641
656
  $fetchdef['headers'][$name] = '' === $prefix
642
- ? $token : $prefix . ' ' . $token;
657
+ ? $value : $prefix . ' ' . $value;
658
+ }
659
+
660
+ // prepare_auth's decision: auth the config declares, not suppressed by
661
+ // `auth: null`.
662
+ private function authon(): bool
663
+ {
664
+ return $this->declared && null !== ($this->liveopts['auth'] ?? null);
665
+ }
666
+
667
+ // Under Basic the token is the key half, as apikey is: prepare_auth's pair.
668
+ private function pair(string $token): string
669
+ {
670
+ $secret = $this->liveopts['secret'] ?? null;
671
+ $pair = base64_encode($token . ':' . (is_string($secret) ? $secret : ''));
672
+ $this->_register($pair);
673
+ return $pair;
643
674
  }
644
675
 
645
676
  // One chain entry as sekreto will take it: a ProviderSpec array (it has
@@ -704,6 +735,31 @@ class ProjectNameSecretsFeature extends ProjectNameBaseFeature
704
735
  return is_array($plugs) ? $plugs : [];
705
736
  }
706
737
 
738
+ // Where a method-keeping redirect points, when that is on the exchange's
739
+ // own origin. Any other redirect would drop the body or leave the origin.
740
+ private static function moved(string $url, string $at, mixed $res): ?string
741
+ {
742
+ $status = is_array($res) && is_numeric($res['status'] ?? null) ? (int)$res['status'] : 0;
743
+ if (307 !== $status && 308 !== $status) {
744
+ return null;
745
+ }
746
+ $location = $res['headers']['location'] ?? null;
747
+ if (!is_string($location) || '' === $location) {
748
+ return null;
749
+ }
750
+ $next = ProjectNameFetcher::resolveLocation($at, $location);
751
+ $origin = function (?string $u): ?string {
752
+ $p = null === $u ? false : parse_url(strtolower($u));
753
+ if (!is_array($p) || !isset($p['scheme'], $p['host'])) {
754
+ return null;
755
+ }
756
+ return $p['scheme'] . '://' . $p['host'] . ':' .
757
+ ($p['port'] ?? ('https' === $p['scheme'] ? 443 : 80));
758
+ };
759
+ $here = $origin($url);
760
+ return null !== $here && $here === $origin($next) ? $next : null;
761
+ }
762
+
707
763
  private static function str(mixed $value, string $dflt): string
708
764
  {
709
765
  return (is_string($value) && '' !== $value) ? $value : $dflt;
@@ -713,6 +713,29 @@ class FeatureTest extends TestCase
713
713
  $this->assertSame(2, $h->client->_cache['bypass']);
714
714
  }
715
715
 
716
+ public function test_cache_a_body_that_declares_another_type_than_json_passes_by_unread(): void
717
+ {
718
+ $this->needs('cache');
719
+ $sent = [];
720
+ $rec = new FtRecorder(function (int $n) use (&$sent) {
721
+ $res = FtHarness::response(200, null, ['content-type' => 'audio/wav']);
722
+ $res['body'] = 'RIFF' . $n;
723
+ $sent[] = $res;
724
+ return $res;
725
+ });
726
+ $h = new FtHarness([
727
+ ['name' => 'cache'],
728
+ ], $rec->server());
729
+ $a = $h->op(['op' => 'load', 'path' => '/w/1/audio'])['ctx']->response;
730
+ $b = $h->op(['op' => 'load', 'path' => '/w/1/audio'])['ctx']->response;
731
+ $this->assertCount(2, $rec->calls, 'the body was stored');
732
+ $this->assertSame($sent[0]['json'], $a->json_func, 'the cache handed on a copy it had read');
733
+ $this->assertSame('RIFF1', $a->body);
734
+ $this->assertSame($sent[1]['json'], $b->json_func);
735
+ $this->assertSame('RIFF2', $b->body);
736
+ $this->assertSame(2, $h->client->_cache['bypass']);
737
+ }
738
+
716
739
  public function test_cache_refetches_after_the_ttl(): void
717
740
  {
718
741
  $this->needs('cache');
@@ -81,6 +81,7 @@ class ProjectNameSecretsWire
81
81
  'url' => $fullurl,
82
82
  'auth' => $headers[self::credheader()] ?? null,
83
83
  'has' => array_key_exists(self::credheader(), $headers),
84
+ 'values' => implode("\n", array_map(fn($v) => is_scalar($v) ? (string)$v : '', $headers)),
84
85
  'body' => $fetchdef['body'] ?? null,
85
86
  'redirect' => $fetchdef['redirect'] ?? null,
86
87
  ];
@@ -134,8 +135,13 @@ class ProjectNameSecretsTest extends TestCase
134
135
 
135
136
  // Read from the config, never from a probe: an SDK whose config declares no
136
137
  // auth places no credential, and these tests assert on the one it places.
138
+ // The auth-off test asserts it places none.
137
139
  protected function setUp(): void
138
140
  {
141
+ $test = method_exists($this, 'name') ? $this->name() : $this->getName();
142
+ if ('testAuthOffPlacesNoCredentialAndBuysNoToken' === $test) {
143
+ return;
144
+ }
139
145
  if (!array_key_exists('auth', ProjectNameConfig::shared_config()['options'] ?? [])) {
140
146
  $this->markTestSkipped("this SDK's config declares no auth, so it places no credential");
141
147
  }
@@ -164,17 +170,23 @@ class ProjectNameSecretsTest extends TestCase
164
170
  // -----------------------------------------------------------------
165
171
  // Support.
166
172
 
167
- // The credential header carries the SPEC's credential prefix (if any),
168
- // which a TEMPLATE cannot know - so assert on the CREDENTIAL and let
169
- // the prefix be whatever this SDK's API declares.
170
- private function assertCredential(mixed $header, string $token): void
173
+ // The header value prepare_auth writes for a token, in the form the
174
+ // DEFAULT client's auth gives: under Basic the token is the key half of
175
+ // the encoded pair, and a prefix is space-joined.
176
+ private function credential(string $token, string $secret = ''): string
177
+ {
178
+ $auth = ProjectNameSDK::test(null, null)->options_map()['auth'] ?? [];
179
+ $prefix = is_string($auth['prefix'] ?? null) ? $auth['prefix'] : '';
180
+ $value = true === ($auth['basic'] ?? null)
181
+ ? base64_encode($token . ':' . $secret) : $token;
182
+ return '' === $prefix ? $value : $prefix . ' ' . $value;
183
+ }
184
+
185
+ private function assertCredential(mixed $header, string $token, string $secret = ''): void
171
186
  {
172
- $ok = $header === $token
173
- || (is_string($header) && str_ends_with($header, ' ' . $token));
174
- $this->assertTrue($ok,
187
+ $this->assertSame($this->credential($token, $secret), $header,
175
188
  'expected the ' . ProjectNameSecretsWire::credheader() .
176
- ' header to carry ' . $token .
177
- ', got: ' . var_export($header, true));
189
+ ' header to carry ' . $token);
178
190
  }
179
191
 
180
192
  private function secretsFeatureOf($client): ?ProjectNameSecretsFeature
@@ -395,6 +407,19 @@ class ProjectNameSecretsTest extends TestCase
395
407
  'the chain credential must never be written into the client options');
396
408
  }
397
409
 
410
+ public function testAResolvedKeyIsPlacedAsPrepareAuthPlacesItTheSecretKept(): void
411
+ {
412
+ $this->setenv('APIKEY', 'ENVKEY04');
413
+ $wire = new ProjectNameSecretsWire();
414
+ $client = $this->secretsClient($wire, [
415
+ 'secret' => 'PASS01',
416
+ 'feature' => $this->secretsOpts(),
417
+ ]);
418
+
419
+ $this->driveOp($client, $wire);
420
+ $this->assertCredential($wire->api()[0]['auth'], 'ENVKEY04', 'PASS01');
421
+ }
422
+
398
423
  // The same pin where it matters most: `auth: null` is the caller saying
399
424
  // NO credential may be sent. Nothing goes on the wire - and nothing is
400
425
  // parked in the options map either, where it would outlive the request
@@ -819,6 +844,28 @@ class ProjectNameSecretsTest extends TestCase
819
844
  $this->assertCredential($api[1]['auth'], 'ACCESS02');
820
845
  }
821
846
 
847
+ public function testATokenBoughtOnARefusalIsPlacedAsPrepareAuthPlacesIt(): void
848
+ {
849
+ $this->setenv('REFRESH_TOKEN', 'REFRESH01');
850
+ $wire = new ProjectNameSecretsWire();
851
+ $wire->apistatus = [401, 200];
852
+
853
+ $client = $this->secretsClient($wire, [
854
+ 'secret' => 'PASS01',
855
+ 'feature' => $this->secretsOpts([
856
+ 'name' => 'refresh_token',
857
+ 'exchange' => ['active' => true],
858
+ ]),
859
+ ]);
860
+
861
+ $this->driveOp($client, $wire);
862
+
863
+ $api = $wire->api();
864
+ $this->assertCount(2, $api, 'expected the request to be retried exactly once');
865
+ $this->assertCredential($api[0]['auth'], 'ACCESS01', 'PASS01');
866
+ $this->assertCredential($api[1]['auth'], 'ACCESS02', 'PASS01');
867
+ }
868
+
822
869
  // `auth: null` SUPPRESSES THE PURCHASE, not just the retry.
823
870
  //
824
871
  // resolve() runs before with_refresh's suppression check, so the
@@ -994,17 +1041,18 @@ class ProjectNameSecretsTest extends TestCase
994
1041
  $this->assertNull($leak, 'the stub outlived stop(): ' . (string)$leak);
995
1042
  }
996
1043
 
997
- // Real listeners and the SDK's raw HTTP function: a stub follows nothing.
998
- // Its own process, for the reason the test above gives.
999
- #[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
1000
- #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
1001
- public function testARedirectAnsweringTheExchangeIsAFailureAndNothingFollowsIt(): void
1044
+ // Real listeners and the SDK's raw HTTP function: a stub follows
1045
+ // nothing. The origin answers the exchange as its `answer.txt` says and
1046
+ // records what else it gets; `exchanged.txt` counts the exchanges.
1047
+ private function withOrigins(callable $run): void
1002
1048
  {
1003
1049
  $record = <<<'ROUTER'
1004
1050
  <?php
1005
- $path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?? '/';
1051
+ // The request target, query included: only the bare token path is the exchange.
1052
+ $path = (string)($_SERVER['REQUEST_URI'] ?? '/');
1006
1053
  $answer = @file_get_contents(__DIR__ . '/answer.txt');
1007
1054
  if (false !== $answer && '/api/auth/token' === $path) {
1055
+ file_put_contents(__DIR__ . '/exchanged.txt', 'x', FILE_APPEND);
1008
1056
  [$status, $location] = explode(' ', $answer, 2);
1009
1057
  header('location: ' . $location, true, (int)$status);
1010
1058
  return;
@@ -1026,38 +1074,71 @@ ROUTER;
1026
1074
  if (0 === $oport || 0 === $aport) {
1027
1075
  $this->markTestSkipped('could not start the local HTTP stubs');
1028
1076
  }
1029
- $base = 'http://127.0.0.1:' . $oport . '/api';
1030
- $away = 'http://127.0.0.1:' . $aport . '/api/auth/token';
1077
+ $run((object)[
1078
+ 'origin' => $origin,
1079
+ 'other' => $other,
1080
+ 'at' => 'http://127.0.0.1:' . $oport,
1081
+ 'base' => 'http://127.0.0.1:' . $oport . '/api',
1082
+ 'away' => 'http://127.0.0.1:' . $aport . '/api/auth/token',
1083
+ ]);
1084
+ } finally {
1085
+ $origin->stop();
1086
+ $other->stop();
1087
+ }
1088
+ }
1089
+
1090
+ private function redirectClient(string $base, array $exchange = [])
1091
+ {
1092
+ return $this->withSecrets(function (bool $adopt) use ($base, $exchange) {
1093
+ $opts = [
1094
+ 'base' => $base,
1095
+ 'feature' => ['secrets' => [
1096
+ 'active' => true,
1097
+ 'name' => 'refresh_token',
1098
+ 'exchange' => array_merge(['active' => true, 'refresh' => 'REFRESH01'], $exchange),
1099
+ ]],
1100
+ ];
1101
+ if ($adopt) {
1102
+ $opts['extend'] = [new ProjectNameSecretsFeature()];
1103
+ }
1104
+ return new ProjectNameSDK($opts);
1105
+ });
1106
+ }
1107
+
1108
+ private function redirectProbe($client): array
1109
+ {
1110
+ try {
1111
+ $res = $client->direct(['path' => '/probe']);
1112
+ $message = is_array($res) && null !== ($res['err'] ?? null)
1113
+ ? (string)(($res['err'] instanceof \Throwable) ? $res['err']->getMessage() : $res['err'])
1114
+ : json_encode($res);
1115
+ return [$res, $message];
1116
+ } catch (\Throwable $e) {
1117
+ return [null, $e->getMessage()];
1118
+ }
1119
+ }
1120
+
1121
+ private static function lines(?string $text): array
1122
+ {
1123
+ return array_values(array_filter(explode("\n", (string)$text)));
1124
+ }
1125
+
1126
+ // Its own process, for the reason the test above gives.
1127
+ #[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
1128
+ #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
1129
+ public function testARedirectAnsweringTheExchangeIsAFailureAndNothingFollowsIt(): void
1130
+ {
1131
+ $this->withOrigins(function (object $o) {
1132
+ $base = $o->base;
1133
+ $away = $o->away;
1031
1134
  $answers = [[307, $away], [308, $away], [302, $away], [307, '/api/moved/token']];
1032
1135
 
1033
1136
  $allowed = [];
1034
1137
  $refused = 0;
1035
1138
 
1036
1139
  foreach ($answers as [$status, $location]) {
1037
- $origin->put('answer.txt', $status . ' ' . $location);
1038
- $client = $this->withSecrets(function (bool $adopt) use ($base) {
1039
- $opts = [
1040
- 'base' => $base,
1041
- 'feature' => ['secrets' => [
1042
- 'active' => true,
1043
- 'name' => 'refresh_token',
1044
- 'exchange' => ['active' => true, 'refresh' => 'REFRESH01'],
1045
- ]],
1046
- ];
1047
- if ($adopt) {
1048
- $opts['extend'] = [new ProjectNameSecretsFeature()];
1049
- }
1050
- return new ProjectNameSDK($opts);
1051
- });
1052
-
1053
- try {
1054
- $res = $client->direct(['path' => '/probe']);
1055
- $message = is_array($res) && null !== ($res['err'] ?? null)
1056
- ? (string)(($res['err'] instanceof \Throwable) ? $res['err']->getMessage() : $res['err'])
1057
- : json_encode($res);
1058
- } catch (\Throwable $e) {
1059
- $message = $e->getMessage();
1060
- }
1140
+ $o->origin->put('answer.txt', $status . ' ' . $location);
1141
+ [, $message] = $this->redirectProbe($this->redirectClient($base));
1061
1142
 
1062
1143
  if (str_contains($message,
1063
1144
  'token exchange failed: ' . $status . ' from ' . $base . '/auth/token')) {
@@ -1067,8 +1148,8 @@ ROUTER;
1067
1148
  }
1068
1149
  }
1069
1150
 
1070
- $elsewhere = array_values(array_filter(explode("\n", (string)$other->read('seen.txt'))));
1071
- $followed = array_values(array_filter(explode("\n", (string)$origin->read('seen.txt'))));
1151
+ $elsewhere = self::lines($o->other->read('seen.txt'));
1152
+ $followed = self::lines($o->origin->read('seen.txt'));
1072
1153
 
1073
1154
  print("\nsecrets: exchange refused " . $refused . ' redirect(s), ' .
1074
1155
  (count($elsewhere) + count($followed)) . " followed\n");
@@ -1077,10 +1158,77 @@ ROUTER;
1077
1158
  $this->assertSame([], $followed, 'a request followed a refused exchange');
1078
1159
  $this->assertSame([], $allowed, 'a redirect was not a failed exchange');
1079
1160
  $this->assertSame(count($answers), $refused);
1080
- } finally {
1081
- $origin->stop();
1082
- $other->stop();
1083
- }
1161
+ });
1162
+ }
1163
+
1164
+ #[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
1165
+ #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
1166
+ public function testWithTheOptionARedirectOnTheExchangesOwnOriginIsFollowedAndNoOther(): void
1167
+ {
1168
+ $this->withOrigins(function (object $o) {
1169
+ $sent = ' {"refresh_token":"REFRESH01"}';
1170
+ // Each case: the status, the Location, and the path the exchange
1171
+ // is asked again at, or null where the redirect is refused.
1172
+ $cases = [
1173
+ [307, '/api/moved/token', '/api/moved/token'],
1174
+ [308, $o->at . '/api/moved/token', '/api/moved/token'],
1175
+ // A Location of a query alone keeps the path that answered.
1176
+ [307, '?step=2', '/api/auth/token?step=2'],
1177
+ [307, $o->away, null],
1178
+ [302, '/api/moved/token', null],
1179
+ [307, '/api/auth/token', null],
1180
+ ];
1181
+
1182
+ $wrong = [];
1183
+ $followed = 0;
1184
+ $refused = 0;
1185
+ $looped = 0;
1186
+
1187
+ foreach ($cases as [$status, $location, $path]) {
1188
+ $o->origin->put('answer.txt', $status . ' ' . $location);
1189
+ $o->origin->put('seen.txt', '');
1190
+ $o->origin->put('exchanged.txt', '');
1191
+
1192
+ [$res, $message] = $this->redirectProbe(
1193
+ $this->redirectClient($o->base, ['redirect' => 'same-origin']));
1194
+ $seen = array_map(function (string $line) {
1195
+ $r = json_decode($line, true);
1196
+ return trim($r['method'] . ' ' . $r['path'] . ' ' . $r['body']);
1197
+ }, self::lines($o->origin->read('seen.txt')));
1198
+ $shown = $status . ' to ' . $location . ': ' . $message . ' ' . json_encode($seen);
1199
+
1200
+ if (null !== $path) {
1201
+ if (is_array($res) && true === ($res['ok'] ?? null) &&
1202
+ 2 === count($seen) && 'POST ' . $path . $sent === $seen[0]) {
1203
+ $followed++;
1204
+ } else {
1205
+ $wrong[] = $shown;
1206
+ }
1207
+ } elseif (0 === count($seen) && str_contains($message,
1208
+ 'token exchange failed: ' . $status . ' from ' . $o->base . '/auth/token')) {
1209
+ $refused++;
1210
+ } else {
1211
+ $wrong[] = $shown;
1212
+ }
1213
+
1214
+ if ('/api/auth/token' === $location) {
1215
+ $looped = strlen((string)$o->origin->read('exchanged.txt'));
1216
+ }
1217
+ }
1218
+
1219
+ $elsewhere = self::lines($o->other->read('seen.txt'));
1220
+
1221
+ print("\nsecrets: exchange opt-in followed " . $followed .
1222
+ ' redirect(s) on its own origin, refused ' . $refused . ', ' .
1223
+ count($elsewhere) . " elsewhere\n");
1224
+
1225
+ $this->assertSame([], $elsewhere, 'the exchange reached another origin');
1226
+ $this->assertSame([], $wrong);
1227
+ $this->assertSame(3, $followed);
1228
+ $this->assertSame(3, $refused);
1229
+ // A redirect to itself is followed up to the hop limit and no more.
1230
+ $this->assertSame(6, $looped);
1231
+ });
1084
1232
  }
1085
1233
 
1086
1234
  // -----------------------------------------------------------------
@@ -1108,6 +1256,66 @@ ROUTER;
1108
1256
  $this->assertSame(1, $calls, 'the refusal must not be retried');
1109
1257
  $this->assertCount(0, $wire->calls);
1110
1258
  }
1259
+
1260
+ // -----------------------------------------------------------------
1261
+ // Off by the config where it declares no auth, else by `auth: null`:
1262
+ // either way the feature places no credential, buys no token and
1263
+ // retries nothing.
1264
+
1265
+ public function testAuthOffPlacesNoCredentialAndBuysNoToken(): void
1266
+ {
1267
+ $declared = array_key_exists('auth', ProjectNameConfig::shared_config()['options'] ?? []);
1268
+ $by = $declared ? 'option' : 'config';
1269
+ $off = $declared ? ['auth' => null] : [];
1270
+ $secrets = ['OFFKEY01', 'OFFKEY02', 'OFFREFRESH01', 'ACCESS01'];
1271
+ $carries = function (array $call) use ($secrets): bool {
1272
+ if ($call['has']) {
1273
+ return true;
1274
+ }
1275
+ foreach ($secrets as $secret) {
1276
+ if (str_contains((string)$call['values'], $secret)) {
1277
+ return true;
1278
+ }
1279
+ }
1280
+ return false;
1281
+ };
1282
+ $sent = [];
1283
+ $bought = 0;
1284
+
1285
+ $scenarios = [
1286
+ ['a key the chain resolves', ['APIKEY' => 'OFFKEY01'], [], [], [200]],
1287
+ ['an explicit apikey', [], ['apikey' => 'OFFKEY02'], [], [200]],
1288
+ ['a refusal', ['REFRESH_TOKEN' => 'OFFREFRESH01'], [],
1289
+ ['name' => 'refresh_token', 'exchange' => ['active' => true]], [401]],
1290
+ ];
1291
+
1292
+ foreach ($scenarios as [$what, $env, $sdkopts, $fopts, $statuses]) {
1293
+ foreach ($env as $name => $value) {
1294
+ $this->setenv($name, $value);
1295
+ }
1296
+ $wire = new ProjectNameSecretsWire();
1297
+ $wire->apistatus = $statuses;
1298
+ $client = $this->secretsClient($wire,
1299
+ array_merge($sdkopts, $off, ['feature' => $this->secretsOpts($fopts)]));
1300
+
1301
+ $this->driveOp($client, $wire);
1302
+
1303
+ $this->assertCount(1, $wire->api(), $what . ': a request was retried with auth off');
1304
+ $this->assertCount(0, $wire->token(), $what . ': a token was bought with auth off');
1305
+ $this->assertFalse($carries($wire->api()[0]), $what . ': a credential was sent with auth off');
1306
+ $this->assertSame('', $this->secretsFeatureOf($client)->credential(),
1307
+ $what . ': the feature resolved a credential with auth off');
1308
+
1309
+ $sent = array_merge($sent, $wire->api());
1310
+ $bought += count($wire->token());
1311
+ foreach ($env as $name => $value) {
1312
+ $this->clearenv($name);
1313
+ }
1314
+ }
1315
+
1316
+ print("\nsecrets: auth off by " . $by . ': ' . count($sent) . ' request(s), ' .
1317
+ count(array_filter($sent, $carries)) . ' credential(s), ' . $bought . " token(s)\n");
1318
+ }
1111
1319
  }
1112
1320
 
1113
1321
  // A one-request-at-a-time HTTP stand-in, run as a child `php -S` process:
@@ -1194,8 +1402,10 @@ ROUTER;
1194
1402
  // harness that captures output must not end up waiting on the
1195
1403
  // server's.
1196
1404
  $pipes = [];
1405
+ // Windows names its null device NUL, and has no /dev/null to open.
1406
+ $null = '\\' === DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null';
1197
1407
  $this->proc = @proc_open($cmd, [
1198
- 0 => ['file', '/dev/null', 'r'],
1408
+ 0 => ['file', $null, 'r'],
1199
1409
  1 => ['file', $this->dir . '/out.log', 'a'],
1200
1410
  2 => ['file', $this->dir . '/err.log', 'a'],
1201
1411
  ], $pipes, $this->dir);
@@ -65,8 +65,8 @@ class ProjectNameMakeSpec
65
65
  if ('graphql' === \Voxgig\Struct\Struct::getprop($point, 'kind')) {
66
66
  // GraphQL addresses one endpoint: no path parts, no query
67
67
  // string, and the body carries the operation. prepare_body is
68
- // skipped deliberately — it only emits a body for data-input
69
- // ops, whereas every GraphQL op posts one, including
68
+ // skipped deliberately — it builds only a REST body,
69
+ // whereas every GraphQL op posts one, including
70
70
  // load/list/remove.
71
71
  $ctx->spec->body = ($utility->graphql_body)($ctx);
72
72
  $ctx->spec->path = '';
@@ -23,6 +23,37 @@ class ProjectNamePrepareBody
23
23
  }
24
24
  return (is_array($body) && 0 === count($body)) ? null : $body;
25
25
  }
26
+ $fields = \Voxgig\Struct\Struct::getprop($ctx->point, 'fields');
27
+ if (is_array($fields) && 0 < count($fields)) {
28
+ return self::matchBody($ctx, $fields);
29
+ }
26
30
  return null;
27
31
  }
32
+
33
+ // A list, load or remove routed to a method with a body sends the match
34
+ // fields that body declares, never a path parameter, and never a header,
35
+ // cookie or query argument the body does not also declare as a field.
36
+ // With none given it is still a JSON object, which an empty array is not.
37
+ private static function matchBody(ProjectNameContext $ctx, array $fields): array|\stdClass
38
+ {
39
+ $point = $ctx->point;
40
+ $params = \Voxgig\Struct\Struct::getprop($point, 'params');
41
+ $routed = is_array($params) ? $params : [];
42
+ foreach (['params', 'header', 'cookie', 'query'] as $kind) {
43
+ $defs = \Voxgig\Struct\Struct::getpath($point, 'args.' . $kind);
44
+ foreach (is_array($defs) ? $defs : [] as $ad) {
45
+ if ('params' === $kind || true !== \Voxgig\Struct\Struct::getprop($ad, 'field')) {
46
+ $routed[] = \Voxgig\Struct\Struct::getprop($ad, 'name');
47
+ }
48
+ }
49
+ }
50
+ $body = [];
51
+ foreach ($fields as $name) {
52
+ $val = \Voxgig\Struct\Struct::getprop($ctx->reqmatch ?? [], $name);
53
+ if (null !== $val && '$action' !== $name && !in_array($name, $routed, true)) {
54
+ $body[$name] = $val;
55
+ }
56
+ }
57
+ return 0 === count($body) ? new \stdClass() : $body;
58
+ }
28
59
  }