@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
|
@@ -88,6 +88,8 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
88
88
|
@specfields Map.keys(Map.from_struct(%Sekreto.ProviderSpec{}))
|
|
89
89
|
@authfields Map.keys(Map.from_struct(%Sekreto.AuthSpec{}))
|
|
90
90
|
|
|
91
|
+
@redirect_max 5
|
|
92
|
+
|
|
91
93
|
def new do
|
|
92
94
|
f = F.base("secrets", "0.1.0")
|
|
93
95
|
F.install(f, "init", fn ctx, opts -> init(f, ctx, opts) end)
|
|
@@ -106,6 +108,12 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
106
108
|
# feature never writes to it.
|
|
107
109
|
S.setprop(f, "_liveopts", S.getprop(client, "options"))
|
|
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
|
+
declared = S.getpath(S.getprop(ctx, "config"), "options.auth")
|
|
114
|
+
S.setprop(f, "_declared", S.ismap(declared))
|
|
115
|
+
S.setprop(f, "_basic", S.ismap(declared) and S.getprop(declared, "basic") == true)
|
|
116
|
+
|
|
109
117
|
name = H.str_or(S.getprop(options, "name"), "apikey")
|
|
110
118
|
S.setprop(f, "_secretname", name)
|
|
111
119
|
S.setprop(f, "_cache", S.getprop(options, "cache") != false)
|
|
@@ -125,7 +133,9 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
125
133
|
request: H.str_or(S.getprop(xopts, "request"), "refresh_token"),
|
|
126
134
|
response: H.str_or(S.getprop(xopts, "response"), "access_token"),
|
|
127
135
|
statuses: statuses(S.getprop(xopts, "statuses")),
|
|
128
|
-
retries: intor(S.getprop(xopts, "retries"), 1)
|
|
136
|
+
retries: intor(S.getprop(xopts, "retries"), 1),
|
|
137
|
+
redirect:
|
|
138
|
+
if(S.getprop(xopts, "redirect") == "same-origin", do: "same-origin", else: "refuse")
|
|
129
139
|
}
|
|
130
140
|
end
|
|
131
141
|
|
|
@@ -473,12 +483,9 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
473
483
|
keep(f)
|
|
474
484
|
:ok
|
|
475
485
|
else
|
|
476
|
-
#
|
|
477
|
-
#
|
|
478
|
-
#
|
|
479
|
-
# other target guards the purchase itself, because their transports
|
|
480
|
-
# resolve first. One rule, one place - and here that place is the
|
|
481
|
-
# transport.
|
|
486
|
+
# No suppression check here: transport/5 returns before resolve/1
|
|
487
|
+
# when auth is off, so a suppressed request never reaches this
|
|
488
|
+
# purchase at all.
|
|
482
489
|
case buy(f, x) do
|
|
483
490
|
{:ok, _token} ->
|
|
484
491
|
keep(f)
|
|
@@ -525,13 +532,9 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
525
532
|
x = S.getprop(f, "_exchange")
|
|
526
533
|
|
|
527
534
|
if suppressed?(f) do
|
|
528
|
-
#
|
|
529
|
-
#
|
|
530
|
-
#
|
|
531
|
-
# fail a request that was never going to carry a credential, and
|
|
532
|
-
# purchasing an access token nobody will send is a wasted exchange.
|
|
533
|
-
# The header is removed again defensively, because this is the
|
|
534
|
-
# function that would otherwise write it.
|
|
535
|
+
# Auth off sends NO credential, as prepare_auth places none. There is
|
|
536
|
+
# then nothing to resolve and nothing to buy: asking the chain could
|
|
537
|
+
# only fail a request that was never going to carry a credential.
|
|
535
538
|
reauth(f, fetchdef, "")
|
|
536
539
|
inner.(ctx, url, fetchdef)
|
|
537
540
|
else
|
|
@@ -557,7 +560,10 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
557
560
|
end
|
|
558
561
|
end
|
|
559
562
|
|
|
560
|
-
|
|
563
|
+
# prepare_auth's decision: suppressed where the config declares no auth,
|
|
564
|
+
# and where `auth` is nil.
|
|
565
|
+
defp suppressed?(f),
|
|
566
|
+
do: S.getprop(f, "_declared") != true or S.getprop(S.getprop(f, "_liveopts"), "auth") == nil
|
|
561
567
|
|
|
562
568
|
# Buy a token and try the request again when the API says the current one
|
|
563
569
|
# is spent.
|
|
@@ -630,15 +636,28 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
630
636
|
if suppressed?(f) or token == "" do
|
|
631
637
|
S.delprop(headers, name)
|
|
632
638
|
else
|
|
639
|
+
value =
|
|
640
|
+
if S.getprop(f, "_basic") == true and
|
|
641
|
+
S.getpath(S.getprop(f, "_liveopts"), "auth.basic") == true,
|
|
642
|
+
do: pair(f, token),
|
|
643
|
+
else: token
|
|
644
|
+
|
|
633
645
|
prefix = S.getpath(S.getprop(f, "_liveopts"), "auth.prefix")
|
|
634
646
|
prefix = if is_binary(prefix), do: prefix, else: ""
|
|
635
|
-
S.setprop(headers, name, if(prefix != "", do: prefix <> " " <>
|
|
647
|
+
S.setprop(headers, name, if(prefix != "", do: prefix <> " " <> value, else: value))
|
|
636
648
|
end
|
|
637
649
|
end
|
|
638
650
|
|
|
639
651
|
nil
|
|
640
652
|
end
|
|
641
653
|
|
|
654
|
+
# Under Basic the token is the key half, as apikey is: prepare_auth's pair.
|
|
655
|
+
defp pair(f, token) do
|
|
656
|
+
joined = Base.encode64(token <> ":" <> tostr(S.getprop(S.getprop(f, "_liveopts"), "secret")))
|
|
657
|
+
register(f, joined)
|
|
658
|
+
joined
|
|
659
|
+
end
|
|
660
|
+
|
|
642
661
|
# ---- the exchange --------------------------------------------------------
|
|
643
662
|
|
|
644
663
|
# Buy an access token with the refresh token.
|
|
@@ -694,7 +713,7 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
694
713
|
"method", x.method,
|
|
695
714
|
"headers", S.jm(["content-type", "application/json"]),
|
|
696
715
|
"body", S.jsonify(S.jm([x.request, refresh]), S.jm(["indent", 0])),
|
|
697
|
-
# Never followed: a redirect would carry the refresh token on.
|
|
716
|
+
# Never followed by the client: a redirect would carry the refresh token on.
|
|
698
717
|
"redirect", "manual"
|
|
699
718
|
])
|
|
700
719
|
|
|
@@ -705,14 +724,21 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
705
724
|
# recurse on the first expiry - and would route the exchange through
|
|
706
725
|
# the test mock, which knows nothing about it. `options.system.fetch`
|
|
707
726
|
# is preferred when set, so a test can intercept the purchase.
|
|
708
|
-
|
|
727
|
+
ask = fn at ->
|
|
709
728
|
try do
|
|
710
729
|
if S.isfunc(sysfetch),
|
|
711
|
-
do: sysfetch.(
|
|
712
|
-
else: raw_exchange_fetch(
|
|
730
|
+
do: sysfetch.(at, fetchdef),
|
|
731
|
+
else: raw_exchange_fetch(at, fetchdef)
|
|
713
732
|
rescue
|
|
714
733
|
e -> {nil, fail(e)}
|
|
715
734
|
end
|
|
735
|
+
end
|
|
736
|
+
|
|
737
|
+
# By option alone, and only while the exchange stays on its own origin.
|
|
738
|
+
{res, ferr} =
|
|
739
|
+
if x.redirect == "same-origin",
|
|
740
|
+
do: follow(url, url, ask.(url), ask, @redirect_max),
|
|
741
|
+
else: ask.(url)
|
|
716
742
|
|
|
717
743
|
cond do
|
|
718
744
|
ferr != nil ->
|
|
@@ -743,6 +769,33 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
743
769
|
end
|
|
744
770
|
end
|
|
745
771
|
|
|
772
|
+
defp follow(url, at, {res, nil} = got, ask, hops) when hops > 0 do
|
|
773
|
+
case moved(url, at, res) do
|
|
774
|
+
nil -> got
|
|
775
|
+
next -> follow(url, next, ask.(next), ask, hops - 1)
|
|
776
|
+
end
|
|
777
|
+
end
|
|
778
|
+
|
|
779
|
+
defp follow(_url, _at, got, _ask, _hops), do: got
|
|
780
|
+
|
|
781
|
+
# Where a redirect keeping method and body (307 or 308) points, on the
|
|
782
|
+
# exchange's own origin. Any other would drop the body or leave the origin.
|
|
783
|
+
defp moved(url, at, res) do
|
|
784
|
+
location = if S.ismap(res), do: S.getprop(S.getprop(res, "headers"), "location")
|
|
785
|
+
|
|
786
|
+
if S.ismap(res) and H.to_int(S.getprop(res, "status")) in [307, 308] and
|
|
787
|
+
is_binary(location) and location != "" do
|
|
788
|
+
try do
|
|
789
|
+
next = URI.merge(URI.parse(at), location)
|
|
790
|
+
if origin(next) == origin(URI.parse(url)), do: URI.to_string(next)
|
|
791
|
+
rescue
|
|
792
|
+
_ -> nil
|
|
793
|
+
end
|
|
794
|
+
end
|
|
795
|
+
end
|
|
796
|
+
|
|
797
|
+
defp origin(uri), do: {uri.scheme, uri.host, uri.port}
|
|
798
|
+
|
|
746
799
|
defp bodyfield(res, name) do
|
|
747
800
|
jf = S.getprop(res, "json")
|
|
748
801
|
|
|
@@ -786,7 +839,7 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
786
839
|
end
|
|
787
840
|
|
|
788
841
|
case :httpc.request(method, request, [autoredirect: false], body_format: :binary) do
|
|
789
|
-
{:ok, {{_v, status, _reason},
|
|
842
|
+
{:ok, {{_v, status, _reason}, rh, resbody}} ->
|
|
790
843
|
text = if is_binary(resbody), do: resbody, else: to_string(resbody)
|
|
791
844
|
|
|
792
845
|
parsed =
|
|
@@ -796,7 +849,10 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
796
849
|
_ -> nil
|
|
797
850
|
end
|
|
798
851
|
|
|
799
|
-
|
|
852
|
+
headers =
|
|
853
|
+
S.jm(Enum.flat_map(rh, fn {k, v} -> [String.downcase(to_string(k)), to_string(v)] end))
|
|
854
|
+
|
|
855
|
+
{S.jm(["status", status, "headers", headers, "json", fn -> parsed end, "body", text]), nil}
|
|
800
856
|
|
|
801
857
|
{:error, reason} ->
|
|
802
858
|
{nil, err("secrets_exchange_failed", "secrets: token exchange transport: " <> inspect(reason))}
|
|
@@ -1238,7 +1238,7 @@ defmodule ProjectName.Utility do
|
|
|
1238
1238
|
if S.getprop(point, "kind") == "graphql" do
|
|
1239
1239
|
# GraphQL addresses one endpoint: no path parts, no query string,
|
|
1240
1240
|
# and the body carries the operation. prepare_body is skipped
|
|
1241
|
-
# deliberately — it only
|
|
1241
|
+
# deliberately — it builds only a REST body, whereas
|
|
1242
1242
|
# every GraphQL op posts one, including load/list/remove.
|
|
1243
1243
|
S.setprop(spec, "body", graphql_body(ctx))
|
|
1244
1244
|
S.setprop(spec, "path", "")
|
|
@@ -2010,12 +2010,54 @@ defmodule ProjectName.Utility do
|
|
|
2010
2010
|
op = S.getprop(ctx, "op")
|
|
2011
2011
|
|
|
2012
2012
|
cond do
|
|
2013
|
-
S.getprop(op, "input") != "data" ->
|
|
2013
|
+
S.getprop(op, "input") != "data" -> match_body(ctx)
|
|
2014
2014
|
raw_request?(S.getprop(ctx, "point")) -> raw_body(S.getprop(ctx, "reqdata"))
|
|
2015
2015
|
true -> transform_request(ctx)
|
|
2016
2016
|
end
|
|
2017
2017
|
end
|
|
2018
2018
|
|
|
2019
|
+
# A list, load or remove routed to a method with a body sends the match
|
|
2020
|
+
# fields that body declares, never a path parameter, and never a header,
|
|
2021
|
+
# cookie or query argument the body does not also declare as a field.
|
|
2022
|
+
defp match_body(ctx) do
|
|
2023
|
+
point = S.getprop(ctx, "point")
|
|
2024
|
+
|
|
2025
|
+
case point_names(point, "fields") do
|
|
2026
|
+
[] ->
|
|
2027
|
+
nil
|
|
2028
|
+
|
|
2029
|
+
fields ->
|
|
2030
|
+
reqmatch = S.getprop(ctx, "reqmatch")
|
|
2031
|
+
|
|
2032
|
+
routed =
|
|
2033
|
+
point_names(point, "params") ++
|
|
2034
|
+
arg_names(point, "args.params") ++
|
|
2035
|
+
Enum.flat_map(["header", "cookie", "query"], &unkept_arg_names(point, &1))
|
|
2036
|
+
|
|
2037
|
+
body = S.jm([])
|
|
2038
|
+
|
|
2039
|
+
Enum.each(fields, fn name ->
|
|
2040
|
+
val = S.getprop(reqmatch, name)
|
|
2041
|
+
if val != nil and name != "$action" and name not in routed, do: S.setprop(body, name, val)
|
|
2042
|
+
end)
|
|
2043
|
+
|
|
2044
|
+
body
|
|
2045
|
+
end
|
|
2046
|
+
end
|
|
2047
|
+
|
|
2048
|
+
defp unkept_arg_names(point, kind) do
|
|
2049
|
+
defs = S.getpath(point, "args." <> kind)
|
|
2050
|
+
|
|
2051
|
+
if S.islist(defs) and S.size(defs) > 0 do
|
|
2052
|
+
0..(S.size(defs) - 1)
|
|
2053
|
+
|> Enum.map(&S.getelem(defs, &1))
|
|
2054
|
+
|> Enum.reject(&(true == S.getprop(&1, "field")))
|
|
2055
|
+
|> Enum.map(&S.getprop(&1, "name"))
|
|
2056
|
+
else
|
|
2057
|
+
[]
|
|
2058
|
+
end
|
|
2059
|
+
end
|
|
2060
|
+
|
|
2019
2061
|
def prepare_params_impl(ctx) do
|
|
2020
2062
|
point = S.getprop(ctx, "point")
|
|
2021
2063
|
|
|
@@ -2065,6 +2107,17 @@ defmodule ProjectName.Utility do
|
|
|
2065
2107
|
S.join(parts, "/", true)
|
|
2066
2108
|
end
|
|
2067
2109
|
|
|
2110
|
+
# The names in one of a point's lists of names, as its params and fields are.
|
|
2111
|
+
defp point_names(nil, _key), do: []
|
|
2112
|
+
|
|
2113
|
+
defp point_names(point, key) do
|
|
2114
|
+
names = S.getprop(point, key)
|
|
2115
|
+
|
|
2116
|
+
if S.islist(names),
|
|
2117
|
+
do: H.entries(names) |> Enum.map(&elem(&1, 1)) |> Enum.filter(&is_binary/1),
|
|
2118
|
+
else: []
|
|
2119
|
+
end
|
|
2120
|
+
|
|
2068
2121
|
# The names in one of a point's lists of argument definitions.
|
|
2069
2122
|
defp arg_names(nil, _path), do: []
|
|
2070
2123
|
|
|
@@ -2100,13 +2153,16 @@ defmodule ProjectName.Utility do
|
|
|
2100
2153
|
# A path parameter travels in the path. The generated config lists them
|
|
2101
2154
|
# as args.params, which prepare_params reads; params is the older list.
|
|
2102
2155
|
# A header or cookie parameter travels in the headers, which prepare_headers
|
|
2103
|
-
# fills,
|
|
2156
|
+
# fills, and a field the point's body declares travels in the body, which
|
|
2157
|
+
# prepare_body fills, unless a query parameter shares its name: then both
|
|
2158
|
+
# are sent.
|
|
2104
2159
|
declared = arg_names(point, "args.query")
|
|
2105
2160
|
|
|
2161
|
+
elsewhere =
|
|
2162
|
+
arg_names(point, "args.header") ++ arg_names(point, "args.cookie") ++ point_names(point, "fields")
|
|
2163
|
+
|
|
2106
2164
|
param_strs =
|
|
2107
|
-
param_strs ++
|
|
2108
|
-
arg_names(point, "args.params") ++
|
|
2109
|
-
Enum.reject(arg_names(point, "args.header") ++ arg_names(point, "args.cookie"), &(&1 in declared))
|
|
2165
|
+
param_strs ++ arg_names(point, "args.params") ++ Enum.reject(elsewhere, &(&1 in declared))
|
|
2110
2166
|
|
|
2111
2167
|
# A query parameter travels under the name the definition gives it, its
|
|
2112
2168
|
# orig, which the model may have renamed for the caller.
|
|
@@ -37,14 +37,12 @@ defmodule ProjectName.SecretsTest do
|
|
|
37
37
|
alias ProjectName.Feature.Secrets
|
|
38
38
|
|
|
39
39
|
# Read from the config, never from a probe: an SDK whose config declares no
|
|
40
|
-
# auth places no credential, and
|
|
41
|
-
# Decided as this file compiles, which `mix test` does after
|
|
42
|
-
# ExUnit can skip
|
|
43
|
-
if
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
@moduletag skip: reason
|
|
47
|
-
end
|
|
40
|
+
# auth places no credential, and the tests that assert on the one it
|
|
41
|
+
# places skip. Decided as this file compiles, which `mix test` does after
|
|
42
|
+
# the SDK, so ExUnit can skip them and say so.
|
|
43
|
+
@noauth if(nil == S.getprop(S.getprop(ProjectName.Config.shared_config(), "options"), "auth"),
|
|
44
|
+
do: "this SDK's config declares no auth, so it places no credential")
|
|
45
|
+
if @noauth, do: IO.puts("SKIP secrets: " <> @noauth)
|
|
48
46
|
|
|
49
47
|
@envprefix "PROJECTENV_TEST_SECRETS_"
|
|
50
48
|
@base "http://raw.test/api"
|
|
@@ -86,10 +84,15 @@ defmodule ProjectName.SecretsTest do
|
|
|
86
84
|
|
|
87
85
|
[{:calls, cs}] = :ets.lookup(t, :calls)
|
|
88
86
|
|
|
87
|
+
values =
|
|
88
|
+
if S.ismap(headers),
|
|
89
|
+
do: S.keysof(headers) |> Enum.map_join("\n", &to_string(S.getprop(headers, &1))),
|
|
90
|
+
else: ""
|
|
91
|
+
|
|
89
92
|
:ets.insert(t,
|
|
90
93
|
{:calls,
|
|
91
94
|
cs ++
|
|
92
|
-
[%{url: url, auth: auth, body: S.getprop(fetchdef, "body"),
|
|
95
|
+
[%{url: url, auth: auth, values: values, body: S.getprop(fetchdef, "body"),
|
|
93
96
|
redirect: S.getprop(fetchdef, "redirect")}]})
|
|
94
97
|
|
|
95
98
|
if String.ends_with?(url, "/" <> path) do
|
|
@@ -185,21 +188,24 @@ defmodule ProjectName.SecretsTest do
|
|
|
185
188
|
|
|
186
189
|
# ---- assertions ----------------------------------------------------------
|
|
187
190
|
|
|
188
|
-
# The
|
|
189
|
-
#
|
|
190
|
-
#
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
191
|
+
# The header value prepare_auth writes for a token, in the form the DEFAULT
|
|
192
|
+
# client's auth gives: under Basic the token is the key half of the encoded
|
|
193
|
+
# pair, and a prefix is space-joined.
|
|
194
|
+
defp credential(token, secret) do
|
|
195
|
+
auth = S.getprop(ProjectName.options_map(ProjectName.test()), "auth")
|
|
196
|
+
prefix = S.getprop(auth, "prefix")
|
|
197
|
+
value = if S.getprop(auth, "basic") == true, do: Base.encode64(token <> ":" <> secret), else: token
|
|
198
|
+
if is_binary(prefix) and prefix != "", do: prefix <> " " <> value, else: value
|
|
199
|
+
end
|
|
196
200
|
|
|
197
|
-
|
|
201
|
+
defp credential_is(header, token, secret \\ "") do
|
|
202
|
+
assert header == credential(token, secret),
|
|
198
203
|
"expected the " <> credheader() <> " header to carry " <> token <> ", got: " <>
|
|
199
204
|
inspect(header)
|
|
200
205
|
end
|
|
201
206
|
|
|
202
|
-
defp auth_is(call, token
|
|
207
|
+
defp auth_is(call, token, secret \\ ""),
|
|
208
|
+
do: credential_is(if(call == nil, do: nil, else: call.auth), token, secret)
|
|
203
209
|
|
|
204
210
|
# Does this options value CARRY the token - bare, or under the spec's
|
|
205
211
|
# prefix? The negative half of credential_is, for the assertions that pin
|
|
@@ -233,6 +239,15 @@ defmodule ProjectName.SecretsTest do
|
|
|
233
239
|
end
|
|
234
240
|
|
|
235
241
|
describe "secrets" do
|
|
242
|
+
if @noauth, do: @describetag(skip: @noauth)
|
|
243
|
+
|
|
244
|
+
test "active: a resolved key is placed as prepare_auth places it, the secret kept" do
|
|
245
|
+
System.put_env(@envprefix <> "APIKEY", "ENVKEY05")
|
|
246
|
+
r = recorder()
|
|
247
|
+
ProjectName.direct(sdk(r, [{"secret", "PASS01"}, {"feature", envchain()}]), S.jm(["path", "/thing"]))
|
|
248
|
+
auth_is(hd(api(r)), "ENVKEY05", "PASS01")
|
|
249
|
+
end
|
|
250
|
+
|
|
236
251
|
test "inactive: the apikey option behaves exactly as before" do
|
|
237
252
|
r = recorder()
|
|
238
253
|
client = sdk(r, [{"apikey", "OPTKEY01"}])
|
|
@@ -824,6 +839,8 @@ defmodule ProjectName.SecretsTest do
|
|
|
824
839
|
# A LIVE client throughout. Test mode is deliberately excluded here - it
|
|
825
840
|
# buys nothing, which is the subject of its own test at the end.
|
|
826
841
|
describe "secrets exchange" do
|
|
842
|
+
if @noauth, do: @describetag(skip: @noauth)
|
|
843
|
+
|
|
827
844
|
@refresh "REFRESH01"
|
|
828
845
|
|
|
829
846
|
defp exchange_sdk(r, xextra \\ [], extra \\ []) do
|
|
@@ -927,6 +944,17 @@ defmodule ProjectName.SecretsTest do
|
|
|
927
944
|
assert S.getprop(res, "ok") == true, "the caller sees the successful retry"
|
|
928
945
|
end
|
|
929
946
|
|
|
947
|
+
test "a token bought on a refusal is placed as prepare_auth places it" do
|
|
948
|
+
System.put_env(@envprefix <> "REFRESH_TOKEN", @refresh)
|
|
949
|
+
|
|
950
|
+
r = recorder(%{api: [401, 200]})
|
|
951
|
+
ProjectName.direct(exchange_sdk(r, [], [{"secret", "PASS01"}]), S.jm(["path", "/thing"]))
|
|
952
|
+
|
|
953
|
+
assert length(api(r)) == 2, "expected the request to be retried"
|
|
954
|
+
auth_is(Enum.at(api(r), 0), "ACCESS01", "PASS01")
|
|
955
|
+
auth_is(Enum.at(api(r), 1), "ACCESS02", "PASS01")
|
|
956
|
+
end
|
|
957
|
+
|
|
930
958
|
test "the retry happens once, not in a loop" do
|
|
931
959
|
System.put_env(@envprefix <> "REFRESH_TOKEN", @refresh)
|
|
932
960
|
|
|
@@ -1084,6 +1112,99 @@ defmodule ProjectName.SecretsTest do
|
|
|
1084
1112
|
end
|
|
1085
1113
|
end
|
|
1086
1114
|
|
|
1115
|
+
test "with the option, a redirect on the exchange's own origin is followed, and no other" do
|
|
1116
|
+
other =
|
|
1117
|
+
listener(:other, fn _path -> {200, nil, ~s({"access_token":"ELSEWHERE01"})} end)
|
|
1118
|
+
|
|
1119
|
+
{:ok, answer} = Agent.start_link(fn -> {307, ""} end)
|
|
1120
|
+
|
|
1121
|
+
origin =
|
|
1122
|
+
listener(:origin, fn
|
|
1123
|
+
"/api/auth/token" -> Tuple.append(Agent.get(answer, & &1), "")
|
|
1124
|
+
_ -> {200, nil, ~s({"access_token":"MOVED01"})}
|
|
1125
|
+
end)
|
|
1126
|
+
|
|
1127
|
+
at = "http://127.0.0.1:#{elem(origin, 1)}"
|
|
1128
|
+
base = at <> "/api"
|
|
1129
|
+
away = "http://127.0.0.1:#{elem(other, 1)}/api/auth/token"
|
|
1130
|
+
sent = ~s({"refresh_token":"#{@refresh}"})
|
|
1131
|
+
|
|
1132
|
+
# Each case: the status, the Location, and the target the exchange is
|
|
1133
|
+
# asked again at, or nil where the redirect is refused.
|
|
1134
|
+
cases = [
|
|
1135
|
+
{307, "/api/moved/token", "/api/moved/token"},
|
|
1136
|
+
{308, at <> "/api/moved/token", "/api/moved/token"},
|
|
1137
|
+
# A Location of a query alone keeps the path that answered.
|
|
1138
|
+
{307, "?step=2", "/api/auth/token?step=2"},
|
|
1139
|
+
{307, away, nil},
|
|
1140
|
+
{302, "/api/moved/token", nil},
|
|
1141
|
+
{307, "/api/auth/token", nil}
|
|
1142
|
+
]
|
|
1143
|
+
|
|
1144
|
+
try do
|
|
1145
|
+
results =
|
|
1146
|
+
Enum.map(cases, fn {status, location, path} ->
|
|
1147
|
+
Agent.update(answer, fn _ -> {status, location} end)
|
|
1148
|
+
|
|
1149
|
+
client =
|
|
1150
|
+
ProjectName.new(S.jm([
|
|
1151
|
+
"base", base,
|
|
1152
|
+
"allow", S.jm(["op", "direct,graphql,list,load"]),
|
|
1153
|
+
"feature",
|
|
1154
|
+
secrets([
|
|
1155
|
+
{"name", "refresh_token"},
|
|
1156
|
+
{"exchange",
|
|
1157
|
+
S.jm(["active", true, "refresh", @refresh, "redirect", "same-origin"])}
|
|
1158
|
+
])
|
|
1159
|
+
]))
|
|
1160
|
+
|
|
1161
|
+
res = ProjectName.direct(client, S.jm(["path", "/thing"]))
|
|
1162
|
+
seen = received([])
|
|
1163
|
+
mine = for {:origin, m, p, b} <- seen, do: {m, p, b}
|
|
1164
|
+
exchanged = Enum.count(mine, &(elem(&1, 1) == "/api/auth/token"))
|
|
1165
|
+
followed = Enum.reject(mine, &(elem(&1, 1) == "/api/auth/token"))
|
|
1166
|
+
elsewhere = Enum.count(seen, &(elem(&1, 0) == :other))
|
|
1167
|
+
message = err_message(res)
|
|
1168
|
+
want = "token exchange failed: #{status} from #{base}/auth/token"
|
|
1169
|
+
|
|
1170
|
+
verdict =
|
|
1171
|
+
cond do
|
|
1172
|
+
path != nil and S.getprop(res, "ok") == true and length(followed) == 2 and
|
|
1173
|
+
hd(followed) == {"POST", path, sent} ->
|
|
1174
|
+
:followed
|
|
1175
|
+
|
|
1176
|
+
path == nil and followed == [] and String.contains?(message, want) ->
|
|
1177
|
+
:refused
|
|
1178
|
+
|
|
1179
|
+
true ->
|
|
1180
|
+
{:wrong, status, location, message, followed}
|
|
1181
|
+
end
|
|
1182
|
+
|
|
1183
|
+
{verdict, location, exchanged, elsewhere}
|
|
1184
|
+
end)
|
|
1185
|
+
|
|
1186
|
+
followed = Enum.count(results, &(elem(&1, 0) == :followed))
|
|
1187
|
+
refused = Enum.count(results, &(elem(&1, 0) == :refused))
|
|
1188
|
+
elsewhere = results |> Enum.map(&elem(&1, 3)) |> Enum.sum()
|
|
1189
|
+
wrong = for {{:wrong, _, _, _, _} = w, _, _, _} <- results, do: w
|
|
1190
|
+
{_, _, looped, _} = List.keyfind(results, "/api/auth/token", 1)
|
|
1191
|
+
|
|
1192
|
+
IO.puts("\nsecrets: exchange opt-in followed #{followed} redirect(s) " <>
|
|
1193
|
+
"on its own origin, refused #{refused}, #{elsewhere} elsewhere")
|
|
1194
|
+
|
|
1195
|
+
assert elsewhere == 0, "the exchange reached another origin"
|
|
1196
|
+
assert wrong == []
|
|
1197
|
+
assert followed == 3
|
|
1198
|
+
assert refused == 3
|
|
1199
|
+
# A redirect to itself stops at the hop limit.
|
|
1200
|
+
assert looped == 6
|
|
1201
|
+
after
|
|
1202
|
+
:gen_tcp.close(elem(origin, 0))
|
|
1203
|
+
:gen_tcp.close(elem(other, 0))
|
|
1204
|
+
Agent.stop(answer)
|
|
1205
|
+
end
|
|
1206
|
+
end
|
|
1207
|
+
|
|
1087
1208
|
test "auth nil suppresses the credential and buys nothing" do
|
|
1088
1209
|
System.put_env(@envprefix <> "REFRESH_TOKEN", @refresh)
|
|
1089
1210
|
|
|
@@ -1140,6 +1261,44 @@ defmodule ProjectName.SecretsTest do
|
|
|
1140
1261
|
|
|
1141
1262
|
# A listener on 127.0.0.1 that answers by `route` and sends the test
|
|
1142
1263
|
# {:req, tag, method, path, body} for each request. {socket, port}.
|
|
1264
|
+
# Off by the config where it declares no auth, else by `auth: nil`: either
|
|
1265
|
+
# way the feature places no credential, buys no token and retries nothing.
|
|
1266
|
+
describe "secrets with auth off" do
|
|
1267
|
+
test "places no credential and buys no token" do
|
|
1268
|
+
off = if @noauth, do: [], else: [{"auth", nil}]
|
|
1269
|
+
secrets = ["OFFKEY01", "OFFKEY02", "OFFREFRESH01", "ACCESS01"]
|
|
1270
|
+
carries = fn c -> c.auth != nil or Enum.any?(secrets, &String.contains?(c.values, &1)) end
|
|
1271
|
+
|
|
1272
|
+
scenarios = [
|
|
1273
|
+
{"a key the chain resolves", [{"APIKEY", "OFFKEY01"}], [], [], [200]},
|
|
1274
|
+
{"an explicit apikey", [], [{"apikey", "OFFKEY02"}], [], [200]},
|
|
1275
|
+
{"a refusal", [{"REFRESH_TOKEN", "OFFREFRESH01"}], [],
|
|
1276
|
+
[{"name", "refresh_token"}, {"exchange", S.jm(["active", true])}], [401]}
|
|
1277
|
+
]
|
|
1278
|
+
|
|
1279
|
+
{sent, bought} =
|
|
1280
|
+
Enum.reduce(scenarios, {[], 0}, fn {what, env, extra, fextra, statuses}, {sent, bought} ->
|
|
1281
|
+
Enum.each(env, fn {k, v} -> System.put_env(@envprefix <> k, v) end)
|
|
1282
|
+
r = recorder(%{api: statuses})
|
|
1283
|
+
client = sdk(r, [{"feature", envchain(fextra)} | extra ++ off])
|
|
1284
|
+
ProjectName.direct(client, S.jm(["path", "/thing"]))
|
|
1285
|
+
Enum.each(env, fn {k, _v} -> System.delete_env(@envprefix <> k) end)
|
|
1286
|
+
|
|
1287
|
+
assert length(api(r)) == 1, what <> ": a request was retried with auth off"
|
|
1288
|
+
assert token(r) == [], what <> ": a token was bought with auth off"
|
|
1289
|
+
refute Enum.any?(api(r), carries), what <> ": a credential was sent with auth off: " <> wire(r)
|
|
1290
|
+
assert Secrets.credential(client) == "", what <> ": the feature resolved a credential with auth off"
|
|
1291
|
+
|
|
1292
|
+
{sent ++ api(r), bought + length(token(r))}
|
|
1293
|
+
end)
|
|
1294
|
+
|
|
1295
|
+
IO.puts(
|
|
1296
|
+
"\nsecrets: auth off by " <> if(@noauth, do: "config", else: "option") <> ": " <>
|
|
1297
|
+
"#{length(sent)} request(s), #{Enum.count(sent, carries)} credential(s), #{bought} token(s)"
|
|
1298
|
+
)
|
|
1299
|
+
end
|
|
1300
|
+
end
|
|
1301
|
+
|
|
1143
1302
|
defp listener(tag, route) do
|
|
1144
1303
|
{:ok, listen} =
|
|
1145
1304
|
:gen_tcp.listen(0, [:binary, active: false, reuseaddr: true, ip: {127, 0, 0, 1}])
|
|
@@ -405,6 +405,26 @@ defmodule ProjectName.FeatureTest do
|
|
|
405
405
|
end
|
|
406
406
|
end
|
|
407
407
|
|
|
408
|
+
test "cache passes a body that declares another type than JSON by, unread" do
|
|
409
|
+
if has?("cache") do
|
|
410
|
+
{server, calls} =
|
|
411
|
+
FH.recording_server(fn n, _fd ->
|
|
412
|
+
res = FH.make_response(200, nil, S.jm(["content-type", "audio/wav"]))
|
|
413
|
+
S.setprop(res, "sent", n)
|
|
414
|
+
{res, nil}
|
|
415
|
+
end)
|
|
416
|
+
|
|
417
|
+
h = harness([fspec("cache")], %{server: server})
|
|
418
|
+
a = FH.op(h, %{path: "/w/1/audio"})
|
|
419
|
+
b = FH.op(h, %{path: "/w/1/audio"})
|
|
420
|
+
assert S.size(calls) == 2, "the body was stored"
|
|
421
|
+
assert S.getprop(S.getprop(a.ctx, "response"), "sent") == 1,
|
|
422
|
+
"the cache handed on a copy it had read"
|
|
423
|
+
assert S.getprop(S.getprop(b.ctx, "response"), "sent") == 2
|
|
424
|
+
assert S.getprop(track(h, "_cache"), "bypass") == 2
|
|
425
|
+
end
|
|
426
|
+
end
|
|
427
|
+
|
|
408
428
|
test "cache refetches after the ttl" do
|
|
409
429
|
if has?("cache") do
|
|
410
430
|
clock = FH.make_clock()
|
|
@@ -16,6 +16,8 @@ type Point struct {
|
|
|
16
16
|
Relations []any
|
|
17
17
|
Alias map[string]any
|
|
18
18
|
Transform map[string]any
|
|
19
|
+
// The fields a list, load or remove sends in a request body.
|
|
20
|
+
Fields []any
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
func NewPoint(altmap map[string]any) *Point {
|
|
@@ -98,5 +100,11 @@ func NewPoint(altmap map[string]any) *Point {
|
|
|
98
100
|
t.Transform = map[string]any{}
|
|
99
101
|
}
|
|
100
102
|
|
|
103
|
+
if f := vs.GetProp(altmap, "fields"); f != nil {
|
|
104
|
+
if fl, ok := f.([]any); ok {
|
|
105
|
+
t.Fields = fl
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
101
109
|
return t
|
|
102
110
|
}
|
|
@@ -4,6 +4,7 @@ import (
|
|
|
4
4
|
"strings"
|
|
5
5
|
|
|
6
6
|
"GOMODULE/core"
|
|
7
|
+
"GOMODULE/utility"
|
|
7
8
|
)
|
|
8
9
|
|
|
9
10
|
type CacheFeature struct {
|
|
@@ -108,11 +109,23 @@ func (f *CacheFeature) through(ctx *core.Context, url string, fetchdef map[strin
|
|
|
108
109
|
}
|
|
109
110
|
|
|
110
111
|
// A body the transport could not read as JSON passes through, for the operation to report.
|
|
112
|
+
// A body that declares another type than JSON passes by, unread.
|
|
111
113
|
func (f *CacheFeature) storable(res any) bool {
|
|
112
114
|
status, ok := fresStatus(res)
|
|
113
115
|
rm, _ := res.(map[string]any)
|
|
114
116
|
unreadable, _ := rm["unreadable"].(bool)
|
|
115
|
-
return ok && status >= 200 && status < 300 && !unreadable
|
|
117
|
+
return ok && status >= 200 && status < 300 && !unreadable && f.json(rm)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
func (f *CacheFeature) json(rm map[string]any) bool {
|
|
121
|
+
headers, _ := rm["headers"].(map[string]any)
|
|
122
|
+
for key, val := range headers {
|
|
123
|
+
if strings.EqualFold("content-type", key) {
|
|
124
|
+
media, _ := val.(string)
|
|
125
|
+
return "" == media || utility.IsJSONMedia(media)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return true
|
|
116
129
|
}
|
|
117
130
|
|
|
118
131
|
func (f *CacheFeature) snapshot(res any) *cacheSnapshot {
|