@voxgig/sdkgen 4.48.1 → 4.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/package.json +1 -1
  3. package/project/.sdk/model/feature/secrets.aontu +3 -0
  4. package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
  5. package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
  6. package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
  7. package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
  8. package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
  9. package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
  10. package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
  11. package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
  12. package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
  13. package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
  14. package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
  15. package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
  16. package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
  17. package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
  18. package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
  19. package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
  20. package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
  21. package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
  22. package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
  23. package/project/.sdk/tm/c/feature/cache.c +16 -1
  24. package/project/.sdk/tm/c/feature/secrets.c +149 -18
  25. package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
  26. package/project/.sdk/tm/c/tests/feature_test.c +31 -0
  27. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
  28. package/project/.sdk/tm/clojure/src/sdk/core.clj +8 -1
  29. package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
  30. package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
  31. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
  32. package/project/.sdk/tm/cpp/core/types.hpp +1 -11
  33. package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
  34. package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
  35. package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
  36. package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
  37. package/project/.sdk/tm/csharp/core/Control.cs +0 -5
  38. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
  39. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
  40. package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
  41. package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
  42. package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
  43. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
  44. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
  45. package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
  46. package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
  47. package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
  48. package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
  49. package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
  50. package/project/.sdk/tm/go/test/feature_test.go +32 -0
  51. package/project/.sdk/tm/go/utility/media.go +2 -2
  52. package/project/.sdk/tm/java/core/Control.java +0 -5
  53. package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
  54. package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
  55. package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
  56. package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
  57. package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
  58. package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
  59. package/project/.sdk/tm/java/utility/Media.java +2 -2
  60. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
  61. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
  62. package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
  63. package/project/.sdk/tm/js/test/feature.test.js +17 -0
  64. package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
  65. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
  66. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
  67. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
  68. package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
  69. package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
  70. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
  71. package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
  72. package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
  73. package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
  74. package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
  75. package/project/.sdk/tm/lua/utility/clean.lua +1 -0
  76. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
  77. package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
  78. package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
  79. package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
  80. package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
  81. package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
  82. package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
  83. package/project/.sdk/tm/perl/t/feature.t +24 -0
  84. package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
  85. package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
  86. package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
  87. package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
  88. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
  89. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
  90. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
  91. package/project/.sdk/tm/py/test/test_feature.py +18 -0
  92. package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
  93. package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
  94. package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
  95. package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
  96. package/project/.sdk/tm/rust/feature/cache.rs +15 -1
  97. package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
  98. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
  99. package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
  100. package/project/.sdk/tm/scala/core/Control.scala +0 -5
  101. package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
  102. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
  103. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
  104. package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
  105. package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
  106. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
  107. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
  108. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
  109. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
  110. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
  111. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
  112. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
  113. package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
  114. package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
  115. package/project/.sdk/tm/zig/feature/cache.zig +15 -1
  116. package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
  117. package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
  118. package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
  119. package/project/sdkgen-package.json +1 -1
@@ -213,15 +213,29 @@ let placed_of (r : recorder) (i : int) : value =
213
213
  | Some c -> placed_cred_of (getp c "fetchdef")
214
214
  | None -> Noval
215
215
 
216
- (* A header credential carries the SPEC's prefix, which a TEMPLATE cannot
217
- * know - so assert on the CREDENTIAL and let the prefix be whatever this
218
- * SDK's API declares. A query parameter and a cookie carry no prefix at all,
219
- * which the bare equality already covers. *)
220
- let credential_is (header : value) (token : string) : bool =
216
+ (* The header value prepare_auth writes for a token, in the form the DEFAULT
217
+ * client's auth gives: under Basic the token is the key half of the encoded
218
+ * pair, and a prefix is space-joined. *)
219
+ let credential ?(secret = "") (token : string) : string =
220
+ let opts = (Sdk_client.test ()).cl_options in
221
+ let value = match getpath_s opts "auth.basic" with
222
+ | Bool true -> base64_encode (token ^ ":" ^ secret)
223
+ | _ -> token in
224
+ match getpath_s opts "auth.prefix" with
225
+ | Str p when p <> "" -> p ^ " " ^ value
226
+ | _ -> value
227
+
228
+ let credential_is ?(secret = "") (header : value) (token : string) : bool =
221
229
  match header with
222
- | Str s -> s = token || Sdk_features.ends_with s (" " ^ token)
230
+ | Str s -> s = credential ~secret token
223
231
  | _ -> false
224
232
 
233
+ (* What prepare_auth placed: the header form, or the bare value in a query
234
+ * parameter or a cookie. *)
235
+ let placed_is (placed : value) (token : string) : bool =
236
+ if Sdk_prepare_auth.cred_where = "header" then credential_is placed token
237
+ else placed = Str token
238
+
225
239
  let absent (header : value) : bool = is_nullish header
226
240
 
227
241
  (* SUPPRESSION IS ABOUT EVERY BAG, not just the header. `auth: null` must
@@ -351,7 +365,7 @@ let () =
351
365
  what the generated prepare_auth placed - in the bag this API's
352
366
  scheme names, which for an apiKey-in-query SDK is the query string
353
367
  and not a header. *)
354
- check "carries OPTKEY01" (credential_is (placed_of r 0) "OPTKEY01"));
368
+ check "carries OPTKEY01" (placed_is (placed_of r 0) "OPTKEY01"));
355
369
 
356
370
  test "inactive.no_apikey_no_header" (fun () ->
357
371
  let (r, fetch) = recorder () in
@@ -592,6 +606,14 @@ let () =
592
606
  check "options.auth survives as a present null"
593
607
  (getprop_raw client.cl_options "auth" = Null));
594
608
 
609
+ test "chain.a_resolved_key_is_placed_as_prepare_auth_places_it" (fun () ->
610
+ let (r, fetch) = recorder () in
611
+ let client = live ~sdkopts:[("secret", Str "PASS01")] fetch
612
+ [("providers", ja [working "CHAINKEY05"])] in
613
+ check "ok" (res_ok (direct client));
614
+ check "carries CHAINKEY05, the secret kept"
615
+ (credential_is ~secret:"PASS01" (auth_of r 0) "CHAINKEY05"));
616
+
595
617
  test "auth.null_suppresses_an_explicit_apikey_too" (fun () ->
596
618
  let (r, fetch) = recorder () in
597
619
  let client = live ~sdkopts:[("auth", Null); ("apikey", Str "OPTKEY01")] fetch
@@ -710,6 +732,23 @@ let () =
710
732
  check_int "two API calls: the refusal and the retry" (List.length (api_calls r)) 2;
711
733
  check "the retry carries the fresh token" (credential_is (auth_of r 1) "ACCESS2"));
712
734
 
735
+ test "exchange.a_token_bought_on_a_refusal_is_placed_as_prepare_auth_places_it" (fun () ->
736
+ let (r, fetch) = recorder () in
737
+ (* Read as each request left: the retry rewrites the fetchdef it shares. *)
738
+ let sent = ref [] in
739
+ token_server r (fun token ->
740
+ sent := !sent @ [Str token];
741
+ if credential_is ~secret:"PASS01" (Str token) "ACCESS1" then 401 else 200);
742
+ let client = live ~sdkopts:[("secret", Str "PASS01")] fetch
743
+ [refresh_chain "R1"; ("name", Str "refresh_token"); xchg []] in
744
+ check "ok" (res_ok (direct client));
745
+ check_int "two API calls: the refusal and the retry" (List.length (api_calls r)) 2;
746
+ let nth i = match List.nth_opt !sent i with Some v -> v | None -> Noval in
747
+ check "the first carries ACCESS1, the secret kept"
748
+ (credential_is ~secret:"PASS01" (nth 0) "ACCESS1");
749
+ check "the retry carries ACCESS2, the secret kept"
750
+ (credential_is ~secret:"PASS01" (nth 1) "ACCESS2"));
751
+
713
752
  test "exchange.a_second_refusal_surfaces_rather_than_spinning" (fun () ->
714
753
  let (r, fetch) = recorder () in
715
754
  token_server r (fun _ -> 401);
@@ -911,6 +950,67 @@ let () =
911
950
  check (label ^ ": not a failed exchange: " ^ message) r) results;
912
951
  check_int "redirects refused" refused 4);
913
952
 
953
+ (* On system.fetch alone: the vendored HTTP client answers no headers, so
954
+ * no Location reaches the feature through it. *)
955
+ test "exchange.with_the_option_a_redirect_on_its_own_origin_is_followed_and_no_other" (fun () ->
956
+ let base = "http://secrets.test/api" in
957
+ let exchange = base ^ "/auth/token" and moved = base ^ "/moved/token" in
958
+ let away = "http://elsewhere.test/api/auth/token" in
959
+ let url_of c = match getp c "url" with Str u -> u | _ -> "" in
960
+ let on_origin u = String.starts_with ~prefix:"http://secrets.test/" u in
961
+ (* Each case: the status, the Location, and the url the exchange is asked
962
+ * again at; a Location of a query alone keeps the path that answered. *)
963
+ let cases = [(307, "/api/moved/token", Some moved); (308, moved, Some moved);
964
+ (307, "?step=2", Some (exchange ^ "?step=2")); (307, away, None);
965
+ (302, "/api/moved/token", None); (307, "/api/auth/token", None)] in
966
+ let results = List.map (fun (status, location, next) ->
967
+ let (r, fetch) = recorder () in
968
+ r.reply <- Some (fun _ url _ ->
969
+ if url = exchange then
970
+ jo [("status", vint_of status); ("statusText", Str "X");
971
+ ("headers", jo [("location", Str location)]); ("body", Str "");
972
+ ("json", json_thunk Noval)]
973
+ else if url = base ^ "/thing" then ok200 (jo [("id", Str "r01")])
974
+ else if on_origin url then ok200 (jo [("access_token", Str "MOVED01")])
975
+ else ok200 (jo [("access_token", Str "ELSEWHERE01")]));
976
+ let client = live ~base fetch [("name", Str "refresh_token");
977
+ xchg [("refresh", Str "REFRESH01");
978
+ ("redirect", Str "same-origin")]] in
979
+ let res = direct client in
980
+ let message = res_err res in
981
+ let asked = List.filter (fun c -> url_of c <> base ^ "/thing") !(r.calls) in
982
+ let manual = List.for_all (fun c ->
983
+ getp (getp c "fetchdef") "redirect" = Str "manual") asked in
984
+ let same = match asked with
985
+ | first :: rest -> List.for_all (fun c ->
986
+ getp (getp c "fetchdef") "method" = getp (getp first "fetchdef") "method"
987
+ && getp (getp c "fetchdef") "body" = getp (getp first "fetchdef") "body") rest
988
+ | [] -> false in
989
+ let urls = List.map url_of asked in
990
+ let want = "token exchange failed: " ^ string_of_int status ^ " from " ^ exchange in
991
+ let verdict =
992
+ if None <> next && res_ok res && manual && same
993
+ && Some urls = Option.map (fun n -> [exchange; n]) next then `Followed
994
+ else if None = next && manual && contains message want
995
+ && List.for_all (fun u -> u = exchange) urls then `Refused
996
+ else `Wrong (string_of_int status ^ " to " ^ location ^ ": " ^ message
997
+ ^ " " ^ String.concat "; " urls) in
998
+ (verdict, location, List.length urls,
999
+ List.length (List.filter (fun c -> not (on_origin (url_of c))) !(r.calls))))
1000
+ cases in
1001
+ let count v = List.length (List.filter (fun (w, _, _, _) -> w = v) results) in
1002
+ let elsewhere = List.fold_left (fun n (_, _, _, e) -> n + e) 0 results in
1003
+ let looped = List.fold_left (fun n (_, l, a, _) ->
1004
+ if l = "/api/auth/token" then a else n) 0 results in
1005
+ Printf.printf "secrets: exchange opt-in followed %d redirect(s) on its own origin, refused %d, %d elsewhere\n"
1006
+ (count `Followed) (count `Refused) elsewhere;
1007
+ check_int "the exchange reached another origin" elsewhere 0;
1008
+ List.iter (function (`Wrong m, _, _, _) -> check m false | _ -> ()) results;
1009
+ check_int "followed" (count `Followed) 3;
1010
+ check_int "refused" (count `Refused) 3;
1011
+ (* A redirect to itself stops at the hop limit. *)
1012
+ check_int "the loop's exchange count" looped 6);
1013
+
914
1014
  test "exchange.test_mode_buys_nothing" (fun () ->
915
1015
  (* A non-live client: the credential becomes the deterministic
916
1016
  * `test-<response>` and no purchase is attempted. Observed through
@@ -944,5 +1044,41 @@ let () =
944
1044
 
945
1045
  ()
946
1046
 
1047
+ (* Off by the config where it declares no auth, else by `auth: null`: either
1048
+ * way the feature places no credential, buys no token and retries nothing. *)
1049
+ let () =
1050
+ Testutil.test "secrets.auth_off.places_no_credential_and_buys_no_token" (fun () ->
1051
+ let off = if places_credential then [("auth", Null)] else [] in
1052
+ let secrets = ["OFFKEY01"; "OFFKEY02"; "OFFREFRESH01"; "ACCESS01"] in
1053
+ let carries (c : value) : bool =
1054
+ match getp (getp c "fetchdef") "headers" with
1055
+ | Map _ as h ->
1056
+ not (absent (Sdk_features.header_ci h credheader))
1057
+ || List.exists (fun k -> match getp h k with
1058
+ | Str v -> List.exists (contains v) secrets
1059
+ | _ -> false) (keysof h)
1060
+ | _ -> false in
1061
+ let sent = ref [] and bought = ref 0 in
1062
+ let refresh = ("providers", ja [memory ~name:"refresh" [("REFRESH_TOKEN", Str "OFFREFRESH01")]]) in
1063
+ List.iter (fun (what, sdkopts, fopts, status) ->
1064
+ let (r, fetch) = recorder () in
1065
+ r.reply <- Some (fun _ url _ ->
1066
+ if is_token url then ok200 (jo [("access_token", Str "ACCESS01")])
1067
+ else status_res status (jo [("id", Str "r01")]));
1068
+ let client = live ~sdkopts:(sdkopts @ off) fetch fopts in
1069
+ ignore (direct client);
1070
+ check_int (what ^ ": one request, not retried") (List.length (api_calls r)) 1;
1071
+ check_int (what ^ ": no token bought") (List.length (token_calls r)) 0;
1072
+ check (what ^ ": no credential sent") (not (List.exists carries (api_calls r)));
1073
+ sent := !sent @ api_calls r;
1074
+ bought := !bought + List.length (token_calls r))
1075
+ [ ("a key the chain resolves", [], [("providers", ja [working "OFFKEY01"])], 200);
1076
+ ("an explicit apikey", [("apikey", Str "OFFKEY02")], [], 200);
1077
+ ("a refusal", [], [refresh; ("name", Str "refresh_token");
1078
+ ("exchange", jo [("active", Bool true)])], 401) ];
1079
+ Printf.printf "secrets: auth off by %s: %d request(s), %d credential(s), %d token(s)\n"
1080
+ (if places_credential then "option" else "config")
1081
+ (List.length !sent) (List.length (List.filter carries !sent)) !bought)
1082
+
947
1083
  (* Printed from EXECUTIONS, for the generator's lane. *)
948
1084
  let () = Printf.printf "feature.secrets: ran %d check(s)\n" !ran
@@ -178,6 +178,21 @@ let () =
178
178
  ignore (op h ~op:"load" ~path:"/w" ()); ignore (op h ~op:"load" ~path:"/w" ());
179
179
  check_int "calls" (List.length !calls) 2;
180
180
  check_vnum "bypass" (getp (track h "cache") "bypass") 2.);
181
+ test "cache.non_json_body_passes_by_unread" (fun () ->
182
+ let sent = ref [] in
183
+ let (srv, calls) = recording_server ~reply:(fun _ _ ->
184
+ let res = make_response ~headers:[("content-type", Str "audio/wav")] 200 Noval in
185
+ sent := !sent @ [res];
186
+ (res, None)) () in
187
+ let h = make_client ~server:srv [("cache", empty_map ())] in
188
+ let a = op h ~op:"load" ~path:"/w/1/audio" () in
189
+ let b = op h ~op:"load" ~path:"/w/1/audio" () in
190
+ check_int "the body was stored" (List.length !calls) 2;
191
+ check "the cache handed on a copy it had read"
192
+ (match !sent with s :: _ -> a.or_response == s | [] -> false);
193
+ check "the second read was handed on as returned"
194
+ (match !sent with _ :: s :: _ -> b.or_response == s | _ -> false);
195
+ check_vnum "bypass" (getp (track h "cache") "bypass") 2.);
181
196
  test "cache.refetches_after_ttl" (fun () ->
182
197
  let c = make_clock () in
183
198
  let (srv, calls) = recording_server () in
@@ -20,6 +20,7 @@ BEGIN { $__dir = File::Basename::dirname(Cwd::abs_path(__FILE__)) }
20
20
  require(Cwd::abs_path("$__dir/../lib/Voxgig/Struct.pm"));
21
21
  require(Cwd::abs_path("$__dir/../core/helpers.pm"));
22
22
  require(Cwd::abs_path("$__dir/base_feature.pm"));
23
+ require(Cwd::abs_path("$__dir/../utility/media.pm"));
23
24
 
24
25
  package ProjectNameCacheFeature;
25
26
 
@@ -83,6 +84,12 @@ sub through {
83
84
 
84
85
  my ($res, $err) = $inner->($ctx, $url, $fetchdef);
85
86
 
87
+ # A body that declares another type than JSON passes by, unread.
88
+ if (!defined $err && $self->_cacheable($res) && !_json($res)) {
89
+ $self->_track('bypass');
90
+ return ($res, $err);
91
+ }
92
+
86
93
  if (!defined $err && $self->_cacheable($res)) {
87
94
  my $snapshot = $self->_snapshot($res);
88
95
  my $ttl = defined $self->{options}{ttl} ? $self->{options}{ttl} : 5000;
@@ -110,6 +117,12 @@ sub _cacheable {
110
117
  return ($status >= 200 && $status < 300) ? 1 : 0;
111
118
  }
112
119
 
120
+ sub _json {
121
+ my ($res) = @_;
122
+ my $type = ProjectNameUtilities::header_value($res->{headers}, 'content-type');
123
+ return '' eq $type || ProjectNameUtilities::is_json_media($type);
124
+ }
125
+
113
126
  sub _snapshot {
114
127
  my ($self, $res) = @_;
115
128
  my $data;
@@ -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
- # `auth => undef` is the documented way to send NO credential, and
433
- # prepare_auth honours it by removing the header. A refusal of a
434
- # deliberately unauthenticated request is not an expired token and
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
- # (utility/prepare_auth.pm). Reached defensively on the exchange path -
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->{'authorization'} = ('' eq $prefix) ? "$token" : "$prefix $token";
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
- 'method' => $x->{method},
604
- 'headers' => { 'content-type' => 'application/json' },
605
- 'body' => $body,
606
- # Never followed: a redirect would carry the refresh token on.
607
- 'redirect' => 'manual',
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;