@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
@@ -63,6 +63,8 @@
63
63
 
64
64
  (def ^:private ERROR-CODE "secrets_provider")
65
65
 
66
+ (def ^:private REDIRECT-MAX 5)
67
+
66
68
 
67
69
  ;; ---------------------------------------------------------------------------
68
70
  ;; Option reading. Options arrive as vendored struct nodes (LinkedHashMap /
@@ -169,6 +171,22 @@
169
171
  (let [o (liveopts fa)] (when (vs/ismap o) (vs/getprop o "auth"))))
170
172
 
171
173
 
174
+ ;; u-prepare-auth's decision: auth the config declares, not suppressed by
175
+ ;; `auth: nil`.
176
+ (defn- authon? [fa]
177
+ (and (st fa :declared) (some? (auth-of fa))))
178
+
179
+
180
+ ;; Under Basic the token is the key half, as apikey is: u-prepare-auth's pair.
181
+ (defn- pair! [fa token]
182
+ (let [secret (vs/getprop (liveopts fa) "secret")
183
+ joined (.encodeToString (java.util.Base64/getEncoder)
184
+ (.getBytes (str token ":" (if (string? secret) secret ""))
185
+ "UTF-8"))]
186
+ (register! fa joined)
187
+ joined))
188
+
189
+
172
190
  ;; The resolved credential ("" when none). Callers and tests read it here
173
191
  ;; rather than from the options map, which this feature never mutates.
174
192
  (defn credential [fa] (or (st fa :cred) ""))
@@ -198,6 +216,25 @@
198
216
  ;; The access-token exchange.
199
217
  ;; ---------------------------------------------------------------------------
200
218
 
219
+ (defn- origin [^java.net.URI u]
220
+ (let [scheme (str/lower-case (str (.getScheme u)))
221
+ port (.getPort u)]
222
+ [scheme (str/lower-case (str (.getHost u)))
223
+ (if (neg? port) (get {"http" 80 "https" 443} scheme -1) (long port))]))
224
+
225
+ ;; Where a redirect keeping method and body (307 or 308) points, on the
226
+ ;; exchange's own origin. Any other would drop the body or leave the origin.
227
+ (defn- moved [url at res]
228
+ (let [status (core/to-int (vs/getprop res "status"))
229
+ location (vs/getprop (vs/getprop res "headers") "location")]
230
+ (when (and (contains? #{307 308} status) (string? location) (seq location))
231
+ (try
232
+ (let [next (core/resolve-location (java.net.URI/create at) location)]
233
+ (when (= (origin next) (origin (java.net.URI/create url)))
234
+ (str next)))
235
+ (catch Exception _ nil)))))
236
+
237
+
201
238
  (defn- buy-once [fa]
202
239
  (let [x (st fa :exchange)
203
240
  refresh (st fa :refresh)]
@@ -228,11 +265,21 @@
228
265
  ;; a configured request-field name) carrying a quote, backslash or
229
266
  ;; newline must arrive as that literal value, not as broken JSON.
230
267
  body (vs/jsonify (vs/jm (:request x) refresh) (vs/jm "indent" 0))
231
- ;; Never followed: a redirect would carry the refresh token on.
232
- [res err] (fetch url (vs/jm "method" (:method x)
233
- "headers" (vs/jm "content-type" "application/json")
234
- "body" body
235
- "redirect" "manual"))]
268
+ ;; Never followed by the client: a redirect would carry the refresh token on.
269
+ fetchdef (vs/jm "method" (:method x)
270
+ "headers" (vs/jm "content-type" "application/json")
271
+ "body" body
272
+ "redirect" "manual")
273
+ ;; By option alone, and only while the exchange stays on its own origin.
274
+ [res err] (loop [at url
275
+ answer (fetch url fetchdef)
276
+ hops 0]
277
+ (let [next (when (and (= "same-origin" (:redirect x)) (< hops REDIRECT-MAX)
278
+ (nil? (second answer)))
279
+ (moved url at (first answer)))]
280
+ (if next
281
+ (recur next (fetch next fetchdef) (inc hops))
282
+ answer)))]
236
283
 
237
284
  (when (some? err)
238
285
  (throw (sekreto/sekretoerror
@@ -284,7 +331,7 @@
284
331
  ;; boolean is the whole of what resolve! needs to tell a cacheable HIT from
285
332
  ;; a miss it must not keep.
286
333
  (defn- resolve-once [fa]
287
- (if-let [s (st fa :sek)]
334
+ (if-let [s (and (authon? fa) (st fa :sek))]
288
335
  ;; tryget: nil for a MISS, THROWS for a provider ERROR.
289
336
  (let [found (sekreto/tryget s (st fa :secretname))]
290
337
  (register! fa found)
@@ -313,16 +360,6 @@
313
360
  (cond
314
361
  (seq apikey) true
315
362
 
316
- ;; `auth: nil` is the documented way to send NO credential,
317
- ;; and a purchase is a credential-bearing call: the refresh
318
- ;; token goes to the token endpoint in the request body.
319
- ;; with-refresh honours suppression for the RETRY, but it runs
320
- ;; after this - by then the refresh token has already left the
321
- ;; process, and no later check can call it back. The
322
- ;; suppression has to be honoured here, before the first
323
- ;; purchase, or it only ever half-held.
324
- (nil? (auth-of fa)) false
325
-
326
363
  :else
327
364
  ;; No starting access token: buy one now. A failure here is a
328
365
  ;; failure of the operation - fail closed.
@@ -377,16 +414,18 @@
377
414
  name (if (and (string? given) (not= "" given))
378
415
  (.toLowerCase ^String given java.util.Locale/ROOT)
379
416
  core/HEADER-AUTH)]
380
- (if (nil? (auth-of fa))
417
+ (if (not (authon? fa))
381
418
  ;; Suppressed auth means NO header, the same answer u-prepare-auth
382
- ;; gives. Reached defensively - nothing calls this with auth
383
- ;; suppressed - but this is the function that writes the
384
- ;; credential, so it is where the rule has to hold.
419
+ ;; gives.
385
420
  (vs/delprop headers name)
386
- (let [prefix (let [p (vs/getpath (liveopts fa) "auth.prefix")]
421
+ (let [value (if (and (st fa :basic)
422
+ (= true (vs/getpath (liveopts fa) "auth.basic")))
423
+ (pair! fa token)
424
+ token)
425
+ prefix (let [p (vs/getpath (liveopts fa) "auth.prefix")]
387
426
  (if (string? p) p ""))]
388
427
  (.put ^java.util.Map headers name
389
- (if (= "" prefix) token (str prefix " " token)))))))))
428
+ (if (= "" prefix) value (str prefix " " value)))))))))
390
429
 
391
430
 
392
431
  (defn- spent? [fa res]
@@ -400,11 +439,9 @@
400
439
  ;; Buy a token and try the request again when the API says the current one
401
440
  ;; is spent.
402
441
  (defn- with-refresh [fa ctx url fetchdef inner]
403
- ;; `auth: nil` is the documented way to send NO credential. A refusal of
404
- ;; a deliberately unauthenticated request is not an expired token and
405
- ;; cannot be fixed by buying one - retrying would transmit exactly the
406
- ;; credential the caller withheld.
407
- (if (nil? (auth-of fa))
442
+ ;; A refusal of an unauthenticated request is not an expired token, and
443
+ ;; retrying would transmit the credential that auth off withholds.
444
+ (if (not (authon? fa))
408
445
  (inner ctx url fetchdef)
409
446
  (let [max (:retries (st fa :exchange))]
410
447
  (loop [attempt 0]
@@ -468,7 +505,8 @@
468
505
  (if (and (vs/islist s) (pos? (vs/size s)))
469
506
  (mapv (fn [v] (core/to-int v)) (vec s))
470
507
  [401]))
471
- :retries (let [r (vs/getprop xopts "retries")] (if (number? r) (long r) 1))})))
508
+ :retries (let [r (vs/getprop xopts "retries")] (if (number? r) (long r) 1))
509
+ :redirect (if (= "same-origin" (vs/getprop xopts "redirect")) "same-origin" "refuse")})))
472
510
 
473
511
 
474
512
  (defn- init! [fa ctx options]
@@ -479,6 +517,13 @@
479
517
  :liveopts (core/oget ctx :options)
480
518
  :active (= true (vs/getprop options "active")))
481
519
 
520
+ ;; What u-prepare-auth was generated from: the config declares auth only
521
+ ;; where it places a credential, and Basic only where it composes one.
522
+ (let [declared (vs/getpath (core/oget ctx :config) "options.auth")]
523
+ (swap! fa assoc
524
+ :declared (vs/ismap declared)
525
+ :basic (and (vs/ismap declared) (= true (vs/getprop declared "basic")))))
526
+
482
527
  (when (st fa :active)
483
528
  (let [secretname (sopt options "name" "apikey")
484
529
  cache (not= false (vs/getprop options "cache"))
@@ -541,7 +586,7 @@
541
586
  ([] (secrets-feature nil))
542
587
  ([plugins]
543
588
  (let [fa (atom {:name "secrets" :active false :version "0.1.0" :_options nil
544
- :client nil :options (vs/jm) :liveopts nil
589
+ :client nil :options (vs/jm) :liveopts nil :declared false :basic false
545
590
  :plugins (vec (or plugins []))
546
591
  :secretname "apikey" :cache true :sek nil
547
592
  :exchange nil :refresh "" :cred "" :initerr nil :resolved false
@@ -693,6 +693,42 @@
693
693
  out)
694
694
  :else (clean-string cfg (str val))))))
695
695
 
696
+ ;; clean-snapshot's walk without the masking, for a record serialised with
697
+ ;; clean off: jsonify renders neither an atom nor a keyword key.
698
+ (defn- plain-snapshot [val depth seen]
699
+ (cond
700
+ (nil? val) nil
701
+ (fn? val) DROP
702
+ (or (string? val) (number? val) (boolean? val) (keyword? val) (symbol? val) (char? val)) val
703
+ (or (<= CLEAN-MAXDEPTH depth) (some #(identical? % val) seen)) CLEAN-CIRCULAR
704
+ (bytes? val) (vec val)
705
+ (instance? java.io.InputStream val) (str val)
706
+ :else
707
+ (let [seen (conj seen val)]
708
+ (cond
709
+ (entity-instance? val) (plain-snapshot (deref (:_data val)) (inc depth) seen)
710
+ (instance? clojure.lang.IAtom val) (plain-snapshot (deref val) (inc depth) seen)
711
+ (instance? Throwable val)
712
+ (let [out (vs/jm "message" (str (.getMessage ^Throwable val)))]
713
+ (when (instance? clojure.lang.IExceptionInfo val)
714
+ (let [d (plain-snapshot (ex-data val) (inc depth) seen)]
715
+ (when-not (identical? DROP d) (.put ^java.util.Map out "data" d))))
716
+ out)
717
+ (instance? java.util.Map val)
718
+ (let [out (vs/jm)]
719
+ (doseq [^java.util.Map$Entry e (.entrySet ^java.util.Map val)]
720
+ (let [v (plain-snapshot (.getValue e) (inc depth) seen)]
721
+ (when-not (identical? DROP v)
722
+ (.put ^java.util.Map out (keyname (.getKey e)) v))))
723
+ out)
724
+ (or (instance? java.util.Collection val) (sequential? val))
725
+ (let [out (vs/jt)]
726
+ (doseq [v (seq val)]
727
+ (let [c (plain-snapshot v (inc depth) seen)]
728
+ (.add ^java.util.List out (if (identical? DROP c) nil c))))
729
+ out)
730
+ :else (str val)))))
731
+
696
732
  ;; An error map is immutable, so "in place" is a cleaned copy carrying the
697
733
  ;; same metadata (the context stays reachable through err-ctx). Its own
698
734
  ;; strings stay strings: the message is what ex-info is given.
@@ -737,18 +773,24 @@
737
773
  ;; strings, so a keyword-keyed map would serialise as nulls.
738
774
  (defn err->data [e]
739
775
  (when (sdk-error? e)
740
- (u-clean (err-ctx e) (into {} (map (fn [[k v]] [(keyname k) v]) e)))))
776
+ (let [data (into {} (map (fn [[k v]] [(keyname k) v]) e))
777
+ out (u-clean (err-ctx e) data)]
778
+ (if (identical? out data) (plain-snapshot data 0 []) out))))
741
779
 
742
780
  ;; The serialised context leaves the pipeline (a logger, an error dump), so
743
781
  ;; it is the cleaned record; the live fields stay raw for the pipeline's use.
782
+ ;; A clean that hands the record back unchanged (clean off) leaves live atoms,
783
+ ;; so the record is copied plain, as the other targets' serialisers show it.
744
784
  (defn ctx->data [ctx]
745
785
  (let [m (deref ctx)
746
786
  u (:utility m)
747
787
  clean (or (when (instance? clojure.lang.IDeref u) (:clean (deref u))) u-clean)
748
- entity (:entity m)]
749
- (clean ctx (vs/jm "id" (:id m) "op" (:op m) "spec" (:spec m)
750
- "entity" (when (map? entity) (entity-get-name entity))
751
- "result" (:result m) "response" (:response m) "meta" (:meta m)))))
788
+ entity (:entity m)
789
+ data (vs/jm "id" (:id m) "op" (:op m) "spec" (:spec m)
790
+ "entity" (when (map? entity) (entity-get-name entity))
791
+ "result" (:result m) "response" (:response m) "meta" (:meta m))
792
+ out (clean ctx data)]
793
+ (if (identical? out data) (plain-snapshot data 0 []) out)))
752
794
 
753
795
  (defmethod print-method ::context [ctx ^java.io.Writer w]
754
796
  (.write w (str "#sdk/context " (vs/jsonify (ctx->data ctx) (vs/jm "indent" 0)))))
@@ -2019,6 +2061,13 @@
2019
2061
  (boolean (or (contains? CREDENTIAL-HEADERS (.toLowerCase (str name) java.util.Locale/ROOT))
2020
2062
  ((if (fn? secret) secret u-clean-secret) ctx name value)))))
2021
2063
 
2064
+ ;; Where a Location points from `from`, as RFC 3986 resolves it: URI.resolve
2065
+ ;; drops the last path segment for a Location of a query alone.
2066
+ (defn resolve-location ^URI [^URI from ^String location]
2067
+ (if (str/starts-with? location "?")
2068
+ (URI/create (str (first (str/split (str from) #"[?#]" 2)) location))
2069
+ (.resolve from (URI/create location))))
2070
+
2022
2071
  ;; The request a followed redirect sends next, or nil where the response is the
2023
2072
  ;; answer. As the client rewrote it: a 303, and a 301 or 302 answering a POST,
2024
2073
  ;; become a GET without the body. A body is not sent on to another origin.
@@ -2027,7 +2076,7 @@
2027
2076
  location (mget (vs/getprop res "headers") "location")
2028
2077
  from (URI/create url)
2029
2078
  ^URI to (when (and (contains? REDIRECT-STATUS status) (string? location))
2030
- (try (.resolve from (URI/create location)) (catch Exception _ nil)))
2079
+ (try (resolve-location from location) (catch Exception _ nil)))
2031
2080
  scheme (when to (.getScheme to))]
2032
2081
  ;; As the client's NORMAL policy: never from https to http.
2033
2082
  (when (and scheme (or (.equalsIgnoreCase ^String scheme (.getScheme from))
@@ -481,6 +481,14 @@
481
481
  cacheable? (fn [res] (and (vs/ismap res)
482
482
  (not (true? (vs/getprop res "unreadable")))
483
483
  (let [s (vs/getprop res "status")] (and (number? s) (>= s 200) (< s 300)))))
484
+ json? (fn [res]
485
+ (let [h (vs/getprop res "headers")
486
+ type (or (when (vs/ismap h)
487
+ (some (fn [item] (when (= "content-type" (str/lower-case (str (vs/getprop item 0))))
488
+ (str (vs/getprop item 1))))
489
+ (or (vs/items h) [])))
490
+ "")]
491
+ (or (= "" type) (core/json-media? type))))
484
492
  snapshot (fn [res]
485
493
  (let [jf (vs/getprop res "json")
486
494
  data (when (fn? jf) (try (jf) (catch Throwable _ nil)))
@@ -509,14 +517,20 @@
509
517
  (if (and (some? hit) (> (mget hit "expiry") now))
510
518
  (do (track! "hit") [(replay (mget hit "snapshot")) nil])
511
519
  (let [[res err] (inner ctx url fetchdef)]
512
- (if (and (nil? err) (cacheable? res))
520
+ (cond
521
+ ;; A body that declares another type than JSON passes by, unread.
522
+ (and (nil? err) (cacheable? res) (not (json? res)))
523
+ (do (track! "bypass") [res err])
524
+
525
+ (and (nil? err) (cacheable? res))
513
526
  (let [snap (snapshot res)
514
527
  ttl (opt fa "ttl" 5000)]
515
528
  (evict!)
516
529
  (swap! fa update :store assoc key (vs/jm "expiry" (+ now ttl) "snapshot" snap))
517
530
  (track! "miss")
518
531
  [(replay snap) nil])
519
- (do (track! "bypass") [res err]))))))))]
532
+
533
+ :else (do (track! "bypass") [res err]))))))))]
520
534
  (swap! fa assoc
521
535
  "init" (fn [ctx options]
522
536
  (swap! fa assoc :client (core/oget ctx :client)
@@ -83,6 +83,9 @@
83
83
  n (inc (count @calls))]
84
84
  (swap! calls conj {:url url
85
85
  :auth (when (some? auth) (str auth))
86
+ :values (if (vs/ismap headers)
87
+ (str/join "\n" (map str (vals headers)))
88
+ "")
86
89
  :method (vs/getprop fetchdef "method")
87
90
  :body (vs/getprop fetchdef "body")
88
91
  :redirect (vs/getprop fetchdef "redirect")})
@@ -153,11 +156,12 @@
153
156
  ;; so core/u-fetcher really does call system.fetch.
154
157
  ;; ---------------------------------------------------------------------------
155
158
 
156
- (defn- live-sdk [stub & {:keys [apikey auth-nil secrets]}]
159
+ (defn- live-sdk [stub & {:keys [apikey auth-nil secret secrets]}]
157
160
  (let [opts (vs/jm "base" BASE
158
161
  "allow" (vs/jm "op" ALLOWOP)
159
162
  "system" (vs/jm "fetch" (:fetch stub)))]
160
163
  (when apikey (.put ^java.util.Map opts "apikey" apikey))
164
+ (when secret (.put ^java.util.Map opts "secret" secret))
161
165
  (when auth-nil (.put ^java.util.Map opts "auth" nil))
162
166
  (when secrets (.put ^java.util.Map opts "feature" (vs/jm "secrets" secrets)))
163
167
  (client/make-sdk opts)))
@@ -172,10 +176,6 @@
172
176
  (deref (core/client-features sdk)))))
173
177
 
174
178
 
175
- ;; The credential header carries the SPEC's credential prefix, which a
176
- ;; TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
177
- ;; `Bearer <token>`, an apiKey scheme the raw token. So assert on the
178
- ;; CREDENTIAL and let the prefix be whatever this SDK's API declares.
179
179
  (defn- listener
180
180
  "A server on 127.0.0.1 that records each request in `seen` and answers it
181
181
  as `answer` says, [status location body]."
@@ -184,7 +184,9 @@
184
184
  (.createContext server "/"
185
185
  (reify com.sun.net.httpserver.HttpHandler
186
186
  (handle [_ ex]
187
- (let [path (.getRawPath (.getRequestURI ex))
187
+ (let [uri (.getRequestURI ex)
188
+ ;; The target with its query: a query alone is another request.
189
+ path (str (.getRawPath uri) (when-let [q (.getRawQuery uri)] (str "?" q)))
188
190
  sent (String. (.readAllBytes (.getRequestBody ex)) "UTF-8")
189
191
  [code to text] (answer path)
190
192
  body (.getBytes ^String text "UTF-8")
@@ -198,11 +200,56 @@
198
200
  (.start server)
199
201
  server))
200
202
 
201
- (defn- credential-is [got token msg]
202
- (let [s (str (or got ""))]
203
- (t/is-true (or (= s token) (str/ends-with? s (str " " token)))
204
- (str msg ": expected the " CREDHEADER " header to carry " token
205
- ", got " (pr-str got)))))
203
+ ;; The header value u-prepare-auth writes for a token, in the form the
204
+ ;; DEFAULT client's auth gives: under Basic the token is the key half of the
205
+ ;; encoded pair, and a prefix is space-joined.
206
+ (defn- credential [token secret]
207
+ (let [auth (vs/getprop (core/client-options-map (client/test-sdk nil nil)) "auth")
208
+ prefix (let [p (vs/getprop auth "prefix")] (if (string? p) p ""))
209
+ value (if (= true (vs/getprop auth "basic"))
210
+ (.encodeToString (java.util.Base64/getEncoder)
211
+ (.getBytes (str token ":" secret) "UTF-8"))
212
+ token)]
213
+ (if (= "" prefix) value (str prefix " " value))))
214
+
215
+ (defn- credential-is
216
+ ([got token msg] (credential-is got token msg ""))
217
+ ([got token msg secret]
218
+ (t/is-eq (str (or got "")) (credential token secret)
219
+ (str msg ": the " CREDHEADER " header carries " token))))
220
+
221
+ (defn- with-origins
222
+ "Runs `run` with two listeners: the origin answers the exchange as the atom
223
+ `:answer` says and records the rest in `:seen`; `:elsewhere` is the other's."
224
+ [run]
225
+ (let [elsewhere (java.util.Collections/synchronizedList (java.util.ArrayList.))
226
+ seen (java.util.Collections/synchronizedList (java.util.ArrayList.))
227
+ answer (atom [307 ""])
228
+ other (listener elsewhere (fn [_] [200 nil "{\"access_token\":\"ELSEWHERE01\"}"]))
229
+ origin (listener seen (fn [path]
230
+ (if (= path "/api/auth/token")
231
+ (conj @answer "")
232
+ [200 nil "{\"access_token\":\"MOVED01\"}"])))
233
+ at (str "http://127.0.0.1:" (.getPort (.getAddress origin)))]
234
+ (try
235
+ (run {:elsewhere elsewhere :seen seen :answer answer :origin at :base (str at "/api")
236
+ :away (str "http://127.0.0.1:" (.getPort (.getAddress other)) "/api/auth/token")})
237
+ (finally
238
+ (.stop origin 0)
239
+ (.stop other 0)))))
240
+
241
+ (defn- redirect-direct [o status location xextra]
242
+ (.clear ^java.util.List (:elsewhere o))
243
+ (.clear ^java.util.List (:seen o))
244
+ (reset! (:answer o) [status location])
245
+ (let [exchange (vs/jm "active" true "refresh" "REFRESH01")]
246
+ (doseq [[k v] xextra] (vs/setprop exchange k v))
247
+ (client/direct (client/make-sdk
248
+ (vs/jm "base" (:base o)
249
+ "allow" (vs/jm "op" ALLOWOP)
250
+ "feature" (vs/jm "secrets" (sekopts "name" "refresh_token"
251
+ "exchange" exchange))))
252
+ (vs/jm "path" "/thing"))))
206
253
 
207
254
 
208
255
  ;; ---------------------------------------------------------------------------
@@ -288,6 +335,16 @@
288
335
  (t/is-eq (sekreto/getfrom (secrets/sek f) "options" "apikey")
289
336
  "OPTKEY01" "directed read of the options store")))))
290
337
 
338
+ (chk "secrets-a-resolved-key-is-placed-as-prepare-auth-places-it"
339
+ (fn []
340
+ (let [stub (counting-fetch)
341
+ sdk (live-sdk stub :secret "PASS01"
342
+ :secrets (sekopts "providers"
343
+ (vs/jt (fixed-provider "apikey" "CHAINKEY05"))))
344
+ res (client/direct sdk (vs/jm "path" "/thing"))]
345
+ (t/is-true (= true (vs/getprop res "ok")) (str "direct failed: " (brief res)))
346
+ (credential-is (:auth (call-at stub 0)) "CHAINKEY05" "the secret kept" "PASS01"))))
347
+
291
348
  (chk "secrets-omitted-apikey-defers-to-the-chain"
292
349
  (fn []
293
350
  (let [stub (counting-fetch)
@@ -579,6 +636,24 @@
579
636
  (credential-is (:auth (call-at stub 1)) "ACCESS01" "attempt 1")
580
637
  (credential-is (:auth (call-at stub 3)) "ACCESS02" "the retry"))))
581
638
 
639
+ (chk "secrets-exchange-a-token-bought-on-a-refusal-is-placed-as-prepare-auth-places-it"
640
+ (fn []
641
+ (let [issued (atom 0)
642
+ apicalls (atom 0)
643
+ stub (counting-fetch
644
+ (fn [_n url _fd]
645
+ (if (str/ends-with? url tokenpath)
646
+ [(response 200 (vs/jm "access_token"
647
+ (str "ACCESS0" (swap! issued inc)))) nil]
648
+ (if (= 1 (swap! apicalls inc))
649
+ [(response 401 (vs/jm "err" "expired")) nil]
650
+ [(response 200 (vs/jm "ok" true)) nil]))))
651
+ sdk (live-sdk stub :secret "PASS01" :secrets (xsecrets))]
652
+ (client/direct sdk (vs/jm "path" "/thing"))
653
+ (t/is-eq (sent stub) 4 (str "token, 401, token, retry: " (wire stub)))
654
+ (credential-is (:auth (call-at stub 1)) "ACCESS01" "attempt 1, the secret kept" "PASS01")
655
+ (credential-is (:auth (call-at stub 3)) "ACCESS02" "the retry, the secret kept" "PASS01"))))
656
+
582
657
  (chk "secrets-exchange-with-auth-nil-never-buys-or-retries"
583
658
  (fn []
584
659
  (let [stub (counting-fetch
@@ -604,51 +679,76 @@
604
679
  ;; Real listeners and core's own raw HTTP: a stub follows nothing.
605
680
  (chk "secrets-exchange-a-redirect-is-a-failure-and-nothing-follows-it"
606
681
  (fn []
607
- (let [elsewhere (java.util.Collections/synchronizedList (java.util.ArrayList.))
608
- seen (java.util.Collections/synchronizedList (java.util.ArrayList.))
609
- answer (atom [307 ""])
610
- other (listener elsewhere (fn [_] [200 nil "{\"access_token\":\"ELSEWHERE01\"}"]))
611
- origin (listener seen (fn [path]
612
- (if (= path "/api/auth/token")
613
- (conj @answer "")
614
- [200 nil "{\"access_token\":\"MOVED01\"}"])))
615
- base (str "http://127.0.0.1:" (.getPort (.getAddress origin)) "/api")
616
- away (str "http://127.0.0.1:" (.getPort (.getAddress other)) "/api/auth/token")]
617
- (try
682
+ (with-origins
683
+ (fn [{:keys [elsewhere seen base away] :as o}]
618
684
  (let [results
619
685
  (doall
620
686
  (for [[status location] [[307 away] [308 away] [302 away] [307 "/api/moved/token"]]]
621
- (do
622
- (.clear elsewhere)
623
- (.clear seen)
624
- (reset! answer [status location])
625
- (let [sdk (client/make-sdk
626
- (vs/jm "base" base
627
- "allow" (vs/jm "op" ALLOWOP)
628
- "feature" (vs/jm "secrets"
629
- (sekopts "name" "refresh_token"
630
- "exchange" (vs/jm "active" true
631
- "refresh" "REFRESH01")))))
632
- res (client/direct sdk (vs/jm "path" "/thing"))
633
- err (vs/getprop res "err")
634
- message (if (some? err) (str (core/err-msg err)) (brief res))
635
- followed (concat (vec elsewhere)
636
- (remove #(= "/api/auth/token" (:path %)) (vec seen)))]
637
- {:label (str status " to " location)
638
- :refused (str/includes? message (str "token exchange failed: " status
639
- " from " base "/auth/token"))
640
- :message message
641
- :followed (vec followed)}))))
687
+ (let [res (redirect-direct o status location nil)
688
+ err (vs/getprop res "err")
689
+ message (if (some? err) (str (core/err-msg err)) (brief res))
690
+ followed (concat (vec elsewhere)
691
+ (remove #(= "/api/auth/token" (:path %)) (vec seen)))]
692
+ {:label (str status " to " location)
693
+ :refused (str/includes? message (str "token exchange failed: " status
694
+ " from " base "/auth/token"))
695
+ :message message
696
+ :followed (vec followed)})))
642
697
  refused (count (filter :refused results))
643
698
  followed (reduce + (map (comp count :followed) results))]
644
699
  (println (str "secrets: exchange refused " refused " redirect(s), " followed " followed"))
645
700
  (doseq [r results]
646
701
  (t/is-eq (:followed r) [] (str (:label r) ": followed"))
647
702
  (t/is-true (:refused r) (str (:label r) ": not a failed exchange: " (:message r))))
648
- (t/is-eq refused 4 "refused"))
649
- (finally
650
- (.stop origin 0)
651
- (.stop other 0))))))
703
+ (t/is-eq refused 4 "refused"))))))
704
+
705
+ (chk "secrets-exchange-with-the-option-a-redirect-on-its-own-origin-is-followed-and-no-other"
706
+ (fn []
707
+ (with-origins
708
+ (fn [{:keys [elsewhere seen base away origin] :as o}]
709
+ (let [moved (fn [path] {:method "POST" :path path
710
+ :body "{\"refresh_token\":\"REFRESH01\"}"})
711
+ ;; Each case: the status, the Location, and the target the exchange
712
+ ;; is asked again at, or nil where the redirect is refused.
713
+ results
714
+ (doall
715
+ (for [[status location path] [[307 "/api/moved/token" "/api/moved/token"]
716
+ [308 (str origin "/api/moved/token") "/api/moved/token"]
717
+ ;; A Location of a query alone keeps the path that answered.
718
+ [307 "?step=2" "/api/auth/token?step=2"]
719
+ [307 away nil]
720
+ [302 "/api/moved/token" nil]
721
+ [307 "/api/auth/token" nil]]]
722
+ (let [res (redirect-direct o status location {"redirect" "same-origin"})
723
+ err (vs/getprop res "err")
724
+ message (if (some? err) (str (core/err-msg err)) (brief res))
725
+ mine (remove #(= "/api/auth/token" (:path %)) (vec seen))
726
+ verdict (cond
727
+ (and path (= true (vs/getprop res "ok"))
728
+ (= 2 (count mine)) (= (moved path) (first mine)))
729
+ :followed
730
+ (and (nil? path) (empty? mine)
731
+ (str/includes? message (str "token exchange failed: " status
732
+ " from " base "/auth/token")))
733
+ :refused
734
+ :else (str status " to " location ": " message " " (vec mine)))]
735
+ {:verdict verdict
736
+ :location location
737
+ :exchanged (count (filter #(= "/api/auth/token" (:path %)) (vec seen)))
738
+ :elsewhere (count elsewhere)})))
739
+ followed (count (filter #(= :followed (:verdict %)) results))
740
+ refused (count (filter #(= :refused (:verdict %)) results))
741
+ elsewhere (reduce + (map :elsewhere results))
742
+ looped (:exchanged (first (filter #(= "/api/auth/token" (:location %)) results)))]
743
+ (println (str "secrets: exchange opt-in followed " followed
744
+ " redirect(s) on its own origin, refused " refused ", "
745
+ elsewhere " elsewhere"))
746
+ (t/is-eq elsewhere 0 "the exchange reached another origin")
747
+ (t/is-eq (vec (remove keyword? (map :verdict results))) [] "wrong")
748
+ (t/is-eq followed 3 "followed")
749
+ (t/is-eq refused 3 "refused")
750
+ ;; A redirect to itself stops at the hop limit.
751
+ (t/is-eq looped 6 "the loop's exchange count"))))))
652
752
 
653
753
  (chk "secrets-exchange-with-no-refresh-token-fails-closed"
654
754
  (fn []
@@ -664,10 +764,53 @@
664
764
  "the missing-refresh-token message")))))))
665
765
 
666
766
 
767
+ ;; Off by the config where it declares no auth, else by `auth: nil`: either
768
+ ;; way the feature places no credential, buys no token and retries nothing.
769
+ (defn- auth-off [rec declared]
770
+ (t/run-check
771
+ rec "secrets-auth-off-places-no-credential-and-buys-no-token"
772
+ (fn []
773
+ (let [secrets ["OFFKEY01" "OFFKEY02" "OFFREFRESH01" "ACCESS01"]
774
+ carries (fn [c] (or (some? (:auth c))
775
+ (boolean (some (fn [s] (str/includes? (str (:values c)) s)) secrets))))
776
+ tokenpath "auth/token"
777
+ token? (fn [c] (str/ends-with? (str (:url c)) tokenpath))
778
+ scenarios [["a key the chain resolves" nil
779
+ (sekopts "providers" (vs/jt (fixed-provider "apikey" "OFFKEY01"))) 200]
780
+ ["an explicit apikey" "OFFKEY02" (sekopts) 200]
781
+ ["a refusal" nil
782
+ (sekopts "name" "refresh_token"
783
+ "providers" (vs/jt (fixed-provider "refresh_token" "OFFREFRESH01"))
784
+ "exchange" (vs/jm "active" true)) 401]]
785
+ totals (atom {:sent [] :bought 0})]
786
+ (doseq [[what apikey secrets-opts status] scenarios]
787
+ (let [stub (counting-fetch
788
+ (fn [_n url _fd]
789
+ (if (str/ends-with? url tokenpath)
790
+ [(response 200 (vs/jm "access_token" "ACCESS01")) nil]
791
+ [(response status (vs/jm "ok" (< status 400))) nil])))
792
+ sdk (live-sdk stub :apikey apikey :auth-nil declared :secrets secrets-opts)]
793
+ (client/direct sdk (vs/jm "path" "/thing"))
794
+ (let [calls (deref (:calls stub))
795
+ api (remove token? calls)
796
+ bought (count (filter token? calls))]
797
+ (t/is-eq (count api) 1 (str what ": one request, not retried"))
798
+ (t/is-eq bought 0 (str what ": no token bought"))
799
+ (t/is-true (not-any? carries api) (str what ": no credential sent: " (wire stub)))
800
+ (t/is-eq (secrets/credential (secrets/of sdk)) "" (str what ": nothing resolved"))
801
+ (swap! totals (fn [t] (-> t (update :sent into api) (update :bought + bought)))))))
802
+ (println (str "secrets: auth off by " (if declared "option" "config") ": "
803
+ (count (:sent @totals)) " request(s), "
804
+ (count (filter carries (:sent @totals))) " credential(s), "
805
+ (:bought @totals) " token(s)"))))))
806
+
807
+
667
808
  ;; Read from the config, never from a probe: an SDK whose config declares no
668
809
  ;; auth places no credential, and these tests assert on the one it places.
669
810
  (defn run [rec]
670
811
  (if (vs/ismap (vs/getprop (vs/getprop (config/make-config) "options") "auth"))
671
- (run-checks rec)
812
+ (do (run-checks rec)
813
+ (auth-off rec true))
672
814
  (do (println "SKIP secrets: this SDK's config declares no auth, so it places no credential")
815
+ (auth-off rec false)
673
816
  :skipped)))