Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit d30f7f4

Browse files
authored
Merge pull request #324 from cloudant/323-auth-with-user-pass-url
Handle cases when username, password, and url exist during client creation
2 parents f85bfc1 + 82e5fe6 commit d30f7f4

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# UNRELEASED
2+
- [FIXED] Case where `username` and `password` options were not used if a `url` was supplied.
23

34
# 2.3.0 (2018-06-08)
45
- [FIXED] Removed addition of `statusCode` to response objects returned by promises.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ var Cloudant = require('@cloudant/cloudant')
148148
var cloudant = Cloudant("http://MYUSERNAME:MYPASSWORD@localhost:5984");
149149
~~~
150150

151+
**Note**: If you pass in a `username`, `password`, and `url` that contains credentials, the `username` and `password` will supercede the credentials within the `url`. For example, `myusername` and `mypassword` will be used in the code below during authentication:
152+
~~~ js
153+
var Cloudant = require('@cloudant/cloudant')
154+
var cloudant = Cloudant({username:'myusername', password:'mypassword', url:'http://user:pass@localhost:5984'});
155+
~~~
156+
151157
#### 2. Using account credentials:
152158

153159
##### 2.1. Connecting to Cloudant

lib/reconfigure.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ module.exports = function(config) {
2929
config = JSON.parse(JSON.stringify(config)); // clone
3030
var outUrl;
3131
var outCreds = {};
32+
var options;
33+
var username;
34+
var password;
3235
// if a full URL is passed in
3336
if (config.url) {
3437
// parse the URL
@@ -58,6 +61,14 @@ module.exports = function(config) {
5861

5962
// reconstruct the URL
6063
config.url = url.format(parsed);
64+
} else {
65+
options = getOptions(config);
66+
username = options.username;
67+
password = options.password;
68+
if (username && password) {
69+
config.url = parsed.protocol + '//' + encodeURIComponent(username) + ':' +
70+
encodeURIComponent(password) + '@' + parsed.host;
71+
}
6172
}
6273
outUrl = config.url;
6374
} else if (config.vcapServices) {
@@ -100,10 +111,9 @@ module.exports = function(config) {
100111
config.account.match &&
101112
config.account.match(/([^.]+)\.cloudant\.com/);
102113
if (match) { config.account = match[1]; }
103-
104-
var options = getOptions(config);
105-
var username = options.username;
106-
var password = options.password;
114+
options = getOptions(config);
115+
username = options.username;
116+
password = options.password;
107117

108118
// Configure for Cloudant, either authenticated or anonymous.
109119
if (config.account && password) {

test/legacy/reconfigure.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ describe('Reconfigure', function() {
6262
done();
6363
});
6464

65+
// Issue cloudant/nodejs-cloudant#323
66+
it('allows a username, a password, and url to be passed in', function(done) {
67+
var credentials = { username: 'myusername', password: 'mypassword', url: 'http://localhost:8081' };
68+
var outCreds = reconfigure(credentials);
69+
outCreds.outUrl.should.be.a.String;
70+
outCreds.outUrl.should.equal('http://myusername:mypassword@localhost:8081');
71+
done();
72+
});
73+
74+
it('allows a username and password to supersede credentials supplied in a url', function(done) {
75+
var credentials = { username: 'myusername', password: 'mypassword', url: 'http://user:pass@localhost:8081' };
76+
var outCreds = reconfigure(credentials);
77+
outCreds.outUrl.should.be.a.String;
78+
outCreds.outUrl.should.equal('http://myusername:mypassword@localhost:8081');
79+
done();
80+
});
81+
6582
it('allows a key and an full domain and a password to be passed in', function(done) {
6683
var credentials = { account: 'myaccount.cloudant.com', password: 'mypassword', key: 'mykey'};
6784
var outCreds = reconfigure(credentials);

0 commit comments

Comments
 (0)