@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
@@ -41,7 +41,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
41
41
  import static org.junit.jupiter.api.Assertions.fail;
42
42
 
43
43
  import java.lang.reflect.Method;
44
+ import java.nio.charset.StandardCharsets;
44
45
  import java.util.ArrayList;
46
+ import java.util.Base64;
45
47
  import java.util.Collections;
46
48
  import java.util.LinkedHashMap;
47
49
  import java.util.List;
@@ -53,6 +55,7 @@ import java.util.function.Supplier;
53
55
  import org.junit.jupiter.api.Assumptions;
54
56
  import org.junit.jupiter.api.BeforeEach;
55
57
  import org.junit.jupiter.api.Test;
58
+ import org.junit.jupiter.api.TestInfo;
56
59
 
57
60
  import JAVAPACKAGE.core.Config;
58
61
  import JAVAPACKAGE.core.Feature;
@@ -80,6 +83,19 @@ public class SecretsFeatureTest {
80
83
  ? ((String) name).toLowerCase(java.util.Locale.ROOT) : "authorization";
81
84
  }
82
85
 
86
+ // The form prepareAuth writes, from the DEFAULT client's auth: under Basic
87
+ // the token is the key half of the encoded pair, and a prefix is
88
+ // space-joined.
89
+ static final Map<String, Object> CREDFORM = credform();
90
+
91
+ static Map<String, Object> credform() {
92
+ Object auth = ProjectNameSDK.testSDK(null, null).optionsMap().get("auth");
93
+ Map<String, Object> am = auth instanceof Map ? (Map<String, Object>) auth : Map.of();
94
+ Object prefix = am.get("prefix");
95
+ return Map.of("prefix", prefix instanceof String ? prefix : "",
96
+ "basic", Boolean.TRUE.equals(am.get("basic")));
97
+ }
98
+
83
99
  // ------------------------------------------------------------------
84
100
  // The recording transport: system.fetch for a LIVE client, scripting one
85
101
  // status per API call (the last repeating) and a token endpoint for the
@@ -89,13 +105,15 @@ public class SecretsFeatureTest {
89
105
  final String url;
90
106
  final String auth;
91
107
  final boolean has;
108
+ final String values;
92
109
  final String body;
93
110
  final Object redirect;
94
111
 
95
- Call(String url, String auth, boolean has, String body, Object redirect) {
112
+ Call(String url, String auth, boolean has, String values, String body, Object redirect) {
96
113
  this.url = url;
97
114
  this.auth = auth;
98
115
  this.has = has;
116
+ this.values = values;
99
117
  this.body = body;
100
118
  this.redirect = redirect;
101
119
  }
@@ -114,6 +132,7 @@ public class SecretsFeatureTest {
114
132
  public synchronized Object apply(String url, Map<String, Object> fetchdef) {
115
133
  String auth = "";
116
134
  boolean has = false;
135
+ List<String> values = new ArrayList<>();
117
136
  Object headers = fetchdef == null ? null : fetchdef.get("headers");
118
137
  if (headers instanceof Map) {
119
138
  Map<String, Object> hm = (Map<String, Object>) headers;
@@ -122,9 +141,13 @@ public class SecretsFeatureTest {
122
141
  Object v = hm.get(CREDHEADER);
123
142
  auth = v instanceof String ? (String) v : "";
124
143
  }
144
+ for (Object v : hm.values()) {
145
+ values.add(String.valueOf(v));
146
+ }
125
147
  }
126
148
  Object bodyRaw = fetchdef == null ? null : fetchdef.get("body");
127
- calls.add(new Call(url, auth, has, bodyRaw instanceof String ? (String) bodyRaw : "",
149
+ calls.add(new Call(url, auth, has, String.join("\n", values),
150
+ bodyRaw instanceof String ? (String) bodyRaw : "",
128
151
  fetchdef == null ? null : fetchdef.get("redirect")));
129
152
 
130
153
  if (url.endsWith("/" + tokenpath)) {
@@ -191,16 +214,22 @@ public class SecretsFeatureTest {
191
214
  // ------------------------------------------------------------------
192
215
  // Support.
193
216
 
194
- // The credential header carries the SPEC's credential prefix, which a
195
- // TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
196
- // `Bearer <token>`, an apiKey scheme the raw token. So assert on the
197
- // CREDENTIAL and let the prefix be whatever this SDK's API declares -
198
- // pinning the whole header value passes only for a prefix-less API, and
199
- // this file ships to every project that selects the feature.
217
+ // The header value prepareAuth writes for a token.
218
+ static String credential(String token, String secret) {
219
+ String value = Boolean.TRUE.equals(CREDFORM.get("basic"))
220
+ ? Base64.getEncoder().encodeToString((token + ":" + secret).getBytes(StandardCharsets.UTF_8))
221
+ : token;
222
+ String prefix = (String) CREDFORM.get("prefix");
223
+ return "".equals(prefix) ? value : prefix + " " + value;
224
+ }
225
+
200
226
  static void credentialIs(String header, String token) {
201
- String got = header == null ? "" : header;
202
- assertTrue(got.equals(token) || got.endsWith(" " + token),
203
- "expected the " + CREDHEADER + " header to carry " + token + ", got: \"" + got + "\"");
227
+ credentialIs(header, token, "");
228
+ }
229
+
230
+ static void credentialIs(String header, String token, String secret) {
231
+ assertEquals(credential(token, secret), header,
232
+ "expected the " + CREDHEADER + " header to carry " + token);
204
233
  }
205
234
 
206
235
  static Map<String, Object> map(Object... kv) {
@@ -380,8 +409,12 @@ public class SecretsFeatureTest {
380
409
 
381
410
  // Read from the config, never from a probe: an SDK whose config declares no
382
411
  // auth places no credential, and these tests assert on the one it places.
412
+ // The auth-off test asserts it places none.
383
413
  @BeforeEach
384
- public void placesCredential() {
414
+ public void placesCredential(TestInfo test) {
415
+ if (test.getTestMethod().map(m -> m.getName().startsWith("authOff_")).orElse(false)) {
416
+ return;
417
+ }
385
418
  Map<String, Object> options = Helpers.toMapAny(Config.sharedConfig().get("options"));
386
419
  Assumptions.assumeTrue(null != options && options.containsKey("auth"),
387
420
  "this SDK's config declares no auth, so it places no credential");
@@ -459,6 +492,17 @@ public class SecretsFeatureTest {
459
492
  "the shared options map must stay unwritten");
460
493
  }
461
494
 
495
+ @Test
496
+ public void active_aResolvedKeyIsPlacedAsPrepareAuthPlacesItTheSecretKept() {
497
+ Wire wire = new Wire();
498
+ Map<String, Object> opts = secretsOpts(List.of(memoryStore("envlike", "APIKEY", "ENVKEY04")));
499
+ opts.put("secret", "PASS01");
500
+ ProjectNameSDK client = liveClient(wire, opts);
501
+
502
+ driveEntityOp(client, wire);
503
+ credentialIs(wire.api().get(0).auth, "ENVKEY04", "PASS01");
504
+ }
505
+
462
506
  @Test
463
507
  public void active_customProviderObjectsAreAcceptedVerbatim() {
464
508
  Wire wire = new Wire();
@@ -994,6 +1038,21 @@ public class SecretsFeatureTest {
994
1038
  "the caller sees the successful retry: " + res.get("err"));
995
1039
  }
996
1040
 
1041
+ @Test
1042
+ public void exchange_aTokenBoughtOnARefusalIsPlacedAsPrepareAuthPlacesIt() {
1043
+ Wire wire = new Wire();
1044
+ wire.apistatus = new ArrayList<>(List.of(401, 200));
1045
+ Map<String, Object> opts = exchangeOpts();
1046
+ opts.put("secret", "PASS01");
1047
+ ProjectNameSDK client = liveClient(wire, opts);
1048
+
1049
+ client.direct(map("path", "/thing"));
1050
+
1051
+ assertEquals(2, wire.api().size(), "expected the request to be retried exactly once");
1052
+ credentialIs(wire.api().get(0).auth, "ACCESS01", "PASS01");
1053
+ credentialIs(wire.api().get(1).auth, "ACCESS02", "PASS01");
1054
+ }
1055
+
997
1056
  @Test
998
1057
  public void exchange_reachesTheRawPathsAndOnePurchaseServesMany() {
999
1058
  Wire wire = new Wire();
@@ -1077,19 +1136,33 @@ public class SecretsFeatureTest {
1077
1136
  assertEquals("test-access_token", requireFeature(client).credential());
1078
1137
  }
1079
1138
 
1080
- // Real listeners and the feature's own HTTP client: a stub follows nothing.
1081
- @Test
1082
- public void exchange_aRedirectAnsweringTheExchangeIsAFailureAndNothingFollowsIt() throws Exception {
1083
- List<String> elsewhere = Collections.synchronizedList(new ArrayList<>());
1084
- List<String> followed = Collections.synchronizedList(new ArrayList<>());
1085
- Object[] answer = new Object[] {307, ""};
1139
+ // Two real listeners and the feature's own HTTP client: a stub follows
1140
+ // nothing. The origin answers the exchange as `answer` says and records
1141
+ // what else it gets.
1142
+ private static final class Origins {
1143
+ final List<String> elsewhere = Collections.synchronizedList(new ArrayList<>());
1144
+ final List<String> followed = Collections.synchronizedList(new ArrayList<>());
1145
+ final java.util.concurrent.atomic.AtomicInteger exchanged =
1146
+ new java.util.concurrent.atomic.AtomicInteger();
1147
+ volatile Object[] answer = new Object[] {307, ""};
1148
+ String origin;
1149
+ String base;
1150
+ String away;
1151
+ }
1152
+
1153
+ private interface OriginsRun {
1154
+ void run(Origins o) throws Exception;
1155
+ }
1156
+
1157
+ private static void withOrigins(OriginsRun run) throws Exception {
1158
+ Origins o = new Origins();
1086
1159
 
1087
1160
  com.sun.net.httpserver.HttpServer other = com.sun.net.httpserver.HttpServer.create(
1088
1161
  new java.net.InetSocketAddress("127.0.0.1", 0), 0);
1089
1162
  other.createContext("/", (ex) -> {
1090
1163
  String body = new String(ex.getRequestBody().readAllBytes(),
1091
1164
  java.nio.charset.StandardCharsets.UTF_8);
1092
- elsewhere.add(ex.getRequestMethod() + " " + ex.getRequestURI().getRawPath() + " " + body);
1165
+ o.elsewhere.add(ex.getRequestMethod() + " " + ex.getRequestURI().getRawPath() + " " + body);
1093
1166
  byte[] out = "{\"access_token\":\"ELSEWHERE01\"}".getBytes();
1094
1167
  ex.getResponseHeaders().set("Content-Type", "application/json");
1095
1168
  ex.sendResponseHeaders(200, out.length);
@@ -1099,15 +1172,19 @@ public class SecretsFeatureTest {
1099
1172
  com.sun.net.httpserver.HttpServer origin = com.sun.net.httpserver.HttpServer.create(
1100
1173
  new java.net.InetSocketAddress("127.0.0.1", 0), 0);
1101
1174
  origin.createContext("/", (ex) -> {
1102
- ex.getRequestBody().readAllBytes();
1103
- String path = ex.getRequestURI().getRawPath();
1175
+ String body = new String(ex.getRequestBody().readAllBytes(),
1176
+ java.nio.charset.StandardCharsets.UTF_8);
1177
+ String query = ex.getRequestURI().getRawQuery();
1178
+ String path = ex.getRequestURI().getRawPath() + (query == null ? "" : "?" + query);
1104
1179
  if ("/api/auth/token".equals(path)) {
1180
+ o.exchanged.incrementAndGet();
1181
+ Object[] answer = o.answer;
1105
1182
  ex.getResponseHeaders().set("Location", (String) answer[1]);
1106
1183
  ex.sendResponseHeaders((int) answer[0], -1);
1107
1184
  ex.close();
1108
1185
  return;
1109
1186
  }
1110
- followed.add(ex.getRequestMethod() + " " + path);
1187
+ o.followed.add((ex.getRequestMethod() + " " + path + " " + body).trim());
1111
1188
  byte[] out = "{\"access_token\":\"MOVED01\"}".getBytes();
1112
1189
  ex.getResponseHeaders().set("Content-Type", "application/json");
1113
1190
  ex.sendResponseHeaders(200, out.length);
@@ -1117,37 +1194,59 @@ public class SecretsFeatureTest {
1117
1194
  other.start();
1118
1195
  origin.start();
1119
1196
 
1120
- String base = "http://127.0.0.1:" + origin.getAddress().getPort() + "/api";
1121
- String away = "http://127.0.0.1:" + other.getAddress().getPort() + "/api/auth/token";
1197
+ o.origin = "http://127.0.0.1:" + origin.getAddress().getPort();
1198
+ o.base = o.origin + "/api";
1199
+ o.away = "http://127.0.0.1:" + other.getAddress().getPort() + "/api/auth/token";
1200
+
1201
+ try {
1202
+ run.run(o);
1203
+ }
1204
+ finally {
1205
+ origin.stop(0);
1206
+ other.stop(0);
1207
+ }
1208
+ }
1209
+
1210
+ private static ProjectNameSDK redirectClient(Origins o, Map<String, Object> exchange) {
1211
+ Map<String, Object> x = map("active", true, "refresh", "REFRESH01");
1212
+ if (exchange != null) {
1213
+ x.putAll(exchange);
1214
+ }
1215
+ Map<String, Object> opts = map(
1216
+ "base", o.base,
1217
+ "allow", map("op", "direct,graphql,load,list"),
1218
+ "feature", map("secrets", map(
1219
+ "active", true,
1220
+ "name", "refresh_token",
1221
+ "exchange", x)));
1222
+ return withSecrets(opts, ProjectNameSDK::new);
1223
+ }
1122
1224
 
1225
+ // The error message of a direct call, or the result when there is none.
1226
+ private static String directMessage(Map<String, Object>[] out, ProjectNameSDK client) {
1123
1227
  try {
1124
- Object[][] answers = {{307, away}, {308, away}, {302, away}, {307, "/api/moved/token"}};
1228
+ Map<String, Object> res = client.direct(map("path", "/thing"));
1229
+ out[0] = res;
1230
+ Object err = res == null ? null : res.get("err");
1231
+ return String.valueOf(err instanceof Throwable ? ((Throwable) err).getMessage() : res);
1232
+ }
1233
+ catch (RuntimeException e) {
1234
+ return String.valueOf(e.getMessage());
1235
+ }
1236
+ }
1237
+
1238
+ @Test
1239
+ public void exchange_aRedirectAnsweringTheExchangeIsAFailureAndNothingFollowsIt() throws Exception {
1240
+ withOrigins((o) -> {
1241
+ Object[][] answers = {{307, o.away}, {308, o.away}, {302, o.away}, {307, "/api/moved/token"}};
1125
1242
  List<String> allowed = new ArrayList<>();
1126
1243
  int refused = 0;
1127
1244
 
1128
1245
  for (Object[] a : answers) {
1129
- answer[0] = a[0];
1130
- answer[1] = a[1];
1131
- Map<String, Object> opts = map(
1132
- "base", base,
1133
- "allow", map("op", "direct,graphql,load,list"),
1134
- "feature", map("secrets", map(
1135
- "active", true,
1136
- "name", "refresh_token",
1137
- "exchange", map("active", true, "refresh", "REFRESH01"))));
1138
- ProjectNameSDK client = withSecrets(opts, ProjectNameSDK::new);
1139
-
1140
- String message;
1141
- try {
1142
- Map<String, Object> res = client.direct(map("path", "/thing"));
1143
- Object err = res == null ? null : res.get("err");
1144
- message = String.valueOf(err instanceof Throwable ? ((Throwable) err).getMessage() : res);
1145
- }
1146
- catch (RuntimeException e) {
1147
- message = String.valueOf(e.getMessage());
1148
- }
1246
+ o.answer = a;
1247
+ String message = directMessage(new Map[1], redirectClient(o, null));
1149
1248
 
1150
- if (message.contains("token exchange failed: " + a[0] + " from " + base + "/auth/token")) {
1249
+ if (message.contains("token exchange failed: " + a[0] + " from " + o.base + "/auth/token")) {
1151
1250
  refused++;
1152
1251
  }
1153
1252
  else {
@@ -1156,16 +1255,131 @@ public class SecretsFeatureTest {
1156
1255
  }
1157
1256
 
1158
1257
  System.out.println("secrets: exchange refused " + refused + " redirect(s), "
1159
- + (elsewhere.size() + followed.size()) + " followed");
1258
+ + (o.elsewhere.size() + o.followed.size()) + " followed");
1160
1259
 
1161
- assertEquals(List.of(), elsewhere, "the exchange reached another origin");
1162
- assertEquals(List.of(), followed, "a request followed a refused exchange");
1260
+ assertEquals(List.of(), o.elsewhere, "the exchange reached another origin");
1261
+ assertEquals(List.of(), o.followed, "a request followed a refused exchange");
1163
1262
  assertEquals(List.of(), allowed, "a redirect was not a failed exchange");
1164
1263
  assertEquals(answers.length, refused);
1264
+ });
1265
+ }
1266
+
1267
+ @Test
1268
+ public void exchange_withTheOptionARedirectOnTheExchangesOwnOriginIsFollowedAndNoOther() throws Exception {
1269
+ withOrigins((o) -> {
1270
+ String sent = " {\"refresh_token\":\"REFRESH01\"}";
1271
+ // Each case: the status, the Location, and the path the exchange is
1272
+ // asked again at, or null where the redirect is refused.
1273
+ Object[][] cases = {
1274
+ {307, "/api/moved/token", "/api/moved/token"},
1275
+ {308, o.origin + "/api/moved/token", "/api/moved/token"},
1276
+ // A Location of a query alone keeps the path that answered.
1277
+ {307, "?step=2", "/api/auth/token?step=2"},
1278
+ {307, o.away, null},
1279
+ {302, "/api/moved/token", null},
1280
+ {307, "/api/auth/token", null},
1281
+ };
1282
+
1283
+ List<String> wrong = new ArrayList<>();
1284
+ int followed = 0;
1285
+ int refused = 0;
1286
+ int looped = 0;
1287
+
1288
+ for (Object[] c : cases) {
1289
+ o.answer = new Object[] {c[0], c[1]};
1290
+ o.followed.clear();
1291
+ o.exchanged.set(0);
1292
+
1293
+ Map<String, Object>[] out = new Map[1];
1294
+ String message = directMessage(out,
1295
+ redirectClient(o, map("redirect", "same-origin")));
1296
+ String seen = c[0] + " to " + c[1] + ": " + message + " " + o.followed;
1297
+
1298
+ if (c[2] != null) {
1299
+ if (out[0] != null && Boolean.TRUE.equals(out[0].get("ok"))
1300
+ && 2 == o.followed.size() && ("POST " + c[2] + sent).equals(o.followed.get(0))) {
1301
+ followed++;
1302
+ }
1303
+ else {
1304
+ wrong.add(seen);
1305
+ }
1306
+ }
1307
+ else if (o.followed.isEmpty() && message.contains(
1308
+ "token exchange failed: " + c[0] + " from " + o.base + "/auth/token")) {
1309
+ refused++;
1310
+ }
1311
+ else {
1312
+ wrong.add(seen);
1313
+ }
1314
+
1315
+ if ("/api/auth/token".equals(c[1])) {
1316
+ looped = o.exchanged.get();
1317
+ }
1318
+ }
1319
+
1320
+ System.out.println("secrets: exchange opt-in followed " + followed
1321
+ + " redirect(s) on its own origin, refused " + refused + ", "
1322
+ + o.elsewhere.size() + " elsewhere");
1323
+
1324
+ assertEquals(List.of(), o.elsewhere, "the exchange reached another origin");
1325
+ assertEquals(List.of(), wrong);
1326
+ assertEquals(3, followed);
1327
+ assertEquals(3, refused);
1328
+ // A redirect to itself stops at the hop limit.
1329
+ assertEquals(6, looped);
1330
+ });
1331
+ }
1332
+
1333
+ // ------------------------------------------------------------------
1334
+ // Off by the config where it declares no auth, else by `auth: null`:
1335
+ // either way the feature places no credential, buys no token and retries
1336
+ // nothing.
1337
+
1338
+ @Test
1339
+ public void authOff_placesNoCredentialAndBuysNoToken() {
1340
+ Map<String, Object> options = Helpers.toMapAny(Config.sharedConfig().get("options"));
1341
+ boolean declared = null != options && options.containsKey("auth");
1342
+ List<String> secrets = List.of("OFFKEY01", "OFFKEY02", "OFFREFRESH01", "ACCESS01");
1343
+ java.util.function.Predicate<Call> carries = (call) ->
1344
+ call.has || secrets.stream().anyMatch(call.values::contains);
1345
+ List<Call> sent = new ArrayList<>();
1346
+ int bought = 0;
1347
+
1348
+ Object[][] scenarios = {
1349
+ {"a key the chain resolves", map(), secretsOpts(List.of(
1350
+ memoryStore("envlike", "APIKEY", "OFFKEY01"))), List.of(200)},
1351
+ {"an explicit apikey", map("apikey", "OFFKEY02"), secretsOpts(List.of()), List.of(200)},
1352
+ {"a refusal", map(), secretsOpts(List.of(
1353
+ memoryStore("envlike", "REFRESH_TOKEN", "OFFREFRESH01")),
1354
+ "name", "refresh_token", "exchange", map("active", true)), List.of(401)},
1355
+ };
1356
+
1357
+ for (Object[] scenario : scenarios) {
1358
+ String what = (String) scenario[0];
1359
+ Wire wire = new Wire();
1360
+ wire.apistatus = new ArrayList<>((List<Integer>) scenario[3]);
1361
+ Map<String, Object> opts = new LinkedHashMap<>((Map<String, Object>) scenario[1]);
1362
+ opts.putAll((Map<String, Object>) scenario[2]);
1363
+ if (declared) {
1364
+ opts.put("auth", null);
1365
+ }
1366
+ ProjectNameSDK client = liveClient(wire, opts);
1367
+
1368
+ client.direct(map("path", "/thing"));
1369
+
1370
+ assertEquals(1, wire.api().size(), what + ": a request was retried with auth off");
1371
+ assertEquals(0, wire.token().size(), what + ": a token was bought with auth off");
1372
+ assertFalse(carries.test(wire.api().get(0)),
1373
+ what + ": a credential was sent with auth off: " + wire.leak());
1374
+ assertEquals("", requireFeature(client).credential(),
1375
+ what + ": the feature resolved a credential with auth off");
1376
+
1377
+ sent.addAll(wire.api());
1378
+ bought += wire.token().size();
1165
1379
  }
1166
- finally {
1167
- origin.stop(0);
1168
- other.stop(0);
1169
- }
1380
+
1381
+ System.out.println("secrets: auth off by " + (declared ? "option" : "config") + ": "
1382
+ + sent.size() + " request(s), " + sent.stream().filter(carries).count()
1383
+ + " credential(s), " + bought + " token(s)");
1170
1384
  }
1171
1385
  }
@@ -28,7 +28,7 @@ import JAVAPACKAGE.core.SdkError;
28
28
  import JAVAPACKAGE.utility.struct.Struct;
29
29
 
30
30
  @SuppressWarnings({"unchecked"})
31
- final class Fetcher {
31
+ public final class Fetcher {
32
32
 
33
33
  private Fetcher() {}
34
34
 
@@ -103,6 +103,22 @@ final class Fetcher {
103
103
  : Clean.cleanSecret(ctx, name, value);
104
104
  }
105
105
 
106
+ // A reference of a query alone keeps the path, as RFC 3986 resolves it;
107
+ // URI.resolve follows RFC 2396 there and drops the last segment.
108
+ public static URI resolveLocation(URI from, String location) {
109
+ String loc = location.trim();
110
+ if (loc.startsWith("?")) {
111
+ String base = from.toString();
112
+ int cut = base.indexOf('?');
113
+ int hash = base.indexOf('#');
114
+ if (cut < 0 || (0 <= hash && hash < cut)) {
115
+ cut = hash;
116
+ }
117
+ return URI.create((cut < 0 ? base : base.substring(0, cut)) + loc);
118
+ }
119
+ return from.resolve(URI.create(loc));
120
+ }
121
+
106
122
  // Left to follow, the JDK client sends another origin every header it does
107
123
  // not know for a credential, and a 307's body with them. Followed here
108
124
  // instead: each hop passes the GET-only guard; past REDIRECT_MAX hops, or
@@ -127,7 +143,7 @@ final class Fetcher {
127
143
  URI next;
128
144
  try {
129
145
  from = URI.create(url);
130
- next = from.resolve(URI.create(((String) location).trim()));
146
+ next = resolveLocation(from, (String) location);
131
147
  }
132
148
  catch (RuntimeException e) {
133
149
  return res;
@@ -12,7 +12,7 @@ import JAVAPACKAGE.utility.struct.Struct;
12
12
 
13
13
  // The media types a point declares: `response` (the model's `rs`) for the
14
14
  // Accept header, and `body` (the model's `rb`) for the request body.
15
- final class Media {
15
+ public final class Media {
16
16
 
17
17
  // The data key holding a raw request body. Like `$action`, it can never be
18
18
  // a declared argument name.
@@ -20,7 +20,7 @@ final class Media {
20
20
 
21
21
  private Media() {}
22
22
 
23
- static boolean isJsonMedia(Object media) {
23
+ public static boolean isJsonMedia(Object media) {
24
24
  String m = (media instanceof String ? (String) media : "").split(";", 2)[0]
25
25
  .trim().toLowerCase(Locale.ROOT);
26
26
  return "application/json".equals(m) || "text/json".equals(m) || m.endsWith("+json");
@@ -1,6 +1,7 @@
1
1
 
2
2
 
3
3
  const { BaseFeature } = require('../base/BaseFeature')
4
+ const { headerValue, isJsonMedia } = require('../../utility/MediaUtility')
4
5
 
5
6
 
6
7
  // Response caching for safe (read) requests. Wraps the active transport and
@@ -61,6 +62,12 @@ class CacheFeature extends BaseFeature {
61
62
 
62
63
  const res = await inner(ctx, url, fetchdef)
63
64
 
65
+ // A body that declares another type than JSON passes by, unread.
66
+ if (this._cacheable(res) && !this._json(res)) {
67
+ this._track('bypass')
68
+ return res
69
+ }
70
+
64
71
  if (this._cacheable(res)) {
65
72
  const snapshot = await this._snapshot(res)
66
73
 
@@ -92,6 +99,12 @@ class CacheFeature extends BaseFeature {
92
99
  }
93
100
 
94
101
 
102
+ _json(res) {
103
+ const type = headerValue(res.headers, 'content-type')
104
+ return '' === type || isJsonMedia(type)
105
+ }
106
+
107
+
95
108
  async _snapshot(res) {
96
109
  let data = undefined
97
110
  let text = undefined