Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,21 @@ import { clearCachedConfig, encryptValue, getMergedConfig, getNcurcPath } from '

export default lazy(auth);

function errorExit(message) {
process.stderr.write(`${message}\n`);
process.exit(1);
}

function check(username, token, format = /^[A-Za-z0-9_]+$/) {
if (typeof username !== 'string') {
errorExit(`username must be a string, received ${typeof username}`);
throw new Error(`username must be a string, received ${typeof username}`);
}
if (!/^[a-zA-Z0-9-]+$/.test(username)) {
errorExit(
throw new Error(
'username may only contain alphanumeric characters or hyphens, ' +
`received ${username}`
);
}
if (typeof token !== 'string') {
errorExit(`token must be a string, received ${typeof token}`);
throw new Error(`token must be a string, received ${typeof token}`);
}
if (!format.test(token)) {
errorExit(`token is misformatted: ${token}`);
throw new Error(`token is misformatted: ${token}`);
}
}

Expand All @@ -51,7 +46,7 @@ async function tryCreateGitHubToken(githubAuth) {
noDeviceFlow: true
});
} catch (e) {
errorExit(`Could not get token: ${e.message}`);
throw new Error(`Could not get token: ${e.message}`, { cause: e });
}
return credentials;
}
Expand Down Expand Up @@ -93,7 +88,7 @@ async function auth(
get jenkins() {
const { username, jenkins_token } = getMergedConfig();
if (!username || !jenkins_token) {
errorExit(
throw new Error(
'Get your Jenkins API token in https://ci.nodejs.org/me/security ' +
'and run the following command to add it to your ncu config: ' +
'ncu-config --global set -x jenkins_token'
Expand Down
11 changes: 5 additions & 6 deletions test/unit/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,40 +62,39 @@ describe('auth', async function() {
await runAuthScript(
{},
[FIRST_TIME_MSG],
'Could not get token: Bad credentials\n', 'run-auth-error'
/Could not get token: Bad credentials\n/, 'run-auth-error'
);
});

it('does not accept a non-string username', async function() {
await runAuthScript(
{ HOME: { username: {}, token: '0123456789abcdef' } },
[],
'username must be a string, received object\n'
/username must be a string, received object\n/
);
});

it('does not accept a non-string token', async function() {
await runAuthScript(
{ HOME: { username: 'nyancat', token: 42 } },
[],
'token must be a string, received number\n'
/token must be a string, received number\n/
);
});

it('does not accept an invalid username format', async function() {
await runAuthScript(
{ HOME: { username: ' ^^^ ', token: '0123456789abcdef' } },
[],
'username may only contain alphanumeric characters or hyphens, ' +
'received ^^^ \n'
/username may only contain alphanumeric characters or hyphens, received {2}\^\^\^ \n/
);
});

it('does not accept an invalid token format', async function() {
await runAuthScript(
{ HOME: { username: 'nyancat', token: '@fhqwhgads' } },
[],
'token is misformatted: @fhqwhgads\n'
/token is misformatted: @fhqwhgads\n/
);
});

Expand Down
Loading