Skip to content

Commit

Permalink
v0.2.1: DomainNameRule.validregex added. Attempt to work in Appcelera…
Browse files Browse the repository at this point in the history
…tor Titanium environments.
  • Loading branch information
nicjansma committed Jul 17, 2013
1 parent d0990be commit d4ae397
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 35 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# saltthepass.js

v0.2.0
v0.2.1

Copyright 2013 Nic Jansma

Expand Down Expand Up @@ -244,14 +244,18 @@ Creates a Domain Name Rule.
__Arguments__

* `data` - Can contain any of the following options:
* `domain` - Domain name (eg. `'foo.com'`)
* `aliases` - Array of additional domain names that will match (eg. `['a.foo.com', 'b.foo.com']`)
* `description` - Description
* `min` - Minimum number of characters in the password
* `max` - Maximum number of characters in the password
* `invalid` - An array of characters that are not allowed in the password (eg. `['!', '_']`)
* `required` - An array of characters where one of the characters needs to be in the password (eg. `['-', '!']`)
* `regex` - A regular expression of valid characters (eg. `'A-Z0-9'`) (*The regex is run case-insensitively*)
* `domain` - Domain name (eg. `'foo.com'`)
* `aliases` - Array of additional domain names that will match (eg. `['a.foo.com', 'b.foo.com']`)
* `description` - Description
* `min` - Minimum number of characters in the password
* `max` - Maximum number of characters in the password
* `invalid` - An array of characters that are not allowed in the password (eg. `['!', '_']`)
* `required` - An array of characters where one of the characters needs to be in the password (eg. `['-', '!']`)
* `validregex` - A simplified regular expression that would fit in a character set (eg. `A-Z0-9`, which would fit in
`[A-Z0-9]`). The regular expression is run case-insensitively. `validregex` should be used in preference over
`regex` (which can contain full regular expressions, not just a character sets), as `validregex` can easily
be inverted (eg `[^A-Z0-9]`) so passwords can be rewritten if they contain invalid characters.
* `regex` - A full regular expression that the password must match. The regex is run case-insensitively.

__Returns__

Expand Down Expand Up @@ -313,4 +317,5 @@ The tests can also be run in a web browser:
## Version History

* v0.1.0 - 2013-05-22: Initial version
* v0.2.0 - 2013-07-16: `DomainNameRule` and `standardizeDomain()` added.
* v0.2.0 - 2013-07-16: `DomainNameRule` and `standardizeDomain()` added.
* v0.2.1 - 2013-07-17: `DomainNameRule.validregex` added
4 changes: 2 additions & 2 deletions dist/saltthepass.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/saltthepass.withdeps.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "SaltThePass.com algorithm",
"main": "./src/saltthepass",
"author": "Nic Jansma",
"version": "0.2.0",
"version": "0.2.1",
"repository" : {
"type" : "git",
"url" : "http://github.com/nicjansma/saltthepass.js.git"
Expand All @@ -27,6 +27,6 @@
"grunt-contrib-uglify": ">0.0.0"
},
"dependencies": {
"crypto-js": ">= 3.1.2-1"
"crypto-js": ">= 3.1.2"
}
}
60 changes: 52 additions & 8 deletions src/domainnamerule.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
this.required = (typeof(data.required) !== 'undefined') ? data.required : '';

this.regex = data.regex;
this.validregex = data.validregex;
}

/**
Expand Down Expand Up @@ -111,6 +112,15 @@
return typeof(this.regex) !== 'undefined';
};

/**
* Determines if the domain has a regular expression of valid characters
*
* @returns {boolean}
*/
DomainNameRule.prototype.hasValidRegex = function() {
return typeof(this.validregex) !== 'undefined';
};

/**
* Determines if the specified domain matches this rule's domain
* or one of its aliases.
Expand Down Expand Up @@ -168,6 +178,10 @@
return false;
}

if (!this.isValidValidRegex(password)) {
return false;
}

return true;
};

Expand Down Expand Up @@ -250,22 +264,52 @@
return true;
};

/**
* Determines if the password passes this rule's valid characters regular expression
*
* @returns {boolean}
*/
DomainNameRule.prototype.isValidValidRegex = function(password) {
if (this.validregex) {
var reg = this.getValidRegEx();

if (!reg.test(password)) {
return false;
}
}

return true;
};

/**
* Gets this rule's regular expression
*
* @returns {RegExp} Regular expression
*/
DomainNameRule.prototype.getRegEx = function() {
if (!this.regex) {
return;
}

return new RegExp(this.regex, 'i');
};

/**
* Gets this rule's regular expression
*
* @param {boolean} reverse If set, return an inverse of the regular expression
*
* @returns {RegExp} Regular expression
*/
DomainNameRule.prototype.getRegEx = function(reverse) {
if (!this.regex) {
DomainNameRule.prototype.getValidRegEx = function(reverse) {
if (!this.validregex) {
return;
}

if (reverse) {
return new RegExp('[^' + this.regex + ']', 'gi');
return new RegExp('[^' + this.validregex + ']', 'gi');
} else {
return new RegExp('^[' + this.regex + ']+$', 'i');
return new RegExp('^[' + this.validregex + ']+$', 'i');
}
};

Expand Down Expand Up @@ -295,11 +339,11 @@
}
}

// if we have a regex, remove invalid characters from it
if (this.regex) {
var reg = this.getRegEx();
// if we have a valid characters regex, remove invalid characters from it
if (this.validregex) {
var reg = this.getValidRegEx();
if (!reg.test(newPass)) {
var nonRegEx = this.getRegEx(true);
var nonRegEx = this.getValidRegEx(true);
newPass = newPass.replace(nonRegEx, '');
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/saltthepass.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,25 @@
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.

var modulePath = '';
try {
require('crypto-js/md5');
} catch (e) {
// If we're in Appcelerator's environment, they don't support
// node_module loading, try to load it from our current path.
modulePath = '../node_modules/';
}

module.exports = factory(
require('./domainnamerule'),
require('./utils'),
require('crypto-js/md5'),
require('crypto-js/sha1'),
require('crypto-js/sha512'),
require('crypto-js/sha3'),
require('crypto-js/ripemd160'),
require('crypto-js/enc-base64'));
require(modulePath + 'crypto-js/md5'),
require(modulePath + 'crypto-js/sha1'),
require(modulePath + 'crypto-js/sha512'),
require(modulePath + 'crypto-js/sha3'),
require(modulePath + 'crypto-js/ripemd160'),
require(modulePath + 'crypto-js/enc-base64'));
} else {
// Browser globals (root is window)
root.SaltThePass = factory(
Expand Down
38 changes: 33 additions & 5 deletions test/domainnamerule.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@
test.done();
};

testcases['.isValid() - regex'] = function(test) {
testcases['.isValid() - validregex'] = function(test) {
var dnr = new DomainNameRule({
domain: 'foo.com',
regex: 'A-Z0-9'
validregex: 'A-Z0-9'
});

// true
Expand All @@ -143,7 +143,7 @@
test.ok(dnr.isValid('aa01asd12e12d'));
test.ok(dnr.isValid('aA1z091AZfa'));

// under minimum - false
// not valid per regex
test.ok(!dnr.isValid('a-'));
test.ok(!dnr.isValid('a.'));
test.ok(!dnr.isValid('a?'));
Expand All @@ -153,6 +153,34 @@
test.done();
};

testcases['.isValid() - regex'] = function(test) {
var dnr = new DomainNameRule({
domain: 'foo.com',
regex: '([A-Za-z])+([0-9])+|([0-9])+([A-Za-z])+'
});

// this regex enforces at least one letter and one number

// true
test.ok(dnr.isValid('1a'));
test.ok(dnr.isValid('a1'));
test.ok(dnr.isValid('1A'));
test.ok(dnr.isValid('A1'));
test.ok(dnr.isValid('1a1'));
test.ok(dnr.isValid('a1a'));
test.ok(dnr.isValid('aa01asd12e12d'));
test.ok(dnr.isValid('aA1z091AZfa-123123-21=312x-=321=3213-=s21=-3'));

// does not pass regex
test.ok(!dnr.isValid('aa'));
test.ok(!dnr.isValid('aA'));
test.ok(!dnr.isValid('AAA'));
test.ok(!dnr.isValid('1'));
test.ok(!dnr.isValid('11122'));

test.done();
};

//
// .rewrite()
//
Expand Down Expand Up @@ -204,10 +232,10 @@
test.done();
};

testcases['.rewrite() - regex'] = function(test) {
testcases['.rewrite() - validregex'] = function(test) {
var dnr = new DomainNameRule({
domain: 'foo.com',
regex: 'A-Z0-9'
validregex: 'A-Z0-9'
});

// no change - good
Expand Down

0 comments on commit d4ae397

Please sign in to comment.