Skip to content

Commit 5fb0b09

Browse files
committed
feat: Support legacy authentication
1 parent 187b823 commit 5fb0b09

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,26 @@ Publish the package on the `npm` registry.
2020

2121
## Configuration
2222

23-
For each plugin, the `npm` authentication token has to be configured with the environment variable `NPM_TOKEN`.
23+
### Environment variables
2424

25-
All the plugins are based on `npm` and will use the configuration from `.npmrc`. Any parameter returned by `npm config list` will be used by each plugin.
25+
The `npm` authentication configuration is **required** and can be set via environment variables.
2626

27-
The registry and dist-tag can be configured in the `package.json` and will take precedence on the configuration in `.npmrc`:
27+
Both the [token](https://docs.npmjs.com/getting-started/working_with_tokens) and the legacy (`username`, `password` and `email`) authentication are supported. It is recommended to use the [token](https://docs.npmjs.com/getting-started/working_with_tokens) authentication. The legacy authentication is supported as the alternative npm registries [Artifactory](https://www.jfrog.com/open-source/#os-arti) and [npm-registry-couchapp](https://github.com/npm/npm-registry-couchapp) only supports that form of authentication at this point.
28+
29+
| Variable | Description
30+
| -------------- | ----------------------------------------------------------------------------------------------------------------------------- |
31+
| `NPM_TOKEN` | Npm token created via [npm token create](https://docs.npmjs.com/getting-started/working_with_tokens#how-to-create-new-tokens) |
32+
| `NPM_USERNAME` | Npm username created via [npm adduser](https://docs.npmjs.com/cli/adduser) or on [npmjs.com](https://www.npmjs.com) |
33+
| `NPM_PASSWORD` | Password of the npm user. |
34+
| `NPM_EMAIL` | Email address associated with the npm user |
35+
36+
Use either `NPM_TOKEN` for token authentication or `NPM_USERNAME`, `NPM_PASSWORD` and `NPM_EMAIL` for legacy authentication
37+
38+
### Options
39+
40+
The plugins are based on `npm` and will use the configuration from [`.npmrc`](https://docs.npmjs.com/files/npmrc). See [npm config](https://docs.npmjs.com/misc/config) for the option list.
41+
42+
The [`registry`](https://docs.npmjs.com/misc/registry) and [`dist-tag`](https://docs.npmjs.com/cli/dist-tag) can be configured in the `package.json` and will take precedence over the configuration in `.npmrc`:
2843
```json
2944
{
3045
"publishConfig": {
@@ -33,6 +48,9 @@ The registry and dist-tag can be configured in the `package.json` and will take
3348
}
3449
}
3550
```
51+
52+
### Usage
53+
3654
The plugins are used by default by [semantic-release](https://github.com/semantic-release/semantic-release) so no specific configuration is requiered to use them.
3755

3856
Each individual plugin can be disabled, replaced or used with other plugins in the `package.json`:

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const setLegacyToken = require('./lib/set-legacy-token');
12
const getPkg = require('./lib/get-pkg');
23
const verifyNpm = require('./lib/verify');
34
const publishNpm = require('./lib/publish');
@@ -6,12 +7,14 @@ const getLastReleaseNpm = require('./lib/get-last-release');
67
let verified;
78

89
async function verifyConditions(pluginConfig, {logger}) {
10+
setLegacyToken();
911
const pkg = await getPkg();
1012
await verifyNpm(pkg, logger);
1113
verified = true;
1214
}
1315

1416
async function getLastRelease(pluginConfig, {logger}) {
17+
setLegacyToken();
1518
// Reload package.json in case a previous external step updated it
1619
const pkg = await getPkg();
1720
if (!verified) {
@@ -22,6 +25,7 @@ async function getLastRelease(pluginConfig, {logger}) {
2225
}
2326

2427
async function publish(pluginConfig, {nextRelease: {version}, logger}) {
28+
setLegacyToken();
2529
// Reload package.json in case a previous external step updated it
2630
const pkg = await getPkg();
2731
if (!verified) {

lib/set-legacy-token.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = () => {
2+
// Set the environment variable `LEGACY_TOKEN` when user use the legacy auth, so it can be resolved by npm CLI
3+
if (process.env.NPM_USERNAME && process.env.NPM_PASSWORD && process.env.NPM_EMAIL) {
4+
process.env.LEGACY_TOKEN = Buffer.from(`${process.env.NPM_USERNAME}:${process.env.NPM_PASSWORD}`, 'utf8').toString(
5+
'base64'
6+
);
7+
}
8+
};

lib/set-npmrc-auth.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ module.exports = async (registry, logger) => {
1111
return;
1212
}
1313
if (NPM_USERNAME && NPM_PASSWORD && NPM_EMAIL) {
14-
// Using the old auth token format is not considered part of the public API
15-
// This might go away anytime (i.e. once we have a better testing strategy)
16-
await appendFile(
17-
'./.npmrc',
18-
`\n_auth = ${Buffer.from(`${NPM_USERNAME}:${NPM_PASSWORD}`, 'utf8').toString('base64')}\nemail = \${NPM_EMAIL}`
19-
);
14+
await appendFile('./.npmrc', `\n_auth = ${Buffer.from(`\${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`)}`);
2015
logger.log('Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to .npmrc.');
2116
} else if (NPM_TOKEN) {
2217
await appendFile('./.npmrc', `\n${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`);

test/integration.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ let processStderr;
1111
let processStdout;
1212

1313
test.before(async () => {
14-
// Start the local NPM registry
15-
await npmRegistry.start();
1614
// Disable npm logger during tests
1715
processStderr = stub(process.stderr, 'write');
1816
processStdout = stub(process.stdout, 'write');
17+
// Start the local NPM registry
18+
await npmRegistry.start();
1919
});
2020

2121
test.beforeEach(t => {

test/set-npmrc-auth.test.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ test.serial('Set auth with "NPM_USERNAME", "NPM_PASSWORD" and "NPM_EMAIL"', asyn
4545
await setNpmrcAuth('http://custom.registry.com', t.context.logger);
4646

4747
const npmrc = (await readFile('.npmrc')).toString();
48-
t.regex(
49-
npmrc,
50-
new RegExp(
51-
`_auth = ${Buffer.from('npm_username:npm_pasword', 'utf8').toString('base64')}\\W+email = \\\${NPM_EMAIL}`
52-
)
53-
);
48+
t.is(npmrc, `\n_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`);
5449

5550
t.true(t.context.log.calledWith('Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to .npmrc.'));
5651
});

0 commit comments

Comments
 (0)