@middy/sts 3.0.0-alpha.3 → 3.0.0-alpha.4

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 (2) hide show
  1. package/index.js +42 -52
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,81 +1,71 @@
1
- import {
2
- canPrefetch,
3
- createPrefetchClient,
4
- createClient,
5
- getCache,
6
- getInternal,
7
- processCache,
8
- modifyCache
9
- } from '@middy/util'
10
- import STS from 'aws-sdk/clients/sts.js' // v2
11
- // import { STS } from '@aws-sdk/client-sts' // v3
12
-
1
+ import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache } from '@middy/util';
2
+ import STS from 'aws-sdk/clients/sts.js';
13
3
  const defaults = {
14
4
  AwsClient: STS,
15
5
  awsClientOptions: {},
16
- // awsClientAssumeRole: undefined, // Not Applicable, as this is the middleware that defines the roles
17
6
  awsClientCapture: undefined,
18
- fetchData: {}, // { contextKey: {RoleArn, RoleSessionName} }
7
+ fetchData: {},
19
8
  disablePrefetch: false,
20
9
  cacheKey: 'sts',
21
10
  cacheExpiry: -1,
22
11
  setToContext: false
23
- }
12
+ };
24
13
 
25
14
  const stsMiddleware = (opts = {}) => {
26
- const options = { ...defaults, ...opts }
15
+ const options = { ...defaults,
16
+ ...opts
17
+ };
27
18
 
28
19
  const fetch = (request, cachedValues = {}) => {
29
- const values = {}
20
+ const values = {};
30
21
 
31
22
  for (const internalKey of Object.keys(options.fetchData)) {
32
- if (cachedValues[internalKey]) continue
33
- const assumeRoleOptions = options.fetchData[internalKey]
34
- // Date cannot be used here to assign default session name, possibility of collision when > 1 role defined
35
- assumeRoleOptions.RoleSessionName ??= 'middy-sts-session-' + Math.ceil(Math.random() * 99999)
36
- values[internalKey] = client
37
- .assumeRole(assumeRoleOptions)
38
- .promise() // Required for aws-sdk v2
39
- .then((resp) => ({
40
- accessKeyId: resp.Credentials.AccessKeyId,
41
- secretAccessKey: resp.Credentials.SecretAccessKey,
42
- sessionToken: resp.Credentials.SessionToken
43
- }))
44
- .catch((e) => {
45
- const value = getCache(options.cacheKey).value ?? {}
46
- value[internalKey] = undefined
47
- modifyCache(options.cacheKey, value)
48
- throw e
49
- })
23
+ if (cachedValues[internalKey]) continue;
24
+ const assumeRoleOptions = options.fetchData[internalKey];
25
+ assumeRoleOptions.RoleSessionName ?? (assumeRoleOptions.RoleSessionName = 'middy-sts-session-' + Math.ceil(Math.random() * 99999));
26
+ values[internalKey] = client.assumeRole(assumeRoleOptions).promise().then(resp => ({
27
+ accessKeyId: resp.Credentials.AccessKeyId,
28
+ secretAccessKey: resp.Credentials.SecretAccessKey,
29
+ sessionToken: resp.Credentials.SessionToken
30
+ })).catch(e => {
31
+ const value = getCache(options.cacheKey).value ?? {};
32
+ value[internalKey] = undefined;
33
+ modifyCache(options.cacheKey, value);
34
+ throw e;
35
+ });
50
36
  }
51
37
 
52
- return values
53
- }
38
+ return values;
39
+ };
40
+
41
+ let prefetch, client;
54
42
 
55
- let prefetch, client
56
43
  if (canPrefetch(options)) {
57
- client = createPrefetchClient(options)
58
- prefetch = processCache(options, fetch)
44
+ client = createPrefetchClient(options);
45
+ prefetch = processCache(options, fetch);
59
46
  }
60
47
 
61
- const stsMiddlewareBefore = async (request) => {
48
+ const stsMiddlewareBefore = async request => {
62
49
  if (!client) {
63
- client = await createClient(options, request)
50
+ client = await createClient(options, request);
64
51
  }
65
52
 
66
- const { value } = prefetch ?? processCache(options, fetch, request)
67
-
68
- Object.assign(request.internal, value)
53
+ const {
54
+ value
55
+ } = prefetch ?? processCache(options, fetch, request);
56
+ Object.assign(request.internal, value);
69
57
 
70
58
  if (options.setToContext) {
71
- const data = await getInternal(Object.keys(options.fetchData), request)
72
- if (options.setToContext) Object.assign(request.context, data)
59
+ const data = await getInternal(Object.keys(options.fetchData), request);
60
+ if (options.setToContext) Object.assign(request.context, data);
73
61
  }
74
- prefetch = null
75
- }
62
+
63
+ prefetch = null;
64
+ };
76
65
 
77
66
  return {
78
67
  before: stsMiddlewareBefore
79
- }
80
- }
81
- export default stsMiddleware
68
+ };
69
+ };
70
+
71
+ export default stsMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/sts",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.0-alpha.4",
4
4
  "description": "STS (Security Token Service) credentials middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -48,12 +48,12 @@
48
48
  },
49
49
  "homepage": "https://github.com/middyjs/middy#readme",
50
50
  "dependencies": {
51
- "@middy/util": "^3.0.0-alpha.3"
51
+ "@middy/util": "^3.0.0-alpha.4"
52
52
  },
53
53
  "devDependencies": {
54
- "@middy/core": "^3.0.0-alpha.3",
54
+ "@middy/core": "^3.0.0-alpha.4",
55
55
  "aws-sdk": "^2.939.0",
56
56
  "aws-xray-sdk": "^3.3.3"
57
57
  },
58
- "gitHead": "1441158711580313765e6d156046ef0fade0d156"
58
+ "gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53"
59
59
  }