@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
@@ -38,6 +38,7 @@
38
38
  local sdk = require("project-name-_sdk")
39
39
  local SecretsFeature = require("feature.secrets_feature")
40
40
  local sekreto = require("feature.secrets.sekreto")
41
+ local base64 = require("utility.clean").base64
41
42
 
42
43
 
43
44
  -- The header the credential travels in: the DEFAULT client's auth.name,
@@ -53,6 +54,17 @@ local CREDHEADER = (function()
53
54
  end)()
54
55
 
55
56
 
57
+ -- The form prepare_auth writes, from the DEFAULT client's auth: under Basic
58
+ -- the token is the key half of the encoded pair, and a prefix is
59
+ -- space-joined.
60
+ local CREDFORM = (function()
61
+ local auth = sdk.test(nil, nil):options_map().auth
62
+ auth = type(auth) == "table" and auth or {}
63
+ local prefix = type(auth.prefix) == "string" and auth.prefix or ""
64
+ return { prefix = prefix, basic = auth.basic == true }
65
+ end)()
66
+
67
+
56
68
  -- ---------------------------------------------------------------------
57
69
  -- The recording transport: system.fetch for a LIVE client, scripting one
58
70
  -- status per API call (the last repeating) and a token endpoint for the
@@ -78,8 +90,10 @@ local function make_wire(script)
78
90
  w.fetch = function(url, fetchdef)
79
91
  local headers = type(fetchdef) == "table" and fetchdef.headers or {}
80
92
  local auth = headers[CREDHEADER]
93
+ local values = {}
94
+ for _, v in pairs(headers) do values[#values + 1] = tostring(v) end
81
95
  w.calls[#w.calls + 1] = {
82
- url = url, auth = auth, has = auth ~= nil,
96
+ url = url, auth = auth, has = auth ~= nil, values = table.concat(values, "\n"),
83
97
  body = fetchdef.body, method = fetchdef.method, redirect = fetchdef.redirect,
84
98
  }
85
99
 
@@ -126,14 +140,22 @@ end
126
140
  -- ---------------------------------------------------------------------
127
141
  -- Support.
128
142
 
129
- -- The credential header carries the SPEC's credential prefix (if any),
130
- -- which a TEMPLATE cannot know - so assert on the CREDENTIAL and let the
131
- -- prefix be whatever this SDK's API declares.
132
- local function assert_credential(header, token)
133
- local ok = header == token
134
- or (type(header) == "string" and header:sub(-(#token + 1)) == " " .. token)
135
- assert.is_true(ok, "expected the " .. CREDHEADER .. " header to carry " ..
136
- token .. ", got: " .. tostring(header))
143
+ -- The header value prepare_auth writes for a token.
144
+ local function credential(token, secret)
145
+ local value = token
146
+ if CREDFORM.basic then
147
+ value = base64(token .. ":" .. (secret or ""))
148
+ end
149
+ if CREDFORM.prefix == "" then
150
+ return value
151
+ end
152
+ return CREDFORM.prefix .. " " .. value
153
+ end
154
+
155
+
156
+ local function assert_credential(header, token, secret)
157
+ assert.are.equal(credential(token, secret), header,
158
+ "expected the " .. CREDHEADER .. " header to carry " .. token)
137
159
  end
138
160
 
139
161
 
@@ -342,6 +364,17 @@ describe("secrets", function()
342
364
  "the options map must stay unwritten")
343
365
  end)
344
366
 
367
+ it("a resolved key is placed as prepare_auth places it, the secret kept", function()
368
+ local w = make_wire()
369
+ local client = secrets_client(w, {
370
+ secret = "PASS01",
371
+ feature = chain({ memory("CHAIN05") }),
372
+ })
373
+
374
+ drive_entity_op(client, w)
375
+ assert_credential(w.api()[1].auth, "CHAIN05", "PASS01")
376
+ end)
377
+
345
378
  it("custom provider objects are accepted verbatim", function()
346
379
  local asked = {}
347
380
  local w = make_wire()
@@ -822,6 +855,17 @@ describe("secrets", function()
822
855
  assert.are.equal(2, #w.token())
823
856
  end)
824
857
 
858
+ it("a token bought on a refusal is placed as prepare_auth places it", function()
859
+ local w = make_wire({ apistatus = { 401, 200 } })
860
+ local client = exchange_client(w, nil, { secret = "PASS01" })
861
+ local res = client:direct({ path = "/probe" })
862
+ assert.is_true(res.ok, tostring(res.err))
863
+
864
+ assert.are.equal(2, #w.api(), "expected the refused request and one retry")
865
+ assert_credential(w.api()[1].auth, "ACCESS01", "PASS01")
866
+ assert_credential(w.api()[2].auth, "ACCESS02", "PASS01")
867
+ end)
868
+
825
869
  it("a second refusal surfaces rather than spinning", function()
826
870
  local w = make_wire({ apistatus = { 401 } })
827
871
  local client = exchange_client(w)
@@ -876,6 +920,8 @@ describe("secrets", function()
876
920
  local other = assert(socket.bind("127.0.0.1", 0))
877
921
  if LOCATION == "OTHER" then
878
922
  LOCATION = "http://127.0.0.1:" .. select(2, other:getsockname()) .. "/api/auth/token"
923
+ elseif LOCATION == "ORIGIN" then
924
+ LOCATION = "http://127.0.0.1:" .. select(2, api:getsockname()) .. "/api/moved/token"
879
925
  end
880
926
  io.stdout:write(select(2, api:getsockname()), "\t", LOCATION, "\n")
881
927
  io.stdout:flush()
@@ -908,50 +954,60 @@ describe("secrets", function()
908
954
  end
909
955
  ]]
910
956
 
911
- -- Real listeners and the feature's own transport: a stub follows nothing.
912
- local has_socket = pcall(require, "socket.http")
913
- ;(has_socket and it or pending)("a redirect answering the exchange is a failure, and nothing follows it",
914
- function()
957
+ -- One exchange against the two listeners, the origin answering it with
958
+ -- `status` and `location`: the result, the base, the location as served
959
+ -- and every request the listeners got.
960
+ local function against_origins(status, location, xextra)
915
961
  local first = 0
916
962
  while arg and arg[first - 1] ~= nil do first = first - 1 end
917
963
  local lua = arg and first < 0 and arg[first] or "lua"
918
964
 
965
+ local script = os.tmpname()
966
+ local f = assert(io.open(script, "w"))
967
+ f:write(string.format("package.path = %q\npackage.cpath = %q\nSTATUS = %d\nLOCATION = %q\n",
968
+ package.path, package.cpath, status, location), origins_server)
969
+ f:close()
970
+ local child = assert(io.popen(string.format("%q %q", lua, script)))
971
+ local port, served = string.match(assert(child:read("*l")), "^(%d+)\t(.*)$")
972
+ local base = "http://127.0.0.1:" .. port .. "/api"
973
+
974
+ local exchange = { active = true, refresh = "REFRESH01" }
975
+ for k, v in pairs(xextra or {}) do exchange[k] = v end
976
+ local client = secrets_client(nil, {
977
+ base = base,
978
+ feature = { secrets = { active = true, name = "refresh_token", exchange = exchange } },
979
+ })
980
+ local res = client:direct({ path = "/thing" })
981
+
982
+ local stop = require("socket").connect("127.0.0.1", tonumber(port))
983
+ if stop then
984
+ stop:send("GET /stop HTTP/1.0\r\n\r\n")
985
+ stop:close()
986
+ end
987
+ local seen = {}
988
+ for line in child:lines() do
989
+ local where, method, path, body = string.match(line, "^(%S+)\t(%S*)\t(%S*)\t(.*)$")
990
+ seen[#seen + 1] = { where = where, method = method, path = path, body = body }
991
+ end
992
+ child:close()
993
+ os.remove(script)
994
+ return res, base, served, seen
995
+ end
996
+
997
+ -- Real listeners and the feature's own transport: a stub follows nothing.
998
+ local has_socket = pcall(require, "socket.http")
999
+ ;(has_socket and it or pending)("a redirect answering the exchange is a failure, and nothing follows it",
1000
+ function()
919
1001
  local refused, leaked, failures = 0, 0, {}
920
1002
  for _, case in ipairs({ { 307, "OTHER" }, { 308, "OTHER" }, { 302, "OTHER" },
921
1003
  { 307, "/api/moved/token" } }) do
922
- local script = os.tmpname()
923
- local f = assert(io.open(script, "w"))
924
- f:write(string.format("package.path = %q\npackage.cpath = %q\nSTATUS = %d\nLOCATION = %q\n",
925
- package.path, package.cpath, case[1], case[2]), origins_server)
926
- f:close()
927
- local child = assert(io.popen(string.format("%q %q", lua, script)))
928
- local port, location = string.match(assert(child:read("*l")), "^(%d+)\t(.*)$")
929
- local base = "http://127.0.0.1:" .. port .. "/api"
930
-
931
- local client = secrets_client(nil, {
932
- base = base,
933
- feature = { secrets = {
934
- active = true,
935
- name = "refresh_token",
936
- exchange = { active = true, refresh = "REFRESH01" },
937
- } },
938
- })
939
- local res = client:direct({ path = "/thing" })
940
-
941
- local stop = require("socket").connect("127.0.0.1", tonumber(port))
942
- if stop then
943
- stop:send("GET /stop HTTP/1.0\r\n\r\n")
944
- stop:close()
945
- end
1004
+ local res, base, location, seen = against_origins(case[1], case[2])
946
1005
  local followed = {}
947
- for line in child:lines() do
948
- local where, method, path, body = string.match(line, "^(%S+)\t(%S*)\t(%S*)\t(.*)$")
949
- if where == "other" or path ~= "/api/auth/token" then
950
- followed[#followed + 1] = where .. " " .. method .. " " .. path .. " " .. body
1006
+ for _, r in ipairs(seen) do
1007
+ if r.where == "other" or r.path ~= "/api/auth/token" then
1008
+ followed[#followed + 1] = r.where .. " " .. r.method .. " " .. r.path .. " " .. r.body
951
1009
  end
952
1010
  end
953
- child:close()
954
- os.remove(script)
955
1011
 
956
1012
  local label = case[1] .. " to " .. location
957
1013
  local message = tostring(res and res.err)
@@ -973,6 +1029,56 @@ describe("secrets", function()
973
1029
  assert.are.equal(4, refused)
974
1030
  end)
975
1031
 
1032
+ ;(has_socket and it or pending)(
1033
+ "with the option, a redirect on the exchange's own origin is followed, and no other",
1034
+ function()
1035
+ local sent = ' {"refresh_token":"REFRESH01"}'
1036
+ local followed, refused, elsewhere, looped, wrong = 0, 0, 0, 0, {}
1037
+ -- Each case: the status, the Location, and the target the exchange is
1038
+ -- asked again at, or false where the redirect is refused. A Location of
1039
+ -- a query alone keeps the path that answered.
1040
+ for _, case in ipairs({ { 307, "/api/moved/token", "/api/moved/token" },
1041
+ { 308, "ORIGIN", "/api/moved/token" }, { 307, "?step=2", "/api/auth/token?step=2" },
1042
+ { 307, "OTHER", false }, { 302, "/api/moved/token", false },
1043
+ { 307, "/api/auth/token", false } }) do
1044
+ local res, base, location, seen = against_origins(case[1], case[2],
1045
+ { redirect = "same-origin" })
1046
+ local mine, exchanged = {}, 0
1047
+ for _, r in ipairs(seen) do
1048
+ if r.where == "other" then
1049
+ elsewhere = elsewhere + 1
1050
+ elseif r.path == "/api/auth/token" then
1051
+ exchanged = exchanged + 1
1052
+ else
1053
+ mine[#mine + 1] = r.method .. " " .. r.path .. " " .. r.body
1054
+ end
1055
+ end
1056
+
1057
+ local message = tostring(res and res.err)
1058
+ local want = "token exchange failed: " .. case[1] .. " from " .. base .. "/auth/token"
1059
+ if case[3] and res and res.ok == true and #mine == 2
1060
+ and mine[1] == "POST " .. case[3] .. sent then
1061
+ followed = followed + 1
1062
+ elseif not case[3] and #mine == 0 and message:find(want, 1, true) then
1063
+ refused = refused + 1
1064
+ else
1065
+ wrong[#wrong + 1] = case[1] .. " to " .. location .. ": " .. message .. " " ..
1066
+ table.concat(mine, "; ")
1067
+ end
1068
+ if case[2] == "/api/auth/token" then looped = exchanged end
1069
+ end
1070
+
1071
+ print("\nsecrets: exchange opt-in followed " .. followed .. " redirect(s) on its own origin, " ..
1072
+ "refused " .. refused .. ", " .. elsewhere .. " elsewhere")
1073
+
1074
+ assert.are.equal(0, elsewhere, "the exchange reached another origin")
1075
+ assert.are.same({}, wrong)
1076
+ assert.are.equal(3, followed)
1077
+ assert.are.equal(3, refused)
1078
+ -- A redirect to itself stops at the hop limit.
1079
+ assert.are.equal(6, looped)
1080
+ end)
1081
+
976
1082
  it("test mode buys nothing", function()
977
1083
  -- A LIVE construction (so the feature is installed through extend),
978
1084
  -- switched to test mode before the first request: the wrapper runs
@@ -991,3 +1097,62 @@ describe("secrets", function()
991
1097
  end)
992
1098
  end)
993
1099
  end)
1100
+
1101
+
1102
+ -- Off by the config where it declares no auth, else by clearing
1103
+ -- `options.auth` after construction, the suppression Lua can express:
1104
+ -- either way the feature places no credential, buys no token and retries
1105
+ -- nothing.
1106
+ describe("secrets with auth off", function()
1107
+
1108
+ it("places no credential and buys no token", function()
1109
+ local declared = nil ~= (require("config_shared")().options or {}).auth
1110
+ local by = declared and "option" or "config"
1111
+ local secrets = { "OFFKEY01", "OFFKEY02", "OFFREFRESH01", "ACCESS01" }
1112
+ local function carries(call)
1113
+ if call.has then return true end
1114
+ for _, secret in ipairs(secrets) do
1115
+ if call.values:find(secret, 1, true) then return true end
1116
+ end
1117
+ return false
1118
+ end
1119
+ local sent, bought = {}, 0
1120
+
1121
+ local refresh = { kind = "memory", values = { REFRESH_TOKEN = "OFFREFRESH01" } }
1122
+ local scenarios = {
1123
+ { "a key the chain resolves", {}, chain({ memory("OFFKEY01") }), { 200 } },
1124
+ { "an explicit apikey", { apikey = "OFFKEY02" }, { secrets = { active = true } }, { 200 } },
1125
+ { "a refusal", {},
1126
+ chain({ refresh }, { name = "refresh_token", exchange = { active = true } }), { 401 } },
1127
+ }
1128
+
1129
+ for _, scenario in ipairs(scenarios) do
1130
+ local what, sdkopts, feature, statuses = table.unpack(scenario)
1131
+ local w = make_wire({ apistatus = statuses })
1132
+ local opts = { feature = feature }
1133
+ for k, v in pairs(sdkopts) do opts[k] = v end
1134
+ local client = secrets_client(w, opts)
1135
+ if declared then
1136
+ client.options.auth = nil
1137
+ end
1138
+
1139
+ client:direct({ path = "/probe" })
1140
+
1141
+ assert.are.equal(1, #w.api(), what .. ": a request was retried with auth off")
1142
+ assert.are.equal(0, #w.token(), what .. ": a token was bought with auth off")
1143
+ assert.is_false(carries(w.api()[1]), what .. ": a credential was sent with auth off")
1144
+ assert.is_nil(secrets_feature_of(client):credential(),
1145
+ what .. ": the feature resolved a credential with auth off")
1146
+
1147
+ for _, call in ipairs(w.api()) do sent[#sent + 1] = call end
1148
+ bought = bought + #w.token()
1149
+ end
1150
+
1151
+ local carried = 0
1152
+ for _, call in ipairs(sent) do
1153
+ if carries(call) then carried = carried + 1 end
1154
+ end
1155
+ print("\nsecrets: auth off by " .. by .. ": " .. #sent .. " request(s), " ..
1156
+ carried .. " credential(s), " .. bought .. " token(s)")
1157
+ end)
1158
+ end)
@@ -668,6 +668,24 @@ describe("feature", function()
668
668
  assert.are.equal(2, h.client._cache.bypass)
669
669
  end)
670
670
 
671
+ it("passes a body that declares another type than JSON by, unread", function()
672
+ local sent = {}
673
+ local rec = recording_server(function()
674
+ local res = make_response(200, nil, { ["content-type"] = "audio/wav" })
675
+ sent[#sent + 1] = res
676
+ return res
677
+ end)
678
+ local h = make_client({ features = {
679
+ { name = "cache" },
680
+ }, server = rec.server })
681
+ local a = h.op({ op = "load", path = "/w/1/audio" })
682
+ local b = h.op({ op = "load", path = "/w/1/audio" })
683
+ assert.are.equal(2, #rec.calls, "the body was stored")
684
+ assert.are.equal(sent[1], a.ctx.response, "the cache handed on a copy it had read")
685
+ assert.are.equal(sent[2], b.ctx.response)
686
+ assert.are.equal(2, h.client._cache.bypass)
687
+ end)
688
+
671
689
  it("re-fetches after the ttl", function()
672
690
  local clock = make_clock()
673
691
  local rec = recording_server()
@@ -501,6 +501,7 @@ end
501
501
 
502
502
 
503
503
  return {
504
+ base64 = base64,
504
505
  clean = clean_util,
505
506
  clean_add = clean_add_util,
506
507
  clean_add_sensitive = clean_add_sensitive_util,
@@ -84,6 +84,7 @@ type xchg = {
84
84
  xresponse : string;
85
85
  xstatuses : int list;
86
86
  xretries : int;
87
+ xsameorigin : bool;
87
88
  }
88
89
 
89
90
  (* The message an exception carries, whichever library raised it: sekreto's
@@ -138,6 +139,64 @@ let trim_slashes_left (s : string) : string =
138
139
  while !i < n && s.[!i] = '/' do incr i done;
139
140
  String.sub s !i (n - !i)
140
141
 
142
+ let redirect_max = 5
143
+
144
+ (* An absolute http or https url as its scheme, its authority and the rest. *)
145
+ let url_parts (url : string) : (string * string * string) option =
146
+ let scheme, start =
147
+ if String.starts_with ~prefix:"https://" (String.lowercase_ascii url) then ("https", 8)
148
+ else if String.starts_with ~prefix:"http://" (String.lowercase_ascii url) then ("http", 7)
149
+ else ("", 0) in
150
+ if 0 = start then None
151
+ else begin
152
+ let n = String.length url in
153
+ let rec stop i = if i >= n || String.contains "/?#" url.[i] then i else stop (i + 1) in
154
+ let e = stop start in
155
+ let auth = String.sub url start (e - start) in
156
+ if "" = auth || String.contains auth '@' then None
157
+ else Some (scheme, auth, String.sub url e (n - e))
158
+ end
159
+
160
+ (* Scheme, host and port, with the scheme's default port. *)
161
+ let url_origin (url : string) : string option =
162
+ match url_parts url with
163
+ | None -> None
164
+ | Some (scheme, auth, _) ->
165
+ let auth = String.lowercase_ascii auth in
166
+ let ported = match String.rindex_opt auth ':' with
167
+ | Some i -> not (String.contains_from auth i ']')
168
+ | None -> false in
169
+ Some (scheme ^ "://" ^ auth ^ (if ported then "" else if "https" = scheme then ":443" else ":80"))
170
+
171
+ let resolve_url (at : string) (location : string) : string option =
172
+ if None <> url_parts location then Some location
173
+ else match url_parts at with
174
+ | None -> None
175
+ | Some (scheme, auth, rest) ->
176
+ if String.starts_with ~prefix:"//" location then Some (scheme ^ ":" ^ location)
177
+ else if String.starts_with ~prefix:"/" location then Some (scheme ^ "://" ^ auth ^ location)
178
+ else if String.starts_with ~prefix:"?" location then
179
+ let cut = match String.index_from_opt rest 0 '?', String.index_from_opt rest 0 '#' with
180
+ | Some i, Some j -> min i j | Some i, None | None, Some i -> i
181
+ | None, None -> String.length rest in
182
+ Some (scheme ^ "://" ^ auth ^ String.sub rest 0 cut ^ location)
183
+ else
184
+ let path = match String.index_opt rest '?' with Some i -> String.sub rest 0 i | None -> rest in
185
+ let dir = match String.rindex_opt path '/' with
186
+ | Some i -> String.sub path 0 (i + 1) | None -> "/" in
187
+ Some (scheme ^ "://" ^ auth ^ dir ^ location)
188
+
189
+ (* Where a redirect keeping method and body (307 or 308) points, on the
190
+ * exchange's own origin. Any other would drop the body or leave the origin.
191
+ * The bundled transport answers no headers: only a supplied fetch is followed. *)
192
+ let moved (url : string) (at : string) (res : value) : string option =
193
+ match to_int (getp res "status"), getp (getp res "headers") "location" with
194
+ | (307 | 308), Str location when "" <> location ->
195
+ (match resolve_url at location with
196
+ | Some next when None <> url_origin next && url_origin next = url_origin url -> Some next
197
+ | _ -> None)
198
+ | _ -> None
199
+
141
200
  (* Build the feature. `plugins` are the plugin DEFINITIONS the model
142
201
  * selected for this feature, emitted by Config_ocaml from the catalogue's
143
202
  * active `plugin.def.ocaml` entries (`Sdk_config.feature_plugins "secrets"`).
@@ -164,6 +223,10 @@ let make ?(plugins : Defs.definition list = [])
164
223
  let cred = ref "" in
165
224
  (* The refresh credential the chain resolved, for every later purchase. *)
166
225
  let refresh = ref "" in
226
+ (* What prepare_auth was generated from: the config declares auth only
227
+ * where it places a credential, and Basic only where it composes one. *)
228
+ let declared = ref false in
229
+ let basic = ref false in
167
230
 
168
231
  let refuse (msg : string) =
169
232
  (match !initerr with None -> initerr := Some msg | Some _ -> ()) in
@@ -179,6 +242,18 @@ let make ?(plugins : Defs.definition list = [])
179
242
  let live_options () : value =
180
243
  match !client with Some c -> c.cl_options | None -> empty_map () in
181
244
 
245
+ (* prepare_auth's decision: auth the config declares, not suppressed by
246
+ * `auth: null`. *)
247
+ let authon () : bool =
248
+ !declared && (match getp (live_options ()) "auth" with Map _ -> true | _ -> false) in
249
+
250
+ (* Under Basic the token is the key half, as apikey is: prepare_auth's pair. *)
251
+ let pair (token : string) : string =
252
+ let secret = match getp (live_options ()) "secret" with Str s -> s | _ -> "" in
253
+ let joined = base64_encode (token ^ ":" ^ secret) in
254
+ register joined;
255
+ joined in
256
+
182
257
  let bucket () : value =
183
258
  match !client with
184
259
  | Some c -> track_bucket c "secrets" (fun () ->
@@ -197,11 +272,14 @@ let make ?(plugins : Defs.definition list = [])
197
272
  let name = match getpath_s opts "auth.name" with
198
273
  | Str s when s <> "" -> String.lowercase_ascii s
199
274
  | _ -> "authorization" in
200
- (match getp opts "auth" with
201
- | Map _ ->
202
- let prefix = match getpath_s opts "auth.prefix" with Str s -> s | _ -> "" in
203
- setp headers name (Str (if prefix <> "" then prefix ^ " " ^ token else token))
204
- | _ -> ignore (delprop headers (Str name)))
275
+ if authon () then begin
276
+ let value =
277
+ if !basic && (match getpath_s opts "auth.basic" with Bool b -> b | _ -> false)
278
+ then pair token else token in
279
+ let prefix = match getpath_s opts "auth.prefix" with Str s -> s | _ -> "" in
280
+ setp headers name (Str (if prefix <> "" then prefix ^ " " ^ value else value))
281
+ end
282
+ else ignore (delprop headers (Str name))
205
283
  | _ -> () in
206
284
 
207
285
  let spent (x : xchg) (res : value) : bool =
@@ -230,7 +308,7 @@ let make ?(plugins : Defs.definition list = [])
230
308
  ("method", Str x.xmethod);
231
309
  ("headers", jo [("content-type", Str "application/json")]);
232
310
  ("body", Str (jsonify (jo [(x.xrequest, Str !refresh)])));
233
- (* Never followed: a redirect would carry the refresh token on. *)
311
+ (* Never followed by the client: a redirect would carry the refresh token on. *)
234
312
  ("redirect", Str "manual")] in
235
313
  let fetch =
236
314
  match getpath_s opts "system.fetch" with
@@ -243,7 +321,13 @@ let make ?(plugins : Defs.definition list = [])
243
321
  ^ "HTTP client is not compiled; supply options.system.fetch or activate a "
244
322
  ^ "plugin group")
245
323
  | Some fetch ->
246
- (match fetch url fetchdef with
324
+ (* By option alone, and only while the exchange stays on its own origin. *)
325
+ let rec ask at hops =
326
+ let res = fetch at fetchdef in
327
+ match if x.xsameorigin && hops < redirect_max then moved url at res else None with
328
+ | Some next -> ask next (hops + 1)
329
+ | None -> res in
330
+ (match ask url 0 with
247
331
  | exception e -> Error (exn_message e)
248
332
  | res ->
249
333
  (match get_str res "__err__" with
@@ -293,6 +377,7 @@ let make ?(plugins : Defs.definition list = [])
293
377
  let resolve () : (unit, string) Stdlib.result =
294
378
  match !sek with
295
379
  | None -> Ok ()
380
+ | Some _ when not (authon ()) -> Ok ()
296
381
  | Some s ->
297
382
  bump_num (bucket ()) "resolves" 1.;
298
383
  (match Sekreto.tryget s !secretname with
@@ -325,13 +410,10 @@ let make ?(plugins : Defs.definition list = [])
325
410
  * one is spent. The retry rewrites the credential header IN PLACE on
326
411
  * the fetchdef, because the header carries the token that just failed. *)
327
412
  let withrefresh (x : xchg) fctx url fetchdef inner : (value * sdk_error option) =
328
- (* `auth: null` is the documented way to send NO credential, and
329
- * prepare_auth honours it by removing the header. A refusal of a
330
- * deliberately unauthenticated request is not an expired token and
331
- * cannot be fixed by buying one. *)
332
- match getp (live_options ()) "auth" with
333
- | Noval | Null -> inner fctx url fetchdef
334
- | _ ->
413
+ (* A refusal of an unauthenticated request is not an expired token, and
414
+ * retrying would transmit the credential that auth off withholds. *)
415
+ if not (authon ()) then inner fctx url fetchdef
416
+ else
335
417
  let rec attempt (n : int) =
336
418
  (* The credential THIS attempt goes out with, captured before it
337
419
  * leaves: it is what tells a stale refusal apart from a fresh one. *)
@@ -385,6 +467,9 @@ let make ?(plugins : Defs.definition list = [])
385
467
  if f.f_active then begin
386
468
  let c = cc ctx in
387
469
  client := Some c;
470
+ let declared_auth = getpath_s ctx.c_config "options.auth" in
471
+ declared := (match declared_auth with Map _ -> true | _ -> false);
472
+ basic := (match getp declared_auth "basic" with Bool b -> b | _ -> false);
388
473
  secretname := (let n = opt_str opts "name" ~default:"apikey" in if "" = n then "apikey" else n);
389
474
  cache := (getp opts "cache" <> Bool false);
390
475
 
@@ -403,6 +488,7 @@ let make ?(plugins : Defs.definition list = [])
403
488
  xresponse = opt_str xopts "response" ~default:"access_token";
404
489
  xstatuses = (if [] = statuses then [401] else statuses);
405
490
  xretries = opt_int xopts "retries" ~default:1;
491
+ xsameorigin = getp xopts "redirect" = Str "same-origin";
406
492
  }
407
493
  end else None);
408
494
 
@@ -276,6 +276,10 @@ let cache_feature () : feature =
276
276
  | Map _ when getp res "unreadable" = Bool true -> false
277
277
  | Map _ -> (match getp res "status" with Num n -> n >= 200. && n < 300. | _ -> false)
278
278
  | _ -> false in
279
+ let json res =
280
+ match header_ci (getp res "headers") "content-type" with
281
+ | Str t -> "" = t || is_json_media t
282
+ | _ -> true in
279
283
  let snapshot res =
280
284
  let data = match getp res "json" with Func _ as fn -> (try call_json fn with _ -> Noval) | _ -> Noval in
281
285
  let headers = empty_map () in
@@ -301,7 +305,9 @@ let cache_feature () : feature =
301
305
  track ctx "hit"; (replay (getp hit "snapshot"), None)
302
306
  | _ ->
303
307
  let (res, err) = inner ctx url fetchdef in
304
- if err = None && cacheable res then begin
308
+ (* A body that declares another type than JSON passes by, unread. *)
309
+ if err = None && cacheable res && not (json res) then (track ctx "bypass"; (res, err))
310
+ else if err = None && cacheable res then begin
305
311
  let snap = snapshot res in
306
312
  let ttl = opt_num !options "ttl" ~default:5000. in
307
313
  evict ();