@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.
- package/bin/voxgig-sdkgen +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utility.js +9 -1
- package/dist/utility.js.map +1 -1
- package/package.json +1 -1
- package/project/.sdk/model/feature/secrets.aontu +3 -0
- package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
- package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
- package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
- package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
- package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
- package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
- package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
- package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
- package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
- package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
- package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
- package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
- package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
- package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
- package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
- package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
- package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
- package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
- package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
- package/project/.sdk/tm/c/feature/cache.c +16 -1
- package/project/.sdk/tm/c/feature/secrets.c +149 -18
- package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
- package/project/.sdk/tm/c/tests/feature_test.c +31 -0
- package/project/.sdk/tm/c/utility/make_spec.c +1 -1
- package/project/.sdk/tm/c/utility/prepare_body.c +64 -1
- package/project/.sdk/tm/c/utility/prepare_query.c +11 -6
- package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
- package/project/.sdk/tm/clojure/src/sdk/core.clj +48 -10
- package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
- package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
- package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
- package/project/.sdk/tm/cpp/core/types.hpp +1 -11
- package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
- package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
- package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
- package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
- package/project/.sdk/tm/cpp/utility/pipeline.hpp +52 -4
- package/project/.sdk/tm/csharp/core/Control.cs +0 -5
- package/project/.sdk/tm/csharp/core/Point.cs +6 -0
- package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
- package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
- package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
- package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
- package/project/.sdk/tm/csharp/utility/MakeSpec.cs +2 -2
- package/project/.sdk/tm/csharp/utility/PrepareBody.cs +42 -0
- package/project/.sdk/tm/csharp/utility/PrepareQuery.cs +13 -2
- package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
- package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
- package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
- package/project/.sdk/tm/elixir/lib/projectname/utility.ex +62 -6
- package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
- package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
- package/project/.sdk/tm/go/core/target.go +8 -0
- package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
- package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
- package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
- package/project/.sdk/tm/go/test/feature_test.go +32 -0
- package/project/.sdk/tm/go/utility/media.go +2 -2
- package/project/.sdk/tm/go/utility/prepare_body.go +37 -1
- package/project/.sdk/tm/go/utility/prepare_query.go +10 -1
- package/project/.sdk/tm/java/core/Control.java +0 -5
- package/project/.sdk/tm/java/core/Point.java +7 -0
- package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
- package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
- package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
- package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
- package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
- package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
- package/project/.sdk/tm/java/utility/MakeSpec.java +1 -1
- package/project/.sdk/tm/java/utility/Media.java +2 -2
- package/project/.sdk/tm/java/utility/PrepareBody.java +50 -0
- package/project/.sdk/tm/java/utility/PrepareQuery.java +11 -1
- package/project/.sdk/tm/js/src/Point.js +2 -0
- package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
- package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
- package/project/.sdk/tm/js/src/utility/MakeSpecUtility.js +1 -1
- package/project/.sdk/tm/js/src/utility/PrepareBodyUtility.js +29 -0
- package/project/.sdk/tm/js/src/utility/PrepareQueryUtility.js +5 -3
- package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
- package/project/.sdk/tm/js/test/feature.test.js +17 -0
- package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
- package/project/.sdk/tm/kotlin/core/Point.kt +7 -0
- package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
- package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
- package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
- package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
- package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
- package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
- package/project/.sdk/tm/kotlin/utility/MakeSpecUrl.kt +1 -1
- package/project/.sdk/tm/kotlin/utility/Prepare.kt +40 -10
- package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
- package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
- package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
- package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
- package/project/.sdk/tm/lua/utility/clean.lua +1 -0
- package/project/.sdk/tm/lua/utility/make_spec.lua +1 -1
- package/project/.sdk/tm/lua/utility/prepare_body.lua +40 -0
- package/project/.sdk/tm/lua/utility/prepare_query.lua +11 -1
- package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
- package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
- package/project/.sdk/tm/ocaml/sdk_runtime.ml +40 -5
- package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
- package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
- package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
- package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
- package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
- package/project/.sdk/tm/perl/t/feature.t +24 -0
- package/project/.sdk/tm/perl/utility/make_spec.pm +1 -1
- package/project/.sdk/tm/perl/utility/prepare_body.pm +38 -4
- package/project/.sdk/tm/perl/utility/prepare_query.pm +7 -1
- package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
- package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
- package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
- package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
- package/project/.sdk/tm/php/utility/MakeSpec.php +2 -2
- package/project/.sdk/tm/php/utility/PrepareBody.php +31 -0
- package/project/.sdk/tm/php/utility/PrepareQuery.php +9 -2
- package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
- package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
- package/project/.sdk/tm/py/pkg/utility/make_spec.py +1 -1
- package/project/.sdk/tm/py/pkg/utility/prepare_body.py +26 -0
- package/project/.sdk/tm/py/pkg/utility/prepare_query.py +7 -2
- package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
- package/project/.sdk/tm/py/test/test_feature.py +18 -0
- package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
- package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
- package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
- package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
- package/project/.sdk/tm/rb/utility/make_spec.rb +1 -1
- package/project/.sdk/tm/rb/utility/prepare_body.rb +31 -3
- package/project/.sdk/tm/rb/utility/prepare_query.rb +7 -1
- package/project/.sdk/tm/rust/feature/cache.rs +15 -1
- package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
- package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
- package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
- package/project/.sdk/tm/rust/utility/prepare_body.rs +51 -0
- package/project/.sdk/tm/rust/utility/prepare_query.rs +12 -4
- package/project/.sdk/tm/scala/core/Control.scala +0 -5
- package/project/.sdk/tm/scala/core/Point.scala +3 -0
- package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
- package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
- package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
- package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
- package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
- package/project/.sdk/tm/scala/utility/MakeSpec.scala +1 -1
- package/project/.sdk/tm/scala/utility/Prepare.scala +53 -12
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Point.swift +3 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Make.swift +1 -1
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Prepare.swift +36 -1
- package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
- package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
- package/project/.sdk/tm/ts/src/Point.ts +3 -0
- package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
- package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
- package/project/.sdk/tm/ts/src/utility/PrepareBodyUtility.ts +29 -0
- package/project/.sdk/tm/ts/src/utility/PrepareQueryUtility.ts +5 -3
- package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
- package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
- package/project/.sdk/tm/zig/core/utility.zig +47 -4
- package/project/.sdk/tm/zig/feature/cache.zig +15 -1
- package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
- package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
- package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
- package/project/sdkgen-package.json +1 -1
- package/src/utility.ts +12 -1
|
@@ -53,6 +53,7 @@ use warnings;
|
|
|
53
53
|
|
|
54
54
|
use File::Basename ();
|
|
55
55
|
use Cwd ();
|
|
56
|
+
use MIME::Base64 ();
|
|
56
57
|
use Scalar::Util ();
|
|
57
58
|
|
|
58
59
|
my $__dir;
|
|
@@ -83,6 +84,8 @@ package ProjectNameSecretsFeature;
|
|
|
83
84
|
|
|
84
85
|
our @ISA = ('ProjectNameBaseFeature');
|
|
85
86
|
|
|
87
|
+
my $REDIRECT_MAX = 5;
|
|
88
|
+
|
|
86
89
|
sub new {
|
|
87
90
|
my ($class) = @_;
|
|
88
91
|
my $self = ProjectNameBaseFeature::new($class);
|
|
@@ -96,6 +99,8 @@ sub new {
|
|
|
96
99
|
# The LIVE options map (root ctx options, which IS the client's options
|
|
97
100
|
# hash). Read for `auth` and the explicit apikey; never written.
|
|
98
101
|
$self->{liveopts} = {};
|
|
102
|
+
$self->{declared} = 0;
|
|
103
|
+
$self->{basic} = 0;
|
|
99
104
|
|
|
100
105
|
$self->{secretname} = 'apikey';
|
|
101
106
|
$self->{cache} = 1;
|
|
@@ -135,6 +140,12 @@ sub init {
|
|
|
135
140
|
$self->{active} = ProjectNameHelpers::is_true($self->{options}{active});
|
|
136
141
|
$self->{liveopts} = Voxgig::Struct::ismap($ctx->{options}) ? $ctx->{options} : {};
|
|
137
142
|
|
|
143
|
+
# What prepare_auth was generated from: the config declares auth only
|
|
144
|
+
# where it places a credential, and Basic only where it composes one.
|
|
145
|
+
my $declared = ProjectNameHelpers::gpath($ctx->{config}, 'options.auth');
|
|
146
|
+
$self->{declared} = Voxgig::Struct::ismap($declared) ? 1 : 0;
|
|
147
|
+
$self->{basic} = $self->{declared} && ProjectNameHelpers::is_true($declared->{basic}) ? 1 : 0;
|
|
148
|
+
|
|
138
149
|
return unless $self->{active};
|
|
139
150
|
|
|
140
151
|
# THE CHAIN IS BUILT FIRST AND THE TRANSPORT IS WRAPPED UNCONDITIONALLY.
|
|
@@ -203,6 +214,7 @@ sub _build {
|
|
|
203
214
|
'statuses' => \@statuses,
|
|
204
215
|
'retries' => (defined $xopts->{retries}
|
|
205
216
|
? ProjectNameHelpers::to_int($xopts->{retries}) : 1),
|
|
217
|
+
'redirect' => ('same-origin' eq _str($xopts->{redirect}, '') ? 'same-origin' : 'refuse'),
|
|
206
218
|
};
|
|
207
219
|
$self->{exchange}{retries} = 1 if 0 > $self->{exchange}{retries};
|
|
208
220
|
}
|
|
@@ -294,6 +306,14 @@ sub sekreto {
|
|
|
294
306
|
}
|
|
295
307
|
|
|
296
308
|
|
|
309
|
+
# prepare_auth's decision: auth the config declares, not suppressed by
|
|
310
|
+
# `auth => undef`.
|
|
311
|
+
sub _authon {
|
|
312
|
+
my ($self) = @_;
|
|
313
|
+
return $self->{declared} && defined ProjectNameHelpers::gp($self->{liveopts}, 'auth');
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
|
|
297
317
|
# The resolved credential (empty when none) - the state the transport
|
|
298
318
|
# injects. Tests and callers read it here rather than from the options
|
|
299
319
|
# map, which this feature never mutates.
|
|
@@ -363,7 +383,7 @@ sub resolve {
|
|
|
363
383
|
sub _resolve_once {
|
|
364
384
|
my ($self) = @_;
|
|
365
385
|
|
|
366
|
-
return (undef, 0) unless defined $self->{sek};
|
|
386
|
+
return (undef, 0) unless defined $self->{sek} && $self->_authon();
|
|
367
387
|
|
|
368
388
|
# sekreto's miss-vs-error rule, inherited: `try` returns undef on a MISS
|
|
369
389
|
# and dies on a provider ERROR.
|
|
@@ -400,16 +420,6 @@ sub _resolve_once {
|
|
|
400
420
|
# the same path expiry takes anyway.
|
|
401
421
|
return (undef, 1) if '' ne $self->{cred};
|
|
402
422
|
|
|
403
|
-
# `auth => undef` is the documented way to send NO credential, and a
|
|
404
|
-
# purchase is a credential-bearing call: the refresh token goes to the
|
|
405
|
-
# token endpoint in the request body. _with_refresh honours suppression
|
|
406
|
-
# for the RETRY, but it runs after this - by then the refresh token has
|
|
407
|
-
# already left the process, and no later check can call it back. The
|
|
408
|
-
# suppression has to be honoured here, before the first purchase, or it
|
|
409
|
-
# only ever half-held.
|
|
410
|
-
return (undef, 0)
|
|
411
|
-
if !defined ProjectNameHelpers::gp($self->{liveopts}, 'auth');
|
|
412
|
-
|
|
413
423
|
my ($token, $err) = $self->_buy();
|
|
414
424
|
return ($err, 0) if defined $err;
|
|
415
425
|
|
|
@@ -429,13 +439,9 @@ sub _resolve_once {
|
|
|
429
439
|
sub _with_refresh {
|
|
430
440
|
my ($self, $ctx, $url, $fetchdef, $inner) = @_;
|
|
431
441
|
|
|
432
|
-
#
|
|
433
|
-
#
|
|
434
|
-
|
|
435
|
-
# cannot be fixed by buying one - retrying would transmit exactly the
|
|
436
|
-
# credential the caller suppressed.
|
|
437
|
-
return $inner->($ctx, $url, $fetchdef)
|
|
438
|
-
if !defined ProjectNameHelpers::gp($self->{liveopts}, 'auth');
|
|
442
|
+
# A refusal of an unauthenticated request is not an expired token, and
|
|
443
|
+
# retrying would transmit the credential that auth off withholds.
|
|
444
|
+
return $inner->($ctx, $url, $fetchdef) if !$self->_authon();
|
|
439
445
|
|
|
440
446
|
my $max = $self->{exchange}{retries};
|
|
441
447
|
my $attempt = 0;
|
|
@@ -498,26 +504,41 @@ sub _reauth {
|
|
|
498
504
|
my $headers = $fetchdef->{headers};
|
|
499
505
|
return unless Voxgig::Struct::ismap($headers);
|
|
500
506
|
|
|
501
|
-
# Suppressed auth means NO header, the same answer prepare_auth gives
|
|
502
|
-
|
|
503
|
-
# _with_refresh does not retry at all when auth is suppressed - but this
|
|
504
|
-
# is the function that writes the credential, so it is where the rule
|
|
505
|
-
# has to hold.
|
|
506
|
-
if (!defined ProjectNameHelpers::gp($self->{liveopts}, 'auth')) {
|
|
507
|
+
# Suppressed auth means NO header, the same answer prepare_auth gives.
|
|
508
|
+
if (!$self->_authon()) {
|
|
507
509
|
delete $headers->{'authorization'};
|
|
508
510
|
return;
|
|
509
511
|
}
|
|
510
512
|
|
|
513
|
+
# The header prepare_auth wrote: auth.name, lower-cased.
|
|
514
|
+
my $name = ProjectNameHelpers::gpath($self->{liveopts}, 'auth.name');
|
|
515
|
+
$name = (defined $name && !ref $name && '' ne $name) ? lc($name) : 'authorization';
|
|
516
|
+
|
|
517
|
+
my $value = ($self->{basic}
|
|
518
|
+
&& ProjectNameHelpers::is_true(ProjectNameHelpers::gpath($self->{liveopts}, 'auth.basic')))
|
|
519
|
+
? $self->_pair($token) : "$token";
|
|
520
|
+
|
|
511
521
|
my $prefix = ProjectNameHelpers::gpath($self->{liveopts}, 'auth.prefix');
|
|
512
522
|
$prefix = '' unless defined $prefix && !ref $prefix;
|
|
513
523
|
|
|
514
524
|
# Empty prefix (raw apiKey credential) must not add a leading space.
|
|
515
|
-
$headers->{
|
|
525
|
+
$headers->{$name} = ('' eq $prefix) ? $value : "$prefix $value";
|
|
516
526
|
|
|
517
527
|
return;
|
|
518
528
|
}
|
|
519
529
|
|
|
520
530
|
|
|
531
|
+
# Under Basic the token is the key half, as apikey is: prepare_auth's pair.
|
|
532
|
+
sub _pair {
|
|
533
|
+
my ($self, $token) = @_;
|
|
534
|
+
my $secret = ProjectNameHelpers::gp($self->{liveopts}, 'secret');
|
|
535
|
+
$secret = '' unless defined $secret && !ref $secret;
|
|
536
|
+
my $pair = MIME::Base64::encode_base64("$token:$secret", '');
|
|
537
|
+
$self->_register($pair);
|
|
538
|
+
return $pair;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
|
|
521
542
|
# Buy an access token with the refresh token. Returns (token, undef) or
|
|
522
543
|
# (undef, message).
|
|
523
544
|
sub _buy {
|
|
@@ -597,15 +618,27 @@ sub _buy_once {
|
|
|
597
618
|
# must arrive as that literal value, not as malformed JSON.
|
|
598
619
|
my $body = Voxgig::Struct::jsonify({ $x->{request} => $self->{refresh} });
|
|
599
620
|
|
|
621
|
+
my $fetchdef = {
|
|
622
|
+
'method' => $x->{method},
|
|
623
|
+
'headers' => { 'content-type' => 'application/json' },
|
|
624
|
+
'body' => $body,
|
|
625
|
+
# Never followed by the client: a redirect would carry the refresh token on.
|
|
626
|
+
'redirect' => 'manual',
|
|
627
|
+
};
|
|
628
|
+
|
|
600
629
|
my ($res, $err);
|
|
601
630
|
my $ok = eval {
|
|
602
|
-
($res, $err) = $fetch->($url,
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
'
|
|
608
|
-
|
|
631
|
+
($res, $err) = $fetch->($url, $fetchdef);
|
|
632
|
+
|
|
633
|
+
# By option alone, and only while the exchange stays on its own origin.
|
|
634
|
+
my $at = $url;
|
|
635
|
+
for (my $hops = 0;
|
|
636
|
+
!defined $err && 'same-origin' eq $x->{redirect} && $hops < $REDIRECT_MAX; $hops++) {
|
|
637
|
+
my $next = _moved($url, $at, $res);
|
|
638
|
+
last unless defined $next;
|
|
639
|
+
$at = $next;
|
|
640
|
+
($res, $err) = $fetch->($at, $fetchdef);
|
|
641
|
+
}
|
|
609
642
|
1;
|
|
610
643
|
};
|
|
611
644
|
|
|
@@ -680,6 +713,28 @@ sub _provider {
|
|
|
680
713
|
}
|
|
681
714
|
|
|
682
715
|
|
|
716
|
+
# Where a method-keeping redirect points, when that is on the exchange's
|
|
717
|
+
# own origin. Any other redirect would drop the body or leave the origin.
|
|
718
|
+
sub _moved {
|
|
719
|
+
my ($url, $at, $res) = @_;
|
|
720
|
+
return undef unless Voxgig::Struct::ismap($res);
|
|
721
|
+
my $status = ProjectNameHelpers::to_int($res->{status});
|
|
722
|
+
return undef unless 307 == $status || 308 == $status;
|
|
723
|
+
my $location = Voxgig::Struct::ismap($res->{headers}) ? $res->{headers}{location} : undef;
|
|
724
|
+
return undef unless defined $location && !ref $location && '' ne $location;
|
|
725
|
+
my $next = ProjectNameUtilities::resolve_location($at, $location);
|
|
726
|
+
my $origin = sub {
|
|
727
|
+
my ($scheme, $host, $port) = lc($_[0] // '')
|
|
728
|
+
=~ m{\A([a-z][a-z0-9+.-]*)://(?:[^@/?#]*@)?(\[[^\]]*\]|[^:/?#]+)(?::(\d+))?};
|
|
729
|
+
return undef unless defined $host;
|
|
730
|
+
return join ':', $scheme, $host, $port // ('https' eq $scheme ? 443 : 80);
|
|
731
|
+
};
|
|
732
|
+
my $here = $origin->($url);
|
|
733
|
+
my $there = $origin->($next);
|
|
734
|
+
return defined $here && defined $there && $here eq $there ? $next : undef;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
|
|
683
738
|
sub _str {
|
|
684
739
|
my ($v, $dflt) = @_;
|
|
685
740
|
return (defined $v && !ref $v && '' ne $v) ? "$v" : $dflt;
|
|
@@ -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
|
|
147
|
-
#
|
|
148
|
-
#
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
|
|
155
|
-
|
|
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
|
|
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',
|
|
1051
|
-
|
|
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
|
-
|
|
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();
|
|
@@ -70,7 +70,7 @@ $REGISTRY{make_spec} = sub {
|
|
|
70
70
|
if (defined $pkind && 'graphql' eq $pkind) {
|
|
71
71
|
# GraphQL addresses one endpoint: no path parts, no query string, and
|
|
72
72
|
# the body carries the operation. prepare_body is skipped deliberately
|
|
73
|
-
# — it only
|
|
73
|
+
# — it builds only a REST body, whereas every GraphQL op
|
|
74
74
|
# posts one, including load/list/remove.
|
|
75
75
|
$ctx->{spec}{body} = $utility->{graphql_body}->($ctx);
|
|
76
76
|
$ctx->{spec}{path} = '';
|
|
@@ -8,18 +8,52 @@ use Cwd ();
|
|
|
8
8
|
|
|
9
9
|
my $__dir;
|
|
10
10
|
BEGIN { $__dir = File::Basename::dirname(Cwd::abs_path(__FILE__)) }
|
|
11
|
+
require(Cwd::abs_path("$__dir/../lib/Voxgig/Struct.pm"));
|
|
12
|
+
require(Cwd::abs_path("$__dir/../core/helpers.pm"));
|
|
11
13
|
require(Cwd::abs_path("$__dir/media.pm"));
|
|
12
14
|
|
|
13
15
|
package ProjectNameUtilities;
|
|
14
16
|
|
|
15
17
|
our %REGISTRY;
|
|
16
18
|
|
|
19
|
+
# A list, load or remove routed to a method with a body sends the match
|
|
20
|
+
# fields that body declares, never a path parameter, and never a header,
|
|
21
|
+
# cookie or query argument the body does not also declare as a field.
|
|
22
|
+
my $match_body = sub {
|
|
23
|
+
my ($ctx, $fields) = @_;
|
|
24
|
+
my $point = $ctx->{point};
|
|
25
|
+
my $params = ProjectNameHelpers::gp($point, 'params');
|
|
26
|
+
my %routed = map { defined $_ && !ref $_ ? ($_ => 1) : () }
|
|
27
|
+
Voxgig::Struct::islist($params) ? @$params : ();
|
|
28
|
+
for my $kind (qw(params header cookie query)) {
|
|
29
|
+
my $defs = ProjectNameHelpers::gpath($point, "args.$kind");
|
|
30
|
+
next unless Voxgig::Struct::islist($defs);
|
|
31
|
+
for my $ad (@$defs) {
|
|
32
|
+
next unless 'params' eq $kind || !ProjectNameHelpers::is_true(ProjectNameHelpers::gp($ad, 'field'));
|
|
33
|
+
my $name = ProjectNameHelpers::gp($ad, 'name');
|
|
34
|
+
$routed{$name} = 1 if defined $name && !ref $name;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
my $body = {};
|
|
38
|
+
for my $name (@$fields) {
|
|
39
|
+
next unless defined $name && !ref $name;
|
|
40
|
+
next if '$action' eq $name || $routed{$name};
|
|
41
|
+
my $val = ProjectNameHelpers::gp($ctx->{reqmatch} || {}, $name);
|
|
42
|
+
$body->{$name} = $val if defined $val && !Voxgig::Struct::is_jnull($val);
|
|
43
|
+
}
|
|
44
|
+
return $body;
|
|
45
|
+
};
|
|
46
|
+
|
|
17
47
|
$REGISTRY{prepare_body} = sub {
|
|
18
48
|
my ($ctx) = @_;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
49
|
+
if ('data' eq $ctx->{op}{input}) {
|
|
50
|
+
return ProjectNameUtilities::raw_body($ctx->{reqdata})
|
|
51
|
+
if ProjectNameUtilities::is_raw_request($ctx->{point});
|
|
52
|
+
return $ctx->{utility}{transform_request}->($ctx);
|
|
53
|
+
}
|
|
54
|
+
my $fields = $ctx->{point} ? ProjectNameHelpers::gp($ctx->{point}, 'fields') : undef;
|
|
55
|
+
return undef unless Voxgig::Struct::islist($fields) && @$fields;
|
|
56
|
+
return $match_body->($ctx, $fields);
|
|
23
57
|
};
|
|
24
58
|
|
|
25
59
|
1;
|
|
@@ -34,7 +34,9 @@ $REGISTRY{prepare_query} = sub {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
# A header or cookie parameter travels in the headers, which prepare_headers
|
|
37
|
-
# fills,
|
|
37
|
+
# fills, and a field the point's body declares travels in the body, which
|
|
38
|
+
# prepare_body fills, unless a query parameter shares its name: then both
|
|
39
|
+
# are sent.
|
|
38
40
|
my $ql = ProjectNameHelpers::gpath($point, 'args.query');
|
|
39
41
|
my %declared = Voxgig::Struct::islist($ql)
|
|
40
42
|
? map { my $n = ProjectNameHelpers::gp($_, 'name'); defined $n && !ref $n ? ($n => 1) : () } @$ql
|
|
@@ -47,6 +49,10 @@ $REGISTRY{prepare_query} = sub {
|
|
|
47
49
|
push @$params, $name if defined $name && !ref $name && !$declared{$name};
|
|
48
50
|
}
|
|
49
51
|
}
|
|
52
|
+
my $fields = ProjectNameHelpers::gp($point, 'fields');
|
|
53
|
+
if (Voxgig::Struct::islist($fields)) {
|
|
54
|
+
push @$params, grep { defined $_ && !ref $_ && !$declared{$_} } @$fields;
|
|
55
|
+
}
|
|
50
56
|
}
|
|
51
57
|
# A query parameter travels under the name the definition gives it, its
|
|
52
58
|
# orig, which the model may have renamed for the caller.
|
|
@@ -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;
|