@voxgig/sdkgen 4.35.0 → 4.35.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.
- package/bin/voxgig-sdkgen +1 -1
- package/package.json +1 -1
- package/project/.sdk/tm/c/feature/secrets.c +21 -6
- package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +24 -9
- package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +16 -10
- package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +16 -7
- package/project/.sdk/tm/cpp/feature/secrets.hpp +9 -4
- package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +20 -7
- package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +10 -6
- package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +21 -9
- package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +7 -3
- package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +15 -5
- package/project/.sdk/tm/go/feature/secrets_feature.go +9 -2
- package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +21 -10
- package/project/.sdk/tm/java/feature/SecretsFeature.java +10 -5
- package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +21 -9
- package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +8 -4
- package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +35 -24
- package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +11 -6
- package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +17 -8
- package/project/.sdk/tm/lua/feature/secrets_feature.lua +11 -4
- package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +22 -9
- package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +8 -4
- package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +19 -10
- package/project/.sdk/tm/php/feature/SecretsFeature.php +10 -5
- package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +33 -11
- package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +8 -3
- package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +27 -13
- package/project/.sdk/tm/rb/feature/secrets_feature.rb +9 -5
- package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +20 -11
- package/project/.sdk/tm/rust/feature/secrets.rs +8 -7
- package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +17 -2
- package/project/.sdk/tm/scala/feature/SecretsFeature.scala +10 -5
- package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +19 -7
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +9 -5
- package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +15 -7
- package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +5 -1
- package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +31 -20
- package/project/.sdk/tm/zig/feature/secrets.zig +12 -6
- package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +21 -5
- package/project/sdkgen-package.json +1 -1
package/bin/voxgig-sdkgen
CHANGED
package/package.json
CHANGED
|
@@ -277,8 +277,8 @@ static bool spec_of_map(sek_pool* pool, voxgig_value* m, sek_spec* spec, const c
|
|
|
277
277
|
|
|
278
278
|
// ---- credential injection ---------------------------------------------------
|
|
279
279
|
|
|
280
|
-
// Write the resolved credential into THIS request's
|
|
281
|
-
// the way prepare_auth builds it (same options auth
|
|
280
|
+
// Write the resolved credential into THIS request's credential header,
|
|
281
|
+
// the way prepare_auth builds it (same options auth name and prefix, same
|
|
282
282
|
// suppression rule), so the two cannot drift.
|
|
283
283
|
//
|
|
284
284
|
// `auth: null` is the documented way to send NO credential, and
|
|
@@ -292,26 +292,41 @@ static void secrets_reauth(SecretsFeature* f, voxgig_value* fetchdef) {
|
|
|
292
292
|
voxgig_value* headers = getp(fetchdef, "headers");
|
|
293
293
|
if (!voxgig_is_map(headers)) return;
|
|
294
294
|
|
|
295
|
+
// The header prepare_auth writes: auth.name, lower-cased into a copy this
|
|
296
|
+
// function frees.
|
|
297
|
+
voxgig_value* namev = getpath2(f->liveopts, "auth", "name");
|
|
298
|
+
const char* given = voxgig_is_string(namev) ? voxgig_as_string(namev) : "";
|
|
299
|
+
char* name = strdup('\0' == given[0] ? "authorization" : given);
|
|
300
|
+
if (NULL == name) return;
|
|
301
|
+
for (char* c = name; '\0' != *c; c++) {
|
|
302
|
+
if ('A' <= *c && *c <= 'Z') *c = (char)(*c - 'A' + 'a');
|
|
303
|
+
}
|
|
304
|
+
|
|
295
305
|
voxgig_value* auth = getp(f->liveopts, "auth");
|
|
296
306
|
if (v_is_noval(auth) || v_is_null(auth)) {
|
|
297
|
-
voxgig_value* k = voxgig_new_string(
|
|
307
|
+
voxgig_value* k = voxgig_new_string(name);
|
|
298
308
|
voxgig_delprop(headers, k);
|
|
299
309
|
voxgig_release(k);
|
|
310
|
+
free(name);
|
|
300
311
|
return;
|
|
301
312
|
}
|
|
302
313
|
|
|
303
|
-
if (NULL == f->cred || '\0' == f->cred[0])
|
|
314
|
+
if (NULL == f->cred || '\0' == f->cred[0]) {
|
|
315
|
+
free(name);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
304
318
|
|
|
305
319
|
const char* prefix = get_str(auth, "prefix");
|
|
306
320
|
if (NULL == prefix || '\0' == prefix[0]) {
|
|
307
|
-
setp(headers,
|
|
321
|
+
setp(headers, name, v_str(f->cred));
|
|
308
322
|
} else {
|
|
309
323
|
size_t n = strlen(prefix) + 1 + strlen(f->cred) + 1;
|
|
310
324
|
char* buf = (char*)malloc(n);
|
|
311
325
|
snprintf(buf, n, "%s %s", prefix, f->cred);
|
|
312
|
-
setp(headers,
|
|
326
|
+
setp(headers, name, v_str(buf));
|
|
313
327
|
free(buf);
|
|
314
328
|
}
|
|
329
|
+
free(name);
|
|
315
330
|
}
|
|
316
331
|
|
|
317
332
|
// Every value this feature resolves or buys is a secret the SDK handles,
|
|
@@ -54,6 +54,21 @@ static int CASES = 0;
|
|
|
54
54
|
fn(); \
|
|
55
55
|
} while (0)
|
|
56
56
|
|
|
57
|
+
// The header the credential travels in: the DEFAULT client's auth.name,
|
|
58
|
+
// lower-cased as prepare_auth sends it. Read from a default client because a
|
|
59
|
+
// test's `auth: null` leaves no name to read. Computed once, held for the run.
|
|
60
|
+
static const char* cred_header(void) {
|
|
61
|
+
static char* name = NULL;
|
|
62
|
+
if (NULL != name) return name;
|
|
63
|
+
voxgig_value* v = getpath2(sdk_options_map(test_sdk(NULL, NULL)), "auth", "name");
|
|
64
|
+
const char* given = voxgig_is_string(v) ? voxgig_as_string(v) : "";
|
|
65
|
+
name = strdup('\0' == given[0] ? "authorization" : given);
|
|
66
|
+
for (char* c = name; '\0' != *c; c++) {
|
|
67
|
+
if ('A' <= *c && *c <= 'Z') *c = (char)(*c - 'A' + 'a');
|
|
68
|
+
}
|
|
69
|
+
return name;
|
|
70
|
+
}
|
|
71
|
+
|
|
57
72
|
// ---- the recording live transport ------------------------------------------
|
|
58
73
|
|
|
59
74
|
#define MAX_CALLS 16
|
|
@@ -95,7 +110,7 @@ static voxgig_value* live_fetch(void* ud, voxgig_value* args) {
|
|
|
95
110
|
voxgig_value* urlv = voxgig_getelem(args, v_int(0), voxgig_new_undef());
|
|
96
111
|
voxgig_value* fetchdef = voxgig_getelem(args, v_int(1), voxgig_new_undef());
|
|
97
112
|
const char* url = voxgig_is_string(urlv) ? voxgig_as_string(urlv) : "";
|
|
98
|
-
// A SNAPSHOT, not a share: the exchange rewrites the
|
|
113
|
+
// A SNAPSHOT, not a share: the exchange rewrites the credential header
|
|
99
114
|
// of the SAME fetchdef in place before it retries, so a shared reference
|
|
100
115
|
// would show the retry's token on the first attempt's record.
|
|
101
116
|
voxgig_list_push(voxgig_as_list(r->calls),
|
|
@@ -119,20 +134,20 @@ static voxgig_value* call_fetchdef(Rec* r, int i) {
|
|
|
119
134
|
return getp(voxgig_getelem(r->calls, v_int(i), voxgig_new_undef()), "fetchdef");
|
|
120
135
|
}
|
|
121
136
|
|
|
122
|
-
// The
|
|
137
|
+
// The credential header of call i, or NULL when absent. Presence is read
|
|
123
138
|
// off the map directly: a present-but-EMPTY header is not suppression, and
|
|
124
139
|
// get_str alone cannot tell the two apart.
|
|
125
140
|
static const char* call_auth(Rec* r, int i, bool* present) {
|
|
126
141
|
voxgig_value* headers = getp(call_fetchdef(r, i), "headers");
|
|
127
142
|
if (present) {
|
|
128
143
|
*present = voxgig_is_map(headers) &&
|
|
129
|
-
NULL != voxgig_map_get(voxgig_as_map(headers),
|
|
144
|
+
NULL != voxgig_map_get(voxgig_as_map(headers), cred_header());
|
|
130
145
|
}
|
|
131
|
-
return get_str(headers,
|
|
146
|
+
return get_str(headers, cred_header());
|
|
132
147
|
}
|
|
133
148
|
|
|
134
|
-
// The
|
|
135
|
-
// TEMPLATE cannot know (a bearer scheme gives `Bearer <token>`, an apiKey
|
|
149
|
+
// The credential header carries the SPEC's credential prefix (if any), which
|
|
150
|
+
// a TEMPLATE cannot know (a bearer scheme gives `Bearer <token>`, an apiKey
|
|
136
151
|
// scheme the raw token). So assert on the CREDENTIAL and let the prefix be
|
|
137
152
|
// whatever this SDK's API declares.
|
|
138
153
|
static bool credential_is(const char* header, const char* token) {
|
|
@@ -246,7 +261,7 @@ static void t_inactive_no_apikey_no_header(void) {
|
|
|
246
261
|
bool present = true;
|
|
247
262
|
call_auth(r, 0, &present);
|
|
248
263
|
CHECK_INT_EQ(r->api, 1, "inactive: one call");
|
|
249
|
-
CHECK(!present, "inactive: no apikey means no
|
|
264
|
+
CHECK(!present, "inactive: no apikey means no credential header");
|
|
250
265
|
}
|
|
251
266
|
|
|
252
267
|
// ---- active: the chain ------------------------------------------------------
|
|
@@ -412,7 +427,7 @@ static void t_provider_error_fails_entity_op(void) {
|
|
|
412
427
|
FhOpResult res2 = fh_op(&h2, o);
|
|
413
428
|
CHECK(res2.ok, "control: the entity op with a working chain succeeds");
|
|
414
429
|
CHECK_INT_EQ(rec_count(calls2), 1, "control: the entity op reached the transport once");
|
|
415
|
-
CHECK(credential_is(get_str(rec_headers(calls2, 0),
|
|
430
|
+
CHECK(credential_is(get_str(rec_headers(calls2, 0), cred_header()), "CUSTOM01"),
|
|
416
431
|
"control: the entity op carried the credential");
|
|
417
432
|
}
|
|
418
433
|
|
|
@@ -753,7 +768,7 @@ static void t_exchange_test_mode_buys_nothing(void) {
|
|
|
753
768
|
FhOpResult res = fh_op(&h, o);
|
|
754
769
|
CHECK(res.ok, "test mode: the op succeeds");
|
|
755
770
|
CHECK_INT_EQ(rec_count(calls), 1, "test mode: one call, and it is the API call");
|
|
756
|
-
CHECK(credential_is(get_str(rec_headers(calls, 0),
|
|
771
|
+
CHECK(credential_is(get_str(rec_headers(calls, 0), cred_header()), "test-access_token"),
|
|
757
772
|
"test mode: buys nothing and carries the deterministic fake token");
|
|
758
773
|
CHECK_INT_EQ(fh_track_int(feats[0].f, "buys"), 1, "test mode: counted as a (fake) purchase");
|
|
759
774
|
}
|
|
@@ -369,16 +369,22 @@
|
|
|
369
369
|
(defn- reauth! [fa fetchdef token]
|
|
370
370
|
(let [headers (vs/getprop fetchdef "headers")]
|
|
371
371
|
(when (vs/ismap headers)
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
(
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
372
|
+
;; The header u-prepare-auth writes: auth.name, lower-cased, else the
|
|
373
|
+
;; name the API declares.
|
|
374
|
+
(let [given (vs/getpath (liveopts fa) "auth.name")
|
|
375
|
+
name (if (and (string? given) (not= "" given))
|
|
376
|
+
(.toLowerCase ^String given java.util.Locale/ROOT)
|
|
377
|
+
core/HEADER-AUTH)]
|
|
378
|
+
(if (nil? (auth-of fa))
|
|
379
|
+
;; Suppressed auth means NO header, the same answer u-prepare-auth
|
|
380
|
+
;; gives. Reached defensively - nothing calls this with auth
|
|
381
|
+
;; suppressed - but this is the function that writes the
|
|
382
|
+
;; credential, so it is where the rule has to hold.
|
|
383
|
+
(vs/delprop headers name)
|
|
384
|
+
(let [prefix (let [p (vs/getpath (liveopts fa) "auth.prefix")]
|
|
385
|
+
(if (string? p) p ""))]
|
|
386
|
+
(.put ^java.util.Map headers name
|
|
387
|
+
(if (= "" prefix) token (str prefix " " token)))))))))
|
|
382
388
|
|
|
383
389
|
|
|
384
390
|
(defn- spent? [fa res]
|
|
@@ -40,6 +40,15 @@
|
|
|
40
40
|
|
|
41
41
|
(def ^:private BASE "http://secrets.test/api")
|
|
42
42
|
|
|
43
|
+
;; The header the credential travels in: the DEFAULT client's auth.name,
|
|
44
|
+
;; lower-cased as u-prepare-auth sends it, else the name the API declares.
|
|
45
|
+
;; Read from a default client because a test's `auth: nil` leaves no name.
|
|
46
|
+
(def ^:private CREDHEADER
|
|
47
|
+
(let [name (vs/getpath (core/client-options-map (client/test-sdk nil nil)) "auth.name")]
|
|
48
|
+
(if (and (string? name) (not= "" name))
|
|
49
|
+
(.toLowerCase ^String name java.util.Locale/ROOT)
|
|
50
|
+
core/HEADER-AUTH)))
|
|
51
|
+
|
|
43
52
|
;; Named explicitly: a project that narrows the default allow.op set would
|
|
44
53
|
;; otherwise turn these into a false RED (the control leg refused before it
|
|
45
54
|
;; reached the transport), and the rule under test lives at the transport,
|
|
@@ -58,7 +67,7 @@
|
|
|
58
67
|
|
|
59
68
|
|
|
60
69
|
;; `reply` is (fn [n url fetchdef] -> [res err]); nil means 200 {ok true}.
|
|
61
|
-
;; The
|
|
70
|
+
;; The credential header is snapshotted AS A STRING at call time: the
|
|
62
71
|
;; fetchdef's header map is the live spec map the retry rewrites IN PLACE,
|
|
63
72
|
;; so holding the map would make every recorded call show the last token.
|
|
64
73
|
(defn- counting-fetch
|
|
@@ -69,7 +78,7 @@
|
|
|
69
78
|
(assoc stub :fetch
|
|
70
79
|
(fn [url fetchdef]
|
|
71
80
|
(let [headers (vs/getprop fetchdef "headers")
|
|
72
|
-
auth (when (vs/ismap headers) (vs/getprop headers
|
|
81
|
+
auth (when (vs/ismap headers) (vs/getprop headers CREDHEADER))
|
|
73
82
|
n (inc (count @calls))]
|
|
74
83
|
(swap! calls conj {:url url
|
|
75
84
|
:auth (when (some? auth) (str auth))
|
|
@@ -161,14 +170,14 @@
|
|
|
161
170
|
(deref (core/client-features sdk)))))
|
|
162
171
|
|
|
163
172
|
|
|
164
|
-
;; The
|
|
173
|
+
;; The credential header carries the SPEC's credential prefix, which a
|
|
165
174
|
;; TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
|
|
166
175
|
;; `Bearer <token>`, an apiKey scheme the raw token. So assert on the
|
|
167
176
|
;; CREDENTIAL and let the prefix be whatever this SDK's API declares.
|
|
168
177
|
(defn- credential-is [got token msg]
|
|
169
178
|
(let [s (str (or got ""))]
|
|
170
179
|
(t/is-true (or (= s token) (str/ends-with? s (str " " token)))
|
|
171
|
-
(str msg ": expected the
|
|
180
|
+
(str msg ": expected the " CREDHEADER " header to carry " token
|
|
172
181
|
", got " (pr-str got)))))
|
|
173
182
|
|
|
174
183
|
|
|
@@ -232,7 +241,7 @@
|
|
|
232
241
|
sdk (live-sdk stub)]
|
|
233
242
|
(client/direct sdk (vs/jm "path" "/thing"))
|
|
234
243
|
(t/is-eq (sent stub) 1 "one request")
|
|
235
|
-
(t/is-nil (:auth (call-at stub 0)) "no
|
|
244
|
+
(t/is-nil (:auth (call-at stub 0)) "no credential header"))))
|
|
236
245
|
|
|
237
246
|
;; ---- apikey KEEPS ITS MEANING ----
|
|
238
247
|
|
|
@@ -289,7 +298,7 @@
|
|
|
289
298
|
sdk (live-sdk stub :secrets (sekopts "providers" (vs/jt (miss-provider))))]
|
|
290
299
|
(client/direct sdk (vs/jm "path" "/thing"))
|
|
291
300
|
(t/is-eq (sent stub) 1 "the request still goes out")
|
|
292
|
-
(t/is-nil (:auth (call-at stub 0)) "no
|
|
301
|
+
(t/is-nil (:auth (call-at stub 0)) "no credential header"))))
|
|
293
302
|
|
|
294
303
|
;; ---- FAIL CLOSED: a provider ERROR is not a miss ----
|
|
295
304
|
;;
|
|
@@ -565,7 +574,7 @@
|
|
|
565
574
|
;; unsend it. The suppression is now honoured before the
|
|
566
575
|
;; first purchase.
|
|
567
576
|
(t/is-eq (sent stub) 1 (str "one API call, no purchase: " (wire stub)))
|
|
568
|
-
(t/is-nil (:auth (call-at stub 0)) "no
|
|
577
|
+
(t/is-nil (:auth (call-at stub 0)) "no credential header"))))
|
|
569
578
|
|
|
570
579
|
(chk "secrets-exchange-with-no-refresh-token-fails-closed"
|
|
571
580
|
(fn []
|
|
@@ -487,8 +487,8 @@ private:
|
|
|
487
487
|
|
|
488
488
|
// ---- credential injection ---------------------------------------------------
|
|
489
489
|
|
|
490
|
-
// Write the resolved credential into THIS request's
|
|
491
|
-
// the way prepareAuth builds it (same options auth
|
|
490
|
+
// Write the resolved credential into THIS request's credential header,
|
|
491
|
+
// the way prepareAuth builds it (same options auth name and prefix, same
|
|
492
492
|
// suppression rule), so the two cannot drift.
|
|
493
493
|
//
|
|
494
494
|
// `auth: null` is the documented way to send NO credential, and
|
|
@@ -503,15 +503,20 @@ private:
|
|
|
503
503
|
Value headers = getp(fetchdef, "headers");
|
|
504
504
|
if (!headers.is_map()) return;
|
|
505
505
|
|
|
506
|
+
// The header prepareAuth writes: auth.name, lower-cased.
|
|
507
|
+
std::string name = as_str(Struct::getpath(liveopts_, {"auth", "name"}));
|
|
508
|
+
if (name.empty()) name = "authorization";
|
|
509
|
+
for (auto& c : name) c = ('A' <= c && c <= 'Z') ? static_cast<char>(c - 'A' + 'a') : c;
|
|
510
|
+
|
|
506
511
|
if (authSuppressed()) {
|
|
507
|
-
map_remove(headers,
|
|
512
|
+
map_remove(headers, name);
|
|
508
513
|
return;
|
|
509
514
|
}
|
|
510
515
|
|
|
511
516
|
if (cred_.empty()) return;
|
|
512
517
|
|
|
513
518
|
std::string prefix = as_str(Struct::getpath(liveopts_, {"auth", "prefix"}));
|
|
514
|
-
map_put(headers,
|
|
519
|
+
map_put(headers, name,
|
|
515
520
|
Value(prefix.empty() ? cred_ : prefix + " " + cred_));
|
|
516
521
|
}
|
|
517
522
|
|
|
@@ -63,6 +63,19 @@ static int CASES = 0;
|
|
|
63
63
|
T_RUN(fn); \
|
|
64
64
|
} while (0)
|
|
65
65
|
|
|
66
|
+
// The header the credential travels in: the DEFAULT client's auth.name,
|
|
67
|
+
// lower-cased as prepareAuth sends it. Read from a default client because a
|
|
68
|
+
// test's `auth: null` leaves no name to read.
|
|
69
|
+
static const std::string& credHeader() {
|
|
70
|
+
static const std::string name = [] {
|
|
71
|
+
std::string n = as_str(Struct::getpath(ProjectNameSDK::testSDK()->options, {"auth", "name"}));
|
|
72
|
+
if (n.empty()) return std::string("authorization");
|
|
73
|
+
for (auto& c : n) c = ('A' <= c && c <= 'Z') ? static_cast<char>(c - 'A' + 'a') : c;
|
|
74
|
+
return n;
|
|
75
|
+
}();
|
|
76
|
+
return name;
|
|
77
|
+
}
|
|
78
|
+
|
|
66
79
|
// ---- the recording live transport ------------------------------------------
|
|
67
80
|
|
|
68
81
|
struct Rec {
|
|
@@ -76,7 +89,7 @@ struct Rec {
|
|
|
76
89
|
std::string tokenfield = "access_token";
|
|
77
90
|
|
|
78
91
|
Value fetch(const std::string& url, const Value& fetchdef) {
|
|
79
|
-
// SNAPSHOT the fetchdef: the exchange retry rewrites the
|
|
92
|
+
// SNAPSHOT the fetchdef: the exchange retry rewrites the credential
|
|
80
93
|
// header of the same map in place (as go does), so a recorded reference
|
|
81
94
|
// would show every attempt carrying the LAST token.
|
|
82
95
|
calls.push_back(vmap({{"url", Value(url)}, {"fetchdef", Struct::clone(fetchdef)}}));
|
|
@@ -94,19 +107,19 @@ struct Rec {
|
|
|
94
107
|
return fhResponse(status, vmap({{"ok", Value(true)}, {"n", Value(n)}}), Value::undef());
|
|
95
108
|
}
|
|
96
109
|
|
|
97
|
-
// The
|
|
110
|
+
// The credential header the i-th call left with ("" when none).
|
|
98
111
|
std::string auth(size_t i) const {
|
|
99
112
|
if (i >= calls.size()) return "";
|
|
100
113
|
Value fd = getp(calls[i], "fetchdef");
|
|
101
114
|
Value h = getp(fd, "headers");
|
|
102
|
-
Value a = getp(h,
|
|
115
|
+
Value a = getp(h, credHeader());
|
|
103
116
|
return a.is_string() ? a.as_string() : "";
|
|
104
117
|
}
|
|
105
118
|
|
|
106
119
|
bool hasAuth(size_t i) const {
|
|
107
120
|
if (i >= calls.size()) return false;
|
|
108
121
|
Value h = getp(getp(calls[i], "fetchdef"), "headers");
|
|
109
|
-
return h.is_map() && !mapget(h,
|
|
122
|
+
return h.is_map() && !mapget(h, credHeader()).is_undef();
|
|
110
123
|
}
|
|
111
124
|
|
|
112
125
|
std::string body(size_t i) const {
|
|
@@ -373,7 +386,7 @@ static void t_provider_error_fails_entity_op() {
|
|
|
373
386
|
FhOpResult cr = ch->op(fhOp("load"));
|
|
374
387
|
ASSERT_TRUE(cr.ok, "entity control: op failed: " + (cr.err ? cr.err->msg : std::string("")));
|
|
375
388
|
ASSERT_EQ(crec->calls.size(), (size_t)1, "entity control: exactly one call");
|
|
376
|
-
Value auth = getp(crec->headers(0),
|
|
389
|
+
Value auth = getp(crec->headers(0), credHeader());
|
|
377
390
|
ASSERT_EQ(as_str(auth), expectAuth(ch->client.get(), "CHAINKEY"),
|
|
378
391
|
"entity control: the chain credential reached the header");
|
|
379
392
|
}
|
|
@@ -745,7 +758,7 @@ static void t_exchange_test_mode_buys_nothing() {
|
|
|
745
758
|
FhOpResult r = h->op(fhOp("load"));
|
|
746
759
|
ASSERT_TRUE(r.ok, "test mode: op failed: " + (r.err ? r.err->msg : std::string("")));
|
|
747
760
|
ASSERT_EQ(rec->calls.size(), (size_t)1, "test mode: exactly one call, and it is the API call");
|
|
748
|
-
Value auth = getp(rec->headers(0),
|
|
761
|
+
Value auth = getp(rec->headers(0), credHeader());
|
|
749
762
|
ASSERT_EQ(as_str(auth), expectAuth(h->client.get(), "test-access_token"),
|
|
750
763
|
"test mode: the deterministic fake token");
|
|
751
764
|
}
|
|
@@ -762,7 +775,7 @@ static void t_entity_op_gets_chain_credential() {
|
|
|
762
775
|
FhOpResult r = h->op(fhOp("list"));
|
|
763
776
|
ASSERT_TRUE(r.ok, "entity: op failed: " + (r.err ? r.err->msg : std::string("")));
|
|
764
777
|
ASSERT_EQ(rec->calls.size(), (size_t)1, "entity: one call");
|
|
765
|
-
ASSERT_EQ(as_str(getp(rec->headers(0),
|
|
778
|
+
ASSERT_EQ(as_str(getp(rec->headers(0), credHeader())), expectAuth(h->client.get(), "CHAINKEY"),
|
|
766
779
|
"entity: the chain credential on the entity path");
|
|
767
780
|
}
|
|
768
781
|
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
//
|
|
39
39
|
// ONE VISIBLE CONSEQUENCE, worth knowing before it surprises someone:
|
|
40
40
|
// ProjectNameSDK.Prepare() returns the fetchdef WITHOUT crossing the
|
|
41
|
-
// transport, so its
|
|
41
|
+
// transport, so its credential header is the one PrepareAuth built from
|
|
42
42
|
// the apikey OPTION - never the chain-resolved credential. Prepare is a
|
|
43
43
|
// what-would-I-send inspector; what is actually sent is what Direct,
|
|
44
44
|
// Graphql and the entity ops put on the wire, and those all cross the seam.
|
|
@@ -524,11 +524,11 @@ public class SecretsFeature : BaseFeature
|
|
|
524
524
|
// Buy a token and try the request again when the API says the current
|
|
525
525
|
// one is spent.
|
|
526
526
|
//
|
|
527
|
-
// The retry rewrites the
|
|
527
|
+
// The retry rewrites the credential header IN PLACE on the fetchdef,
|
|
528
528
|
// because the header was built by the synchronous PrepareAuth before
|
|
529
529
|
// this request left and carries the token that just failed. Rebuilt the
|
|
530
|
-
// way PrepareAuth builds it, from the same options auth
|
|
531
|
-
// two cannot drift.
|
|
530
|
+
// way PrepareAuth builds it, from the same options auth name and prefix,
|
|
531
|
+
// so the two cannot drift.
|
|
532
532
|
private object? WithRefresh(Context ctx, string url,
|
|
533
533
|
Dictionary<string, object?> fetchdef, FetcherFunc inner)
|
|
534
534
|
{
|
|
@@ -639,20 +639,24 @@ public class SecretsFeature : BaseFeature
|
|
|
639
639
|
return;
|
|
640
640
|
}
|
|
641
641
|
|
|
642
|
+
// The header PrepareAuth writes: auth.name, lower-cased.
|
|
643
|
+
var name = StructUtils.GetPath(_liveopts, StructUtils.Jt("auth", "name"))
|
|
644
|
+
is string n && "" != n ? n.ToLowerInvariant() : "authorization";
|
|
645
|
+
|
|
642
646
|
// Suppressed auth means NO header, the same answer PrepareAuth
|
|
643
647
|
// gives. Reached defensively - WithRefresh does not retry at all
|
|
644
648
|
// when auth is suppressed - but this is the function that writes
|
|
645
649
|
// the credential, so it is where the rule has to hold.
|
|
646
650
|
if (AuthSuppressed())
|
|
647
651
|
{
|
|
648
|
-
headers.Remove(
|
|
652
|
+
headers.Remove(name);
|
|
649
653
|
return;
|
|
650
654
|
}
|
|
651
655
|
|
|
652
656
|
var prefix = StructUtils.GetPath(_liveopts, StructUtils.Jt("auth", "prefix"))
|
|
653
657
|
as string ?? "";
|
|
654
658
|
|
|
655
|
-
headers[
|
|
659
|
+
headers[name] = "" == prefix ? token : prefix + " " + token;
|
|
656
660
|
}
|
|
657
661
|
|
|
658
662
|
// ---------------------------------------------------------------
|
|
@@ -106,7 +106,7 @@ internal class Wire
|
|
|
106
106
|
|
|
107
107
|
if (fetchdef.TryGetValue("headers", out var hraw) &&
|
|
108
108
|
hraw is Dictionary<string, object?> headers &&
|
|
109
|
-
headers.TryGetValue(
|
|
109
|
+
headers.TryGetValue(SecretsFeatureTest.CredHeader, out var auth))
|
|
110
110
|
{
|
|
111
111
|
call.HasAuth = true;
|
|
112
112
|
call.Auth = auth as string ?? "";
|
|
@@ -176,7 +176,19 @@ public class SecretsFeatureTest
|
|
|
176
176
|
private const string EnvPrefix = "PROJECTENV_TEST_SECRETS_";
|
|
177
177
|
private const string SecretsBase = "http://secrets.test/api";
|
|
178
178
|
|
|
179
|
-
// The
|
|
179
|
+
// The header the credential travels in: the DEFAULT client's auth.name,
|
|
180
|
+
// lower-cased as PrepareAuth sends it. Read from a default client because
|
|
181
|
+
// a test's `auth: null` leaves no name to read.
|
|
182
|
+
internal static readonly string CredHeader = DefaultCredHeader();
|
|
183
|
+
|
|
184
|
+
private static string DefaultCredHeader()
|
|
185
|
+
{
|
|
186
|
+
var auth = StructUtils.GetProp(ProjectNameSDK.TestSDK(null, null).OptionsMap(), "auth");
|
|
187
|
+
return StructUtils.GetProp(auth, "name") is string name && "" != name
|
|
188
|
+
? name.ToLowerInvariant() : "authorization";
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// The credential header carries the SPEC's credential prefix, which a
|
|
180
192
|
// TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
|
|
181
193
|
// `Bearer <token>`, an apiKey scheme the raw token. So assert on the
|
|
182
194
|
// CREDENTIAL and let the prefix be whatever this SDK's API declares -
|
|
@@ -186,7 +198,7 @@ public class SecretsFeatureTest
|
|
|
186
198
|
{
|
|
187
199
|
var got = header ?? "";
|
|
188
200
|
Assert.True(got == token || got.EndsWith(" " + token),
|
|
189
|
-
"expected the
|
|
201
|
+
"expected the " + CredHeader + " header to carry " + token + ", got: " + got);
|
|
190
202
|
}
|
|
191
203
|
|
|
192
204
|
// Construct the client and ADOPT the feature via `extend` ONLY when the
|
|
@@ -395,22 +407,22 @@ public class SecretsFeatureTest
|
|
|
395
407
|
var fetchdef = client.Prepare(new Dictionary<string, object?> { ["path"] = "/" });
|
|
396
408
|
var headers = fetchdef["headers"] as Dictionary<string, object?>;
|
|
397
409
|
|
|
398
|
-
CredentialIs(headers![
|
|
410
|
+
CredentialIs(headers![CredHeader] as string ?? "", "OPTKEY01");
|
|
399
411
|
|
|
400
412
|
Assert.True(null == SecretsOf(client),
|
|
401
413
|
"no model activation and no extend: the feature must not be installed");
|
|
402
414
|
}
|
|
403
415
|
|
|
404
416
|
[Fact]
|
|
405
|
-
public void
|
|
417
|
+
public void InactiveNoApikeyMeansNoCredentialHeader()
|
|
406
418
|
{
|
|
407
419
|
var client = ProjectNameSDK.TestSDK(null, null);
|
|
408
420
|
|
|
409
421
|
var fetchdef = client.Prepare(new Dictionary<string, object?> { ["path"] = "/" });
|
|
410
422
|
var headers = fetchdef["headers"] as Dictionary<string, object?>;
|
|
411
423
|
|
|
412
|
-
Assert.False(headers!.ContainsKey(
|
|
413
|
-
"an SDK with no apikey must send no
|
|
424
|
+
Assert.False(headers!.ContainsKey(CredHeader),
|
|
425
|
+
"an SDK with no apikey must send no " + CredHeader + " header");
|
|
414
426
|
}
|
|
415
427
|
|
|
416
428
|
|
|
@@ -626,7 +638,7 @@ public class SecretsFeatureTest
|
|
|
626
638
|
|
|
627
639
|
// With `cache: false`, a provider that answered once and then reports a
|
|
628
640
|
// MISS (a revoked secret) must RETRACT the credential: the next request
|
|
629
|
-
// must go out with no
|
|
641
|
+
// must go out with no credential header.
|
|
630
642
|
[Fact]
|
|
631
643
|
public void UncachedMissRetractsTheCredential()
|
|
632
644
|
{
|
|
@@ -1648,7 +1660,7 @@ public class SecretsFeatureTest
|
|
|
1648
1660
|
}
|
|
1649
1661
|
else
|
|
1650
1662
|
{
|
|
1651
|
-
gotauth = ctx.Request.Headers[
|
|
1663
|
+
gotauth = ctx.Request.Headers[CredHeader] ?? "";
|
|
1652
1664
|
payload = "{}";
|
|
1653
1665
|
served.Set();
|
|
1654
1666
|
}
|
|
@@ -562,7 +562,7 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
562
562
|
# Buy a token and try the request again when the API says the current one
|
|
563
563
|
# is spent.
|
|
564
564
|
#
|
|
565
|
-
# The retry rewrites the
|
|
565
|
+
# The retry rewrites the credential header IN PLACE on the fetchdef,
|
|
566
566
|
# because the header was built by the synchronous prepare_auth before this
|
|
567
567
|
# request left and it carries the token that just failed. Rebuilt the way
|
|
568
568
|
# prepare_auth builds it, from the same options auth.prefix, so the two
|
|
@@ -623,12 +623,16 @@ defmodule ProjectName.Feature.Secrets do
|
|
|
623
623
|
# Reached defensively - the transport does not resolve at all when
|
|
624
624
|
# auth is suppressed - but this is the function that writes the
|
|
625
625
|
# credential, so it is where the rule has to hold.
|
|
626
|
+
# The header prepare_auth writes: auth.name, lower-cased.
|
|
627
|
+
name = S.getpath(S.getprop(f, "_liveopts"), "auth.name")
|
|
628
|
+
name = if is_binary(name) and name != "", do: String.downcase(name), else: "authorization"
|
|
629
|
+
|
|
626
630
|
if suppressed?(f) or token == "" do
|
|
627
|
-
S.delprop(headers,
|
|
631
|
+
S.delprop(headers, name)
|
|
628
632
|
else
|
|
629
633
|
prefix = S.getpath(S.getprop(f, "_liveopts"), "auth.prefix")
|
|
630
634
|
prefix = if is_binary(prefix), do: prefix, else: ""
|
|
631
|
-
S.setprop(headers,
|
|
635
|
+
S.setprop(headers, name, if(prefix != "", do: prefix <> " " <> token, else: token))
|
|
632
636
|
end
|
|
633
637
|
end
|
|
634
638
|
|
|
@@ -39,6 +39,14 @@ defmodule ProjectName.SecretsTest do
|
|
|
39
39
|
@envprefix "PROJECTENV_TEST_SECRETS_"
|
|
40
40
|
@base "http://raw.test/api"
|
|
41
41
|
|
|
42
|
+
# The header the credential travels in: the DEFAULT client's auth.name,
|
|
43
|
+
# lower-cased as prepare_auth sends it. Read from a default client because
|
|
44
|
+
# a test's `auth: nil` leaves no name to read.
|
|
45
|
+
defp credheader do
|
|
46
|
+
name = S.getpath(ProjectName.options_map(ProjectName.test()), "auth.name")
|
|
47
|
+
if is_binary(name) and name != "", do: String.downcase(name), else: "authorization"
|
|
48
|
+
end
|
|
49
|
+
|
|
42
50
|
# ---- the recorder --------------------------------------------------------
|
|
43
51
|
|
|
44
52
|
# A transport that records every call it is handed, so "sent" is a fact
|
|
@@ -56,13 +64,14 @@ defmodule ProjectName.SecretsTest do
|
|
|
56
64
|
apistatus = Map.get(opts, :api, [200])
|
|
57
65
|
resfield = Map.get(opts, :response, "access_token")
|
|
58
66
|
tokenfail = Map.get(opts, :tokenfail, nil)
|
|
67
|
+
credheader = credheader()
|
|
59
68
|
|
|
60
69
|
fetch = fn url, fetchdef ->
|
|
61
70
|
headers = S.getprop(fetchdef, "headers")
|
|
62
71
|
|
|
63
72
|
auth =
|
|
64
73
|
if S.ismap(headers),
|
|
65
|
-
do: ProjectName.Feature.header_get(headers,
|
|
74
|
+
do: ProjectName.Feature.header_get(headers, credheader),
|
|
66
75
|
else: nil
|
|
67
76
|
|
|
68
77
|
[{:calls, cs}] = :ets.lookup(t, :calls)
|
|
@@ -163,7 +172,7 @@ defmodule ProjectName.SecretsTest do
|
|
|
163
172
|
|
|
164
173
|
# ---- assertions ----------------------------------------------------------
|
|
165
174
|
|
|
166
|
-
# The
|
|
175
|
+
# The credential header carries the SPEC's credential prefix, which a
|
|
167
176
|
# TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
|
|
168
177
|
# `Bearer <token>`, an apiKey scheme the raw token. So assert on the
|
|
169
178
|
# CREDENTIAL and let the prefix be whatever this SDK's API declares -
|
|
@@ -173,7 +182,8 @@ defmodule ProjectName.SecretsTest do
|
|
|
173
182
|
got = if is_binary(header), do: header, else: ""
|
|
174
183
|
|
|
175
184
|
assert got == token or String.ends_with?(got, " " <> token),
|
|
176
|
-
"expected the
|
|
185
|
+
"expected the " <> credheader() <> " header to carry " <> token <> ", got: " <>
|
|
186
|
+
inspect(header)
|
|
177
187
|
end
|
|
178
188
|
|
|
179
189
|
defp auth_is(call, token), do: credential_is(if(call == nil, do: nil, else: call.auth), token)
|
|
@@ -223,7 +233,7 @@ defmodule ProjectName.SecretsTest do
|
|
|
223
233
|
assert Secrets.sekreto(client) == nil
|
|
224
234
|
end
|
|
225
235
|
|
|
226
|
-
test "inactive: no apikey means no
|
|
236
|
+
test "inactive: no apikey means no credential header" do
|
|
227
237
|
r = recorder()
|
|
228
238
|
client = sdk(r, [])
|
|
229
239
|
ProjectName.direct(client, S.jm(["path", "/thing"]))
|
|
@@ -794,7 +804,7 @@ defmodule ProjectName.SecretsTest do
|
|
|
794
804
|
#
|
|
795
805
|
# The shape these tests pin: what the chain resolves is a REFRESH token,
|
|
796
806
|
# which is POSTed to a token endpoint for a short-lived ACCESS token; the
|
|
797
|
-
# access token is what the
|
|
807
|
+
# access token is what the credential header carries; and when the API
|
|
798
808
|
# answers 401 the client buys another and tries the same request again,
|
|
799
809
|
# once.
|
|
800
810
|
#
|
|
@@ -393,11 +393,18 @@ func (f *SecretsFeature) reauth(fetchdef map[string]any, token string) {
|
|
|
393
393
|
return
|
|
394
394
|
}
|
|
395
395
|
|
|
396
|
+
// The header prepareAuth wrote: auth.name, lower-cased.
|
|
397
|
+
name, _ := auth["name"].(string)
|
|
398
|
+
if "" == name {
|
|
399
|
+
name = "authorization"
|
|
400
|
+
}
|
|
401
|
+
name = strings.ToLower(name)
|
|
402
|
+
|
|
396
403
|
prefix, _ := auth["prefix"].(string)
|
|
397
404
|
if "" == prefix {
|
|
398
|
-
headers[
|
|
405
|
+
headers[name] = token
|
|
399
406
|
} else {
|
|
400
|
-
headers[
|
|
407
|
+
headers[name] = prefix + " " + token
|
|
401
408
|
}
|
|
402
409
|
}
|
|
403
410
|
|