@lwrjs/auth-middleware 0.6.5 → 0.7.0-alpha.10

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/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ MIT LICENSE
2
+
3
+ Copyright (c) 2020, Salesforce.com, Inc.
4
+ All rights reserved.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -55,10 +55,14 @@ function getOauthInfoFromCookie(req) {
55
55
  const oauthInfoStr = req.cookie(COOKIE_NAME);
56
56
  return oauthInfoStr ? JSON.parse(oauthInfoStr) : void 0;
57
57
  }
58
- function buildProxyRequest(req, {access_token, instance_url}) {
58
+ function buildProxyRequest(req, {access_token, instance_url}, existingHeader) {
59
59
  const headers = {
60
+ ...existingHeader,
60
61
  Authorization: `Bearer ${access_token}`
61
62
  };
63
+ delete headers.host;
64
+ delete headers.origin;
65
+ delete headers.referer;
62
66
  let body;
63
67
  if (req.method !== "GET" && req.body) {
64
68
  body = JSON.stringify(req.body);
@@ -47,7 +47,8 @@ function platformWebServerAuthMiddleware(server, {proxyEndpoint = import_utils.P
47
47
  headers: {"Content-Type": "application/x-www-form-urlencoded", Accept: "application/json"},
48
48
  body: `grant_type=authorization_code&code=${code}&client_id=${clientKey}&client_secret=${clientSecret}&redirect_uri=${redirectUri}`
49
49
  });
50
- const data = await response.json();
50
+ const contentType = response.headers.get("Content-Type") || "";
51
+ const data = await (contentType.includes("application/json") ? response.json() : response.text());
51
52
  if (response.ok) {
52
53
  const {access_token, instance_url} = data;
53
54
  res.cookie(import_utils.COOKIE_NAME, JSON.stringify({access_token, instance_url}), {httpOnly: true});
@@ -62,7 +63,7 @@ function platformWebServerAuthMiddleware(server, {proxyEndpoint = import_utils.P
62
63
  server.all(`${proxyEndpoint}/${server.getRegexWildcard()}`, async (req, res) => {
63
64
  const oauthInfo = (0, import_utils.getOauthInfoFromCookie)(req);
64
65
  if (oauthInfo) {
65
- const {url, options} = (0, import_utils.buildProxyRequest)(req, oauthInfo);
66
+ const {url, options} = (0, import_utils.buildProxyRequest)(req, oauthInfo, req.headers);
66
67
  const proxyRes = await (0, import_node_fetch.default)(url, options);
67
68
  const contentType = proxyRes.headers.get("Content-Type") || "";
68
69
  const data = await (contentType.includes("application/json") ? proxyRes.json() : proxyRes.text());
@@ -1,3 +1,4 @@
1
+ import type { RequestInit } from 'node-fetch';
1
2
  export interface PlatformWebServerInput {
2
3
  /** base URL to the Connected App, e.g. "https://lwr.lightning.force.com" (no trailing slash) */
3
4
  myDomain: string;
@@ -18,10 +19,6 @@ export interface OAuthInfo {
18
19
  }
19
20
  export interface RequestOptions {
20
21
  url: string;
21
- options: {
22
- method: string;
23
- headers?: Record<string, string>;
24
- body?: string;
25
- };
22
+ options: RequestInit;
26
23
  }
27
24
  //# sourceMappingURL=types.d.ts.map
@@ -1,4 +1,4 @@
1
- import type { MiddlewareRequest } from '@lwrjs/types';
1
+ import type { MiddlewareRequest, Headers } from '@lwrjs/types';
2
2
  import type { OAuthInfo, RequestOptions, PlatformWebServerInput } from './types.js';
3
3
  export declare const LOGIN_ENDPOINT = "/login";
4
4
  export declare const REDIRECT_ENDPOINT = "/oauth";
@@ -8,5 +8,5 @@ export declare function buildRedirectUrl(req: MiddlewareRequest): string;
8
8
  export declare function getPlatformWebServerEnvVars(): PlatformWebServerInput;
9
9
  export declare function getAppPathAsState(req: MiddlewareRequest): string;
10
10
  export declare function getOauthInfoFromCookie(req: MiddlewareRequest): OAuthInfo | undefined;
11
- export declare function buildProxyRequest(req: MiddlewareRequest, { access_token, instance_url }: OAuthInfo): RequestOptions;
11
+ export declare function buildProxyRequest(req: MiddlewareRequest, { access_token, instance_url }: OAuthInfo, existingHeader: Headers): RequestOptions;
12
12
  //# sourceMappingURL=utils.d.ts.map
package/build/es/utils.js CHANGED
@@ -42,11 +42,16 @@ export function getOauthInfoFromCookie(req) {
42
42
  return oauthInfoStr ? JSON.parse(oauthInfoStr) : undefined;
43
43
  }
44
44
  // Build an authenticated request based on a request to the proxy endpoint
45
- export function buildProxyRequest(req, { access_token, instance_url }) {
46
- // Add the oauth header, future: forward any headers from the client?
45
+ export function buildProxyRequest(req, { access_token, instance_url }, existingHeader) {
46
+ // Add the oauth header to existing headers
47
47
  const headers = {
48
+ ...existingHeader,
48
49
  Authorization: `Bearer ${access_token}`, // add the oauth header
49
50
  };
51
+ // must delete certain headers to satisfy CORS
52
+ delete headers.host;
53
+ delete headers.origin;
54
+ delete headers.referer;
50
55
  // Pull the body from non-GET requests
51
56
  let body;
52
57
  if (req.method !== 'GET' && req.body) {
@@ -37,7 +37,10 @@ export function platformWebServerAuthMiddleware(server, { proxyEndpoint = PROXY_
37
37
  body: `grant_type=authorization_code&code=${code}&client_id=${clientKey}&client_secret=${clientSecret}&redirect_uri=${redirectUri}`,
38
38
  });
39
39
  // Response handling
40
- const data = await response.json();
40
+ const contentType = response.headers.get('Content-Type') || ''; // parse as json or text
41
+ const data = await (contentType.includes('application/json')
42
+ ? response.json()
43
+ : response.text());
41
44
  if (response.ok) {
42
45
  // Store the OAuth info in a cookie and redirect to the app
43
46
  const { access_token, instance_url } = data;
@@ -57,7 +60,7 @@ export function platformWebServerAuthMiddleware(server, { proxyEndpoint = PROXY_
57
60
  server.all(`${proxyEndpoint}/${server.getRegexWildcard()}`, async (req, res) => {
58
61
  const oauthInfo = getOauthInfoFromCookie(req);
59
62
  if (oauthInfo) {
60
- const { url, options } = buildProxyRequest(req, oauthInfo);
63
+ const { url, options } = buildProxyRequest(req, oauthInfo, req.headers);
61
64
  const proxyRes = await fetch(url, options);
62
65
  const contentType = proxyRes.headers.get('Content-Type') || ''; // parse as json or text
63
66
  const data = await (contentType.includes('application/json') ? proxyRes.json() : proxyRes.text());
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.6.5",
7
+ "version": "0.7.0-alpha.10",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -29,13 +29,16 @@
29
29
  "build/**/*.cjs",
30
30
  "build/**/*.d.ts"
31
31
  ],
32
- "devDependencies": {
33
- "@lwrjs/server": "0.6.5",
34
- "@lwrjs/types": "0.6.5",
35
- "@types/node-fetch": "^2.5.12",
32
+ "dependencies": {
36
33
  "node-fetch": "^2.6.1"
37
34
  },
35
+ "devDependencies": {
36
+ "@lwrjs/server": "0.7.0-alpha.10",
37
+ "@lwrjs/types": "0.7.0-alpha.10",
38
+ "@types/node-fetch": "^2.5.12"
39
+ },
38
40
  "engines": {
39
41
  "node": ">=14.15.4 <17"
40
- }
42
+ },
43
+ "gitHead": "83c1e65e2169094cb55ac2c37e5aef16d3a9aa4a"
41
44
  }