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

Commit f912c52

Browse files
authored
Merge pull request #412 from cloudant/json-session-request
Used application/json for session POST
2 parents 28fb98b + 245cbb0 commit f912c52

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# UNRELEASED
22
- [FIXED] Expose BasePlugin.
3+
- [FIXED] Prevent double encoding of credentials passed in URL user information
4+
when using the `cookieauth` plugin.
5+
- [IMPROVED] Documented the characters that are required to be encoded in URL
6+
user information.
37
- [IMPROVED] Documented the legacy compatibility behaviour that always adds the
48
`cookieauth` plugin when using the initialization callback functionality.
59

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,20 @@ var Cloudant = require('@cloudant/cloudant');
156156
var cloudant = Cloudant("http://MYUSERNAME:MYPASSWORD@localhost:5984");
157157
~~~
158158

159-
**Note**: If you pass in a `username`, `password`, and `url` that contains
159+
**Note**: It is preferred to pass credentials using the `account`/`username` and
160+
`password` configuration options rather than as part of the URL. However, if you
161+
choose to pass credentials in the user information subcomponent of the URL then
162+
they must be [percent encoded](https://tools.ietf.org/html/rfc3986#section-3.2.1).
163+
Specifically within either the username or passowrd the characters `: / ? # [ ] @ %`
164+
_MUST_ be precent-encoded, other characters _MAY_ be percent encoded.
165+
For example for the username `user123` and password `colon:at@321`:
166+
```
167+
https://user123:colon%3aat%40321@localhost:5984
168+
```
169+
Credentials must not be percent encoded when passing them via other configuration
170+
options besides `url`.
171+
172+
If you pass in `username` and `password` options and a `url` that contains
160173
credentials, the `username` and `password` will supercede the credentials within
161174
the `url`. For example, `myusername` and `mypassword` will be used in the code
162175
below during authentication:

lib/tokens/CookieTokenManager.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class CookieTokenManager extends TokenManager {
2929
this._client({
3030
url: this._sessionUrl,
3131
method: 'POST',
32-
form: {
32+
json: true,
33+
body: {
3334
name: this._username,
3435
password: this._password
3536
},

plugins/cookieauth.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ class CookiePlugin extends BasePlugin {
4949
client,
5050
this._jar,
5151
u.format(sessionUrl, {auth: false}),
52-
sessionUrl.username,
53-
sessionUrl.password
52+
// Extract creds from URL and decode
53+
decodeURIComponent(sessionUrl.username),
54+
decodeURIComponent(sessionUrl.password)
5455
);
5556

5657
if (cfg.autoRenew) {

test/plugins/cookieauth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ const nock = require('../nock.js');
2121
const uuidv4 = require('uuid/v4'); // random
2222

2323
const ME = process.env.cloudant_username || 'nodejs';
24-
const PASSWORD = process.env.cloudant_password || 'sjedon';
24+
const PASSWORD = process.env.cloudant_password || 'sjedon!@#"£$%^&*()';
2525
const SERVER = process.env.SERVER_URL || `https://${ME}.cloudant.com`;
2626
const SERVER_NO_PROTOCOL = SERVER.replace(/^https?:\/\//, '');
27-
const SERVER_WITH_CREDS = `https://${ME}:${PASSWORD}@${SERVER_NO_PROTOCOL}`;
27+
const SERVER_WITH_CREDS = `https://${ME}:${encodeURIComponent(PASSWORD)}@${SERVER_NO_PROTOCOL}`;
2828
const DBNAME = `/nodejs-cloudant-${uuidv4()}`;
2929
const COOKIEAUTH_PLUGIN = [ { cookieauth: { autoRenew: false } } ];
3030

0 commit comments

Comments
 (0)