Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
new auth workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed May 22, 2015
1 parent cf95d9b commit 226de7b
Show file tree
Hide file tree
Showing 8 changed files with 995 additions and 966 deletions.
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = require('./lib/npm')
106 changes: 106 additions & 0 deletions lib/auth.js
@@ -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;

0 comments on commit 226de7b

Please sign in to comment.