@forklaunch/core 0.11.0 → 0.11.2

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.
@@ -74,16 +74,24 @@ function discriminateAuthMethod(auth) {
74
74
  if ("basic" in auth) {
75
75
  return {
76
76
  type: "basic",
77
- auth: auth.basic
77
+ auth: {
78
+ decodeResource: auth.decodeResource,
79
+ login: auth.basic.login
80
+ }
78
81
  };
79
82
  } else if ("jwt" in auth) {
80
83
  return {
81
84
  type: "jwt",
82
- auth: auth.jwt
85
+ auth: {
86
+ decodeResource: auth.decodeResource
87
+ }
83
88
  };
84
89
  } else {
85
90
  return {
86
- type: "jwt"
91
+ type: "jwt",
92
+ auth: {
93
+ decodeResource: auth.decodeResource
94
+ }
87
95
  };
88
96
  }
89
97
  }
@@ -136,32 +144,38 @@ async function checkAuthorizationToken(authorizationMethod, authorizationToken,
136
144
  return invalidAuthorizationTokenFormat;
137
145
  }
138
146
  try {
139
- const decodedJwt = await jwtVerify(
147
+ const decodedJwt = await auth?.decodeResource?.(token) ?? (await jwtVerify(
140
148
  token,
141
149
  new TextEncoder().encode(process.env.JWT_SECRET)
142
- );
143
- if (!decodedJwt.payload.sub) {
150
+ )).payload;
151
+ if (!decodedJwt) {
144
152
  return invalidAuthorizationSubject;
145
153
  }
146
- resourceId = decodedJwt.payload.sub;
154
+ resourceId = decodedJwt;
147
155
  } catch (error) {
148
- req.openTelemetryCollector.error(error);
156
+ req?.openTelemetryCollector.error(error);
149
157
  return invalidAuthorizationToken;
150
158
  }
151
159
  break;
152
160
  }
153
161
  case "basic": {
154
- if (authorizationToken !== (authorizationMethod.tokenPrefix ?? "Basic")) {
155
- return invalidAuthorizationTokenFormat;
156
- }
157
- const [username, password] = Buffer.from(token, "base64").toString("utf-8").split(":");
158
- if (!username || !password) {
162
+ if (tokenPrefix !== (authorizationMethod.tokenPrefix ?? "Basic")) {
159
163
  return invalidAuthorizationTokenFormat;
160
164
  }
161
- if (!auth.login(username, password)) {
162
- return invalidAuthorizationLogin;
165
+ if (auth.decodeResource) {
166
+ resourceId = await auth.decodeResource(token);
167
+ } else {
168
+ const [username, password] = Buffer.from(token, "base64").toString("utf-8").split(":");
169
+ if (!username || !password) {
170
+ return invalidAuthorizationTokenFormat;
171
+ }
172
+ if (!auth.login(username, password)) {
173
+ return invalidAuthorizationLogin;
174
+ }
175
+ resourceId = {
176
+ sub: username
177
+ };
163
178
  }
164
- resourceId = username;
165
179
  break;
166
180
  }
167
181
  default:
@@ -567,7 +581,18 @@ function parse(req, res, next) {
567
581
  enumerable: true,
568
582
  configurable: false
569
583
  });
570
- req.headers = parsedRequest.value.headers ?? {};
584
+ const parsedHeaders = parsedRequest.value.headers ?? {};
585
+ req.headers = Object.keys(req.headers).reduce(
586
+ (acc, key) => {
587
+ if (parsedHeaders?.[key]) {
588
+ acc[key] = parsedHeaders[key];
589
+ } else {
590
+ acc[key] = req.headers[key];
591
+ }
592
+ return acc;
593
+ },
594
+ {}
595
+ );
571
596
  }
572
597
  if (!parsedRequest.ok) {
573
598
  switch (req.contractDetails.options?.requestValidation) {