@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
@@ -115,6 +115,7 @@ const Exchange = struct {
115
115
  response: []const u8,
116
116
  statuses: []const i64,
117
117
  retries: i64,
118
+ sameorigin: bool,
118
119
  };
119
120
 
120
121
  /// Everything the transport wrapper needs, heap-allocated once per feature
@@ -126,6 +127,11 @@ pub const State = struct {
126
127
  rootctx: ?*Context = null,
127
128
  liveopts: Value = .{ .null = {} },
128
129
 
130
+ /// What prepare_auth was generated from: the config declares auth only
131
+ /// where it places a credential, and Basic only where it composes one.
132
+ declared: bool = false,
133
+ basic: bool = false,
134
+
129
135
  secretname: []const u8 = "apikey",
130
136
  cache: bool = true,
131
137
  exchange: ?Exchange = null,
@@ -248,6 +254,13 @@ pub const SecretsFeature = struct {
248
254
  st.rootctx = ctx;
249
255
  st.liveopts = ctx.options;
250
256
 
257
+ const declared = h.getpath(&.{ "options", "auth" }, ctx.config);
258
+ st.declared = declared == .object;
259
+ st.basic = st.declared and switch (h.getp(declared, "basic")) {
260
+ .bool => |b| b,
261
+ else => false,
262
+ };
263
+
251
264
  // Wrap the transport FIRST. The fail-closed gate needs the seam
252
265
  // whenever the feature is active - hooks cannot fail an operation
253
266
  // and the raw paths run none - and the exchange additionally needs
@@ -277,6 +290,7 @@ pub const SecretsFeature = struct {
277
290
  .response = sup.fopt_str(xopts, "response", "access_token"),
278
291
  .statuses = statuses.items,
279
292
  .retries = sup.fopt_int(xopts, "retries", 1),
293
+ .sameorigin = std.mem.eql(u8, sup.fopt_str(xopts, "redirect", "refuse"), "same-origin"),
280
294
  };
281
295
  }
282
296
 
@@ -565,6 +579,7 @@ fn resolve(st: *State, ctx: *Context) E!void {
565
579
 
566
580
  fn resolve_once(st: *State, ctx: *Context) E!void {
567
581
  const s = st.sek orelse return;
582
+ if (!auth_on(st)) return;
568
583
 
569
584
  const found = s.trysecret(st.secretname) catch
570
585
  return ctx.fail(ERRCODE, "secrets: out of memory");
@@ -619,12 +634,9 @@ fn resolve_once(st: *State, ctx: *Context) E!void {
619
634
  // prepare_auth builds it, from the same options.auth name and prefix, so
620
635
  // the two cannot drift.
621
636
  fn with_refresh(st: *State, ctx: *Context, url: []const u8, fetchdef: Value, inner: Fetcher) E!Value {
622
- // `auth: null` is the documented way to send NO credential, and
623
- // prepare_auth honours it by removing the header. A refusal of a
624
- // deliberately unauthenticated request is not an expired token and
625
- // cannot be fixed by buying one - retrying would transmit exactly the
626
- // credential the caller suppressed.
627
- if (!auth_active(st.liveopts)) return inner.invoke(ctx, url, fetchdef);
637
+ // A refusal of an unauthenticated request is not an expired token, and
638
+ // retrying would transmit the credential that auth off withholds.
639
+ if (!auth_on(st)) return inner.invoke(ctx, url, fetchdef);
628
640
 
629
641
  const max = if (st.exchange) |x| x.retries else 0;
630
642
  var attempt: i64 = 0;
@@ -682,6 +694,26 @@ fn auth_active(options: Value) bool {
682
694
  };
683
695
  }
684
696
 
697
+ // prepare_auth's decision: auth the config declares, not suppressed by
698
+ // `auth: null`.
699
+ fn auth_on(st: *State) bool {
700
+ return st.declared and auth_active(st.liveopts);
701
+ }
702
+
703
+ // Under Basic the token is the key half, as apikey is: prepare_auth's pair.
704
+ fn pair(st: *State, token: []const u8) []const u8 {
705
+ const secret: []const u8 = switch (h.getp(st.liveopts, "secret")) {
706
+ .string => |s| s,
707
+ else => "",
708
+ };
709
+ const text = std.fmt.allocPrint(h.A(), "{s}:{s}", .{ token, secret }) catch return token;
710
+ const enc = std.base64.standard.Encoder;
711
+ const buf = h.A().alloc(u8, enc.calcSize(text.len)) catch return token;
712
+ const out = enc.encode(buf, text);
713
+ if (st.rootctx) |ctx| ctx.util().clean_add(ctx, out);
714
+ return out;
715
+ }
716
+
685
717
  fn reauth(st: *State, fetchdef: Value, token: []const u8) void {
686
718
  const headers = h.getp(fetchdef, "headers");
687
719
  if (headers != .object) return;
@@ -693,14 +725,17 @@ fn reauth(st: *State, fetchdef: Value, token: []const u8) void {
693
725
  };
694
726
 
695
727
  // Suppressed auth means NO header, the same answer prepare_auth gives.
696
- // Reached defensively - with_refresh does not retry at all when auth is
697
- // suppressed - but this is the function that writes the credential, so
698
- // it is where the rule has to hold.
699
- if (!auth_active(st.liveopts)) {
728
+ if (!auth_on(st)) {
700
729
  h.del_prop(headers, h.vstr(name));
701
730
  return;
702
731
  }
703
732
 
733
+ const basic = st.basic and switch (h.getpath(&.{ "auth", "basic" }, st.liveopts)) {
734
+ .bool => |b| b,
735
+ else => false,
736
+ };
737
+ const value = if (basic) pair(st, token) else token;
738
+
704
739
  const prefix: []const u8 = switch (h.getpath(&.{ "auth", "prefix" }, st.liveopts)) {
705
740
  .string => |s| s,
706
741
  else => "",
@@ -708,9 +743,9 @@ fn reauth(st: *State, fetchdef: Value, token: []const u8) void {
708
743
 
709
744
  // Empty prefix (a raw apiKey credential) must not add a leading space.
710
745
  if (0 == prefix.len) {
711
- h.setp(headers, name, h.vstr(token));
746
+ h.setp(headers, name, h.vstr(value));
712
747
  } else {
713
- h.setp(headers, name, h.vstr(std.fmt.allocPrint(h.A(), "{s} {s}", .{ prefix, token }) catch token));
748
+ h.setp(headers, name, h.vstr(std.fmt.allocPrint(h.A(), "{s} {s}", .{ prefix, value }) catch value));
714
749
  }
715
750
  }
716
751
 
@@ -763,26 +798,14 @@ fn buy(st: *State, ctx: *Context) E![]const u8 {
763
798
 
764
799
  const sysfetch = h.getpath(&.{ "system", "fetch" }, options);
765
800
 
766
- var res: Value = undefined;
767
- if (sysfetch == .function) {
768
- // The caller's transport, when there is one - the same seam the SDK
769
- // itself uses for every request. Never followed: a redirect would
770
- // carry the refresh token on.
771
- res = h.call_vfn(sysfetch, h.ja(&.{
772
- h.vstr(url),
773
- h.jo(&.{
774
- .{ "method", h.vstr(x.method) },
775
- .{ "headers", h.jo(&.{.{ "content-type", h.vstr("application/json") }}) },
776
- .{ "body", h.vstr(body) },
777
- .{ "redirect", h.vstr("manual") },
778
- }),
779
- }));
780
- if (h.get_str(res, "__err__")) |message| return ctx.fail(ERRCODE, message);
781
- } else {
782
- // No custom transport supplied - the ordinary case (make_options
783
- // leaves system.fetch unset, and the SDK's default transport has no
784
- // HTTP client). See raw_exchange_fetch.
785
- res = try raw_exchange_fetch(ctx, url, x.method, body);
801
+ var res = try send(ctx, sysfetch, url, x.method, body);
802
+
803
+ // By option alone, and only while the exchange stays on its own origin.
804
+ var at = url;
805
+ var hops: usize = 0;
806
+ while (x.sameorigin and hops < REDIRECT_MAX) : (hops += 1) {
807
+ at = moved(url, at, res) orelse break;
808
+ res = try send(ctx, sysfetch, at, x.method, body);
786
809
  }
787
810
 
788
811
  const status = h.to_int(h.getp(res, "status"));
@@ -812,6 +835,59 @@ fn buy(st: *State, ctx: *Context) E![]const u8 {
812
835
  return token;
813
836
  }
814
837
 
838
+ const REDIRECT_MAX = 5;
839
+
840
+ // One request of the exchange.
841
+ fn send(ctx: *Context, sysfetch: Value, url: []const u8, method: []const u8, body: []const u8) E!Value {
842
+ if (sysfetch == .function) {
843
+ // The caller's transport, when there is one - the same seam the SDK
844
+ // itself uses for every request. Never followed: a redirect would
845
+ // carry the refresh token on.
846
+ const res = h.call_vfn(sysfetch, h.ja(&.{
847
+ h.vstr(url),
848
+ h.jo(&.{
849
+ .{ "method", h.vstr(method) },
850
+ .{ "headers", h.jo(&.{.{ "content-type", h.vstr("application/json") }}) },
851
+ .{ "body", h.vstr(body) },
852
+ .{ "redirect", h.vstr("manual") },
853
+ }),
854
+ }));
855
+ if (h.get_str(res, "__err__")) |message| return ctx.fail(ERRCODE, message);
856
+ return res;
857
+ }
858
+ // No custom transport supplied - the ordinary case (make_options leaves
859
+ // system.fetch unset, and the SDK's default transport has no HTTP
860
+ // client). See raw_exchange_fetch.
861
+ return raw_exchange_fetch(ctx, url, method, body);
862
+ }
863
+
864
+ // Where a 307 or 308 points, when that is on the exchange's own origin. Any
865
+ // other redirect would drop the body or leave the origin.
866
+ fn moved(url: []const u8, at: []const u8, res: Value) ?[]const u8 {
867
+ const status = h.to_int(h.getp(res, "status"));
868
+ if (307 != status and 308 != status) return null;
869
+ const location = h.get_str(h.getp(res, "headers"), "location") orelse return null;
870
+ if (0 == location.len) return null;
871
+ const own = std.Uri.parse(url) catch return null;
872
+ const from = std.Uri.parse(at) catch return null;
873
+ const buf = h.A().alloc(u8, 2 * location.len + at.len) catch return null;
874
+ @memcpy(buf[0..location.len], location);
875
+ var aux = buf;
876
+ const next = from.resolveInPlace(location.len, &aux) catch return null;
877
+ if (!std.ascii.eqlIgnoreCase(own.scheme, next.scheme) or port_of(own) != port_of(next)) return null;
878
+ const ownhost = (own.host orelse return null).toRawMaybeAlloc(h.A()) catch return null;
879
+ const nexthost = (next.host orelse return null).toRawMaybeAlloc(h.A()) catch return null;
880
+ if (!std.ascii.eqlIgnoreCase(ownhost, nexthost)) return null;
881
+ return std.fmt.allocPrint(h.A(), "{f}", .{next}) catch null;
882
+ }
883
+
884
+ fn port_of(uri: std.Uri) ?u16 {
885
+ if (uri.port) |port| return port;
886
+ if (std.ascii.eqlIgnoreCase(uri.scheme, "https")) return 443;
887
+ if (std.ascii.eqlIgnoreCase(uri.scheme, "http")) return 80;
888
+ return null;
889
+ }
890
+
815
891
  // The token-exchange transport of last resort: the vendored sekreto
816
892
  // plugins' own HTTP round-trip (std.http.Client + std TLS, always exported
817
893
  // by the generated feature/secrets/plugins.zig), answering in the shape the
@@ -836,6 +912,7 @@ fn raw_exchange_fetch(ctx: *Context, url: []const u8, method_name: []const u8, b
836
912
  .ok => |response| h.jo(&.{
837
913
  .{ "status", h.vnum(@intCast(response.status)) },
838
914
  .{ "statusText", h.vstr("") },
915
+ // sekreto's client answers no headers, so no Location reaches the redirect option.
839
916
  .{ "headers", h.omap() },
840
917
  .{ "json", h.json_thunk(from_std_json(response.body)) },
841
918
  }),
@@ -103,6 +103,7 @@ fn cred_header() []const u8 {
103
103
  const Call = struct {
104
104
  url: []const u8,
105
105
  auth: ?[]const u8,
106
+ values: []const u8 = "",
106
107
  body: ?[]const u8,
107
108
  redirect: ?[]const u8,
108
109
  };
@@ -140,7 +141,17 @@ const Wire = struct {
140
141
  const auth: ?[]const u8 = if (h.get_str(headers, cred_header())) |a| (h.A().dupe(u8, a) catch a) else null;
141
142
  const body: ?[]const u8 = if (h.get_str(fetchdef, "body")) |b| (h.A().dupe(u8, b) catch b) else null;
142
143
  const redirect = h.get_str(fetchdef, "redirect");
143
- self.calls.append(h.A(), .{ .url = h.A().dupe(u8, url) catch url, .auth = auth, .body = body, .redirect = redirect }) catch {};
144
+ var values: std.ArrayList(u8) = .empty;
145
+ if (headers == .object) {
146
+ var it = headers.object.iterator();
147
+ while (it.next()) |kv| {
148
+ if (kv.value_ptr.* == .string) {
149
+ values.appendSlice(h.A(), kv.value_ptr.*.string) catch {};
150
+ values.append(h.A(), '\n') catch {};
151
+ }
152
+ }
153
+ }
154
+ self.calls.append(h.A(), .{ .url = h.A().dupe(u8, url) catch url, .auth = auth, .values = values.items, .body = body, .redirect = redirect }) catch {};
144
155
 
145
156
  const suffix = std.fmt.allocPrint(h.A(), "/{s}", .{self.tokenpath}) catch "/auth/token";
146
157
  if (std.mem.endsWith(u8, url, suffix)) {
@@ -262,19 +273,43 @@ fn count_named(client: *sdk.SDK, name: []const u8) usize {
262
273
  return n;
263
274
  }
264
275
 
265
- // The credential header carries the SPEC's credential prefix (if any), which
266
- // a TEMPLATE cannot know: an OpenAPI bearer scheme gives `Bearer <token>`, an
267
- // apiKey scheme the raw token. So assert on the CREDENTIAL and let the
268
- // prefix be whatever this SDK's API declares.
269
- fn credential_is(auth: ?[]const u8, token: []const u8) !void {
276
+ // The header value prepare_auth writes for a token, in the form the DEFAULT
277
+ // client's auth gives: under Basic the token is the key half of the encoded
278
+ // pair, and a prefix is space-joined.
279
+ fn credential(token: []const u8, secret: []const u8) []const u8 {
280
+ const auth = h.getp(sdk.test_sdk(vnull(), vnull()).options_map(), "auth");
281
+ const prefix: []const u8 = switch (h.getp(auth, "prefix")) {
282
+ .string => |p| p,
283
+ else => "",
284
+ };
285
+ const basic = switch (h.getp(auth, "basic")) {
286
+ .bool => |b| b,
287
+ else => false,
288
+ };
289
+ var value = token;
290
+ if (basic) {
291
+ const text = std.fmt.allocPrint(h.A(), "{s}:{s}", .{ token, secret }) catch token;
292
+ const enc = std.base64.standard.Encoder;
293
+ const buf = h.A().alloc(u8, enc.calcSize(text.len)) catch unreachable;
294
+ value = enc.encode(buf, text);
295
+ }
296
+ if (0 == prefix.len) return value;
297
+ return std.fmt.allocPrint(h.A(), "{s} {s}", .{ prefix, value }) catch value;
298
+ }
299
+
300
+ fn paired_is(auth: ?[]const u8, token: []const u8, secret: []const u8) !void {
270
301
  const got = auth orelse "";
271
- const suffix = std.fmt.allocPrint(h.A(), " {s}", .{token}) catch token;
272
- if (!(std.mem.eql(u8, got, token) or std.mem.endsWith(u8, got, suffix))) {
273
- std.debug.print("expected the {s} header to carry {s}, got: {s}\n", .{ cred_header(), token, got });
302
+ const want = credential(token, secret);
303
+ if (!std.mem.eql(u8, got, want)) {
304
+ std.debug.print("expected the {s} header to carry {s} as {s}, got: {s}\n", .{ cred_header(), token, want, got });
274
305
  return error.TestUnexpectedResult;
275
306
  }
276
307
  }
277
308
 
309
+ fn credential_is(auth: ?[]const u8, token: []const u8) !void {
310
+ return paired_is(auth, token, "");
311
+ }
312
+
278
313
  fn contains(text: []const u8, needle: []const u8) bool {
279
314
  return std.mem.indexOf(u8, text, needle) != null;
280
315
  }
@@ -658,6 +693,16 @@ test "secrets active: auth null suppresses the credential, chain or no chain" {
658
693
  try testing.expect(raw == .null);
659
694
  }
660
695
 
696
+ test "secrets active: a resolved key is placed as prepare_auth places it, the secret kept" {
697
+ if (!present() or !placesCredential()) return error.SkipZigTest;
698
+ const w = Wire.new();
699
+ const chain = Scripted.new("CHAINKEY05", null);
700
+ const client = secrets_client(w, h.jo(&.{.{ "secret", h.vstr("PASS01") }}), secrets_opts(h.ja(&.{chain.fn_val()}), vnull()));
701
+ _ = direct_probe(client);
702
+ try testing.expect(w.api().len == 1);
703
+ try paired_is(w.api()[0].auth, "CHAINKEY05", "PASS01");
704
+ }
705
+
661
706
  test "secrets active: the secret name is configurable" {
662
707
  if (!present() or !placesCredential()) return error.SkipZigTest;
663
708
  const w = Wire.new();
@@ -792,6 +837,17 @@ test "secrets exchange: a 401 buys another token and retries the SAME request" {
792
837
  try testing.expectEqualStrings(w.api()[0].url, w.api()[1].url);
793
838
  }
794
839
 
840
+ test "secrets exchange: a token bought on a refusal is placed as prepare_auth places it" {
841
+ if (!present() or !placesCredential()) return error.SkipZigTest;
842
+ const w = Wire.new();
843
+ w.apistatus = &.{ 401, 200 };
844
+ const client = secrets_client(w, h.jo(&.{.{ "secret", h.vstr("PASS01") }}), exchange_opts(h.ja(&.{Scripted.new("REFRESH01", null).fn_val()}), vnull()));
845
+ _ = direct_probe(client);
846
+ try testing.expect(w.api().len == 2);
847
+ try paired_is(w.api()[0].auth, "ACCESS01", "PASS01");
848
+ try paired_is(w.api()[1].auth, "ACCESS02", "PASS01");
849
+ }
850
+
795
851
  test "secrets exchange: the retry happens once, not in a loop" {
796
852
  if (!present() or !placesCredential()) return error.SkipZigTest;
797
853
  const w = Wire.new();
@@ -886,6 +942,7 @@ test "secrets exchange: auth null suppresses the credential, refusal or not" {
886
942
  _ = direct_probe(client);
887
943
  try testing.expect(w.api().len == 1);
888
944
  try testing.expect(w.api()[0].auth == null);
945
+ try testing.expect(w.token_calls() == 0);
889
946
  }
890
947
 
891
948
  test "secrets exchange: exchange off leaves the feature exactly as it was" {
@@ -1069,3 +1126,169 @@ test "secrets exchange: a redirect answering the exchange is a failure, and noth
1069
1126
  try testing.expect(cases.len == refused);
1070
1127
  try testing.expect(0 == followed);
1071
1128
  }
1129
+
1130
+ // A supplied fetch answering the exchange with `status` to `location`, every
1131
+ // other exchange URL with a token, and the API call with a record. It keeps
1132
+ // each exchange request as "url method redirect body".
1133
+ const Mover = struct {
1134
+ status: i64,
1135
+ location: []const u8,
1136
+ calls: std.ArrayList([]const u8) = .empty,
1137
+ elsewhere: usize = 0,
1138
+
1139
+ fn call(p: *anyopaque, _: std.mem.Allocator, arg: Value) anyerror!Value {
1140
+ const self: *Mover = @ptrCast(@alignCast(p));
1141
+ const url: []const u8 = if (arg.array.data.items[0] == .string) arg.array.data.items[0].string else "";
1142
+ const fetchdef = arg.array.data.items[1];
1143
+ if (!std.mem.startsWith(u8, url, "http://api.test/")) self.elsewhere += 1;
1144
+ if (std.mem.endsWith(u8, url, "/api/thing")) return response(200, h.jo(&.{.{ "ok", h.vbool(true) }}));
1145
+ try self.calls.append(h.A(), try std.fmt.allocPrint(h.A(), "{s} {s} {s} {s}", .{
1146
+ url,
1147
+ h.get_str(fetchdef, "method") orelse "",
1148
+ h.get_str(fetchdef, "redirect") orelse "",
1149
+ h.get_str(fetchdef, "body") orelse "",
1150
+ }));
1151
+ if (!std.mem.eql(u8, url, "http://api.test/api/auth/token")) {
1152
+ return response(200, h.jo(&.{.{ "access_token", h.vstr("MOVED01") }}));
1153
+ }
1154
+ return h.jo(&.{
1155
+ .{ "status", h.vnum(self.status) },
1156
+ .{ "headers", h.jo(&.{.{ "location", h.vstr(self.location) }}) },
1157
+ .{ "body", h.vstr("") },
1158
+ });
1159
+ }
1160
+ };
1161
+
1162
+ // Through the caller's system.fetch: the exchange's own client reports no
1163
+ // Location, and the SDK has no live transport for the API call.
1164
+ test "secrets exchange: with the option, a redirect on the exchange's own origin is followed, and no other" {
1165
+ if (!present() or !placesCredential()) return error.SkipZigTest;
1166
+ const base = "http://api.test/api";
1167
+ const sent = " POST manual {\"refresh_token\":\"REFRESH01\"}";
1168
+ const asked = base ++ "/auth/token" ++ sent;
1169
+ // The request the exchange is asked again with, null when refused; a
1170
+ // Location of a query alone keeps the path that answered.
1171
+ const Case = struct { status: i64, location: []const u8, next: ?[]const u8 };
1172
+ const cases = [_]Case{
1173
+ .{ .status = 307, .location = "/api/moved/token", .next = base ++ "/moved/token" ++ sent },
1174
+ .{ .status = 308, .location = "http://api.test/api/moved/token", .next = base ++ "/moved/token" ++ sent },
1175
+ .{ .status = 307, .location = "?step=2", .next = base ++ "/auth/token?step=2" ++ sent },
1176
+ .{ .status = 307, .location = "http://other.test/api/auth/token", .next = null },
1177
+ .{ .status = 302, .location = "/api/moved/token", .next = null },
1178
+ .{ .status = 307, .location = "/api/auth/token", .next = null },
1179
+ };
1180
+
1181
+ var followed: usize = 0;
1182
+ var refused: usize = 0;
1183
+ var elsewhere: usize = 0;
1184
+ var looped: usize = 0;
1185
+ var wrong: std.ArrayList([]const u8) = .empty;
1186
+
1187
+ for (cases) |c| {
1188
+ const m = try h.A().create(Mover);
1189
+ m.* = .{ .status = c.status, .location = c.location };
1190
+ const opts = h.jo(&.{
1191
+ .{ "base", h.vstr(base) },
1192
+ .{ "system", h.jo(&.{.{ "fetch", h.callable(@ptrCast(m), Mover.call) }}) },
1193
+ .{ "feature", h.jo(&.{.{ "secrets", h.jo(&.{
1194
+ .{ "active", h.vbool(true) },
1195
+ .{ "name", h.vstr("refresh_token") },
1196
+ .{ "exchange", h.jo(&.{
1197
+ .{ "active", h.vbool(true) },
1198
+ .{ "refresh", h.vstr("REFRESH01") },
1199
+ .{ "redirect", h.vstr("same-origin") },
1200
+ }) },
1201
+ }) }}) },
1202
+ });
1203
+ const client = sdk.SDK.new_with(opts, &.{sdk.SecretsFeature.make()}).ok;
1204
+ const res = client.direct(h.jo(&.{.{ "path", h.vstr("/thing") }}));
1205
+
1206
+ const ok = h.get_bool(res, "ok") orelse false;
1207
+ const msg = h.get_str(res, "err") orelse "";
1208
+ const want = try std.fmt.allocPrint(h.A(), "token exchange failed: {d} from {s}/auth/token", .{ c.status, base });
1209
+ const loop = std.mem.eql(u8, c.location, "/api/auth/token");
1210
+ if (loop) looped = m.calls.items.len;
1211
+ elsewhere += m.elsewhere;
1212
+
1213
+ const follows = null != c.next;
1214
+ var right = if (c.next) |next|
1215
+ ok and 2 == m.calls.items.len and std.mem.eql(u8, m.calls.items[1], next)
1216
+ else
1217
+ !ok and contains(msg, want) and 0 < m.calls.items.len and (loop or 1 == m.calls.items.len);
1218
+ for (m.calls.items, 0..) |seen, i| {
1219
+ if ((0 == i or !follows) and !std.mem.eql(u8, seen, asked)) right = false;
1220
+ }
1221
+ if (!right) {
1222
+ try wrong.append(h.A(), try std.fmt.allocPrint(h.A(), "{d} to {s}: {s} {d} call(s)", .{ c.status, c.location, msg, m.calls.items.len }));
1223
+ } else if (follows) {
1224
+ followed += 1;
1225
+ } else {
1226
+ refused += 1;
1227
+ }
1228
+ }
1229
+
1230
+ std.debug.print("secrets: exchange opt-in followed {d} redirect(s) on its own origin, refused {d}, {d} elsewhere\n", .{ followed, refused, elsewhere });
1231
+ for (wrong.items) |w| std.debug.print("same-origin not as expected: {s}\n", .{w});
1232
+ try testing.expect(0 == wrong.items.len);
1233
+ try testing.expect(0 == elsewhere);
1234
+ try testing.expect(3 == followed);
1235
+ try testing.expect(3 == refused);
1236
+ // A redirect to itself is followed five times and no more.
1237
+ try testing.expect(6 == looped);
1238
+ }
1239
+
1240
+ // ---- auth off ----------------------------------------------------------------
1241
+
1242
+ // Off by the config where it declares no auth, else by `auth: null`: either
1243
+ // way the feature places no credential, buys no token and retries nothing.
1244
+ fn carries(c: Call) bool {
1245
+ if (c.auth != null) return true;
1246
+ const secrets = [_][]const u8{ "OFFKEY01", "OFFKEY02", "OFFREFRESH01", "ACCESS01" };
1247
+ for (secrets) |secret| {
1248
+ if (contains(c.values, secret)) return true;
1249
+ }
1250
+ return false;
1251
+ }
1252
+
1253
+ test "auth off: the feature places no credential and buys no token" {
1254
+ if (!present()) return error.SkipZigTest;
1255
+ const declared = h.getp(h.getp(sdk.make_config(), "options"), "auth") == .object;
1256
+ var sent: usize = 0;
1257
+ var credentials: usize = 0;
1258
+ var bought: usize = 0;
1259
+
1260
+ var scenario: usize = 0;
1261
+ while (scenario < 3) : (scenario += 1) {
1262
+ const w = Wire.new();
1263
+ const sdkopts = h.omap();
1264
+ if (declared) h.setp(sdkopts, "auth", h.vnull());
1265
+ var fopts: Value = undefined;
1266
+ if (0 == scenario) {
1267
+ fopts = secrets_opts(h.ja(&.{Scripted.new("OFFKEY01", null).fn_val()}), vnull());
1268
+ } else if (1 == scenario) {
1269
+ h.setp(sdkopts, "apikey", h.vstr("OFFKEY02"));
1270
+ fopts = secrets_opts(h.ja(&.{}), vnull());
1271
+ } else {
1272
+ w.apistatus = &.{401};
1273
+ fopts = exchange_opts(h.ja(&.{Scripted.new("OFFREFRESH01", null).fn_val()}), vnull());
1274
+ }
1275
+ const client = secrets_client(w, sdkopts, fopts);
1276
+ _ = direct_probe(client);
1277
+
1278
+ try testing.expect(w.api().len == 1);
1279
+ try testing.expect(w.token_calls() == 0);
1280
+ try testing.expect(!carries(w.api()[0]));
1281
+ const sf = secrets_of(client) orelse return error.TestUnexpectedResult;
1282
+ try testing.expectEqualStrings("", sf.credential());
1283
+
1284
+ for (w.api()) |c| {
1285
+ if (carries(c)) credentials += 1;
1286
+ }
1287
+ sent += w.api().len;
1288
+ bought += w.token_calls();
1289
+ }
1290
+
1291
+ std.debug.print("\nsecrets: auth off by {s}: {d} request(s), {d} credential(s), {d} token(s)\n", .{
1292
+ if (declared) "option" else "config", sent, credentials, bought,
1293
+ });
1294
+ }
@@ -416,6 +416,39 @@ test "feature cache: does not cache non-2xx" {
416
416
  try testing.expect(cf.track.bypass == 2);
417
417
  }
418
418
 
419
+ var audio_sent: [2]Value = undefined;
420
+ var audio_n: usize = 0;
421
+
422
+ fn replyAudio(_: i64, _: *sdk.Context, _: Value) fh.E!Value {
423
+ const res = fh.fh_response(200, vnull(), h.jo(&.{.{ "content-type", h.vstr("audio/wav") }}));
424
+ if (audio_n < audio_sent.len) audio_sent[audio_n] = res;
425
+ audio_n += 1;
426
+ return res;
427
+ }
428
+
429
+ // The response's headers are the transport's own map; a rebuilt copy has another.
430
+ fn handedIs(r: fh.FhOpResult, i: usize) bool {
431
+ const res = r.ctx.response orelse return false;
432
+ if (i >= audio_n) return false;
433
+ const own = h.getp(audio_sent[i], "headers");
434
+ return res.headers == .object and own == .object and res.headers.object == own.object;
435
+ }
436
+
437
+ test "feature cache: a body that declares another type than JSON passes by, unread" {
438
+ if (!fh.fh_present(&.{"cache"})) return;
439
+ audio_n = 0;
440
+ const rec = fh.Recorder.new(replyAudio);
441
+ const f = sdk.CacheFeature.make();
442
+ const cf: *sdk.CacheFeature = @ptrCast(@alignCast(f.ptr));
443
+ const hh = fh.fh_make(rec.fetcher(), &.{.{ .feat = f, .options = vnull() }});
444
+ const a = hh.op(.{ .op = "load", .path = "/w/1/audio" });
445
+ const b = hh.op(.{ .op = "load", .path = "/w/1/audio" });
446
+ try testing.expect(rec.count() == 2);
447
+ try testing.expect(handedIs(a, 0));
448
+ try testing.expect(handedIs(b, 1));
449
+ try testing.expect(cf.track.bypass == 2);
450
+ }
451
+
419
452
  test "feature cache: refetches after ttl" {
420
453
  if (!fh.fh_present(&.{"cache"})) return;
421
454
  const clock = fh.FhClock.new();
@@ -3,7 +3,7 @@
3
3
  "package": 1
4
4
  },
5
5
  "name": "@voxgig/sdkgen",
6
- "version": "4.48.1",
6
+ "version": "4.49.1",
7
7
  "provides": {
8
8
  "target": [
9
9
  "c",