Skip to content

Commit f6dfc1d

Browse files
author
Aaron Aichlmayr
committed
Added a hook for the authentication header
1 parent a36db5f commit f6dfc1d

6 files changed

Lines changed: 34 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: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function get(path: string, {configuration, headers, ident, authType
5656
if (typeof registry !== `string`)
5757
throw new Error(`Assertion failed: The registry should be a string`);
5858

59-
const auth = getAuthenticationHeader(registry, {authType, configuration, ident});
59+
const auth = await getAuthenticationHeader(registry, {authType, configuration, ident});
6060
if (auth)
6161
headers = {...headers, authorization: auth};
6262

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

86-
const auth = getAuthenticationHeader(registry, {authType, configuration, ident});
86+
const auth = await getAuthenticationHeader(registry, {authType, configuration, ident});
8787
if (auth)
8888
headers = {...headers, authorization: auth};
8989

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

120-
const auth = getAuthenticationHeader(registry, {authType, configuration, ident});
120+
const auth = await getAuthenticationHeader(registry, {authType, configuration, ident});
121121
if (auth)
122122
headers = {...headers, authorization: auth};
123123

@@ -151,7 +151,7 @@ export async function del(path: string, {attemptedAs, configuration, headers, id
151151
if (typeof registry !== `string`)
152152
throw new Error(`Assertion failed: The registry should be a string`);
153153

154-
const auth = getAuthenticationHeader(registry, {authType, configuration, ident});
154+
const auth = await getAuthenticationHeader(registry, {authType, configuration, ident});
155155
if (auth)
156156
headers = {...headers, authorization: auth};
157157

@@ -178,13 +178,21 @@ export async function del(path: string, {attemptedAs, configuration, headers, id
178178
}
179179
}
180180

181-
function getAuthenticationHeader(registry: string, {authType = AuthType.CONFIGURATION, configuration, ident}: {authType?: AuthType, configuration: Configuration, ident: RegistryOptions['ident']}) {
181+
async function getAuthenticationHeader(registry: string, {authType = AuthType.CONFIGURATION, configuration, ident}: {authType?: AuthType, configuration: Configuration, ident: RegistryOptions['ident']}) {
182182
const effectiveConfiguration = npmConfigUtils.getAuthConfiguration(registry, {configuration, ident});
183183
const mustAuthenticate = shouldAuthenticate(effectiveConfiguration, authType);
184184

185185
if (!mustAuthenticate)
186186
return null;
187187

188+
const header = await configuration.reduceHook(hooks => {
189+
return hooks.getNpmAuthenticationHeader;
190+
}, undefined, registry, {configuration, ident});
191+
192+
if (header)
193+
return header;
194+
195+
188196
if (effectiveConfiguration.get(`npmAuthToken`))
189197
return `Bearer ${effectiveConfiguration.get(`npmAuthToken`)}`;
190198
if (effectiveConfiguration.get(`npmAuthIdent`))

0 commit comments

Comments
 (0)