@pnpm/network.auth-header 0.0.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2016 Rico Sta. Cruz and other contributors
4
+ Copyright (c) 2016-2022 Zoltan Kochan and other contributors
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # @pnpm/network.auth-header
2
+
3
+ > Gets the authorization header for the given URI
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@pnpm/network.auth-header.svg)](https://www.npmjs.com/package/@pnpm/network.auth-header)
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ pnpm add @pnpm/network.auth-header
11
+ ```
12
+
13
+ ## License
14
+
15
+ MIT
@@ -0,0 +1,4 @@
1
+ export declare function getAuthHeadersFromConfig({ allSettings, userSettings }: {
2
+ allSettings: Record<string, string>;
3
+ userSettings: Record<string, string>;
4
+ }): {};
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getAuthHeadersFromConfig = void 0;
7
+ const error_1 = __importDefault(require("@pnpm/error"));
8
+ const child_process_1 = require("child_process");
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const path_1 = __importDefault(require("path"));
11
+ const nerf_dart_1 = __importDefault(require("nerf-dart"));
12
+ function getAuthHeadersFromConfig({ allSettings, userSettings }) {
13
+ const authHeaderValueByURI = {};
14
+ for (const [key, value] of Object.entries(allSettings)) {
15
+ const [uri, authType] = splitKey(key);
16
+ switch (authType) {
17
+ case '_authToken': {
18
+ authHeaderValueByURI[uri] = `Bearer ${value}`;
19
+ continue;
20
+ }
21
+ case '_auth': {
22
+ authHeaderValueByURI[uri] = `Basic ${value}`;
23
+ continue;
24
+ }
25
+ case 'username': {
26
+ if (`${uri}:_password` in allSettings) {
27
+ const password = Buffer.from(allSettings[`${uri}:_password`], 'base64').toString('utf8');
28
+ authHeaderValueByURI[uri] = `Basic ${Buffer.from(`${value}:${password}`).toString('base64')}`;
29
+ }
30
+ }
31
+ }
32
+ }
33
+ for (const [key, value] of Object.entries(userSettings)) {
34
+ const [uri, authType] = splitKey(key);
35
+ if (authType === 'tokenHelper') {
36
+ authHeaderValueByURI[uri] = loadToken(value, key);
37
+ }
38
+ }
39
+ const registry = allSettings['registry'] ? (0, nerf_dart_1.default)(allSettings['registry']) : '//registry.npmjs.org/';
40
+ if (userSettings['tokenHelper']) {
41
+ authHeaderValueByURI[registry] = loadToken(userSettings['tokenHelper'], 'tokenHelper');
42
+ }
43
+ else if (allSettings['_authToken']) {
44
+ authHeaderValueByURI[registry] = `Bearer ${allSettings['_authToken']}`;
45
+ }
46
+ else if (allSettings['_auth']) {
47
+ authHeaderValueByURI[registry] = `Basic ${allSettings['_auth']}`;
48
+ }
49
+ else if (allSettings['_password'] && allSettings['username']) {
50
+ authHeaderValueByURI[registry] = `Basic ${Buffer.from(`${allSettings['username']}:${allSettings['_password']}`).toString('base64')}`;
51
+ }
52
+ return authHeaderValueByURI;
53
+ }
54
+ exports.getAuthHeadersFromConfig = getAuthHeadersFromConfig;
55
+ function splitKey(key) {
56
+ const index = key.lastIndexOf(':');
57
+ if (index === -1) {
58
+ return [key, ''];
59
+ }
60
+ return [key.slice(0, index), key.slice(index + 1)];
61
+ }
62
+ function loadToken(helperPath, settingName) {
63
+ var _a, _b;
64
+ if (!path_1.default.isAbsolute(helperPath) || !fs_1.default.existsSync(helperPath)) {
65
+ throw new error_1.default('BAD_TOKEN_HELPER_PATH', `${settingName} must be an absolute path, without arguments`);
66
+ }
67
+ const spawnResult = (0, child_process_1.spawnSync)(helperPath, { shell: true });
68
+ if (spawnResult.status !== 0) {
69
+ throw new error_1.default('TOKEN_HELPER_ERROR_STATUS', `Error running "${helperPath}" as a token helper, configured as ${settingName}. Exit code ${(_b = (_a = spawnResult.status) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ''}`);
70
+ }
71
+ return spawnResult.stdout.toString('utf8').trimEnd();
72
+ }
73
+ //# sourceMappingURL=getAuthHeadersFromConfig.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getAuthHeadersFromConfig.js","sourceRoot":"","sources":["../src/getAuthHeadersFromConfig.ts"],"names":[],"mappings":";;;;;;AAAA,wDAAmC;AACnC,iDAAyC;AACzC,4CAAmB;AACnB,gDAAuB;AACvB,0DAAgC;AAEhC,SAAgB,wBAAwB,CACtC,EAAE,WAAW,EAAE,YAAY,EAG1B;IAED,MAAM,oBAAoB,GAAG,EAAE,CAAA;IAC/B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE;QACtD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,CAAC,CAAA;KAC3G;IAED,MAAM,WAAW,GAAG,IAAA,yBAAS,EAAC,UAAU,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;IAE1D,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE;QAC5B,MAAM,IAAI,eAAS,CAAC,2BAA2B,EAAE,kBAAkB,UAAU,sCAAsC,WAAW,eAAe,MAAA,MAAA,WAAW,CAAC,MAAM,0CAAE,QAAQ,EAAE,mCAAI,EAAE,EAAE,CAAC,CAAA;KACrL;IACD,OAAO,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAA;AACtD,CAAC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export declare function createGetAuthHeaderByURI(opts: {
2
+ allSettings: Record<string, string>;
3
+ userSettings?: Record<string, string>;
4
+ }): (uri: string) => string | undefined;
package/lib/index.js ADDED
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createGetAuthHeaderByURI = void 0;
7
+ const nerf_dart_1 = __importDefault(require("nerf-dart"));
8
+ const getAuthHeadersFromConfig_1 = require("./getAuthHeadersFromConfig");
9
+ function createGetAuthHeaderByURI(opts) {
10
+ var _a;
11
+ const authHeaders = (0, getAuthHeadersFromConfig_1.getAuthHeadersFromConfig)({
12
+ allSettings: opts.allSettings,
13
+ userSettings: (_a = opts.userSettings) !== null && _a !== void 0 ? _a : {},
14
+ });
15
+ if (Object.keys(authHeaders).length === 0)
16
+ return () => undefined;
17
+ return getAuthHeaderByURI.bind(null, authHeaders, getMaxParts(Object.keys(authHeaders)));
18
+ }
19
+ exports.createGetAuthHeaderByURI = createGetAuthHeaderByURI;
20
+ function getMaxParts(uris) {
21
+ return uris.reduce((max, uri) => {
22
+ const parts = uri.split('/').length;
23
+ return parts > max ? parts : max;
24
+ }, 0);
25
+ }
26
+ function getAuthHeaderByURI(authHeaders, maxParts, uri) {
27
+ const nerfed = (0, nerf_dart_1.default)(uri);
28
+ const parts = nerfed.split('/');
29
+ for (let i = Math.min(parts.length, maxParts) - 1; i >= 3; i--) {
30
+ const key = `${parts.slice(0, i).join('/')}/`; // eslint-disable-line
31
+ if (authHeaders[key])
32
+ return authHeaders[key];
33
+ }
34
+ return undefined;
35
+ }
36
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,0DAAgC;AAChC,yEAAqE;AAErE,SAAgB,wBAAwB,CACtC,IAGC;;IAED,MAAM,WAAW,GAAG,IAAA,mDAAwB,EAAC;QAC3C,WAAW,EAAE,IAAI,CAAC,WAAW;QAC7B,YAAY,EAAE,MAAA,IAAI,CAAC,YAAY,mCAAI,EAAE;KACtC,CAAC,CAAA;IACF,IAAI,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,GAAG,EAAE,CAAC,SAAS,CAAA;IACjE,OAAO,kBAAkB,CAAC,IAAI,CAAC,IAAI,EAAE,WAAW,EAAE,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAA;AAC1F,CAAC;AAZD,4DAYC;AAED,SAAS,WAAW,CAAE,IAAc;IAClC,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC9B,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAA;QACnC,OAAO,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAA;IAClC,CAAC,EAAE,CAAC,CAAC,CAAA;AACP,CAAC;AAED,SAAS,kBAAkB,CAAE,WAAmC,EAAE,QAAgB,EAAE,GAAW;IAC7F,MAAM,MAAM,GAAG,IAAA,mBAAQ,EAAC,GAAG,CAAC,CAAA;IAC5B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC/B,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;QAC9D,MAAM,GAAG,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAA,CAAC,sBAAsB;QACpE,IAAI,WAAW,CAAC,GAAG,CAAC;YAAE,OAAO,WAAW,CAAC,GAAG,CAAC,CAAA;KAC9C;IACD,OAAO,SAAS,CAAA;AAClB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@pnpm/network.auth-header",
3
+ "version": "0.0.0",
4
+ "description": "Gets the authorization header for the given URI",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "files": [
8
+ "lib",
9
+ "!*.map"
10
+ ],
11
+ "engines": {
12
+ "node": ">=12.17"
13
+ },
14
+ "repository": "https://github.com/pnpm/pnpm/blob/main/packages/network.auth-header",
15
+ "keywords": [
16
+ "pnpm6",
17
+ "pnpm",
18
+ "auth"
19
+ ],
20
+ "license": "MIT",
21
+ "bugs": {
22
+ "url": "https://github.com/pnpm/pnpm/issues"
23
+ },
24
+ "homepage": "https://github.com/pnpm/pnpm/blob/main/packages/network.auth-header#readme",
25
+ "devDependencies": {
26
+ "@pnpm/network.auth-header": "0.0.0",
27
+ "safe-buffer": "5.2.1"
28
+ },
29
+ "dependencies": {
30
+ "@pnpm/error": "2.1.0",
31
+ "nerf-dart": "1.0.0"
32
+ },
33
+ "funding": "https://opencollective.com/pnpm",
34
+ "exports": {
35
+ ".": "./lib/index.js"
36
+ },
37
+ "scripts": {
38
+ "lint": "eslint src/**/*.ts test/**/*.ts",
39
+ "_test": "jest",
40
+ "test": "pnpm run compile && pnpm run _test",
41
+ "compile": "tsc --build && pnpm run lint --fix"
42
+ }
43
+ }