-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tencho Tenev
committed
Feb 7, 2015
1 parent
2ca2a8e
commit 617be86
Showing
10 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# This file is for unifying the coding style for different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
end_of_line = crlf | ||
charset = utf-8 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_style = space | ||
indent_size = 4 | ||
|
||
[Makefile] | ||
indent_style = tab | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
// Environment | ||
"browser": true, | ||
"node": true, | ||
"mocha": true, | ||
"predef": [ | ||
"expect" | ||
], | ||
"devel": false, | ||
|
||
// Enforcing | ||
"bitwise": true, | ||
"curly": false, | ||
"eqeqeq": true, | ||
"latedef": true, | ||
"maxparams": 4, | ||
"maxdepth": 4, | ||
"maxstatements": 15, | ||
"maxcomplexity": 6, | ||
"noarg": true, | ||
"shadow": false, | ||
"undef": true, | ||
|
||
// Relaxing | ||
"asi": true, | ||
"boss": false, | ||
"debug": false, | ||
"eqnull": true, | ||
"evil": false, | ||
"expr": false, | ||
"loopfunc": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- '0.10' | ||
script: 'make test-cov' | ||
after_success: 'make coveralls' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Detect Windows OS | ||
ifdef SystemRoot | ||
# Fix slashes of path to work on Windows | ||
FixPath = $(subst /,\,$1) | ||
else | ||
FixPath = $1 | ||
endif | ||
|
||
NODE_MODULES = ./node_modules/.bin/ | ||
MOCHA = ./node_modules/mocha/bin/ | ||
TESTS_PATH = test/ | ||
COVERAGE_REPORT = ./coverage/lcov.info | ||
COVERALLS = ./node_modules/coveralls/bin/coveralls.js | ||
|
||
test: | ||
$(call FixPath, $(NODE_MODULES))mocha $(TEST_PATH) | ||
|
||
test-cov: istanbul | ||
|
||
istanbul: | ||
$(call FixPath, $(NODE_MODULES))istanbul cover$(call FixPath, $(MOCHA))_mocha $(TESTS_INTEGRATION) $(TESTS_UNIT) | ||
|
||
coveralls: | ||
cat $(COVERAGE_REPORT) | $(COVERALLS) | ||
|
||
.PHONY: test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
var bcrypt = require('bcrypt') | ||
|
||
function verify(value, hash, next) { | ||
bcrypt.compare(value, hash, function(err, res) { | ||
if (err) | ||
return next(err) | ||
return next(null, res) | ||
}) | ||
} | ||
|
||
function set(value, hashField, next) { | ||
var self = this | ||
|
||
bcrypt.genSalt(10, function(err, salt) { | ||
if (err) | ||
return next(err) | ||
bcrypt.hash(value, salt, function(err, hash) { | ||
if (err) | ||
return next(err) | ||
|
||
// Set new hash | ||
self[hashField] = hash | ||
return next() | ||
}) | ||
}) | ||
} | ||
|
||
function getVerifierForField(fieldName) { | ||
return function(value, next) { | ||
bcrypt.compare(value, this[fieldName], function(err, res) { | ||
if (err) | ||
return next(err) | ||
return next(null, res) | ||
}) | ||
} | ||
} | ||
|
||
function getSetterForField(fieldName) { | ||
return function(value, next) { | ||
var self = this | ||
|
||
bcrypt.genSalt(10, function(err, salt) { | ||
if (err) | ||
return next(err) | ||
bcrypt.hash(value, salt, function(err, hash) { | ||
if (err) | ||
return next(err) | ||
|
||
// Set new hash | ||
self[fieldName] = hash | ||
return next() | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
function setEncryption(schema, options) { | ||
var fieldName, capitlaizedFieldName, | ||
verifyMethodName, setMethodName | ||
|
||
if (typeof options === 'string') { | ||
capitlaizedFieldName = options.charAt(0).toUpperCase() + options.slice(1) | ||
|
||
options = { | ||
field: options, | ||
verify: 'verify' + capitlaizedFieldName, | ||
set: 'set' + capitlaizedFieldName | ||
} | ||
} | ||
schema.methods[options.verify] = getVerifierForField(options.field) | ||
schema.methods[options.set] = getSetterForField(options.field) | ||
} | ||
|
||
module.exports = { | ||
verify: verify, | ||
set: set, | ||
setEncryption: setEncryption, | ||
_getVerifierForField: getVerifierForField, | ||
_getSetterForField: getSetterForField | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "bcrypt-schema", | ||
"version": "0.1.0", | ||
"description": "Encrypt schema fields. Use it as a mongoose plugin or as a standalone module.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test", | ||
"test-cov": "make test-cov" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/tenevdev/bcrypt-schema.git" | ||
}, | ||
"keywords": [ | ||
"encrypt", | ||
"mongoose", | ||
"bcrypt", | ||
"password", | ||
"auth", | ||
"hash" | ||
], | ||
"author": "Tencho Tenev", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/tenevdev/bcrypt-schema/issues" | ||
}, | ||
"homepage": "https://github.com/tenevdev/bcrypt-schema", | ||
"dependencies": { | ||
"bcrypt": "^0.8.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.1.0", | ||
"istanbul": "^0.3.5", | ||
"coveralls": "^2.11.2", | ||
"sinon": "^1.12.2", | ||
"chai": "^1.10.0", | ||
"sinon-chai": "^2.6.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Configure chai for global usage | ||
var chai = require('chai'), | ||
sinonChai = require('sinon-chai') | ||
|
||
chai.use(sinonChai) | ||
chai.config.includeStack = true | ||
|
||
global.expect = chai.expect | ||
global.AssertionError = chai.AssertionError | ||
global.Assertion = chai.Assertion | ||
global.assert = chai.assert |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Configure test environment | ||
process.env.NODE_ENV = 'test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
var bcryptSchema = require('..'), | ||
bcrypt = require('bcrypt'), | ||
sinon = require('sinon'), | ||
compare, genSalt, hash | ||
|
||
describe('bcrypt-schema', function() { | ||
before('mock bcrypt', function() { | ||
compare = sinon.stub(bcrypt, 'compare').yields(null, true) | ||
genSalt = sinon.stub(bcrypt, 'genSalt').yields(null, 'test-salt') | ||
hash = sinon.stub(bcrypt, 'hash').yields(null, 'test-hash') | ||
}) | ||
after('restore bcrypt', function() { | ||
bcrypt.compare.restore() | ||
bcrypt.genSalt.restore() | ||
bcrypt.hash.restore() | ||
}) | ||
describe('verify(value, hash, next)', function() { | ||
beforeEach('call verify', function(done) { | ||
var value = 'test-value', | ||
hash = 'test-hash' | ||
bcryptSchema.verify(value, hash, function() { | ||
done() | ||
}) | ||
}) | ||
it('should compare a given value with a given hash', function() { | ||
expect(compare).calledOnce | ||
}) | ||
}) | ||
describe('set(value, hashField, next)', function() { | ||
beforeEach('call set', function(done) { | ||
var value = 'test-value', | ||
hashField = 'hashedValue' | ||
bcryptSchema.set(value, hashField, function() { | ||
done() | ||
}) | ||
}) | ||
it('should hash a value', function() { | ||
expect(genSalt).calledOnce | ||
expect(hash).calledOnce | ||
}) | ||
it('should set the field with the generate hash', function() { | ||
expect(bcryptSchema.hashedValue).to.equal('test-hash') | ||
}) | ||
}) | ||
describe('setEncyption(schema, options)', function() { | ||
var schema = { | ||
methods: {} | ||
} | ||
afterEach('reset schema object', function() { | ||
schema = { | ||
methods: {} | ||
} | ||
}) | ||
describe('when shorthand', function() { | ||
beforeEach('call setEncryption(schema, field)', function() { | ||
var field = 'hashedFieldName' | ||
bcryptSchema.setEncryption(schema, field) | ||
}) | ||
it('should set schema methods names', function() { | ||
expect(schema.methods).to.have.property('verifyHashedFieldName') | ||
expect(schema.methods).to.have.property('setHashedFieldName') | ||
}) | ||
it('should set schema methods', function() { | ||
expect(schema.methods.verifyHashedFieldName).to.be.a('function') | ||
expect(schema.methods.setHashedFieldName).to.be.a('function') | ||
}) | ||
}) | ||
describe('when full', function() { | ||
beforeEach('call setEncryption(schema, options)', function() { | ||
var options = { | ||
field: 'hashedFieldName', | ||
verify: 'customVerifyName', | ||
set: 'customSetName' | ||
} | ||
bcryptSchema.setEncryption(schema, options) | ||
}) | ||
it('should set schema method names', function() { | ||
expect(schema.methods).to.have.property('customVerifyName') | ||
expect(schema.methods).to.have.property('customSetName') | ||
}) | ||
it('should set schema methods', function() { | ||
expect(schema.methods.customSetName).to.be.a('function') | ||
expect(schema.methods.customVerifyName).to.be.a('function') | ||
}) | ||
}) | ||
}) | ||
describe('getVerifierForField(fieldName)', function() { | ||
var result | ||
beforeEach('call getVerifierForField', function() { | ||
result = bcryptSchema._getVerifierForField('test-field') | ||
}) | ||
it('should return a function', function() { | ||
expect(result).to.be.a('function') | ||
}) | ||
}) | ||
describe('getSetterForField(fieldName)', function() { | ||
var result | ||
beforeEach('call getSetterForField', function() { | ||
result = bcryptSchema._getSetterForField('test-field') | ||
}) | ||
it('should return a function', function() { | ||
expect(result).to.be.a('function') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-r test/helpers/env | ||
-r test/helpers/chai | ||
-R spec | ||
-u bdd |