@aglyn/shared-util-http 1.0.0-beta.143
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 +201 -0
- package/README.md +3 -0
- package/jest.config.d.ts +30 -0
- package/package.json +38 -0
- package/src/index.d.ts +17 -0
- package/src/index.js +18 -0
- package/src/index.js.map +1 -0
- package/src/lib/authorized-token.d.ts +109 -0
- package/src/lib/authorized-token.js +189 -0
- package/src/lib/authorized-token.js.map +1 -0
- package/src/lib/cookie-get-user-id-token.d.ts +19 -0
- package/src/lib/cookie-get-user-id-token.js +23 -0
- package/src/lib/cookie-get-user-id-token.js.map +1 -0
- package/src/lib/cookie-set-user-id-token.d.ts +20 -0
- package/src/lib/cookie-set-user-id-token.js +25 -0
- package/src/lib/cookie-set-user-id-token.js.map +1 -0
- package/src/lib/cors-build-response-handler.d.ts +7 -0
- package/src/lib/cors-build-response-handler.js +25 -0
- package/src/lib/cors-build-response-handler.js.map +1 -0
- package/src/lib/cors-handle-response.d.ts +22 -0
- package/src/lib/cors-handle-response.js +94 -0
- package/src/lib/cors-handle-response.js.map +1 -0
- package/src/lib/get-absolute-url.d.ts +34 -0
- package/src/lib/get-absolute-url.js +48 -0
- package/src/lib/get-absolute-url.js.map +1 -0
- package/src/lib/get-allowed-headers.d.ts +26 -0
- package/src/lib/get-allowed-headers.js +39 -0
- package/src/lib/get-allowed-headers.js.map +1 -0
- package/src/lib/get-origin-headers-from-request.d.ts +20 -0
- package/src/lib/get-origin-headers-from-request.js +26 -0
- package/src/lib/get-origin-headers-from-request.js.map +1 -0
- package/src/lib/get-origin-headers.d.ts +3 -0
- package/src/lib/get-origin-headers.js +37 -0
- package/src/lib/get-origin-headers.js.map +1 -0
- package/src/lib/get-request-header.d.ts +20 -0
- package/src/lib/get-request-header.js +21 -0
- package/src/lib/get-request-header.js.map +1 -0
- package/src/lib/http.d.ts +27 -0
- package/src/lib/http.js +28 -0
- package/src/lib/http.js.map +1 -0
- package/src/lib/is-origin-allowed.d.ts +19 -0
- package/src/lib/is-origin-allowed.js +21 -0
- package/src/lib/is-origin-allowed.js.map +1 -0
- package/src/lib/safe-redirect.d.ts +41 -0
- package/src/lib/safe-redirect.js +101 -0
- package/src/lib/safe-redirect.js.map +1 -0
- package/src/lib/safe-url-scheme.d.ts +42 -0
- package/src/lib/safe-url-scheme.js +124 -0
- package/src/lib/safe-url-scheme.js.map +1 -0
- package/src/lib/types.d.ts +30 -0
- package/src/lib/types.js +18 -0
- package/src/lib/types.js.map +1 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { type NextRequest } from 'next/server';
|
|
18
|
+
export declare function cookieGetUserIdToken(request: NextRequest): string | undefined;
|
|
19
|
+
export default cookieGetUserIdToken;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/ import { COOKIE_KEY_USER_TOKEN } from "@aglyn/shared-data-enums";
|
|
17
|
+
export function cookieGetUserIdToken(request) {
|
|
18
|
+
var _request_cookies_get;
|
|
19
|
+
return (_request_cookies_get = request.cookies.get(COOKIE_KEY_USER_TOKEN)) == null ? void 0 : _request_cookies_get.value;
|
|
20
|
+
}
|
|
21
|
+
export default cookieGetUserIdToken;
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=cookie-get-user-id-token.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/shared/util/http/src/lib/cookie-get-user-id-token.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2022 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {COOKIE_KEY_USER_TOKEN} from '@aglyn/shared-data-enums'\nimport {type NextRequest} from 'next/server'\n\n\nexport function cookieGetUserIdToken(request: NextRequest): string | undefined {\n return request.cookies.get(COOKIE_KEY_USER_TOKEN)?.value\n}\nexport default cookieGetUserIdToken\n"],"names":["COOKIE_KEY_USER_TOKEN","cookieGetUserIdToken","request","cookies","get","value"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAAQA,qBAAqB,QAAO,2BAA0B;AAI9D,OAAO,SAASC,qBAAqBC,OAAoB;QAChDA;IAAP,QAAOA,uBAAAA,QAAQC,OAAO,CAACC,GAAG,CAACJ,2CAApBE,qBAA4CG,KAAK;AAC1D;AACA,eAAeJ,qBAAoB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies';
|
|
18
|
+
import type { NextRequest, NextResponse } from 'next/server';
|
|
19
|
+
export declare function cookieSetUserIdToken(request: NextRequest, response: NextResponse, token: string, options?: Partial<ResponseCookie>): import("next/dist/compiled/@edge-runtime/cookies").ResponseCookies;
|
|
20
|
+
export default cookieSetUserIdToken;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2022 Aglyn LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/ import { COOKIE_KEY_USER_TOKEN } from "@aglyn/shared-data-enums";
|
|
18
|
+
export function cookieSetUserIdToken(request, response, token, options) {
|
|
19
|
+
return response.cookies.set(COOKIE_KEY_USER_TOKEN, token, _extends({
|
|
20
|
+
httpOnly: true
|
|
21
|
+
}, options));
|
|
22
|
+
}
|
|
23
|
+
export default cookieSetUserIdToken;
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=cookie-set-user-id-token.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/shared/util/http/src/lib/cookie-set-user-id-token.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2022 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {COOKIE_KEY_USER_TOKEN} from '@aglyn/shared-data-enums'\nimport type {ResponseCookie} from 'next/dist/compiled/@edge-runtime/cookies'\nimport type {NextRequest, NextResponse} from 'next/server'\n\n\nexport function cookieSetUserIdToken(\n request: NextRequest,\n response: NextResponse,\n token: string,\n options?: Partial<ResponseCookie>,\n) {\n return response.cookies.set(COOKIE_KEY_USER_TOKEN, token, {\n httpOnly: true,\n ...options,\n })\n}\nexport default cookieSetUserIdToken\n"],"names":["COOKIE_KEY_USER_TOKEN","cookieSetUserIdToken","request","response","token","options","cookies","set","httpOnly"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAAQA,qBAAqB,QAAO,2BAA0B;AAK9D,OAAO,SAASC,qBACdC,OAAoB,EACpBC,QAAsB,EACtBC,KAAa,EACbC,OAAiC;IAEjC,OAAOF,SAASG,OAAO,CAACC,GAAG,CAACP,uBAAuBI,OAAO;QACxDI,UAAU;OACPH;AAEP;AACA,eAAeJ,qBAAoB"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CorsOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Curry the {@link corsHandleResponse} to handle a reusable cors config
|
|
4
|
+
* @param options - optional object of {@link CorsOptions}
|
|
5
|
+
*/
|
|
6
|
+
export declare function corsBuildResponseHandler(options?: CorsOptions): (req: Request, res: Response) => Promise<Response>;
|
|
7
|
+
export default corsBuildResponseHandler;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/ import corsHandleResponse from "./cors-handle-response.js";
|
|
17
|
+
/**
|
|
18
|
+
* Curry the {@link corsHandleResponse} to handle a reusable cors config
|
|
19
|
+
* @param options - optional object of {@link CorsOptions}
|
|
20
|
+
*/ export function corsBuildResponseHandler(options) {
|
|
21
|
+
return (req, res)=>corsHandleResponse(req, res, options);
|
|
22
|
+
}
|
|
23
|
+
export default corsBuildResponseHandler;
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=cors-build-response-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/shared/util/http/src/lib/cors-build-response-handler.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2022 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport corsHandleResponse from './cors-handle-response'\nimport type {CorsOptions} from './types'\n\n\n/**\n * Curry the {@link corsHandleResponse} to handle a reusable cors config\n * @param options - optional object of {@link CorsOptions}\n */\nexport function corsBuildResponseHandler(options?: CorsOptions) {\n return (req: Request, res: Response) => corsHandleResponse(req, res, options)\n}\nexport default corsBuildResponseHandler\n"],"names":["corsHandleResponse","corsBuildResponseHandler","options","req","res"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GACD,OAAOA,wBAAwB,4BAAwB;AAIvD;;;CAGC,GACD,OAAO,SAASC,yBAAyBC,OAAqB;IAC5D,OAAO,CAACC,KAAcC,MAAkBJ,mBAAmBG,KAAKC,KAAKF;AACvE;AACA,eAAeD,yBAAwB"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CorsOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Handle cors for HTTP requests
|
|
4
|
+
* @example - pages/middleware.ts
|
|
5
|
+
* export function middleware(req: NextRequest) {
|
|
6
|
+
* // `cors` also takes care of handling OPTIONS requests
|
|
7
|
+
* return cors(
|
|
8
|
+
* req,
|
|
9
|
+
* new Response(JSON.stringify({ message: 'Hello World!' }), {
|
|
10
|
+
* status: 200,
|
|
11
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
12
|
+
* })
|
|
13
|
+
* )
|
|
14
|
+
* }
|
|
15
|
+
* @param req - HTTP {@link Request} object
|
|
16
|
+
* @param res - HTTP {@link Response} object
|
|
17
|
+
* @param options - optional object of {@link CorsOptions}
|
|
18
|
+
* @returns the modified {@link Response} object to send answer the request
|
|
19
|
+
* @see {@link corsBuildResponseHandler} when reusing same {@link CorsOptions}
|
|
20
|
+
*/
|
|
21
|
+
export declare function corsHandleResponse(req: Request, res: Response, options?: CorsOptions): Promise<Response>;
|
|
22
|
+
export default corsHandleResponse;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2022 Aglyn LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/ import { HttpStatusCode } from "@aglyn/shared-data-enums";
|
|
18
|
+
import getAllowedHeaders from "./get-allowed-headers.js";
|
|
19
|
+
import getOriginHeadersFromRequest from "./get-origin-headers-from-request.js";
|
|
20
|
+
// Deny-by-default (AGL-520): callers must opt into an origin or allowlist
|
|
21
|
+
// rather than inheriting `*`, so this helper can never silently expose an
|
|
22
|
+
// authenticated endpoint cross-origin.
|
|
23
|
+
const defaultOptions = {
|
|
24
|
+
origin: false,
|
|
25
|
+
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
|
|
26
|
+
preflightContinue: false,
|
|
27
|
+
optionsSuccessStatus: HttpStatusCode.NO_CONTENT
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Handle cors for HTTP requests
|
|
31
|
+
* @example - pages/middleware.ts
|
|
32
|
+
* export function middleware(req: NextRequest) {
|
|
33
|
+
* // `cors` also takes care of handling OPTIONS requests
|
|
34
|
+
* return cors(
|
|
35
|
+
* req,
|
|
36
|
+
* new Response(JSON.stringify({ message: 'Hello World!' }), {
|
|
37
|
+
* status: 200,
|
|
38
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
39
|
+
* })
|
|
40
|
+
* )
|
|
41
|
+
* }
|
|
42
|
+
* @param req - HTTP {@link Request} object
|
|
43
|
+
* @param res - HTTP {@link Response} object
|
|
44
|
+
* @param options - optional object of {@link CorsOptions}
|
|
45
|
+
* @returns the modified {@link Response} object to send answer the request
|
|
46
|
+
* @see {@link corsBuildResponseHandler} when reusing same {@link CorsOptions}
|
|
47
|
+
*/ export async function corsHandleResponse(req, res, options) {
|
|
48
|
+
var _opts_origin;
|
|
49
|
+
const opts = _extends({}, defaultOptions, options);
|
|
50
|
+
const { headers } = res;
|
|
51
|
+
const originHeaders = await getOriginHeadersFromRequest(req, (_opts_origin = opts.origin) != null ? _opts_origin : false);
|
|
52
|
+
const mergeHeaders = (v, k)=>{
|
|
53
|
+
if (k === 'Vary') headers.append(k, v);
|
|
54
|
+
else headers.set(k, v);
|
|
55
|
+
};
|
|
56
|
+
// If there's no origin we won't touch the response
|
|
57
|
+
if (!originHeaders) return res;
|
|
58
|
+
originHeaders.forEach(mergeHeaders);
|
|
59
|
+
if (opts.credentials) {
|
|
60
|
+
// Never pair credentials with a wildcard origin (AGL-520): browsers
|
|
61
|
+
// reject it, and honoring it would be a serious cross-origin leak.
|
|
62
|
+
if (opts.origin === '*') {
|
|
63
|
+
console.error('CORS: refusing Access-Control-Allow-Credentials with origin "*"');
|
|
64
|
+
} else {
|
|
65
|
+
headers.set('Access-Control-Allow-Credentials', 'true');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const exposed = Array.isArray(opts.exposedHeaders) ? opts.exposedHeaders.join(',') : opts.exposedHeaders;
|
|
69
|
+
if (exposed) {
|
|
70
|
+
headers.set('Access-Control-Expose-Headers', exposed);
|
|
71
|
+
}
|
|
72
|
+
// Handle the preflight request
|
|
73
|
+
if (req.method === 'OPTIONS') {
|
|
74
|
+
if (opts.methods) {
|
|
75
|
+
const methods = Array.isArray(opts.methods) ? opts.methods.join(',') : opts.methods;
|
|
76
|
+
headers.set('Access-Control-Allow-Methods', methods);
|
|
77
|
+
}
|
|
78
|
+
getAllowedHeaders(req, opts.allowedHeaders).forEach(mergeHeaders);
|
|
79
|
+
if (typeof opts.maxAge === 'number') {
|
|
80
|
+
headers.set('Access-Control-Max-Age', String(opts.maxAge));
|
|
81
|
+
}
|
|
82
|
+
if (opts.preflightContinue) return res;
|
|
83
|
+
headers.set('Content-Length', '0');
|
|
84
|
+
return new Response(null, {
|
|
85
|
+
status: opts.optionsSuccessStatus,
|
|
86
|
+
headers
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
// If we got here, it's a normal request
|
|
90
|
+
return res;
|
|
91
|
+
}
|
|
92
|
+
export default corsHandleResponse;
|
|
93
|
+
|
|
94
|
+
//# sourceMappingURL=cors-handle-response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/shared/util/http/src/lib/cors-handle-response.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2022 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { HttpStatusCode } from '@aglyn/shared-data-enums'\nimport getAllowedHeaders from './get-allowed-headers'\nimport getOriginHeadersFromRequest from './get-origin-headers-from-request'\nimport type { CorsOptions } from './types'\n\n// Deny-by-default (AGL-520): callers must opt into an origin or allowlist\n// rather than inheriting `*`, so this helper can never silently expose an\n// authenticated endpoint cross-origin.\nconst defaultOptions: CorsOptions = {\n origin: false,\n methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',\n preflightContinue: false,\n optionsSuccessStatus: HttpStatusCode.NO_CONTENT,\n}\n\n/**\n * Handle cors for HTTP requests\n * @example - pages/middleware.ts\n * export function middleware(req: NextRequest) {\n * // `cors` also takes care of handling OPTIONS requests\n * return cors(\n * req,\n * new Response(JSON.stringify({ message: 'Hello World!' }), {\n * status: 200,\n * headers: { 'Content-Type': 'application/json' },\n * })\n * )\n * }\n * @param req - HTTP {@link Request} object\n * @param res - HTTP {@link Response} object\n * @param options - optional object of {@link CorsOptions}\n * @returns the modified {@link Response} object to send answer the request\n * @see {@link corsBuildResponseHandler} when reusing same {@link CorsOptions}\n */\nexport async function corsHandleResponse(\n req: Request,\n res: Response,\n options?: CorsOptions,\n) {\n const opts = { ...defaultOptions, ...options }\n const { headers } = res\n const originHeaders = await getOriginHeadersFromRequest(\n req,\n opts.origin ?? false,\n )\n const mergeHeaders = (v: string, k: string) => {\n if (k === 'Vary') headers.append(k, v)\n else headers.set(k, v)\n }\n\n // If there's no origin we won't touch the response\n if (!originHeaders) return res\n\n originHeaders.forEach(mergeHeaders)\n\n if (opts.credentials) {\n // Never pair credentials with a wildcard origin (AGL-520): browsers\n // reject it, and honoring it would be a serious cross-origin leak.\n if (opts.origin === '*') {\n console.error(\n 'CORS: refusing Access-Control-Allow-Credentials with origin \"*\"',\n )\n } else {\n headers.set('Access-Control-Allow-Credentials', 'true')\n }\n }\n\n const exposed = Array.isArray(opts.exposedHeaders)\n ? opts.exposedHeaders.join(',')\n : opts.exposedHeaders\n\n if (exposed) {\n headers.set('Access-Control-Expose-Headers', exposed)\n }\n\n // Handle the preflight request\n if (req.method === 'OPTIONS') {\n if (opts.methods) {\n const methods = Array.isArray(opts.methods)\n ? opts.methods.join(',')\n : opts.methods\n\n headers.set('Access-Control-Allow-Methods', methods)\n }\n\n getAllowedHeaders(req, opts.allowedHeaders).forEach(mergeHeaders)\n\n if (typeof opts.maxAge === 'number') {\n headers.set('Access-Control-Max-Age', String(opts.maxAge))\n }\n\n if (opts.preflightContinue) return res\n\n headers.set('Content-Length', '0')\n return new Response(null, { status: opts.optionsSuccessStatus, headers })\n }\n\n // If we got here, it's a normal request\n return res\n}\n\nexport default corsHandleResponse\n"],"names":["HttpStatusCode","getAllowedHeaders","getOriginHeadersFromRequest","defaultOptions","origin","methods","preflightContinue","optionsSuccessStatus","NO_CONTENT","corsHandleResponse","req","res","options","opts","headers","originHeaders","mergeHeaders","v","k","append","set","forEach","credentials","console","error","exposed","Array","isArray","exposedHeaders","join","method","allowedHeaders","maxAge","String","Response","status"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GACD,SAASA,cAAc,QAAQ,2BAA0B;AACzD,OAAOC,uBAAuB,2BAAuB;AACrD,OAAOC,iCAAiC,uCAAmC;AAG3E,0EAA0E;AAC1E,0EAA0E;AAC1E,uCAAuC;AACvC,MAAMC,iBAA8B;IAClCC,QAAQ;IACRC,SAAS;IACTC,mBAAmB;IACnBC,sBAAsBP,eAAeQ,UAAU;AACjD;AAEA;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,eAAeC,mBACpBC,GAAY,EACZC,GAAa,EACbC,OAAqB;QAMnBC;IAJF,MAAMA,OAAO,aAAKV,gBAAmBS;IACrC,MAAM,EAAEE,OAAO,EAAE,GAAGH;IACpB,MAAMI,gBAAgB,MAAMb,4BAC1BQ,MACAG,eAAAA,KAAKT,MAAM,YAAXS,eAAe;IAEjB,MAAMG,eAAe,CAACC,GAAWC;QAC/B,IAAIA,MAAM,QAAQJ,QAAQK,MAAM,CAACD,GAAGD;aAC/BH,QAAQM,GAAG,CAACF,GAAGD;IACtB;IAEA,mDAAmD;IACnD,IAAI,CAACF,eAAe,OAAOJ;IAE3BI,cAAcM,OAAO,CAACL;IAEtB,IAAIH,KAAKS,WAAW,EAAE;QACpB,oEAAoE;QACpE,mEAAmE;QACnE,IAAIT,KAAKT,MAAM,KAAK,KAAK;YACvBmB,QAAQC,KAAK,CACX;QAEJ,OAAO;YACLV,QAAQM,GAAG,CAAC,oCAAoC;QAClD;IACF;IAEA,MAAMK,UAAUC,MAAMC,OAAO,CAACd,KAAKe,cAAc,IAC7Cf,KAAKe,cAAc,CAACC,IAAI,CAAC,OACzBhB,KAAKe,cAAc;IAEvB,IAAIH,SAAS;QACXX,QAAQM,GAAG,CAAC,iCAAiCK;IAC/C;IAEA,+BAA+B;IAC/B,IAAIf,IAAIoB,MAAM,KAAK,WAAW;QAC5B,IAAIjB,KAAKR,OAAO,EAAE;YAChB,MAAMA,UAAUqB,MAAMC,OAAO,CAACd,KAAKR,OAAO,IACtCQ,KAAKR,OAAO,CAACwB,IAAI,CAAC,OAClBhB,KAAKR,OAAO;YAEhBS,QAAQM,GAAG,CAAC,gCAAgCf;QAC9C;QAEAJ,kBAAkBS,KAAKG,KAAKkB,cAAc,EAAEV,OAAO,CAACL;QAEpD,IAAI,OAAOH,KAAKmB,MAAM,KAAK,UAAU;YACnClB,QAAQM,GAAG,CAAC,0BAA0Ba,OAAOpB,KAAKmB,MAAM;QAC1D;QAEA,IAAInB,KAAKP,iBAAiB,EAAE,OAAOK;QAEnCG,QAAQM,GAAG,CAAC,kBAAkB;QAC9B,OAAO,IAAIc,SAAS,MAAM;YAAEC,QAAQtB,KAAKN,oBAAoB;YAAEO;QAAQ;IACzE;IAEA,wCAAwC;IACxC,OAAOH;AACT;AAEA,eAAeF,mBAAkB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* @license
|
|
19
|
+
* Copyright 2022 Aglyn LLC
|
|
20
|
+
*
|
|
21
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
22
|
+
* you may not use this file except in compliance with the License.
|
|
23
|
+
* You may obtain a copy of the License at
|
|
24
|
+
*
|
|
25
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
26
|
+
*
|
|
27
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
28
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
29
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
30
|
+
* See the License for the specific language governing permissions and
|
|
31
|
+
* limitations under the License.
|
|
32
|
+
*/
|
|
33
|
+
export declare function getAbsoluteUrl(url: string, request?: Request): string;
|
|
34
|
+
export default getAbsoluteUrl;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/ import getRequestHeader from "./get-request-header.js";
|
|
17
|
+
/**
|
|
18
|
+
* @license
|
|
19
|
+
* Copyright 2022 Aglyn LLC
|
|
20
|
+
*
|
|
21
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
22
|
+
* you may not use this file except in compliance with the License.
|
|
23
|
+
* You may obtain a copy of the License at
|
|
24
|
+
*
|
|
25
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
26
|
+
*
|
|
27
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
28
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
29
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
30
|
+
* See the License for the specific language governing permissions and
|
|
31
|
+
* limitations under the License.
|
|
32
|
+
*/ export function getAbsoluteUrl(url, request = null) {
|
|
33
|
+
let host;
|
|
34
|
+
if (request) {
|
|
35
|
+
host = getRequestHeader(request, 'host');
|
|
36
|
+
} else {
|
|
37
|
+
if (typeof window === 'undefined') {
|
|
38
|
+
throw new Error('The "req" parameter must be provided if on the server side.');
|
|
39
|
+
}
|
|
40
|
+
host = window.location.host;
|
|
41
|
+
}
|
|
42
|
+
const isLocalhost = host.indexOf('localhost') === 0;
|
|
43
|
+
const protocol = isLocalhost ? 'http' : 'https';
|
|
44
|
+
return `${protocol}://${host}${url}`;
|
|
45
|
+
}
|
|
46
|
+
export default getAbsoluteUrl;
|
|
47
|
+
|
|
48
|
+
//# sourceMappingURL=get-absolute-url.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/shared/util/http/src/lib/get-absolute-url.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2022 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport getRequestHeader from './get-request-header'\n\n\n/**\n * @license\n * Copyright 2022 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n\nexport function getAbsoluteUrl(url: string, request: Request = null) {\n let host\n if (request) {\n host = getRequestHeader(request, 'host')\n }\n else {\n if (typeof window === 'undefined') {\n throw new Error(\n 'The \"req\" parameter must be provided if on the server side.',\n )\n }\n host = window.location.host\n }\n const isLocalhost = host.indexOf('localhost') === 0\n const protocol = isLocalhost ? 'http' : 'https'\n return `${protocol}://${host}${url}`\n}\n\nexport default getAbsoluteUrl\n"],"names":["getRequestHeader","getAbsoluteUrl","url","request","host","window","Error","location","isLocalhost","indexOf","protocol"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,OAAOA,sBAAsB,0BAAsB;AAGnD;;;;;;;;;;;;;;;CAeC,GAGD,OAAO,SAASC,eAAeC,GAAW,EAAEC,UAAmB,IAAI;IACjE,IAAIC;IACJ,IAAID,SAAS;QACXC,OAAOJ,iBAAiBG,SAAS;IACnC,OACK;QACH,IAAI,OAAOE,WAAW,aAAa;YACjC,MAAM,IAAIC,MACR;QAEJ;QACAF,OAAOC,OAAOE,QAAQ,CAACH,IAAI;IAC7B;IACA,MAAMI,cAAcJ,KAAKK,OAAO,CAAC,iBAAiB;IAClD,MAAMC,WAAWF,cAAc,SAAS;IACxC,OAAO,GAAGE,SAAS,GAAG,EAAEN,OAAOF,KAAK;AACtC;AAEA,eAAeD,eAAc"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import type { NextRequest } from 'next/server';
|
|
18
|
+
/**
|
|
19
|
+
* If `allowed` param is not provided retrieves `Access-Control-Request-Headers`
|
|
20
|
+
* header value from the `req` param and returns a new {@link Headers} object
|
|
21
|
+
* @param request - HTTP {@link Request} object
|
|
22
|
+
* @param allowed - optionally override which headers to allow
|
|
23
|
+
* @returns a new {@link Headers} object instance
|
|
24
|
+
*/
|
|
25
|
+
export declare function getAllowedHeaders(request: Request | NextRequest, allowed?: string | string[]): Headers;
|
|
26
|
+
export default getAllowedHeaders;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/ import getRequestHeader from "./get-request-header.js";
|
|
17
|
+
/**
|
|
18
|
+
* If `allowed` param is not provided retrieves `Access-Control-Request-Headers`
|
|
19
|
+
* header value from the `req` param and returns a new {@link Headers} object
|
|
20
|
+
* @param request - HTTP {@link Request} object
|
|
21
|
+
* @param allowed - optionally override which headers to allow
|
|
22
|
+
* @returns a new {@link Headers} object instance
|
|
23
|
+
*/ export function getAllowedHeaders(request, allowed) {
|
|
24
|
+
const headers = new Headers();
|
|
25
|
+
if (!allowed) {
|
|
26
|
+
allowed = getRequestHeader(request, 'Access-Control-Request-Headers');
|
|
27
|
+
headers.append('Vary', 'Access-Control-Request-Headers');
|
|
28
|
+
} else if (Array.isArray(allowed)) {
|
|
29
|
+
// If the allowed headers is an array, turn it into a string
|
|
30
|
+
allowed = allowed.join(',');
|
|
31
|
+
}
|
|
32
|
+
if (allowed) {
|
|
33
|
+
headers.set('Access-Control-Allow-Headers', allowed);
|
|
34
|
+
}
|
|
35
|
+
return headers;
|
|
36
|
+
}
|
|
37
|
+
export default getAllowedHeaders;
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=get-allowed-headers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/shared/util/http/src/lib/get-allowed-headers.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2022 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type {NextRequest} from 'next/server'\nimport getRequestHeader from './get-request-header'\n\n\n/**\n * If `allowed` param is not provided retrieves `Access-Control-Request-Headers`\n * header value from the `req` param and returns a new {@link Headers} object\n * @param request - HTTP {@link Request} object\n * @param allowed - optionally override which headers to allow\n * @returns a new {@link Headers} object instance\n */\nexport function getAllowedHeaders(request: Request | NextRequest, allowed?: string | string[]) {\n const headers = new Headers()\n\n if (!allowed) {\n allowed = getRequestHeader(request, 'Access-Control-Request-Headers')!\n headers.append('Vary', 'Access-Control-Request-Headers')\n }\n else if (Array.isArray(allowed)) {\n // If the allowed headers is an array, turn it into a string\n allowed = allowed.join(',')\n }\n if (allowed) {\n headers.set('Access-Control-Allow-Headers', allowed as string)\n }\n\n return headers\n}\n\nexport default getAllowedHeaders\n"],"names":["getRequestHeader","getAllowedHeaders","request","allowed","headers","Headers","append","Array","isArray","join","set"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAGD,OAAOA,sBAAsB,0BAAsB;AAGnD;;;;;;CAMC,GACD,OAAO,SAASC,kBAAkBC,OAA8B,EAAEC,OAA2B;IAC3F,MAAMC,UAAU,IAAIC;IAEpB,IAAI,CAACF,SAAS;QACZA,UAAUH,iBAAiBE,SAAS;QACpCE,QAAQE,MAAM,CAAC,QAAQ;IACzB,OACK,IAAIC,MAAMC,OAAO,CAACL,UAAU;QAC/B,4DAA4D;QAC5DA,UAAUA,QAAQM,IAAI,CAAC;IACzB;IACA,IAAIN,SAAS;QACXC,QAAQM,GAAG,CAAC,gCAAgCP;IAC9C;IAEA,OAAOC;AACT;AAEA,eAAeH,kBAAiB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import type { NextApiRequest } from 'next';
|
|
18
|
+
import type { OriginFn, StaticOrigin } from './types';
|
|
19
|
+
export declare function getOriginHeadersFromRequest(request: Request | NextApiRequest, origin: StaticOrigin | OriginFn): Promise<Headers>;
|
|
20
|
+
export default getOriginHeadersFromRequest;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/ import getOriginHeaders from "./get-origin-headers.js";
|
|
17
|
+
import getRequestHeader from "./get-request-header.js";
|
|
18
|
+
export async function getOriginHeadersFromRequest(request, origin) {
|
|
19
|
+
const reqOrigin = getRequestHeader(request, 'Origin') || undefined;
|
|
20
|
+
const value = typeof origin === 'function' ? await origin(reqOrigin, request) : origin;
|
|
21
|
+
if (!value) return;
|
|
22
|
+
return getOriginHeaders(reqOrigin, value);
|
|
23
|
+
}
|
|
24
|
+
export default getOriginHeadersFromRequest;
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=get-origin-headers-from-request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/shared/util/http/src/lib/get-origin-headers-from-request.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2022 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport type {NextApiRequest} from 'next'\nimport getOriginHeaders from './get-origin-headers'\nimport getRequestHeader from './get-request-header'\nimport type {OriginFn, StaticOrigin} from './types'\n\n\nexport async function getOriginHeadersFromRequest(\n request: Request | NextApiRequest,\n origin: StaticOrigin | OriginFn,\n) {\n const reqOrigin = getRequestHeader(request, 'Origin') || undefined\n const value = typeof origin === 'function'\n ? await origin(reqOrigin, request)\n : origin\n\n if (!value) return\n return getOriginHeaders(reqOrigin as string, value)\n}\n\nexport default getOriginHeadersFromRequest\n"],"names":["getOriginHeaders","getRequestHeader","getOriginHeadersFromRequest","request","origin","reqOrigin","undefined","value"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,OAAOA,sBAAsB,0BAAsB;AACnD,OAAOC,sBAAsB,0BAAsB;AAInD,OAAO,eAAeC,4BACpBC,OAAiC,EACjCC,MAA+B;IAE/B,MAAMC,YAAYJ,iBAAiBE,SAAS,aAAaG;IACzD,MAAMC,QAAQ,OAAOH,WAAW,aAC5B,MAAMA,OAAOC,WAAWF,WACxBC;IAEJ,IAAI,CAACG,OAAO;IACZ,OAAOP,iBAAiBK,WAAqBE;AAC/C;AAEA,eAAeL,4BAA2B"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/ import isOriginAllowed from "./is-origin-allowed.js";
|
|
17
|
+
export function getOriginHeaders(reqOrigin, origin) {
|
|
18
|
+
const headers = new Headers();
|
|
19
|
+
if (origin === '*') {
|
|
20
|
+
// Allow any origin
|
|
21
|
+
headers.set('Access-Control-Allow-Origin', '*');
|
|
22
|
+
} else if (typeof origin === 'string') {
|
|
23
|
+
// Fixed origin
|
|
24
|
+
headers.set('Access-Control-Allow-Origin', origin);
|
|
25
|
+
headers.append('Vary', 'Origin');
|
|
26
|
+
} else {
|
|
27
|
+
const allowed = isOriginAllowed(reqOrigin != null ? reqOrigin : '', origin);
|
|
28
|
+
if (allowed && reqOrigin) {
|
|
29
|
+
headers.set('Access-Control-Allow-Origin', reqOrigin);
|
|
30
|
+
}
|
|
31
|
+
headers.append('Vary', 'Origin');
|
|
32
|
+
}
|
|
33
|
+
return headers;
|
|
34
|
+
}
|
|
35
|
+
export default getOriginHeaders;
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=get-origin-headers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/shared/util/http/src/lib/get-origin-headers.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2022 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport isOriginAllowed from './is-origin-allowed'\nimport type { StaticOrigin } from './types'\n\nexport function getOriginHeaders(\n reqOrigin: string | undefined,\n origin: StaticOrigin,\n) {\n const headers = new Headers()\n\n if (origin === '*') {\n // Allow any origin\n headers.set('Access-Control-Allow-Origin', '*')\n } else if (typeof origin === 'string') {\n // Fixed origin\n headers.set('Access-Control-Allow-Origin', origin)\n headers.append('Vary', 'Origin')\n } else {\n const allowed = isOriginAllowed((reqOrigin as string) ?? '', origin)\n\n if (allowed && reqOrigin) {\n headers.set('Access-Control-Allow-Origin', reqOrigin as string)\n }\n headers.append('Vary', 'Origin')\n }\n\n return headers\n}\n\nexport default getOriginHeaders\n"],"names":["isOriginAllowed","getOriginHeaders","reqOrigin","origin","headers","Headers","set","append","allowed"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GACD,OAAOA,qBAAqB,yBAAqB;AAGjD,OAAO,SAASC,iBACdC,SAA6B,EAC7BC,MAAoB;IAEpB,MAAMC,UAAU,IAAIC;IAEpB,IAAIF,WAAW,KAAK;QAClB,mBAAmB;QACnBC,QAAQE,GAAG,CAAC,+BAA+B;IAC7C,OAAO,IAAI,OAAOH,WAAW,UAAU;QACrC,eAAe;QACfC,QAAQE,GAAG,CAAC,+BAA+BH;QAC3CC,QAAQG,MAAM,CAAC,QAAQ;IACzB,OAAO;QACL,MAAMC,UAAUR,gBAAiBE,oBAAAA,YAAwB,IAAIC;QAE7D,IAAIK,WAAWN,WAAW;YACxBE,QAAQE,GAAG,CAAC,+BAA+BJ;QAC7C;QACAE,QAAQG,MAAM,CAAC,QAAQ;IACzB;IAEA,OAAOH;AACT;AAEA,eAAeH,iBAAgB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2023 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import type { IncomingHttpHeaders } from 'http';
|
|
18
|
+
import type { NextApiRequest } from 'next';
|
|
19
|
+
export declare function getRequestHeader<K extends KeyOf<IncomingHttpHeaders>>(request: Request | NextApiRequest, header: K | string): IncomingHttpHeaders[K] | null;
|
|
20
|
+
export default getRequestHeader;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2023 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/ export function getRequestHeader(request, header) {
|
|
17
|
+
return request instanceof Request ? request.headers.get(header) : request.headers[header];
|
|
18
|
+
}
|
|
19
|
+
export default getRequestHeader;
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=get-request-header.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/shared/util/http/src/lib/get-request-header.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2023 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { IncomingHttpHeaders } from 'http'\nimport type { NextApiRequest } from 'next'\n\nexport function getRequestHeader<K extends KeyOf<IncomingHttpHeaders>>(\n request: Request | NextApiRequest,\n header: K | string,\n): IncomingHttpHeaders[K] | null {\n return request instanceof Request\n ? request.headers.get(header as string)\n : request.headers[header]\n}\nexport default getRequestHeader\n"],"names":["getRequestHeader","request","header","Request","headers","get"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAKD,OAAO,SAASA,iBACdC,OAAiC,EACjCC,MAAkB;IAElB,OAAOD,mBAAmBE,UACtBF,QAAQG,OAAO,CAACC,GAAG,CAACH,UACpBD,QAAQG,OAAO,CAACF,OAAO;AAC7B;AACA,eAAeF,iBAAgB"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
export * from './types';
|
|
18
|
+
export * from './cookie-get-user-id-token';
|
|
19
|
+
export * from './cookie-set-user-id-token';
|
|
20
|
+
export * from './cors-build-response-handler';
|
|
21
|
+
export * from './cors-handle-response';
|
|
22
|
+
export * from './get-absolute-url';
|
|
23
|
+
export * from './get-allowed-headers';
|
|
24
|
+
export * from './get-origin-headers';
|
|
25
|
+
export * from './get-origin-headers-from-request';
|
|
26
|
+
export * from './get-request-header';
|
|
27
|
+
export * from './is-origin-allowed';
|