ag-common 0.0.91 → 0.0.92

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.
@@ -2,6 +2,9 @@ import { error } from '../../common/helpers/log';
2
2
  import { User } from '../../ui/helpers/jwt';
3
3
  import { APIGatewayProxyResult } from '../types';
4
4
  export declare const getAndValidateToken: ({ tokenRaw, jwksRegion, COGNITO_USER_POOL_ID, }: {
5
+ /**
6
+ * default ap-southeast-2
7
+ */
5
8
  jwksRegion?: string | undefined;
6
9
  tokenRaw?: string | undefined;
7
10
  COGNITO_USER_POOL_ID: string;
@@ -65,17 +65,32 @@ const getAndValidateToken = ({ tokenRaw, jwksRegion = 'ap-southeast-2', COGNITO_
65
65
  let token = '';
66
66
  try {
67
67
  if (!tokenRaw) {
68
- (0, log_1.error)('no auth headers');
68
+ const m = 'no auth headers, auth failed';
69
+ (0, log_1.error)(m);
69
70
  return {
70
- error: (0, api_1.returnCode)(403, 'auth failed'),
71
+ error: (0, api_1.returnCode)(403, m),
71
72
  };
72
73
  }
73
74
  token = tokenRaw.substring(tokenRaw.indexOf(' ') + 1);
75
+ if (!token) {
76
+ const m = 'auth error: no token';
77
+ (0, log_1.error)(m);
78
+ return {
79
+ error: (0, api_1.returnCode)(403, m),
80
+ };
81
+ }
74
82
  let subject;
75
83
  try {
76
84
  yield jwtVerify({ token, jwksUri, issuer });
77
85
  const decoded = (0, jsonwebtoken_1.decode)(token);
78
86
  (0, log_1.debug)(`decoded=${JSON.stringify(decoded, null, 2)}`);
87
+ if (!decoded.email) {
88
+ const m = 'auth error, no email';
89
+ (0, log_1.error)(m);
90
+ return {
91
+ error: (0, api_1.returnCode)(403, m),
92
+ };
93
+ }
79
94
  subject = decoded === null || decoded === void 0 ? void 0 : decoded.sub;
80
95
  if (!subject) {
81
96
  const mess = 'user should have responded with subject (sub) field';
@@ -96,12 +111,6 @@ const getAndValidateToken = ({ tokenRaw, jwksRegion = 'ap-southeast-2', COGNITO_
96
111
  picture,
97
112
  updatedAt: parseInt(`${decoded.auth_time}000`, 10),
98
113
  };
99
- if (!userProfile || !token || !userProfile.userId) {
100
- (0, log_1.error)('auth fail');
101
- return {
102
- error: (0, api_1.returnCode)(403, 'auth fail'),
103
- };
104
- }
105
114
  return { token, userProfile };
106
115
  }
107
116
  catch (e) {
@@ -114,9 +123,10 @@ const getAndValidateToken = ({ tokenRaw, jwksRegion = 'ap-southeast-2', COGNITO_
114
123
  }
115
124
  }
116
125
  catch (e) {
117
- (0, log_1.error)('auth error', e);
126
+ const m = 'auth error:' + JSON.stringify(e, null, 2);
127
+ (0, log_1.error)(m);
118
128
  return {
119
- error: (0, api_1.returnCode)(403, 'auth fail'),
129
+ error: (0, api_1.returnCode)(403, m),
120
130
  };
121
131
  }
122
132
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.91",
3
+ "version": "0.0.92",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",