Skip to content

Commit

Permalink
Basic package
Browse files Browse the repository at this point in the history
  • Loading branch information
Tencho Tenev committed Feb 7, 2015
1 parent 2ca2a8e commit 617be86
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
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
32 changes: 32 additions & 0 deletions .jshintrc
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
}
5 changes: 5 additions & 0 deletions .travis.yml
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'
26 changes: 26 additions & 0 deletions Makefile
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
80 changes: 80 additions & 0 deletions index.js
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
}
39 changes: 39 additions & 0 deletions package.json
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"
}
}
11 changes: 11 additions & 0 deletions test/helpers/chai.js
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
2 changes: 2 additions & 0 deletions test/helpers/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Configure test environment
process.env.NODE_ENV = 'test'
105 changes: 105 additions & 0 deletions test/index.spec.js
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')
})
})
})
4 changes: 4 additions & 0 deletions test/mocha.opts
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

0 comments on commit 617be86

Please sign in to comment.