Skip to content

Commit b6068ac

Browse files
authored
Added a hook for the authentication header (#2664)
1 parent 0d4f86c commit b6068ac

6 files changed

Lines changed: 35 additions & 5 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ package.tgz
3737
!/packages/yarnpkg-pnp/lib/hook.js
3838

3939
/vscode-case-study
40+
41+
.idea

.yarn/versions/c743d681.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
releases:
2+
"@yarnpkg/plugin-npm": minor
3+
4+
declined:
5+
- "@yarnpkg/plugin-compat"
6+
- "@yarnpkg/plugin-npm-cli"
7+
- "@yarnpkg/cli"
8+
- "@yarnpkg/core"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {Configuration, Ident} from "@yarnpkg/core";
2+
3+
export type Hooks = {
4+
getNpmAuthenticationHeader?: (currentHeader: string | undefined, registry: string, {
5+
configuration,
6+
ident,
7+
}: { configuration: Configuration, ident?: Ident }) => Promise<string | undefined>
8+
};

packages/plugin-npm/sources/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Plugin, SettingsType, miscUtils} from '@yarnpkg/core';
22

3+
import {Hooks} from './Hooks';
34
import {NpmHttpFetcher} from './NpmHttpFetcher';
45
import {NpmRemapResolver} from './NpmRemapResolver';
56
import {NpmSemverFetcher} from './NpmSemverFetcher';
@@ -12,6 +13,7 @@ import * as npmPublishUtils from './npmPublishUtils';
1213
export {npmConfigUtils};
1314
export {npmHttpUtils};
1415
export {npmPublishUtils};
16+
export type {Hooks};
1517

1618
const authSettings = {
1719
npmAlwaysAuth: {

packages/plugin-npm/sources/npmConfigUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export enum RegistryType {
55
PUBLISH_REGISTRY = `npmPublishRegistry`,
66
}
77

8+
89
export interface MapLike {
910
get(key: string): any;
1011
}

packages/plugin-npm/sources/npmHttpUtils.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {MessageName, ReportError} from '@yarnpkg/core';
33
import {prompt} from 'enquirer';
44
import {URL} from 'url';
55

6+
import {Hooks} from "./Hooks";
67
import * as npmConfigUtils from './npmConfigUtils';
78
import {MapLike} from './npmConfigUtils';
89

@@ -60,7 +61,7 @@ export async function get(path: string, {configuration, headers, ident, authType
6061
if (typeof registry !== `string`)
6162
throw new Error(`Assertion failed: The registry should be a string`);
6263

63-
const auth = getAuthenticationHeader(registry, {authType, configuration, ident});
64+
const auth = await getAuthenticationHeader(registry, {authType, configuration, ident});
6465
if (auth)
6566
headers = {...headers, authorization: auth};
6667

@@ -87,7 +88,7 @@ export async function post(path: string, body: httpUtils.Body, {attemptedAs, con
8788
if (typeof registry !== `string`)
8889
throw new Error(`Assertion failed: The registry should be a string`);
8990

90-
const auth = getAuthenticationHeader(registry, {authType, configuration, ident});
91+
const auth = await getAuthenticationHeader(registry, {authType, configuration, ident});
9192
if (auth)
9293
headers = {...headers, authorization: auth};
9394

@@ -121,7 +122,7 @@ export async function put(path: string, body: httpUtils.Body, {attemptedAs, conf
121122
if (typeof registry !== `string`)
122123
throw new Error(`Assertion failed: The registry should be a string`);
123124

124-
const auth = getAuthenticationHeader(registry, {authType, configuration, ident});
125+
const auth = await getAuthenticationHeader(registry, {authType, configuration, ident});
125126
if (auth)
126127
headers = {...headers, authorization: auth};
127128

@@ -155,7 +156,7 @@ export async function del(path: string, {attemptedAs, configuration, headers, id
155156
if (typeof registry !== `string`)
156157
throw new Error(`Assertion failed: The registry should be a string`);
157158

158-
const auth = getAuthenticationHeader(registry, {authType, configuration, ident});
159+
const auth = await getAuthenticationHeader(registry, {authType, configuration, ident});
159160
if (auth)
160161
headers = {...headers, authorization: auth};
161162

@@ -182,13 +183,21 @@ export async function del(path: string, {attemptedAs, configuration, headers, id
182183
}
183184
}
184185

185-
function getAuthenticationHeader(registry: string, {authType = AuthType.CONFIGURATION, configuration, ident}: {authType?: AuthType, configuration: Configuration, ident: RegistryOptions['ident']}) {
186+
async function getAuthenticationHeader(registry: string, {authType = AuthType.CONFIGURATION, configuration, ident}: {authType?: AuthType, configuration: Configuration, ident: RegistryOptions['ident']}) {
186187
const effectiveConfiguration = npmConfigUtils.getAuthConfiguration(registry, {configuration, ident});
187188
const mustAuthenticate = shouldAuthenticate(effectiveConfiguration, authType);
188189

189190
if (!mustAuthenticate)
190191
return null;
191192

193+
const header = await configuration.reduceHook((hooks: Hooks) => {
194+
return hooks.getNpmAuthenticationHeader;
195+
}, undefined, registry, {configuration, ident});
196+
197+
if (header)
198+
return header;
199+
200+
192201
if (effectiveConfiguration.get(`npmAuthToken`))
193202
return `Bearer ${effectiveConfiguration.get(`npmAuthToken`)}`;
194203
if (effectiveConfiguration.get(`npmAuthIdent`))

0 commit comments

Comments
 (0)