@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
|
@@ -20,6 +20,17 @@ import (
|
|
|
20
20
|
|
|
21
21
|
const envprefix = "PROJECTENV_TEST_SECRETS_"
|
|
22
22
|
|
|
23
|
+
// credheader is the header the credential travels in: the DEFAULT
|
|
24
|
+
// client's auth.name, lower-cased as prepare_auth sends it. Read from a
|
|
25
|
+
// default client because a test's `auth: nil` leaves no name to read.
|
|
26
|
+
var credheader = func() string {
|
|
27
|
+
auth, _ := sdk.TestSDK(nil, nil).OptionsMap()["auth"].(map[string]any)
|
|
28
|
+
if name, _ := auth["name"].(string); "" != name {
|
|
29
|
+
return strings.ToLower(name)
|
|
30
|
+
}
|
|
31
|
+
return "authorization"
|
|
32
|
+
}()
|
|
33
|
+
|
|
23
34
|
// ---------------------------------------------------------------------
|
|
24
35
|
// The recording transport: system.fetch for a LIVE client, scripting one
|
|
25
36
|
// status per API call (the last repeating) and a token endpoint for the
|
|
@@ -59,7 +70,7 @@ func (w *wire) fetch(url string, fetchdef map[string]any) (map[string]any, error
|
|
|
59
70
|
auth := ""
|
|
60
71
|
has := false
|
|
61
72
|
if headers, is := fetchdef["headers"].(map[string]any); is {
|
|
62
|
-
if v, found := headers[
|
|
73
|
+
if v, found := headers[credheader]; found {
|
|
63
74
|
auth, _ = v.(string)
|
|
64
75
|
has = true
|
|
65
76
|
}
|
|
@@ -123,13 +134,13 @@ func (w *wire) token() []wirecall {
|
|
|
123
134
|
// ---------------------------------------------------------------------
|
|
124
135
|
// Support.
|
|
125
136
|
|
|
126
|
-
// The
|
|
127
|
-
// TEMPLATE cannot know - so assert on the CREDENTIAL and let the
|
|
128
|
-
// whatever this SDK's API declares.
|
|
137
|
+
// The credential header carries the SPEC's credential prefix (if any),
|
|
138
|
+
// which a TEMPLATE cannot know - so assert on the CREDENTIAL and let the
|
|
139
|
+
// prefix be whatever this SDK's API declares.
|
|
129
140
|
func credentialIs(t *testing.T, header string, token string) {
|
|
130
141
|
t.Helper()
|
|
131
142
|
if header != token && !strings.HasSuffix(header, " "+token) {
|
|
132
|
-
t.Fatalf("expected the
|
|
143
|
+
t.Fatalf("expected the %s header to carry %s, got: %q", credheader, token, header)
|
|
133
144
|
}
|
|
134
145
|
}
|
|
135
146
|
|
|
@@ -319,7 +330,7 @@ func TestSecretsInactive(t *testing.T) {
|
|
|
319
330
|
t.Fatalf("prepare failed: %v", err)
|
|
320
331
|
}
|
|
321
332
|
headers, _ := fetchdef["headers"].(map[string]any)
|
|
322
|
-
auth, _ := headers[
|
|
333
|
+
auth, _ := headers[credheader].(string)
|
|
323
334
|
credentialIs(t, auth, "OPTKEY01")
|
|
324
335
|
|
|
325
336
|
if nil != secretsFeatureOf(client) {
|
|
@@ -327,15 +338,15 @@ func TestSecretsInactive(t *testing.T) {
|
|
|
327
338
|
}
|
|
328
339
|
})
|
|
329
340
|
|
|
330
|
-
t.Run("no apikey means no
|
|
341
|
+
t.Run("no apikey means no credential header", func(t *testing.T) {
|
|
331
342
|
client := sdk.TestSDK(nil, nil)
|
|
332
343
|
fetchdef, err := client.Prepare(map[string]any{"path": "/"})
|
|
333
344
|
if err != nil {
|
|
334
345
|
t.Fatalf("prepare failed: %v", err)
|
|
335
346
|
}
|
|
336
347
|
headers, _ := fetchdef["headers"].(map[string]any)
|
|
337
|
-
if _, has := headers[
|
|
338
|
-
t.Fatalf("unexpected
|
|
348
|
+
if _, has := headers[credheader]; has {
|
|
349
|
+
t.Fatalf("unexpected %s header: %v", credheader, headers[credheader])
|
|
339
350
|
}
|
|
340
351
|
})
|
|
341
352
|
}
|
|
@@ -730,7 +741,7 @@ func TestSecretsExchangeRawFetch(t *testing.T) {
|
|
|
730
741
|
return
|
|
731
742
|
}
|
|
732
743
|
mu.Lock()
|
|
733
|
-
gotauth = req.Header.Get(
|
|
744
|
+
gotauth = req.Header.Get(credheader)
|
|
734
745
|
mu.Unlock()
|
|
735
746
|
res.Header().Set("content-type", "application/json")
|
|
736
747
|
res.Write([]byte(`{}`))
|
|
@@ -457,11 +457,11 @@ public class SecretsFeature extends BaseFeature {
|
|
|
457
457
|
// withrefresh buys a token and tries the request again when the API says
|
|
458
458
|
// the current one is spent.
|
|
459
459
|
//
|
|
460
|
-
// The retry rewrites the
|
|
460
|
+
// The retry rewrites the credential header IN PLACE on the fetchdef,
|
|
461
461
|
// because the header was built by the synchronous prepareAuth before this
|
|
462
462
|
// request left, and it carries the token that just failed. Rebuilt the
|
|
463
|
-
// way prepareAuth builds it, from the same options auth
|
|
464
|
-
// two cannot drift.
|
|
463
|
+
// way prepareAuth builds it, from the same options auth name and prefix,
|
|
464
|
+
// so the two cannot drift.
|
|
465
465
|
private Object withrefresh(Context ctx, String url, Map<String, Object> fetchdef,
|
|
466
466
|
Utility.FetcherFn inner) {
|
|
467
467
|
|
|
@@ -554,14 +554,19 @@ public class SecretsFeature extends BaseFeature {
|
|
|
554
554
|
return;
|
|
555
555
|
}
|
|
556
556
|
|
|
557
|
+
// The header prepareAuth wrote: auth.name, lower-cased.
|
|
558
|
+
Object nameRaw = ((Map<String, Object>) rawauth).get("name");
|
|
559
|
+
String name = nameRaw instanceof String && !"".equals(nameRaw)
|
|
560
|
+
? ((String) nameRaw).toLowerCase(java.util.Locale.ROOT) : "authorization";
|
|
561
|
+
|
|
557
562
|
Object prefixRaw = ((Map<String, Object>) rawauth).get("prefix");
|
|
558
563
|
String prefix = prefixRaw instanceof String ? (String) prefixRaw : "";
|
|
559
564
|
|
|
560
565
|
if ("".equals(prefix)) {
|
|
561
|
-
headers.put(
|
|
566
|
+
headers.put(name, token);
|
|
562
567
|
}
|
|
563
568
|
else {
|
|
564
|
-
headers.put(
|
|
569
|
+
headers.put(name, prefix + " " + token);
|
|
565
570
|
}
|
|
566
571
|
}
|
|
567
572
|
|
|
@@ -65,6 +65,18 @@ public class SecretsFeatureTest {
|
|
|
65
65
|
|
|
66
66
|
static final String BASE = "http://secrets.test/api";
|
|
67
67
|
|
|
68
|
+
// The header the credential travels in: the DEFAULT client's auth.name,
|
|
69
|
+
// lower-cased as prepareAuth sends it. Read from a default client because
|
|
70
|
+
// a test's `auth: null` leaves no name to read.
|
|
71
|
+
static final String CREDHEADER = credheader();
|
|
72
|
+
|
|
73
|
+
static String credheader() {
|
|
74
|
+
Object auth = ProjectNameSDK.testSDK(null, null).optionsMap().get("auth");
|
|
75
|
+
Object name = auth instanceof Map ? ((Map<String, Object>) auth).get("name") : null;
|
|
76
|
+
return name instanceof String && !"".equals(name)
|
|
77
|
+
? ((String) name).toLowerCase(java.util.Locale.ROOT) : "authorization";
|
|
78
|
+
}
|
|
79
|
+
|
|
68
80
|
// ------------------------------------------------------------------
|
|
69
81
|
// The recording transport: system.fetch for a LIVE client, scripting one
|
|
70
82
|
// status per API call (the last repeating) and a token endpoint for the
|
|
@@ -100,9 +112,9 @@ public class SecretsFeatureTest {
|
|
|
100
112
|
Object headers = fetchdef == null ? null : fetchdef.get("headers");
|
|
101
113
|
if (headers instanceof Map) {
|
|
102
114
|
Map<String, Object> hm = (Map<String, Object>) headers;
|
|
103
|
-
if (hm.containsKey(
|
|
115
|
+
if (hm.containsKey(CREDHEADER)) {
|
|
104
116
|
has = true;
|
|
105
|
-
Object v = hm.get(
|
|
117
|
+
Object v = hm.get(CREDHEADER);
|
|
106
118
|
auth = v instanceof String ? (String) v : "";
|
|
107
119
|
}
|
|
108
120
|
}
|
|
@@ -173,7 +185,7 @@ public class SecretsFeatureTest {
|
|
|
173
185
|
// ------------------------------------------------------------------
|
|
174
186
|
// Support.
|
|
175
187
|
|
|
176
|
-
// The
|
|
188
|
+
// The credential header carries the SPEC's credential prefix, which a
|
|
177
189
|
// TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
|
|
178
190
|
// `Bearer <token>`, an apiKey scheme the raw token. So assert on the
|
|
179
191
|
// CREDENTIAL and let the prefix be whatever this SDK's API declares -
|
|
@@ -182,7 +194,7 @@ public class SecretsFeatureTest {
|
|
|
182
194
|
static void credentialIs(String header, String token) {
|
|
183
195
|
String got = header == null ? "" : header;
|
|
184
196
|
assertTrue(got.equals(token) || got.endsWith(" " + token),
|
|
185
|
-
"expected the
|
|
197
|
+
"expected the " + CREDHEADER + " header to carry " + token + ", got: \"" + got + "\"");
|
|
186
198
|
}
|
|
187
199
|
|
|
188
200
|
static Map<String, Object> map(Object... kv) {
|
|
@@ -371,21 +383,21 @@ public class SecretsFeatureTest {
|
|
|
371
383
|
|
|
372
384
|
Map<String, Object> fetchdef = client.prepare(map("path", "/"));
|
|
373
385
|
Map<String, Object> headers = (Map<String, Object>) fetchdef.get("headers");
|
|
374
|
-
credentialIs((String) headers.get(
|
|
386
|
+
credentialIs((String) headers.get(CREDHEADER), "OPTKEY01");
|
|
375
387
|
|
|
376
388
|
assertNull(featureOf(client),
|
|
377
389
|
"the feature is switched off: it must not be installed");
|
|
378
390
|
}
|
|
379
391
|
|
|
380
392
|
@Test
|
|
381
|
-
public void
|
|
393
|
+
public void inactive_noApikeyMeansNoCredentialHeader() {
|
|
382
394
|
ProjectNameSDK client = ProjectNameSDK.testSDK(null, map(
|
|
383
395
|
"feature", map("secrets", map("active", false))));
|
|
384
396
|
|
|
385
397
|
Map<String, Object> fetchdef = client.prepare(map("path", "/"));
|
|
386
398
|
Map<String, Object> headers = (Map<String, Object>) fetchdef.get("headers");
|
|
387
|
-
assertFalse(headers.containsKey(
|
|
388
|
-
"unexpected
|
|
399
|
+
assertFalse(headers.containsKey(CREDHEADER),
|
|
400
|
+
"unexpected " + CREDHEADER + " header: " + headers.get(CREDHEADER));
|
|
389
401
|
}
|
|
390
402
|
|
|
391
403
|
// ------------------------------------------------------------------
|
|
@@ -926,7 +938,7 @@ public class SecretsFeatureTest {
|
|
|
926
938
|
//
|
|
927
939
|
// What the chain resolves is a REFRESH token, which is POSTed to a token
|
|
928
940
|
// endpoint for a short-lived ACCESS token; the access token is what the
|
|
929
|
-
//
|
|
941
|
+
// credential header carries; and when the API answers 401 the client
|
|
930
942
|
// buys another and tries the same request again, once.
|
|
931
943
|
|
|
932
944
|
static Map<String, Object> exchangeOpts(Object... extra) {
|
|
@@ -367,11 +367,11 @@ class SecretsFeature extends BaseFeature {
|
|
|
367
367
|
// Buy a token and try the request again when the API says the current one
|
|
368
368
|
// is spent. Wraps the transport; installed only when exchanging.
|
|
369
369
|
//
|
|
370
|
-
// The retry rewrites the
|
|
370
|
+
// The retry rewrites the credential header IN PLACE on the fetchdef,
|
|
371
371
|
// because the header was built by the synchronous prepareAuth before this
|
|
372
372
|
// request left, and it carries the token that just failed. Rebuilt the
|
|
373
|
-
// way prepareAuth builds it, from the same options.auth
|
|
374
|
-
// two cannot drift.
|
|
373
|
+
// way prepareAuth builds it, from the same options.auth name and prefix,
|
|
374
|
+
// so the two cannot drift.
|
|
375
375
|
async _withRefresh(ctx, url, fetchdef, inner) {
|
|
376
376
|
const x = this._exchange
|
|
377
377
|
const max = null == x ? 0 : x.retries
|
|
@@ -457,8 +457,12 @@ class SecretsFeature extends BaseFeature {
|
|
|
457
457
|
return
|
|
458
458
|
}
|
|
459
459
|
|
|
460
|
+
// The header prepareAuth wrote: auth.name, lower-cased.
|
|
461
|
+
const name = 'string' === typeof auth.name && '' !== auth.name ?
|
|
462
|
+
auth.name.toLowerCase() : 'authorization'
|
|
463
|
+
|
|
460
464
|
const prefix = auth.prefix
|
|
461
|
-
fetchdef.headers
|
|
465
|
+
fetchdef.headers[name] = prefix ? prefix + ' ' + token : token
|
|
462
466
|
}
|
|
463
467
|
}
|
|
464
468
|
|
|
@@ -21,6 +21,17 @@ const { SDK } = require('../../utility/index')
|
|
|
21
21
|
const ENVPREFIX = 'PROJECTENV_TEST_SECRETS_'
|
|
22
22
|
|
|
23
23
|
|
|
24
|
+
// The header the credential travels in: the DEFAULT client's auth.name,
|
|
25
|
+
// lower-cased as prepareAuth sends it. Read from a default client because
|
|
26
|
+
// a test's `auth: null` leaves no name to read.
|
|
27
|
+
const CREDHEADER = (() => {
|
|
28
|
+
const auth = SDK.test({}, {}).options().auth
|
|
29
|
+
const name = null == auth ? null : auth.name
|
|
30
|
+
return 'string' === typeof name && '' !== name ?
|
|
31
|
+
name.toLowerCase() : 'authorization'
|
|
32
|
+
})()
|
|
33
|
+
|
|
34
|
+
|
|
24
35
|
// prepare() returns the fetchdef the transport would receive — the closest
|
|
25
36
|
// observable point to the wire for header assertions — and it awaits
|
|
26
37
|
// secrets resolution itself, because it bypasses the feature hook pipeline.
|
|
@@ -32,7 +43,7 @@ async function prepared(sdkopts) {
|
|
|
32
43
|
}
|
|
33
44
|
|
|
34
45
|
|
|
35
|
-
// The
|
|
46
|
+
// The credential header carries the SPEC's credential prefix, which a
|
|
36
47
|
// TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
|
|
37
48
|
// `Bearer <token>`, an apiKey scheme the raw token. So assert on the
|
|
38
49
|
// CREDENTIAL and let the prefix be whatever this SDK's API declares —
|
|
@@ -41,7 +52,7 @@ async function prepared(sdkopts) {
|
|
|
41
52
|
function credentialIs(header, token) {
|
|
42
53
|
const got = String(null == header ? '' : header)
|
|
43
54
|
assert.ok(got === token || got.endsWith(' ' + token),
|
|
44
|
-
'expected the
|
|
55
|
+
'expected the ' + CREDHEADER + ' header to carry ' + token + ', got: ' + got)
|
|
45
56
|
}
|
|
46
57
|
|
|
47
58
|
|
|
@@ -67,16 +78,16 @@ describe('secrets', () => {
|
|
|
67
78
|
|
|
68
79
|
test('inactive: apikey option behaves exactly as before', async () => {
|
|
69
80
|
const { sdk, fetchdef } = await prepared({ apikey: 'OPTKEY01' })
|
|
70
|
-
credentialIs(fetchdef.headers[
|
|
81
|
+
credentialIs(fetchdef.headers[CREDHEADER], 'OPTKEY01')
|
|
71
82
|
|
|
72
83
|
// No feature, no instance: the accessor is the only way in.
|
|
73
84
|
assert.equal(sdk.secrets(), undefined)
|
|
74
85
|
})
|
|
75
86
|
|
|
76
87
|
|
|
77
|
-
test('inactive: no apikey means no
|
|
88
|
+
test('inactive: no apikey means no credential header', async () => {
|
|
78
89
|
const { fetchdef } = await prepared({})
|
|
79
|
-
assert.equal(fetchdef.headers[
|
|
90
|
+
assert.equal(fetchdef.headers[CREDHEADER], undefined)
|
|
80
91
|
})
|
|
81
92
|
|
|
82
93
|
|
|
@@ -85,7 +96,7 @@ describe('secrets', () => {
|
|
|
85
96
|
const { sdk, fetchdef } = await prepared(
|
|
86
97
|
Object.assign({ apikey: 'OPTKEY01' }, envchain()))
|
|
87
98
|
|
|
88
|
-
credentialIs(fetchdef.headers[
|
|
99
|
+
credentialIs(fetchdef.headers[CREDHEADER], 'OPTKEY01')
|
|
89
100
|
|
|
90
101
|
// The explicit option is a real store, not a special case: a directed
|
|
91
102
|
// read names it like any other.
|
|
@@ -103,7 +114,7 @@ describe('secrets', () => {
|
|
|
103
114
|
test('active: an OMITTED apikey defers to the chain', async () => {
|
|
104
115
|
process.env[ENVPREFIX + 'APIKEY'] = 'ENVKEY01'
|
|
105
116
|
const { fetchdef } = await prepared(envchain())
|
|
106
|
-
credentialIs(fetchdef.headers[
|
|
117
|
+
credentialIs(fetchdef.headers[CREDHEADER], 'ENVKEY01')
|
|
107
118
|
})
|
|
108
119
|
|
|
109
120
|
|
|
@@ -111,7 +122,7 @@ describe('secrets', () => {
|
|
|
111
122
|
process.env[ENVPREFIX + 'APIKEY'] = 'ENVKEY01'
|
|
112
123
|
const { fetchdef } = await prepared(
|
|
113
124
|
Object.assign({ apikey: '' }, envchain()))
|
|
114
|
-
credentialIs(fetchdef.headers[
|
|
125
|
+
credentialIs(fetchdef.headers[CREDHEADER], 'ENVKEY01')
|
|
115
126
|
})
|
|
116
127
|
|
|
117
128
|
|
|
@@ -137,7 +148,7 @@ describe('secrets', () => {
|
|
|
137
148
|
Object.assign({ auth: null }, envchain()))
|
|
138
149
|
|
|
139
150
|
// Nothing on the wire, even though the chain would have resolved.
|
|
140
|
-
assert.equal(fetchdef.headers[
|
|
151
|
+
assert.equal(fetchdef.headers[CREDHEADER], undefined)
|
|
141
152
|
|
|
142
153
|
// The suppression survives option validation rather than being
|
|
143
154
|
// replaced by the optspec's default auth map.
|
|
@@ -148,7 +159,7 @@ describe('secrets', () => {
|
|
|
148
159
|
test('active: auth null suppresses an EXPLICIT apikey too', async () => {
|
|
149
160
|
const { fetchdef } = await prepared(
|
|
150
161
|
Object.assign({ apikey: 'OPTKEY01', auth: null }, envchain()))
|
|
151
|
-
assert.equal(fetchdef.headers[
|
|
162
|
+
assert.equal(fetchdef.headers[CREDHEADER], undefined)
|
|
152
163
|
})
|
|
153
164
|
|
|
154
165
|
|
|
@@ -165,14 +176,14 @@ describe('secrets', () => {
|
|
|
165
176
|
},
|
|
166
177
|
},
|
|
167
178
|
})
|
|
168
|
-
credentialIs(fetchdef.headers[
|
|
179
|
+
credentialIs(fetchdef.headers[CREDHEADER], 'CUSTOM01')
|
|
169
180
|
assert.deepEqual(asked, ['apikey'])
|
|
170
181
|
})
|
|
171
182
|
|
|
172
183
|
|
|
173
184
|
test('active: a miss everywhere leaves the header off', async () => {
|
|
174
185
|
const { sdk, fetchdef } = await prepared(envchain())
|
|
175
|
-
assert.equal(fetchdef.headers[
|
|
186
|
+
assert.equal(fetchdef.headers[CREDHEADER], undefined)
|
|
176
187
|
assert.equal(sdk.options().apikey, '')
|
|
177
188
|
})
|
|
178
189
|
|
|
@@ -234,7 +245,7 @@ describe('secrets', () => {
|
|
|
234
245
|
stub.calls.push({
|
|
235
246
|
url,
|
|
236
247
|
init,
|
|
237
|
-
auth: ((init || {}).headers || {})[
|
|
248
|
+
auth: ((init || {}).headers || {})[CREDHEADER],
|
|
238
249
|
})
|
|
239
250
|
return { status: 200, json: async () => ({ ok: true }), headers: {} }
|
|
240
251
|
}
|
|
@@ -351,7 +362,7 @@ describe('secrets', () => {
|
|
|
351
362
|
|
|
352
363
|
const second = await sdk.prepare({ path: '/' })
|
|
353
364
|
assert.ok(!(second instanceof Error), 'second attempt should recover, got ' + String(second))
|
|
354
|
-
credentialIs(second.headers[
|
|
365
|
+
credentialIs(second.headers[CREDHEADER], 'RECOVERED01')
|
|
355
366
|
})
|
|
356
367
|
|
|
357
368
|
|
|
@@ -408,11 +419,11 @@ describe('secrets', () => {
|
|
|
408
419
|
})
|
|
409
420
|
|
|
410
421
|
const first = await sdk.prepare({ path: '/' })
|
|
411
|
-
assert.equal(first.headers[
|
|
422
|
+
assert.equal(first.headers[CREDHEADER], undefined,
|
|
412
423
|
'the first resolve missed, so no credential should go out')
|
|
413
424
|
|
|
414
425
|
const second = await sdk.prepare({ path: '/' })
|
|
415
|
-
credentialIs(second.headers[
|
|
426
|
+
credentialIs(second.headers[CREDHEADER], 'LATEKEY')
|
|
416
427
|
|
|
417
428
|
assert.ok(1 < calls,
|
|
418
429
|
'the chain was asked once and the MISS cached: a secret that ' +
|
|
@@ -448,7 +459,7 @@ describe('secrets', () => {
|
|
|
448
459
|
process.env[ENVPREFIX + 'API_TOKEN'] = 'TOKKEY01'
|
|
449
460
|
try {
|
|
450
461
|
const { fetchdef } = await prepared(envchain({ name: 'api.token' }))
|
|
451
|
-
credentialIs(fetchdef.headers[
|
|
462
|
+
credentialIs(fetchdef.headers[CREDHEADER], 'TOKKEY01')
|
|
452
463
|
}
|
|
453
464
|
finally {
|
|
454
465
|
delete process.env[ENVPREFIX + 'API_TOKEN']
|
|
@@ -519,7 +530,7 @@ describe('secrets', () => {
|
|
|
519
530
|
|
|
520
531
|
const fetchdef = await sdk.prepare({ path: '/' })
|
|
521
532
|
assert.ok(!(fetchdef instanceof Error), String(fetchdef))
|
|
522
|
-
credentialIs(fetchdef.headers[
|
|
533
|
+
credentialIs(fetchdef.headers[CREDHEADER], 'VOCAB01')
|
|
523
534
|
})
|
|
524
535
|
|
|
525
536
|
|
|
@@ -581,7 +592,7 @@ describe('secrets', () => {
|
|
|
581
592
|
//
|
|
582
593
|
// The shape these tests pin: what the chain resolves is a REFRESH token,
|
|
583
594
|
// which is POSTed to a token endpoint for a short-lived ACCESS token; the
|
|
584
|
-
// access token is what the
|
|
595
|
+
// access token is what the credential header carries; and when the API
|
|
585
596
|
// answers 401 the client buys another and tries the same request again,
|
|
586
597
|
// once.
|
|
587
598
|
//
|
|
@@ -614,7 +625,7 @@ describe('secrets exchange', () => {
|
|
|
614
625
|
let apicall = -1
|
|
615
626
|
|
|
616
627
|
const fetch = async (url, init) => {
|
|
617
|
-
calls.push({ url, init, auth: (init.headers || {})[
|
|
628
|
+
calls.push({ url, init, auth: (init.headers || {})[CREDHEADER] })
|
|
618
629
|
|
|
619
630
|
if (url.endsWith('/' + (o.path || 'auth/token'))) {
|
|
620
631
|
if (true === o.tokenfails) {
|
|
@@ -957,7 +968,7 @@ describe('secrets exchange', () => {
|
|
|
957
968
|
const bought = stub.token().length
|
|
958
969
|
|
|
959
970
|
const feature = sdk._secrets
|
|
960
|
-
const fetchdef = { headers: {
|
|
971
|
+
const fetchdef = { headers: { [CREDHEADER]: 'Bearer STALE01' } }
|
|
961
972
|
|
|
962
973
|
sdk._options.apikey = 'STALE01'
|
|
963
974
|
|
|
@@ -978,7 +989,7 @@ describe('secrets exchange', () => {
|
|
|
978
989
|
assert.equal(calls, 2, 'the request was retried once')
|
|
979
990
|
assert.equal(stub.token().length, bought,
|
|
980
991
|
'the retry must reuse the token another request already bought')
|
|
981
|
-
authIs({ auth: fetchdef.headers
|
|
992
|
+
authIs({ auth: fetchdef.headers[CREDHEADER] }, 'ACCESS09')
|
|
982
993
|
})
|
|
983
994
|
|
|
984
995
|
|
|
@@ -987,7 +998,7 @@ describe('secrets exchange', () => {
|
|
|
987
998
|
|
|
988
999
|
const { fetchdef } = await prepared(envchain())
|
|
989
1000
|
|
|
990
|
-
credentialIs(fetchdef.headers[
|
|
1001
|
+
credentialIs(fetchdef.headers[CREDHEADER], 'PLAINKEY01')
|
|
991
1002
|
delete process.env[ENVPREFIX + 'APIKEY']
|
|
992
1003
|
})
|
|
993
1004
|
|
|
@@ -1013,7 +1024,7 @@ describe('secrets exchange', () => {
|
|
|
1013
1024
|
|
|
1014
1025
|
assert.equal(stub.calls.length, 0, 'test mode must not do IO')
|
|
1015
1026
|
// A deterministic placeholder, so offline suites need no configuration.
|
|
1016
|
-
credentialIs(fetchdef.headers[
|
|
1027
|
+
credentialIs(fetchdef.headers[CREDHEADER], 'test-access_token')
|
|
1017
1028
|
})
|
|
1018
1029
|
|
|
1019
1030
|
})
|
|
@@ -44,9 +44,9 @@ import KOTLINPACKAGE.utility.struct.Struct
|
|
|
44
44
|
// LinkedHashMap while another mutated it: a ConcurrentModificationException,
|
|
45
45
|
// not merely a torn read. The credential is therefore held in FEATURE STATE
|
|
46
46
|
// behind a lock, the options map stays frozen after construction, and the
|
|
47
|
-
//
|
|
48
|
-
// the same way prepareAuth builds it, from the same options.auth
|
|
49
|
-
// the two cannot drift. This is go's structural deviation, adopted for a
|
|
47
|
+
// credential header is rewritten per request at the transport - built
|
|
48
|
+
// the same way prepareAuth builds it, from the same options.auth name and
|
|
49
|
+
// prefix, so the two cannot drift. This is go's structural deviation, adopted for a
|
|
50
50
|
// sharper version of go's own reason.
|
|
51
51
|
//
|
|
52
52
|
// WHERE RESOLUTION HAPPENS. At the TRANSPORT SEAM, not in the PreSpec hook.
|
|
@@ -456,7 +456,7 @@ class SecretsFeature : BaseFeature("secrets", "0.1.0", true) {
|
|
|
456
456
|
// withrefresh buys a token and tries the request again when the API says
|
|
457
457
|
// the current one is spent.
|
|
458
458
|
//
|
|
459
|
-
// The retry rewrites the
|
|
459
|
+
// The retry rewrites the credential header IN PLACE on the fetchdef,
|
|
460
460
|
// because the header was built by the synchronous prepareAuth before this
|
|
461
461
|
// request left, and it carries the token that just failed.
|
|
462
462
|
private fun withrefresh(
|
|
@@ -543,8 +543,13 @@ class SecretsFeature : BaseFeature("secrets", "0.1.0", true) {
|
|
|
543
543
|
return
|
|
544
544
|
}
|
|
545
545
|
|
|
546
|
-
|
|
547
|
-
|
|
546
|
+
// The header prepareAuth wrote: auth.name, lower-cased.
|
|
547
|
+
val rawname = (auth as Map<String, Any?>)["name"]
|
|
548
|
+
val name =
|
|
549
|
+
if (rawname is String && "" != rawname) rawname.lowercase() else "authorization"
|
|
550
|
+
|
|
551
|
+
val prefix = auth["prefix"]
|
|
552
|
+
headers[name] =
|
|
548
553
|
if (prefix is String && "" != prefix) "$prefix $token" else token
|
|
549
554
|
}
|
|
550
555
|
|
|
@@ -105,7 +105,7 @@ class SecretsTest {
|
|
|
105
105
|
val headers = fetchdef["headers"]
|
|
106
106
|
if (headers is Map<*, *>) {
|
|
107
107
|
for (e in headers.entries) {
|
|
108
|
-
if (
|
|
108
|
+
if (CREDHEADER.equals(e.key.toString(), ignoreCase = true)) {
|
|
109
109
|
hasAuth = true
|
|
110
110
|
auth = e.value?.toString() ?: ""
|
|
111
111
|
}
|
|
@@ -154,6 +154,15 @@ class SecretsTest {
|
|
|
154
154
|
|
|
155
155
|
companion object {
|
|
156
156
|
const val BASE = "http://secrets.test/api"
|
|
157
|
+
|
|
158
|
+
// The header the credential travels in: the DEFAULT client's auth.name,
|
|
159
|
+
// lower-cased as prepareAuth sends it. Read from a default client
|
|
160
|
+
// because a test's `auth: null` leaves no name to read.
|
|
161
|
+
val CREDHEADER: String by lazy {
|
|
162
|
+
val auth = ProjectNameSDK.testSDK(null, null).optionsMap()["auth"]
|
|
163
|
+
val name = if (auth is Map<*, *>) auth["name"] else null
|
|
164
|
+
if (name is String && "" != name) name.lowercase() else "authorization"
|
|
165
|
+
}
|
|
157
166
|
}
|
|
158
167
|
|
|
159
168
|
// A sekreto Provider built in code.
|
|
@@ -165,14 +174,14 @@ class SecretsTest {
|
|
|
165
174
|
override fun describe(): String = label
|
|
166
175
|
}
|
|
167
176
|
|
|
168
|
-
// The
|
|
177
|
+
// The credential header carries the SPEC's credential prefix, which a
|
|
169
178
|
// TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
|
|
170
179
|
// `Bearer <token>`, an apiKey scheme the raw token. So assert on the
|
|
171
180
|
// CREDENTIAL and let the prefix be whatever this SDK's API declares.
|
|
172
181
|
private fun credentialIs(header: String?, token: String) {
|
|
173
182
|
val got = header ?: ""
|
|
174
183
|
assertTrue(got == token || got.endsWith(" $token"),
|
|
175
|
-
"expected the
|
|
184
|
+
"expected the $CREDHEADER header to carry $token, got: \"$got\"")
|
|
176
185
|
}
|
|
177
186
|
|
|
178
187
|
private fun map(vararg pairs: Pair<String, Any?>): MutableMap<String, Any?> =
|
|
@@ -310,7 +319,7 @@ class SecretsTest {
|
|
|
310
319
|
val client = ProjectNameSDK.testSDK(null, map("apikey" to "OPTKEY01"))
|
|
311
320
|
val fetchdef = client.prepare(map("path" to "/"))
|
|
312
321
|
val headers = fetchdef["headers"] as MutableMap<String, Any?>
|
|
313
|
-
credentialIs(headers[
|
|
322
|
+
credentialIs(headers[CREDHEADER] as? String, "OPTKEY01")
|
|
314
323
|
|
|
315
324
|
// No runtime activation, no extend: the feature must not be installed,
|
|
316
325
|
// whatever this project's model says.
|
|
@@ -319,12 +328,12 @@ class SecretsTest {
|
|
|
319
328
|
}
|
|
320
329
|
|
|
321
330
|
@Test
|
|
322
|
-
fun
|
|
331
|
+
fun inactiveNoApikeyMeansNoCredentialHeader() {
|
|
323
332
|
val client = ProjectNameSDK.testSDK(null, null)
|
|
324
333
|
val fetchdef = client.prepare(map("path" to "/"))
|
|
325
334
|
val headers = fetchdef["headers"] as MutableMap<String, Any?>
|
|
326
|
-
assertFalse(headers.containsKey(
|
|
327
|
-
"unexpected
|
|
335
|
+
assertFalse(headers.containsKey(CREDHEADER),
|
|
336
|
+
"unexpected $CREDHEADER header: " + headers[CREDHEADER])
|
|
328
337
|
}
|
|
329
338
|
|
|
330
339
|
// ===================================================================
|
|
@@ -935,7 +944,7 @@ class SecretsTest {
|
|
|
935
944
|
//
|
|
936
945
|
// What the chain resolves is a REFRESH token, which is POSTed to a token
|
|
937
946
|
// endpoint for a short-lived ACCESS token; the access token is what the
|
|
938
|
-
//
|
|
947
|
+
// credential header carries; and when the API answers 401 the client
|
|
939
948
|
// buys another and tries the same request again, once.
|
|
940
949
|
|
|
941
950
|
private fun exchangeClient(
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
-- `graphql()` run no hooks at all. The transport wrapper installed in init
|
|
22
22
|
-- is the one seam every wire path crosses: entity ops (make_request), the
|
|
23
23
|
-- raw paths (_raw_request) and the exchange retries all come through it. It
|
|
24
|
-
-- resolves there, rewrites the
|
|
24
|
+
-- resolves there, rewrites the credential header prepare_auth built from
|
|
25
25
|
-- the options apikey, and - this is the point - REFUSES TO SEND while the
|
|
26
26
|
-- chain is broken.
|
|
27
27
|
--
|
|
@@ -445,7 +445,7 @@ end
|
|
|
445
445
|
|
|
446
446
|
|
|
447
447
|
-- Buy a token and try the request again when the API says the current one
|
|
448
|
-
-- is spent. The retry rewrites the
|
|
448
|
+
-- is spent. The retry rewrites the credential header IN PLACE on the
|
|
449
449
|
-- fetchdef, because the header was built before this request left and it
|
|
450
450
|
-- carries the token that just failed.
|
|
451
451
|
function SecretsFeature:_with_refresh(ctx, fullurl, fetchdef, inner)
|
|
@@ -532,15 +532,22 @@ function SecretsFeature:_reauth(fetchdef, token)
|
|
|
532
532
|
return
|
|
533
533
|
end
|
|
534
534
|
|
|
535
|
+
-- The header prepare_auth writes: auth.name, lower-cased.
|
|
536
|
+
local name = auth["name"]
|
|
537
|
+
if type(name) ~= "string" or name == "" then
|
|
538
|
+
name = "authorization"
|
|
539
|
+
end
|
|
540
|
+
name = string.lower(name)
|
|
541
|
+
|
|
535
542
|
local prefix = auth["prefix"]
|
|
536
543
|
if type(prefix) ~= "string" then
|
|
537
544
|
prefix = ""
|
|
538
545
|
end
|
|
539
546
|
|
|
540
547
|
if prefix == "" then
|
|
541
|
-
headers[
|
|
548
|
+
headers[name] = token
|
|
542
549
|
else
|
|
543
|
-
headers[
|
|
550
|
+
headers[name] = prefix .. " " .. token
|
|
544
551
|
end
|
|
545
552
|
end
|
|
546
553
|
|