@voxgig/sdkgen 4.48.1 → 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 (119) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/package.json +1 -1
  3. package/project/.sdk/model/feature/secrets.aontu +3 -0
  4. package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
  5. package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
  6. package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
  7. package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
  8. package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
  9. package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
  10. package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
  11. package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
  12. package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
  13. package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
  14. package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
  15. package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
  16. package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
  17. package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
  18. package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
  19. package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
  20. package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
  21. package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
  22. package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
  23. package/project/.sdk/tm/c/feature/cache.c +16 -1
  24. package/project/.sdk/tm/c/feature/secrets.c +149 -18
  25. package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
  26. package/project/.sdk/tm/c/tests/feature_test.c +31 -0
  27. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
  28. package/project/.sdk/tm/clojure/src/sdk/core.clj +8 -1
  29. package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
  30. package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
  31. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
  32. package/project/.sdk/tm/cpp/core/types.hpp +1 -11
  33. package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
  34. package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
  35. package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
  36. package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
  37. package/project/.sdk/tm/csharp/core/Control.cs +0 -5
  38. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
  39. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
  40. package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
  41. package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
  42. package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
  43. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
  44. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
  45. package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
  46. package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
  47. package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
  48. package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
  49. package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
  50. package/project/.sdk/tm/go/test/feature_test.go +32 -0
  51. package/project/.sdk/tm/go/utility/media.go +2 -2
  52. package/project/.sdk/tm/java/core/Control.java +0 -5
  53. package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
  54. package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
  55. package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
  56. package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
  57. package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
  58. package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
  59. package/project/.sdk/tm/java/utility/Media.java +2 -2
  60. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
  61. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
  62. package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
  63. package/project/.sdk/tm/js/test/feature.test.js +17 -0
  64. package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
  65. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
  66. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
  67. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
  68. package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
  69. package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
  70. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
  71. package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
  72. package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
  73. package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
  74. package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
  75. package/project/.sdk/tm/lua/utility/clean.lua +1 -0
  76. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
  77. package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
  78. package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
  79. package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
  80. package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
  81. package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
  82. package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
  83. package/project/.sdk/tm/perl/t/feature.t +24 -0
  84. package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
  85. package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
  86. package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
  87. package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
  88. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
  89. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
  90. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
  91. package/project/.sdk/tm/py/test/test_feature.py +18 -0
  92. package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
  93. package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
  94. package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
  95. package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
  96. package/project/.sdk/tm/rust/feature/cache.rs +15 -1
  97. package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
  98. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
  99. package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
  100. package/project/.sdk/tm/scala/core/Control.scala +0 -5
  101. package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
  102. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
  103. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
  104. package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
  105. package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
  106. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
  107. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
  108. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
  109. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
  110. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
  111. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
  112. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
  113. package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
  114. package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
  115. package/project/.sdk/tm/zig/feature/cache.zig +15 -1
  116. package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
  117. package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
  118. package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
  119. package/project/sdkgen-package.json +1 -1
@@ -37,15 +37,11 @@ use warnings;
37
37
  use Test::More;
38
38
  use FindBin;
39
39
  use lib "$FindBin::Bin/../../../lib";
40
+ use MIME::Base64 ();
40
41
  use Scalar::Util ();
41
42
 
42
43
  use ProjectNameSDK;
43
44
 
44
- # Read from the config, never from a probe: an SDK whose config declares no
45
- # auth places no credential, and these tests assert on the one it places.
46
- plan skip_all => "this SDK's config declares no auth, so it places no credential"
47
- unless exists ProjectNameConfig::make_config()->{options}{auth};
48
-
49
45
  my $ENVPREFIX = 'PROJECTENV_TEST_SECRETS_';
50
46
 
51
47
  my $RAWBASE = 'http://raw.test/api';
@@ -85,6 +81,7 @@ sub counting_fetch {
85
81
  'method' => $fetchdef->{method},
86
82
  'body' => $fetchdef->{body},
87
83
  'auth' => $headers->{'authorization'},
84
+ 'values' => join("\n", map { defined $_ ? "$_" : '' } values %$headers),
88
85
  'redirect' => $fetchdef->{redirect},
89
86
  };
90
87
 
@@ -143,18 +140,32 @@ sub wire {
143
140
  }
144
141
 
145
142
 
146
- # The Authorization header carries the SPEC's credential prefix, which a
147
- # TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
148
- # `Bearer <token>`, an apiKey scheme the raw token. So assert on the
149
- # CREDENTIAL and let the prefix be whatever this SDK's API declares -
150
- # pinning the whole header value passes only for a prefix-less API, and
151
- # this file ships to every project that selects the feature.
143
+ # The form prepare_auth writes, from the DEFAULT client's auth: under Basic
144
+ # the token is the key half of the encoded pair, and a prefix is
145
+ # space-joined.
146
+ my $CREDFORM = do {
147
+ my $auth = ProjectNameSDK->test(undef, undef)->options_map->{auth};
148
+ $auth = {} unless 'HASH' eq ref $auth;
149
+ my $prefix = $auth->{prefix};
150
+ { 'prefix' => (defined $prefix && !ref $prefix ? "$prefix" : ''),
151
+ 'basic' => ProjectNameHelpers::is_true($auth->{basic}) ? 1 : 0 };
152
+ };
153
+
154
+
155
+ # The header value prepare_auth writes for a token.
156
+ sub credential {
157
+ my ($token, $secret) = @_;
158
+ $secret = '' unless defined $secret;
159
+ my $value = $CREDFORM->{basic}
160
+ ? MIME::Base64::encode_base64("$token:$secret", '') : $token;
161
+ return '' eq $CREDFORM->{prefix} ? $value : "$CREDFORM->{prefix} $value";
162
+ }
163
+
164
+
152
165
  sub credential_is {
153
- my ($header, $token, $label) = @_;
154
- my $got = defined $header ? "$header" : '';
155
- ok(($got eq $token || $got =~ /\s\Q$token\E\z/),
156
- ($label || 'credential') . ": expected the Authorization header to carry"
157
- . " $token, got: '$got'");
166
+ my ($header, $token, $label, $secret) = @_;
167
+ is($header, credential($token, $secret),
168
+ ($label || 'credential') . ": expected the Authorization header to carry $token");
158
169
  }
159
170
 
160
171
 
@@ -241,6 +252,66 @@ sub clear_env {
241
252
  }
242
253
 
243
254
 
255
+ # --- Auth off ---------------------------------------------------------------
256
+
257
+ # Off by the config where it declares no auth, else by `auth => undef`:
258
+ # either way the feature places no credential, buys no token and retries
259
+ # nothing.
260
+ sub auth_off {
261
+ my ($by, %off) = @_;
262
+ my @secrets = ('OFFKEY01', 'OFFKEY02', 'OFFREFRESH01', 'ACCESS01');
263
+ my $carries = sub {
264
+ my ($call) = @_;
265
+ return 1 if defined $call->{auth};
266
+ return scalar grep { 0 <= index($call->{values}, $_) } @secrets;
267
+ };
268
+ my @sent;
269
+ my $bought = 0;
270
+
271
+ my @scenarios = (
272
+ ['a key the chain resolves', { 'APIKEY' => 'OFFKEY01' }, {}, envchain(), [200]],
273
+ ['an explicit apikey', {}, { 'apikey' => 'OFFKEY02' }, envchain(), [200]],
274
+ ['a refusal', { 'REFRESH_TOKEN' => 'OFFREFRESH01' }, {},
275
+ envchain('name' => 'refresh_token', 'exchange' => { 'active' => 1 }), [401]],
276
+ );
277
+
278
+ for my $scenario (@scenarios) {
279
+ my ($what, $env, $extra, $sopts, $apistatus) = @$scenario;
280
+ clear_env();
281
+ $ENV{$ENVPREFIX . $_} = $env->{$_} for keys %$env;
282
+
283
+ my $stub = counting_fetch('apistatus' => $apistatus);
284
+ my $sdk = secrets_sdk($stub, $sopts, { %$extra, %off, 'base' => $XBASE });
285
+ $sdk->direct({ 'path' => '/thing' });
286
+
287
+ my $api = api_calls($stub);
288
+ is(scalar @$api, 1, "auth off: $what: one request, not retried");
289
+ is(scalar @{ token_calls($stub) }, 0, "auth off: $what: no token bought");
290
+ ok(!$carries->($api->[0]), "auth off: $what: no credential sent");
291
+ is($sdk->{_secrets}->credential, '', "auth off: $what: nothing resolved");
292
+
293
+ push @sent, @$api;
294
+ $bought += scalar @{ token_calls($stub) };
295
+ }
296
+ clear_env();
297
+
298
+ print STDERR "secrets: auth off by $by: " . scalar(@sent) . ' request(s), '
299
+ . scalar(grep { $carries->($_) } @sent) . " credential(s), $bought token(s)\n";
300
+ }
301
+
302
+
303
+ # Read from the config, never from a probe: an SDK whose config declares no
304
+ # auth places no credential, and the tests below assert on the one it places.
305
+ if (!exists ProjectNameConfig::make_config()->{options}{auth}) {
306
+ auth_off('config');
307
+ SKIP: {
308
+ skip("this SDK's config declares no auth, so it places no credential", 1);
309
+ }
310
+ done_testing;
311
+ exit 0;
312
+ }
313
+
314
+
244
315
  # --- Inactive: the feature costs nothing -----------------------------------
245
316
 
246
317
  {
@@ -307,6 +378,16 @@ sub clear_env {
307
378
  'active: an explicitly EMPTY apikey also defers to the chain');
308
379
  }
309
380
 
381
+ {
382
+ clear_env();
383
+ $ENV{$ENVPREFIX . 'APIKEY'} = 'ENVKEY03';
384
+
385
+ my $stub = counting_fetch();
386
+ secrets_sdk($stub, envchain(), { 'secret' => 'PASS01' })->direct({ 'path' => '/thing' });
387
+ credential_is($stub->{calls}[0]{auth}, 'ENVKEY03',
388
+ 'active: a resolved key is placed as prepare_auth places it, the secret kept', 'PASS01');
389
+ }
390
+
310
391
  # THE PERL SPELLING OF A CUSTOM PROVIDER IS A MAP OF CALLABLES.
311
392
  #
312
393
  # make_options deep-clones the option tree, and Voxgig::Struct::clone
@@ -825,6 +906,20 @@ sub exchange_sdk {
825
906
  is($res->{ok}, 1, 'exchange: the caller sees the successful retry');
826
907
  }
827
908
 
909
+ {
910
+ clear_env();
911
+ $ENV{$ENVPREFIX . 'REFRESH_TOKEN'} = 'REFRESH01';
912
+
913
+ my $stub = counting_fetch('apistatus' => [401, 200]);
914
+ exchange_sdk($stub, {}, { 'secret' => 'PASS01' })->direct({ 'path' => '/thing' });
915
+
916
+ is(scalar @{ api_calls($stub) }, 2, 'exchange: the request was retried');
917
+ credential_is(api_calls($stub)->[0]{auth}, 'ACCESS01', 'exchange: first try, the secret kept',
918
+ 'PASS01');
919
+ credential_is(api_calls($stub)->[1]{auth}, 'ACCESS02',
920
+ 'exchange: a token bought on a refusal is placed as prepare_auth places it', 'PASS01');
921
+ }
922
+
828
923
  {
829
924
  clear_env();
830
925
  $ENV{$ENVPREFIX . 'REFRESH_TOKEN'} = 'REFRESH01';
@@ -1017,9 +1112,9 @@ sub exchange_sdk {
1017
1112
 
1018
1113
  # Real listeners and the SDK's raw HTTP fetch: a stub follows nothing. One
1019
1114
  # child per case serves both origins, printing each request it records,
1020
- # until it is told to stop. The exchange itself is not recorded.
1115
+ # until it is told to stop. The exchange itself is only counted.
1021
1116
  sub redirect_exchange {
1022
- my ($status, $location) = @_;
1117
+ my ($status, $location, $exchange) = @_;
1023
1118
  require IO::Socket::INET;
1024
1119
  require IO::Select;
1025
1120
  require POSIX;
@@ -1028,6 +1123,7 @@ sub redirect_exchange {
1028
1123
  my $other = IO::Socket::INET->new(@listen);
1029
1124
  my $apiport = $api->sockport;
1030
1125
  $location =~ s/OTHER/$other->sockport/e;
1126
+ $location =~ s/ORIGIN/$apiport/;
1031
1127
  pipe(my $seen_r, my $seen_w) or return;
1032
1128
  my $pid = fork();
1033
1129
  return unless defined $pid;
@@ -1046,9 +1142,10 @@ sub redirect_exchange {
1046
1142
  $length = $value if 'content-length' eq lc $name;
1047
1143
  }
1048
1144
  read($sock, $sent, $length) if $length;
1145
+ $sent =~ s/\r?\n/ /g;
1049
1146
  my $hop = $srv == $api && '/api/auth/token' eq $rpath;
1050
- print {$seen_w} join("\t", $srv == $api ? 'origin' : 'other', $method, $rpath, $sent), "\n"
1051
- unless $hop;
1147
+ print {$seen_w} join("\t", $hop ? 'exchange' : $srv == $api ? 'origin' : 'other',
1148
+ $method, $rpath, $sent), "\n";
1052
1149
  my $body = $srv == $api ? '{"access_token":"MOVED01"}' : '{"access_token":"ELSEWHERE01"}';
1053
1150
  $body = '' if $hop;
1054
1151
  print {$sock} 'HTTP/1.1 ' . ($hop ? $status : 200) . " X\r\n"
@@ -1069,7 +1166,7 @@ sub redirect_exchange {
1069
1166
  'feature' => { 'secrets' => {
1070
1167
  'active' => 1,
1071
1168
  'name' => 'refresh_token',
1072
- 'exchange' => { 'active' => 1, 'refresh' => 'REFRESH01' },
1169
+ 'exchange' => { 'active' => 1, 'refresh' => 'REFRESH01', %{ $exchange // {} } },
1073
1170
  } },
1074
1171
  });
1075
1172
  my $res = eval { $sdk->direct({ 'path' => '/thing' }) };
@@ -1077,13 +1174,14 @@ sub redirect_exchange {
1077
1174
 
1078
1175
  my $stop = IO::Socket::INET->new(PeerAddr => '127.0.0.1', PeerPort => $apiport);
1079
1176
  print {$stop} "GET /stop HTTP/1.0\r\n\r\n" if $stop;
1080
- my @arrived;
1177
+ my ($exchanged, @arrived) = (0);
1081
1178
  while (defined(my $line = <$seen_r>)) {
1082
1179
  chomp $line;
1083
- push @arrived, $line;
1180
+ if (0 == index($line, "exchange\t")) { $exchanged++ }
1181
+ else { push @arrived, $line }
1084
1182
  }
1085
1183
  waitpid($pid, 0);
1086
- return ($message, \@arrived, $base);
1184
+ return ($message, \@arrived, $base, $exchanged);
1087
1185
  }
1088
1186
 
1089
1187
  SKIP: {
@@ -1112,6 +1210,61 @@ SKIP: {
1112
1210
  is($refused, scalar @answers, 'exchange redirect: every case refused');
1113
1211
  }
1114
1212
 
1213
+ SKIP: {
1214
+ clear_env();
1215
+ require JSON::PP;
1216
+ my $moved = sub {
1217
+ my ($line, $path) = @_;
1218
+ my ($tag, $method, $rpath, $sent) = split /\t/, ($line // ''), 4;
1219
+ my $body = eval { JSON::PP::decode_json($sent // '') };
1220
+ return 'origin' eq $tag && 'POST' eq $method && $path eq ($rpath // '')
1221
+ && 'HASH' eq ref $body && 1 == keys %$body && 'REFRESH01' eq ($body->{refresh_token} // '');
1222
+ };
1223
+ # Each case: the status, the Location, and the path the exchange is asked
1224
+ # again at, or undef where the redirect is refused.
1225
+ my @cases = ([307, '/api/moved/token', '/api/moved/token'],
1226
+ [308, 'http://127.0.0.1:ORIGIN/api/moved/token', '/api/moved/token'],
1227
+ # A Location of a query alone keeps the path that answered.
1228
+ [307, '?step=2', '/api/auth/token?step=2'],
1229
+ [307, 'http://127.0.0.1:OTHER/api/auth/token', undef],
1230
+ [302, '/api/moved/token', undef], [307, '/api/auth/token', undef]);
1231
+ my ($followed, $refused, $looped, $elsewhere, @wrong) = (0, 0, 0, 0);
1232
+ for my $case (@cases) {
1233
+ my ($status, $location, $path) = @$case;
1234
+ my ($message, $arrived, $base, $exchanged) =
1235
+ redirect_exchange($status, $location, { 'redirect' => 'same-origin' });
1236
+ skip('no fork', 5) unless $arrived;
1237
+ my @origin = grep { 0 == index($_, "origin\t") } @$arrived;
1238
+ $elsewhere += grep { 0 == index($_, "other\t") } @$arrived;
1239
+ my $seen = "$status to $location: $message [" . join(' | ', @$arrived) . ']';
1240
+ if (defined $path) {
1241
+ if ('ok' eq $message && 2 == @origin && $moved->($origin[0], $path)) { $followed++ }
1242
+ else { push @wrong, $seen }
1243
+ }
1244
+ elsif (0 == @origin
1245
+ && 0 <= index($message, "token exchange failed: $status from $base/auth/token")) {
1246
+ $refused++;
1247
+ }
1248
+ else {
1249
+ push @wrong, $seen;
1250
+ }
1251
+ $looped = $exchanged if '/api/auth/token' eq $location;
1252
+ }
1253
+
1254
+ print STDERR 'secrets: exchange opt-in followed ' . $followed
1255
+ . ' redirect(s) on its own origin, refused ' . $refused . ', ' . $elsewhere
1256
+ . " elsewhere\n";
1257
+
1258
+ is($elsewhere, 0, 'exchange opt-in: nothing reached another origin');
1259
+ is_deeply(\@wrong, [], 'exchange opt-in: each case as the rule says');
1260
+ is($followed, 3, 'exchange opt-in: three redirects on its own origin followed');
1261
+ is($refused, 3, 'exchange opt-in: three redirects refused');
1262
+ # A redirect to itself is followed up to the hop limit and no more.
1263
+ is($looped, 6, 'exchange opt-in: the loop asked the exchange six times');
1264
+ }
1265
+
1115
1266
  clear_env();
1116
1267
 
1268
+ auth_off('option', 'auth' => undef);
1269
+
1117
1270
  done_testing;
@@ -592,6 +592,30 @@ if (has_feature('cache')) {
592
592
  is($h->track('_cache')->{bypass}, 2, 'cache tracked 2 bypasses');
593
593
  }
594
594
 
595
+ {
596
+ my @sent;
597
+ my ($server, $calls) = recording_server(sub {
598
+ my ($n) = @_;
599
+ my $res = make_response(200, undef, { 'content-type' => 'audio/wav' });
600
+ $res->{body} = "RIFF$n";
601
+ push @sent, $res;
602
+ return ($res, undef);
603
+ });
604
+ my $h = harness([fspec('cache')], server => $server);
605
+ my $a = $h->op('path' => '/w/1/audio')->{ctx}{response};
606
+ my $b = $h->op('path' => '/w/1/audio')->{ctx}{response};
607
+ my $same = sub {
608
+ my ($res, $sent) = @_;
609
+ return Scalar::Util::refaddr($res->{json_func}) == Scalar::Util::refaddr($sent->{json})
610
+ && Scalar::Util::refaddr($res->{headers}) == Scalar::Util::refaddr($sent->{headers})
611
+ && $res->{body} eq $sent->{body};
612
+ };
613
+ is(scalar @$calls, 2, 'cache: a body that declares another type than JSON is not stored');
614
+ ok($same->($a, $sent[0]), 'cache: the transport response is handed on, not a copy it read');
615
+ ok($same->($b, $sent[1]), 'cache: the second read hands on its own response');
616
+ is($h->track('_cache')->{bypass}, 2, 'cache: a body that is not JSON passes by');
617
+ }
618
+
595
619
  {
596
620
  my $clock = HarnessClock->new;
597
621
  my ($server, $calls) = recording_server();
@@ -4,6 +4,7 @@ declare(strict_types=1);
4
4
  // ProjectName SDK cache feature
5
5
 
6
6
  require_once __DIR__ . '/BaseFeature.php';
7
+ require_once __DIR__ . '/../utility/Media.php';
7
8
 
8
9
  // Response caching for safe (read) requests. Wraps the active transport and
9
10
  // serves a fresh cached snapshot instead of hitting the network when the
@@ -71,6 +72,12 @@ class ProjectNameCacheFeature extends ProjectNameBaseFeature
71
72
 
72
73
  [$res, $err] = $inner($ctx, $url, $fetchdef);
73
74
 
75
+ // A body that declares another type than JSON passes by, unread.
76
+ if ($this->_cacheable($res, $err) && !$this->_json($res)) {
77
+ $this->_track('bypass');
78
+ return [$res, $err];
79
+ }
80
+
74
81
  if ($this->_cacheable($res, $err)) {
75
82
  $snapshot = $this->_snapshot($res);
76
83
  $ttl = is_numeric($this->options['ttl'] ?? null) ? (float)$this->options['ttl'] : 5000.0;
@@ -94,6 +101,12 @@ class ProjectNameCacheFeature extends ProjectNameBaseFeature
94
101
  return is_numeric($status) && (int)$status >= 200 && (int)$status < 300;
95
102
  }
96
103
 
104
+ private function _json(array $res): bool
105
+ {
106
+ $type = ProjectNameMedia::headerValue($res['headers'] ?? null, 'content-type');
107
+ return '' === $type || ProjectNameMedia::isJsonMedia($type);
108
+ }
109
+
97
110
  private function _snapshot(array $res): array
98
111
  {
99
112
  $data = null;
@@ -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');