Skip to content

Commit

Permalink
Merge pull request #14 from vitaly-t/secure
Browse files Browse the repository at this point in the history
adding password hash
  • Loading branch information
vitaly-t committed Mar 26, 2019
2 parents 9f642c1 + 68974a7 commit c6f0656
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ as it is not used very often, and even when it is used, it is usually either in
is typically not needed. But if you do need `$` encoded everywhere, pass in `{encodeDollar: true}`.

* `plusForSpace` - Boolean (false) - encodes spaces as `+` instead of `%20`.


* `passwordHash` - Boolean (false)|String - replaces each password symbol with `#`, to generate a secure connection string.
But when set to a non-empty string, its first symbol is used instead. Symbol `#` will prevent parsing such string again,
on purpose. But if you want it parsed again, use a different symbol, for example - `passwordHash: '*'`

### `static parseHost(host) => {name, port, type} | null`

When using an external list of default hosts, you may need to parse them independently, using this method,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "connection-string",
"version": "2.3.0",
"version": "2.4.0",
"description": "Advanced URL Connection String parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare enum HostType {
interface IEncodingOptions {
encodeDollar?: boolean
plusForSpace?: boolean
passwordHash?: boolean | string
}

interface IHost {
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,15 @@
s += '@';
} else {
if (this.password) {
s += ':' + encode(this.password, options) + '@';
s += ':';
var h = options.passwordHash;
if (h) {
var code = (typeof h === 'string' && h[0]) || '#';
s += new Array(this.password.length + 1).join(code);
} else {
s += encode(this.password, options);
}
s += '@';
}
}
if (Array.isArray(this.hosts)) {
Expand Down
2 changes: 1 addition & 1 deletion test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ a.params = {
second: 'hello!'
};

let cs = a.toString({encodeDollar: true, plusForSpace: true});
let cs = a.toString({encodeDollar: true, plusForSpace: true, passwordHash: '*'});
a.setDefaults({
hosts: [
{name: '[::]', port: 123, type: HostType.IPv4}
Expand Down
4 changes: 4 additions & 0 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ describe('toString', () => {
a.params = {};
expect(a.toString()).toBe('');
});
it('must secure passwords', () => {
expect(parse(':abc@').toString({passwordHash: true})).toBe(':###@');
expect(parse(':abc@').toString({passwordHash: '123'})).toBe(':111@');
});
});

describe('host.toString()', () => {
Expand Down

0 comments on commit c6f0656

Please sign in to comment.