This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf95d9b
commit 226de7b
Showing
8 changed files
with
995 additions
and
966 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/npm') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
var asp = require('rsvp').denodeify; | ||
var request = require('request'); | ||
|
||
// avoid storing passwords as plain text in config | ||
function encodeCredentials(username, password) { | ||
var raw = encodeURIComponent(username) + ':' + encodeURIComponent(password); | ||
return new Buffer(raw).toString('base64'); | ||
} | ||
exports.encodeCredentials = encodeCredentials; | ||
function decodeCredentials(str) { | ||
var auth = new Buffer(str, 'base64').toString('ascii').split(':'); | ||
return { | ||
username: decodeURIComponent(auth[0]), | ||
password: decodeURIComponent(auth[1]) | ||
}; | ||
} | ||
exports.decodeCredentials = decodeCredentials; | ||
|
||
// given options for request, add the auth header / option as appropriate | ||
function injectRequestOptions(requestOptions, auth) { | ||
if (!auth) | ||
return requestOptions; | ||
if (auth.username) | ||
requestOptions.auth = { | ||
user: auth.username, | ||
pass: auth.password | ||
}; | ||
else if (auth.token) { | ||
requestOptions.headers = requestOptions.headers || {}; | ||
requestOptions.headers.authorization = 'Bearer ' + auth.token; | ||
} | ||
return requestOptions; | ||
} | ||
exports.injectRequestOptions = injectRequestOptions; | ||
|
||
function configureCredentials(registry, _auth, ui) { | ||
_auth = _auth || {}; | ||
|
||
return Promise.resolve() | ||
.then(function() { | ||
var currentAuth = ''; | ||
if (_auth.token) | ||
currentAuth = 'Currently using an auth token. '; | ||
else if (_auth.username) | ||
currentAuth = 'Currently using a username and password. '; | ||
|
||
return ui.confirm(currentAuth + 'Configure token-based authentication?', !!_auth.token) | ||
}) | ||
.then(function(token) { | ||
if (token) { | ||
return ui.input('Enter your npm token', _auth.token) | ||
.then(function(token) { | ||
_auth.token = token; | ||
}); | ||
} | ||
else { | ||
return ui.input('Enter your npm username', _auth.username) | ||
.then(function(username) { | ||
_auth.username = username; | ||
return ui.input('Enter your npm password', null, true); | ||
}) | ||
.then(function(password) { | ||
_auth.password = password; | ||
}); | ||
} | ||
}) | ||
.then(function() { | ||
return ui.confirm('Would you like to test these credentials?', true); | ||
}) | ||
.then(function(test) { | ||
if (!test) | ||
return true; | ||
|
||
return Promise.resolve() | ||
.then(function() { | ||
return asp(request)(injectRequestOptions({ | ||
uri: registry | ||
}, _auth)); | ||
}) | ||
.then(function(res) { | ||
if (res.statusCode == 401) | ||
ui.log('warn', 'Provided npm credentials are not authorized.'); | ||
|
||
else if (res.statusCode != 200) | ||
ui.log('warn', 'Invalid response code, %' + res.statusCode + '%'); | ||
|
||
else { | ||
ui.log('ok', 'npm authentication is working successfully.'); | ||
return true; | ||
} | ||
}, function(err) { | ||
ui.log('err', err.stack || err); | ||
}); | ||
}) | ||
.then(function(authorized) { | ||
if (!authorized) | ||
return ui.confirm('Would you like to try new credentials?', true) | ||
.then(function(redo) { | ||
if (redo) | ||
return configureCredentials(registry, null, ui); | ||
}); | ||
else | ||
return _auth; | ||
}); | ||
} | ||
exports.configureCredentials = configureCredentials; |
Oops, something went wrong.