lambda-essentials-ts 5.2.1 → 5.3.0

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/CHANGELOG.md CHANGED
@@ -4,6 +4,30 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
6
 
7
+ ## [5.3.0] - 2023-09-07
8
+
9
+ ### Changed
10
+
11
+ The `getUserToken()` and `getUserPrincipal()` methods now support multiple sources of for their values
12
+
13
+ `getUserToken()` in priority order:
14
+
15
+ 1. `request.authorizerContext.accessToken` (new)
16
+ 2. `request.authorizerContext.jwt`
17
+ 3. `request.headers.Authorization`
18
+
19
+ `getUserPrincipal()` in priority order:
20
+
21
+ 1. `authorizerContext.principalId` (new)
22
+ 2. `authorizerContext.canonicalId`
23
+ 3. `request.headers.Authorization`
24
+
25
+ ## [5.2.2] - 2023-08-25
26
+
27
+ ### Added
28
+
29
+ HttpClient now also logs unexpected (e.g. network) errors that are not coming from Axios
30
+
7
31
  ## [5.2.0] - 2023-06-08
8
32
 
9
33
  ### Added
@@ -5,11 +5,7 @@ export interface ApiRequest<Body = any, Query = any> {
5
5
  route: string;
6
6
  isBase64Encoded: Boolean;
7
7
  requestContext: {
8
- authorizer?: {
9
- jwt: string;
10
- canonicalId: string;
11
- principalId: string;
12
- };
8
+ authorizer?: AuthorizerContext;
13
9
  requestId: string;
14
10
  };
15
11
  headers: Record<string, string>;
@@ -18,6 +14,12 @@ export interface ApiRequest<Body = any, Query = any> {
18
14
  queryStringParameters?: Query;
19
15
  multiValueQueryStringParameters?: any;
20
16
  }
17
+ export interface AuthorizerContext {
18
+ jwt?: string;
19
+ accessToken?: string;
20
+ canonicalId?: string;
21
+ principalId?: string;
22
+ }
21
23
  export interface PostRequest<Body = any, Query = any> extends ApiRequest<Body, Query> {
22
24
  body: Body;
23
25
  }
@@ -18,4 +18,5 @@ export default class OpenApiWrapper {
18
18
  getCorrelationId(): string;
19
19
  private clearContext;
20
20
  private generateCorrelationId;
21
+ private determineUserData;
21
22
  }
@@ -47,14 +47,12 @@ class OpenApiWrapper {
47
47
  // @ts-ignore Later Use the options Type from OpenApiFactory
48
48
  this.api = new openapi_factory_1.default({
49
49
  requestMiddleware: async (request) => {
50
- var _a, _b, _c, _d, _e, _f;
50
+ var _a, _b;
51
51
  const correlationId = this.generateCorrelationId(request.headers);
52
52
  requestLogger.startInvocation(null, correlationId);
53
- // TODO: restrict the alternative way of resolving token and principal only for localhost
54
- this.userToken =
55
- (_b = (_a = request.requestContext.authorizer) === null || _a === void 0 ? void 0 : _a.jwt) !== null && _b !== void 0 ? _b : (_c = request.headers.Authorization) === null || _c === void 0 ? void 0 : _c.split(' ')[1];
56
- this.userPrincipal =
57
- (_f = (_e = (_d = request.requestContext.authorizer) === null || _d === void 0 ? void 0 : _d.canonicalId) !== null && _e !== void 0 ? _e : (0, util_1.safeJwtCanonicalIdParse)(this.userToken)) !== null && _f !== void 0 ? _f : 'unknown';
53
+ const userData = this.determineUserData(request.headers, request.requestContext.authorizer);
54
+ this.userToken = (_a = userData.userToken) !== null && _a !== void 0 ? _a : this.notSet;
55
+ this.userPrincipal = (_b = userData.userPrincipal) !== null && _b !== void 0 ? _b : this.notSet;
58
56
  this.requestId = request.requestContext.requestId;
59
57
  requestLogger.log({
60
58
  title: 'RequestLogger',
@@ -146,5 +144,20 @@ class OpenApiWrapper {
146
144
  this.correlationId = existingCorrelationId !== null && existingCorrelationId !== void 0 ? existingCorrelationId : uuid.v4();
147
145
  return this.correlationId;
148
146
  }
147
+ determineUserData(headers, authorizerContext) {
148
+ var _a, _b, _c;
149
+ if (authorizerContext) {
150
+ return {
151
+ userPrincipal: (_a = authorizerContext.principalId) !== null && _a !== void 0 ? _a : authorizerContext.canonicalId,
152
+ userToken: (_b = authorizerContext.accessToken) !== null && _b !== void 0 ? _b : authorizerContext.jwt,
153
+ };
154
+ }
155
+ if (headers.Authorization) {
156
+ const userToken = (_c = headers.Authorization.split(' ')) === null || _c === void 0 ? void 0 : _c[1];
157
+ const userPrincipal = (0, util_1.safeJwtCanonicalIdParse)(userToken);
158
+ return { userToken, userPrincipal };
159
+ }
160
+ return { userToken: undefined, userPrincipal: undefined };
161
+ }
149
162
  }
150
163
  exports.default = OpenApiWrapper;
package/lib/util.js CHANGED
@@ -45,7 +45,10 @@ exports.serializeObject = serializeObject;
45
45
  function serializeAxiosError(error) {
46
46
  var _a;
47
47
  if (!error.response) {
48
- return undefined;
48
+ return {
49
+ status: 500,
50
+ details: error,
51
+ };
49
52
  }
50
53
  const { status, data } = error.response;
51
54
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambda-essentials-ts",
3
- "version": "5.2.1",
3
+ "version": "5.3.0",
4
4
  "description": "A selection of the finest modules supporting authorization, API routing, error handling, logging and sending HTTP requests.",
5
5
  "main": "lib/index.js",
6
6
  "private": false,