@voxgig/sdkgen 4.48.1 → 4.49.1

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 +55 -6
  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
@@ -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
- # NO SUPPRESSION CHECK HERE, and that is deliberate: elixir's
477
- # transport/5 already returns before resolve/1 when `auth: nil`, so
478
- # a suppressed request never reaches this purchase at all. Every
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
- # `auth: nil` is the documented way to send NO credential, and
529
- # prepare_auth honours it by removing the header. There is then
530
- # nothing to resolve and nothing to buy: asking the chain could only
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
- defp suppressed?(f), do: S.getprop(S.getprop(f, "_liveopts"), "auth") == nil
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 <> " " <> token, else: token))
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
- {res, ferr} =
727
+ ask = fn at ->
709
728
  try do
710
729
  if S.isfunc(sysfetch),
711
- do: sysfetch.(url, fetchdef),
712
- else: raw_exchange_fetch(url, fetchdef)
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}, _rh, resbody}} ->
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
- {S.jm(["status", status, "json", fn -> parsed end, "body", text]), nil}
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))}
@@ -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 these tests assert on the one it places.
41
- # Decided as this file compiles, which `mix test` does after the SDK, so
42
- # ExUnit can skip every test and say so.
43
- if nil == S.getprop(S.getprop(ProjectName.Config.shared_config(), "options"), "auth") do
44
- reason = "this SDK's config declares no auth, so it places no credential"
45
- IO.puts("SKIP secrets: " <> reason)
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 credential header carries the SPEC's credential prefix, which a
189
- # TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
190
- # `Bearer <token>`, an apiKey scheme the raw token. So assert on the
191
- # CREDENTIAL and let the prefix be whatever this SDK's API declares -
192
- # pinning the whole header value passes only for a prefix-less API, and
193
- # this file ships to every project that selects the feature.
194
- defp credential_is(header, token) do
195
- got = if is_binary(header), do: header, else: ""
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
- assert got == token or String.ends_with?(got, " " <> token),
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), do: credential_is(if(call == nil, do: nil, else: call.auth), 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()
@@ -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 {