@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,11 +38,42 @@ const { Sekreto, envkey } = require('./sekreto')
38
38
  // bypasses feature hooks entirely, so it awaits client._secrets.resolve()
39
39
  // itself — which is what defends the raw direct()/graphql() paths, since
40
40
  // both reach the wire through prepare().
41
+ const REDIRECT_MAX = 5
42
+
43
+
44
+ // The next URL of a redirect that keeps its method, when that URL is on the
45
+ // exchange's own origin. Any other redirect would drop the body or leave it.
46
+ function moved(url, at, res) {
47
+ const status = null == res ? 0 : res.status
48
+ if (307 !== status && 308 !== status) {
49
+ return undefined
50
+ }
51
+
52
+ const headers = res.headers
53
+ const location = null == headers ? undefined :
54
+ 'function' === typeof headers.get ? headers.get('location') : headers.location
55
+
56
+ if ('string' !== typeof location || '' === location) {
57
+ return undefined
58
+ }
59
+
60
+ try {
61
+ const next = new URL(location, at)
62
+ return next.origin === new URL(url).origin ? next.href : undefined
63
+ }
64
+ catch (_err) {
65
+ return undefined
66
+ }
67
+ }
68
+
69
+
41
70
  class SecretsFeature extends BaseFeature {
42
71
  version = '0.1.0'
43
72
  name = 'secrets'
44
73
 
45
74
  _client
75
+ _declared = false
76
+ _basic = false
46
77
  _sekreto
47
78
  _secretname = 'apikey'
48
79
  _resolving
@@ -81,6 +112,13 @@ class SecretsFeature extends BaseFeature {
81
112
  const plugins = require('../../Config').FEATURE_PLUGINS || {}
82
113
 
83
114
  this._client = client
115
+
116
+ // What PrepareAuth was generated from: the config declares auth only
117
+ // where it places a credential, and Basic only where it composes one.
118
+ const declared = ctx.config?.options?.auth
119
+ this._declared = null != declared
120
+ this._basic = true === declared?.basic
121
+
84
122
  this._secretname = 'string' === typeof fopts.name &&
85
123
  '' !== fopts.name ? fopts.name : 'apikey'
86
124
 
@@ -94,6 +132,7 @@ class SecretsFeature extends BaseFeature {
94
132
  response: 'string' === typeof xopts.response ? xopts.response : 'access_token',
95
133
  statuses: Array.isArray(xopts.statuses) ? xopts.statuses : [401],
96
134
  retries: 'number' === typeof xopts.retries ? xopts.retries : 1,
135
+ redirect: 'same-origin' === xopts.redirect ? 'same-origin' : 'refuse',
97
136
  } : null
98
137
 
99
138
  const providers = []
@@ -164,6 +203,12 @@ class SecretsFeature extends BaseFeature {
164
203
  }
165
204
 
166
205
 
206
+ // PrepareAuth's decision: auth the config declares, not suppressed by `auth: null`.
207
+ _authon() {
208
+ return this._declared && null != this._client._options.auth
209
+ }
210
+
211
+
167
212
  // Every value this feature resolves or buys is a secret the SDK handles,
168
213
  // and none arrives under an option key the intake registration saw.
169
214
  _register(value) {
@@ -223,7 +268,7 @@ class SecretsFeature extends BaseFeature {
223
268
  // boolean is the whole of what resolve() needs to tell a cacheable HIT
224
269
  // from a miss it must not retain.
225
270
  async _resolveonce() {
226
- if (null == this._sekreto) {
271
+ if (null == this._sekreto || !this._authon()) {
227
272
  return false
228
273
  }
229
274
 
@@ -254,17 +299,6 @@ class SecretsFeature extends BaseFeature {
254
299
  return true
255
300
  }
256
301
 
257
- // `auth: null` is the documented way to send NO credential, and a
258
- // purchase is a credential-bearing call: the refresh token goes to the
259
- // token endpoint in the request body. _withRefresh honours suppression
260
- // for the RETRY, but it runs after this — by then the refresh token has
261
- // already left the process, and no later check can call it back. The
262
- // suppression has to be honoured here, before the first purchase, or it
263
- // only ever half-held.
264
- if (null == this._client.options().auth) {
265
- return false
266
- }
267
-
268
302
  this._client._options.apikey = await this._buy()
269
303
 
270
304
  return true
@@ -337,13 +371,26 @@ class SecretsFeature extends BaseFeature {
337
371
  // feature wraps, and sending the token request back through it would
338
372
  // recurse on the first 401 — and would route the exchange through the
339
373
  // test mock, which knows nothing about it.
340
- const res = await fetch(url, {
374
+ const fetchdef = {
341
375
  method: x.method,
342
376
  headers: { 'content-type': 'application/json' },
343
377
  body: JSON.stringify({ [x.request]: this._refresh }),
344
- // Never followed: a redirect would carry the refresh token on.
378
+ // Never followed by the client: a redirect would carry the refresh token on.
345
379
  redirect: 'manual',
346
- })
380
+ }
381
+
382
+ let res = await fetch(url, fetchdef)
383
+
384
+ // By option alone, and only while the exchange stays on its own origin.
385
+ let at = url
386
+ for (let hops = 0; 'same-origin' === x.redirect && hops < REDIRECT_MAX; hops++) {
387
+ const next = moved(url, at, res)
388
+ if (null == next) {
389
+ break
390
+ }
391
+ at = next
392
+ res = await fetch(at, fetchdef)
393
+ }
347
394
 
348
395
  const status = null == res ? 0 : res.status
349
396
 
@@ -378,12 +425,9 @@ class SecretsFeature extends BaseFeature {
378
425
  const x = this._exchange
379
426
  const max = null == x ? 0 : x.retries
380
427
 
381
- // `auth: null` is the documented way to send NO credential, and
382
- // prepareAuth honours it by removing the header. A refusal of a
383
- // deliberately unauthenticated request is not an expired token and
384
- // cannot be fixed by buying one — retrying would transmit exactly the
385
- // credential the caller suppressed.
386
- if (null == this._client.options().auth) {
428
+ // A refusal of an unauthenticated request is not an expired token, and
429
+ // retrying would transmit the credential that auth off withholds.
430
+ if (!this._authon()) {
387
431
  return inner(ctx, url, fetchdef)
388
432
  }
389
433
 
@@ -448,23 +492,29 @@ class SecretsFeature extends BaseFeature {
448
492
  return
449
493
  }
450
494
 
451
- const auth = this._client.options().auth
452
-
453
- // Suppressed auth means NO header, the same answer prepareAuth gives.
454
- // Reached defensively - _withRefresh does not retry at all when auth
455
- // is null - but this is the function that writes the credential, so
456
- // it is where the rule has to hold.
457
- if (null == auth) {
495
+ if (!this._authon()) {
458
496
  delete fetchdef.headers.authorization
459
497
  return
460
498
  }
461
499
 
500
+ const auth = this._client._options.auth
501
+
462
502
  // The header prepareAuth wrote: auth.name, lower-cased.
463
503
  const name = 'string' === typeof auth.name && '' !== auth.name ?
464
504
  auth.name.toLowerCase() : 'authorization'
465
505
 
506
+ const value = this._basic && true === auth.basic ? this._pair(token) : token
466
507
  const prefix = auth.prefix
467
- fetchdef.headers[name] = prefix ? prefix + ' ' + token : token
508
+ fetchdef.headers[name] = prefix ? prefix + ' ' + value : value
509
+ }
510
+
511
+
512
+ // Under Basic the token is the key half, as apikey is: prepareAuth's pair.
513
+ _pair(token) {
514
+ const secret = this._client._options.secret
515
+ const pair = Buffer.from(token + ':' + (null == secret ? '' : secret)).toString('base64')
516
+ this._register(pair)
517
+ return pair
468
518
  }
469
519
  }
470
520
 
@@ -12,7 +12,7 @@
12
12
  // trims it, along with the feature source and the vendored library, for a
13
13
  // project whose model does not select `secrets`.
14
14
 
15
- const { test, describe, beforeEach } = require('node:test')
15
+ const { test, describe, beforeEach, after } = require('node:test')
16
16
  const assert = require('node:assert')
17
17
  const Http = require('node:http')
18
18
 
@@ -52,16 +52,67 @@ async function prepared(sdkopts) {
52
52
  }
53
53
 
54
54
 
55
- // The credential header carries the SPEC's credential prefix, which a
56
- // TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
57
- // `Bearer <token>`, an apiKey scheme the raw token. So assert on the
58
- // CREDENTIAL and let the prefix be whatever this SDK's API declares —
59
- // pinning the whole header value passes only for a prefix-less API, and
60
- // this file ships to every project that selects the feature.
61
- function credentialIs(header, token) {
62
- const got = String(null == header ? '' : header)
63
- assert.ok(got === token || got.endsWith(' ' + token),
64
- 'expected the ' + CREDHEADER + ' header to carry ' + token + ', got: ' + got)
55
+ // The form prepareAuth writes, from the DEFAULT client's auth: under Basic the
56
+ // token is the key half of the encoded pair, and a prefix is space-joined.
57
+ const CREDFORM = (() => {
58
+ const auth = SDK.test({}, {}).options().auth || {}
59
+ return { prefix: 'string' === typeof auth.prefix ? auth.prefix : '', basic: true === auth.basic }
60
+ })()
61
+
62
+
63
+ function credentialIs(header, token, secret) {
64
+ const value = CREDFORM.basic ?
65
+ Buffer.from(token + ':' + (secret || '')).toString('base64') : token
66
+ assert.equal(header, '' === CREDFORM.prefix ? value : CREDFORM.prefix + ' ' + value,
67
+ 'expected the ' + CREDHEADER + ' header to carry ' + token)
68
+ }
69
+
70
+
71
+ // A stub standing in for both endpoints the flow touches: the token
72
+ // endpoint, and everything else.
73
+ //
74
+ // `apiStatus` is a SCRIPT — one status per API call, the last repeating —
75
+ // so a case can say "401 then 200" without counting calls itself.
76
+ function stubfetch(opts) {
77
+ const o = opts || {}
78
+ const tokens = o.tokens || ['ACCESS01', 'ACCESS02', 'ACCESS03']
79
+ const apiStatus = o.apiStatus || [200]
80
+
81
+ const calls = []
82
+ let issued = 0
83
+ let apicall = -1
84
+
85
+ const fetch = async (url, init) => {
86
+ calls.push({ url, init, auth: (init.headers || {})[CREDHEADER] })
87
+
88
+ if (url.endsWith('/' + (o.path || 'auth/token'))) {
89
+ if (true === o.tokenfails) {
90
+ return { status: 401, json: async () => ({ error: 'nope' }), headers: {} }
91
+ }
92
+ const body = {}
93
+ body[o.response || 'access_token'] = tokens[Math.min(issued, tokens.length - 1)]
94
+ issued++
95
+ return { status: 200, json: async () => body, headers: {} }
96
+ }
97
+
98
+ apicall++
99
+ const status = apiStatus[Math.min(apicall, apiStatus.length - 1)]
100
+ return { status, json: async () => ({ ok: status < 400 }), headers: {} }
101
+ }
102
+
103
+ return {
104
+ fetch,
105
+ calls,
106
+ // Only the calls that went to the API, in order.
107
+ api: () => calls.filter((c) => !c.url.endsWith('/' + (o.path || 'auth/token'))),
108
+ token: () => calls.filter((c) => c.url.endsWith('/' + (o.path || 'auth/token'))),
109
+ }
110
+ }
111
+
112
+
113
+ // Same rule as credentialIs, reading the header off a recorded call.
114
+ function authIs(call, token, secret) {
115
+ credentialIs(null == call ? undefined : call.auth, token, secret)
65
116
  }
66
117
 
67
118
 
@@ -127,6 +178,13 @@ describe('secrets', { skip: NOAUTH }, () => {
127
178
  })
128
179
 
129
180
 
181
+ test('active: a resolved key is placed as prepareAuth places it, the secret kept', async () => {
182
+ process.env[ENVPREFIX + 'APIKEY'] = 'ENVKEY03'
183
+ const { fetchdef } = await prepared(Object.assign({ secret: 'PASS01' }, envchain()))
184
+ credentialIs(fetchdef.headers[CREDHEADER], 'ENVKEY03', 'PASS01')
185
+ })
186
+
187
+
130
188
  test('active: an explicitly EMPTY apikey also defers to the chain', async () => {
131
189
  process.env[ENVPREFIX + 'APIKEY'] = 'ENVKEY01'
132
190
  const { fetchdef } = await prepared(
@@ -619,56 +677,9 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
619
677
  })
620
678
 
621
679
 
622
- // A stub standing in for both endpoints the flow touches: the token
623
- // endpoint, and everything else.
624
- //
625
- // `apiStatus` is a SCRIPT — one status per API call, the last repeating —
626
- // so a case can say "401 then 200" without counting calls itself.
627
- function stubfetch(opts) {
628
- const o = opts || {}
629
- const tokens = o.tokens || ['ACCESS01', 'ACCESS02', 'ACCESS03']
630
- const apiStatus = o.apiStatus || [200]
631
-
632
- const calls = []
633
- let issued = 0
634
- let apicall = -1
635
-
636
- const fetch = async (url, init) => {
637
- calls.push({ url, init, auth: (init.headers || {})[CREDHEADER] })
638
-
639
- if (url.endsWith('/' + (o.path || 'auth/token'))) {
640
- if (true === o.tokenfails) {
641
- return { status: 401, json: async () => ({ error: 'nope' }), headers: {} }
642
- }
643
- const body = {}
644
- body[o.response || 'access_token'] = tokens[Math.min(issued, tokens.length - 1)]
645
- issued++
646
- return { status: 200, json: async () => body, headers: {} }
647
- }
648
-
649
- apicall++
650
- const status = apiStatus[Math.min(apicall, apiStatus.length - 1)]
651
- return { status, json: async () => ({ ok: status < 400 }), headers: {} }
652
- }
653
-
654
- return {
655
- fetch,
656
- calls,
657
- // Only the calls that went to the API, in order.
658
- api: () => calls.filter((c) => !c.url.endsWith('/' + (o.path || 'auth/token'))),
659
- token: () => calls.filter((c) => c.url.endsWith('/' + (o.path || 'auth/token'))),
660
- }
661
- }
662
-
663
-
664
- // Same rule as credentialIs, reading the header off a recorded call.
665
- function authIs(call, token) {
666
- credentialIs(null == call ? undefined : call.auth, token)
667
- }
668
-
669
-
670
- function exchangeSdk(stub, extra) {
680
+ function exchangeSdk(stub, extra, sdkopts) {
671
681
  return new SDK({
682
+ ...(sdkopts || {}),
672
683
  base: BASE,
673
684
  system: { fetch: stub.fetch },
674
685
  feature: {
@@ -769,6 +780,21 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
769
780
  })
770
781
 
771
782
 
783
+ test('a token bought on a refusal is placed as prepareAuth places it, the secret kept',
784
+ async () => {
785
+ process.env[ENVPREFIX + 'REFRESH_TOKEN'] = REFRESH
786
+
787
+ const stub = stubfetch({ apiStatus: [401, 200] })
788
+ const sdk = exchangeSdk(stub, {}, { secret: 'PASS01' })
789
+
790
+ await sdk.direct({ path: '/thing' })
791
+
792
+ assert.equal(stub.api().length, 2, 'expected the request to be retried')
793
+ authIs(stub.api()[0], 'ACCESS01', 'PASS01')
794
+ authIs(stub.api()[1], 'ACCESS02', 'PASS01')
795
+ })
796
+
797
+
772
798
  test('the retry happens once, not in a loop', async () => {
773
799
  process.env[ENVPREFIX + 'REFRESH_TOKEN'] = REFRESH
774
800
 
@@ -1039,30 +1065,31 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
1039
1065
  })
1040
1066
 
1041
1067
 
1042
- // Real listeners and the platform fetch: a stub follows nothing.
1043
- test('a redirect answering the exchange is a failure, and nothing follows it', async () => {
1044
- const elsewhere = []
1045
- const followed = []
1046
- let answer = [307, '']
1068
+ // Real listeners and the platform fetch: a stub follows nothing. The
1069
+ // origin answers the exchange as `answer` says and records what else it gets.
1070
+ async function withOrigins(run) {
1071
+ const o = { elsewhere: [], followed: [], exchanged: 0, answer: [307, ''] }
1047
1072
 
1048
1073
  const other = Http.createServer((req, res) => {
1049
1074
  let body = ''
1050
1075
  req.on('data', (chunk) => body += chunk)
1051
1076
  req.on('end', () => {
1052
- elsewhere.push({ method: req.method, url: req.url, body })
1077
+ o.elsewhere.push({ method: req.method, url: req.url, body })
1053
1078
  res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
1054
1079
  res.end('{"access_token":"ELSEWHERE01"}')
1055
1080
  })
1056
1081
  })
1057
1082
  const origin = Http.createServer((req, res) => {
1058
- req.resume()
1083
+ let body = ''
1084
+ req.on('data', (chunk) => body += chunk)
1059
1085
  req.on('end', () => {
1060
1086
  if ('/api/auth/token' === req.url) {
1061
- res.writeHead(answer[0], { location: answer[1], connection: 'close' })
1087
+ o.exchanged++
1088
+ res.writeHead(o.answer[0], { location: o.answer[1], connection: 'close' })
1062
1089
  res.end()
1063
1090
  return
1064
1091
  }
1065
- followed.push(req.method + ' ' + req.url)
1092
+ o.followed.push((req.method + ' ' + req.url + ' ' + body).trim())
1066
1093
  res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
1067
1094
  res.end('{"access_token":"MOVED01"}')
1068
1095
  })
@@ -1070,32 +1097,50 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
1070
1097
 
1071
1098
  const listen = (server) => new Promise((resolve) =>
1072
1099
  server.listen(0, '127.0.0.1', () => resolve(server.address().port)))
1073
- const base = 'http://127.0.0.1:' + (await listen(origin)) + '/api'
1074
- const away = 'http://127.0.0.1:' + (await listen(other)) + '/api/auth/token'
1100
+ o.origin = 'http://127.0.0.1:' + (await listen(origin))
1101
+ o.base = o.origin + '/api'
1102
+ o.away = 'http://127.0.0.1:' + (await listen(other)) + '/api/auth/token'
1075
1103
 
1076
1104
  try {
1077
- const answers = [[307, away], [308, away], [302, away], [307, '/api/moved/token']]
1105
+ await run(o)
1106
+ }
1107
+ finally {
1108
+ origin.close()
1109
+ other.close()
1110
+ }
1111
+ }
1112
+
1113
+
1114
+ function redirectSdk(o, exchange) {
1115
+ return new SDK({
1116
+ base: o.base,
1117
+ feature: {
1118
+ secrets: {
1119
+ active: true,
1120
+ name: 'refresh_token',
1121
+ exchange: Object.assign({ active: true, refresh: REFRESH }, exchange || {}),
1122
+ },
1123
+ },
1124
+ })
1125
+ }
1126
+
1127
+
1128
+ test('a redirect answering the exchange is a failure, and nothing follows it', async () => {
1129
+ await withOrigins(async (o) => {
1130
+ const answers =
1131
+ [[307, o.away], [308, o.away], [302, o.away], [307, '/api/moved/token']]
1078
1132
 
1079
1133
  const allowed = []
1080
1134
  let refused = 0
1081
1135
 
1082
- for (answer of answers) {
1083
- const sdk = new SDK({
1084
- base,
1085
- feature: {
1086
- secrets: {
1087
- active: true,
1088
- name: 'refresh_token',
1089
- exchange: { active: true, refresh: REFRESH },
1090
- },
1091
- },
1092
- })
1136
+ for (const answer of answers) {
1137
+ o.answer = answer
1093
1138
 
1094
- const res = await sdk.direct({ path: '/thing' })
1139
+ const res = await redirectSdk(o).direct({ path: '/thing' })
1095
1140
  const message = String(null == res || null == res.err ? res : res.err.message)
1096
1141
 
1097
1142
  if (message.includes(
1098
- 'token exchange failed: ' + answer[0] + ' from ' + base + '/auth/token')) {
1143
+ 'token exchange failed: ' + answer[0] + ' from ' + o.base + '/auth/token')) {
1099
1144
  refused++
1100
1145
  }
1101
1146
  else {
@@ -1104,17 +1149,161 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
1104
1149
  }
1105
1150
 
1106
1151
  console.log('secrets: exchange refused ' + refused + ' redirect(s), ' +
1107
- (elsewhere.length + followed.length) + ' followed')
1152
+ (o.elsewhere.length + o.followed.length) + ' followed')
1108
1153
 
1109
- assert.deepEqual(elsewhere, [], 'the exchange reached another origin')
1110
- assert.deepEqual(followed, [], 'a request followed a refused exchange')
1154
+ assert.deepEqual(o.elsewhere, [], 'the exchange reached another origin')
1155
+ assert.deepEqual(o.followed, [], 'a request followed a refused exchange')
1111
1156
  assert.deepEqual(allowed, [], 'a redirect was not a failed exchange')
1112
1157
  assert.equal(refused, answers.length)
1113
- }
1114
- finally {
1115
- origin.close()
1116
- other.close()
1117
- }
1158
+ })
1159
+ })
1160
+
1161
+
1162
+ test('with the option, a redirect on the exchange\'s own origin is followed, and no other', async () => {
1163
+ await withOrigins(async (o) => {
1164
+ const sent = ' {"refresh_token":"' + REFRESH + '"}'
1165
+ // Each case: the status, the Location, and the path the exchange is
1166
+ // asked again at, or null where the redirect is refused.
1167
+ const cases = [
1168
+ [307, '/api/moved/token', '/api/moved/token'],
1169
+ [308, o.origin + '/api/moved/token', '/api/moved/token'],
1170
+ // A Location of a query alone keeps the path that answered.
1171
+ [307, '?step=2', '/api/auth/token?step=2'],
1172
+ [307, o.away, null],
1173
+ [302, '/api/moved/token', null],
1174
+ [307, '/api/auth/token', null],
1175
+ ]
1176
+
1177
+ const wrong = []
1178
+ let followed = 0
1179
+ let refused = 0
1180
+ let looped = 0
1181
+
1182
+ for (const [status, location, path] of cases) {
1183
+ o.answer = [status, location]
1184
+ o.followed.length = 0
1185
+ o.exchanged = 0
1186
+
1187
+ const res = await redirectSdk(o, { redirect: 'same-origin' }).direct({ path: '/thing' })
1188
+ const message = String(null == res || null == res.err ? res : res.err.message)
1189
+ const seen = status + ' to ' + location + ': ' + message + ' ' + JSON.stringify(o.followed)
1190
+
1191
+ if (null != path) {
1192
+ if (true === res.ok && 'POST ' + path + sent === o.followed[0] &&
1193
+ 2 === o.followed.length) followed++
1194
+ else wrong.push(seen)
1195
+ }
1196
+ else if (0 === o.followed.length && message.includes(
1197
+ 'token exchange failed: ' + status + ' from ' + o.base + '/auth/token')) {
1198
+ refused++
1199
+ }
1200
+ else {
1201
+ wrong.push(seen)
1202
+ }
1203
+
1204
+ if ('/api/auth/token' === location) looped = o.exchanged
1205
+ }
1206
+
1207
+ console.log('secrets: exchange opt-in followed ' + followed +
1208
+ ' redirect(s) on its own origin, refused ' + refused + ', ' +
1209
+ o.elsewhere.length + ' elsewhere')
1210
+
1211
+ assert.deepEqual(o.elsewhere, [], 'the exchange reached another origin')
1212
+ assert.deepEqual(wrong, [])
1213
+ assert.equal(followed, 3)
1214
+ assert.equal(refused, 3)
1215
+ // A redirect to itself stops at the hop limit.
1216
+ assert.equal(looped, 6)
1217
+ })
1218
+ })
1219
+
1220
+ })
1221
+
1222
+
1223
+ // Off by the config where it declares no auth, else by `auth: null`: either
1224
+ // way the feature places no credential, buys no token and retries nothing.
1225
+ const AUTHOFF = NOAUTH ? { by: 'config', opts: {} } : { by: 'option', opts: { auth: null } }
1226
+
1227
+
1228
+ describe('secrets with auth off', () => {
1229
+
1230
+ const BASE = 'http://authoff.test/api'
1231
+ const SECRETS = ['OFFKEY01', 'OFFKEY02', 'OFFREFRESH01', 'ACCESS01']
1232
+ const sent = []
1233
+ let bought = 0
1234
+
1235
+
1236
+ beforeEach(() => {
1237
+ delete process.env[ENVPREFIX + 'APIKEY']
1238
+ delete process.env[ENVPREFIX + 'REFRESH_TOKEN']
1239
+ })
1240
+
1241
+
1242
+ after(() => {
1243
+ console.log('secrets: auth off by ' + AUTHOFF.by + ': ' + sent.length + ' request(s), ' +
1244
+ sent.filter(carries).length + ' credential(s), ' + bought + ' token(s)')
1245
+ })
1246
+
1247
+
1248
+ // The header prepareAuth writes, or any header holding one of the secrets.
1249
+ function carries(call) {
1250
+ const headers = call.init.headers || {}
1251
+ return undefined !== headers[CREDHEADER] || Object.values(headers)
1252
+ .some((v) => SECRETS.some((secret) => String(v).includes(secret)))
1253
+ }
1254
+
1255
+
1256
+ async function offsdk(stub, sdkopts, fopts) {
1257
+ const sdk = new SDK({
1258
+ ...sdkopts,
1259
+ ...AUTHOFF.opts,
1260
+ base: BASE,
1261
+ system: { fetch: stub.fetch },
1262
+ feature: {
1263
+ secrets: {
1264
+ active: true,
1265
+ providers: [{ kind: 'env', prefix: ENVPREFIX }],
1266
+ ...fopts,
1267
+ },
1268
+ },
1269
+ })
1270
+ await sdk.direct({ path: '/thing' })
1271
+ sent.push(...stub.api())
1272
+ bought += stub.token().length
1273
+ return sdk
1274
+ }
1275
+
1276
+
1277
+ test('a key the chain resolves is not placed', async () => {
1278
+ process.env[ENVPREFIX + 'APIKEY'] = 'OFFKEY01'
1279
+
1280
+ const stub = stubfetch()
1281
+ const sdk = await offsdk(stub, {}, {})
1282
+
1283
+ assert.equal(stub.api().length, 1)
1284
+ assert.ok(!carries(stub.api()[0]), 'a credential was sent with auth off')
1285
+ assert.equal(sdk.options().apikey, '', 'the resolved key was placed where prepareAuth reads it')
1286
+ })
1287
+
1288
+
1289
+ test('an explicit apikey is not placed', async () => {
1290
+ const stub = stubfetch()
1291
+ await offsdk(stub, { apikey: 'OFFKEY02' }, {})
1292
+
1293
+ assert.equal(stub.api().length, 1)
1294
+ assert.ok(!carries(stub.api()[0]), 'a credential was sent with auth off')
1295
+ })
1296
+
1297
+
1298
+ test('a refusal buys no token and is not retried', async () => {
1299
+ process.env[ENVPREFIX + 'REFRESH_TOKEN'] = 'OFFREFRESH01'
1300
+
1301
+ const stub = stubfetch({ apiStatus: [401] })
1302
+ await offsdk(stub, {}, { name: 'refresh_token', exchange: { active: true } })
1303
+
1304
+ assert.equal(stub.token().length, 0, 'a token was bought with auth off')
1305
+ assert.equal(stub.api().length, 1, 'a refusal was retried with auth off')
1306
+ assert.ok(!carries(stub.api()[0]), 'a credential was sent with auth off')
1118
1307
  })
1119
1308
 
1120
1309
  })
@@ -278,6 +278,23 @@ describe('feature', () => {
278
278
  await h.op({ op: 'load', path: '/a' }) // miss again
279
279
  strictEqual(rec.calls.length, 3)
280
280
  })
281
+
282
+ test('a body that declares another type than JSON passes by, unread', async () => {
283
+ const sent = []
284
+ const rec = recordingServer((_n) => {
285
+ const res = makeResponse(200, undefined, { 'content-type': 'audio/wav' })
286
+ res.text = async () => 'RIFF'
287
+ sent.push(res)
288
+ return res
289
+ })
290
+ const h = makeClient({ features: [{ name: 'cache' }], server: rec.server })
291
+ const a = await h.op({ op: 'load', path: '/w/1/audio' })
292
+ const b = await h.op({ op: 'load', path: '/w/1/audio' })
293
+ strictEqual(rec.calls.length, 2, 'the body was stored')
294
+ strictEqual(a.ctx.response, sent[0], 'the cache handed on a copy it had read')
295
+ strictEqual(b.ctx.response, sent[1])
296
+ strictEqual(h.client._cache.bypass, 2)
297
+ })
281
298
  })
282
299
 
283
300
 
@@ -13,9 +13,4 @@ class Control {
13
13
  var actor: String = ""
14
14
 
15
15
  var paging: MutableMap<String, Any?>? = null
16
-
17
- // Outbound streaming marker: the entity `stream` method sets this to the
18
- // caller-supplied streamable payload so the request builder / transport can
19
- // stream it as the request body.
20
- var streamOut: Any? = null
21
16
  }