bcrypt-schema

0.1.3 • Public • Published

bcrypt-schema

Build Status Dependency Status devDependency Status Coverage Status Codacy Badge

NPM package for adding encryption on schema fields. Can be used as a Mongoose plugin. Designed for use with Node.js and installable via npm install bcrypt-schema

Quick examples

  • As a mongoose plugin
var mongoose = require('mongoose'),
    encryption = require('bcrypt-schema').setEncryption

var userSchema = new mongoose.Schema({
    username: String,
    password: {
        type: String,
        select: false
    }
})

userSchema.plugin(encryption, 'password')
  • As a standalone module
// You can use the `verify(value, hash, next)` and `set(value, hashField, next)` methods
// on any object as schema instance methods
// Suppose we use mongoose again the example above would look like the following
var mongoose = require('mongoose'),
    encryption = require('bcrypt-schema')

var userSchema = new mongoose.Schema({
    username: String,
    password: {
        type: String,
        select: false
    }
})

userSchema.methods.verifyPassword = encryption.verify
userSchema.methods.setPassword = encryption.set

// ...
// Before saving a document
// Suppose we keep the plain password as a virual property
userSchema.virtual('plainPassword')
    .get(function() {
        return this._plainPassword
    })
    .set(function(password) {
        this._plainPassword = password
    })

userSchema.pre('save', function(callback) {

    // Password has not changed
    if (!this.isModified('plainPassword') && !this.isNew) {
        return callback()
    }

    //Password has changed or this is a new user
    this.setPassword(this.plainPassword, 'password', callback)
})

// ...
// Later having a user document
user.verifyPassword(password, user.password, function(err, isVerified) {
  // Handle verification result in this callback
}

Documentation

### verify(value, hash, next)

Compares the value of value with the given hash using bcrypt.compare(). The next parameter is a callback function.

Arguments

  • value - The plain value used in comparison.
  • hash - A hash to compare against.
  • next(err, isVeified) - A callback which is called after the comparison has finished.
### set(value, hashField, [saltIterations], next)

Hasheh the given value using bcrypt's genSalt and hash and assigns it to this[hashField].

Arguments

  • value - The plain value to hash.
  • hashField - The name of the property to be set.
  • saltIterations - Optional. A parameter passed to bcrypt.genSalt. Defaults to 10.
  • next(err) - A callback which is called after hashing has finished.
### setEncryption(schema, options)

Accepts a mongoose-like schema object and a set of options. Can be used as a mongoose plugin.

Arguments

  • schema - A mongoose-like schema object.
  • options- An object containing plugin options
    • field - The name of the field to be encrypted
    • verify - The name of the verify method attached as an instance method on the schema.methods
    • set - The name of the set method attached as an instance method on the schema.methods
    • saltIterations - A parameter passed to bcrypt.genSalt. Defaults to 10

Example

var mongoose = require('mongoose'),
    encryption = require('bcrypt-schema').setEncryption

// Define a schema
// ...

schema.plugin(encryption, {
        field: 'password',
        verify: 'customVerifyName',
        set: 'customSetName',
        saltIterations: 100
    })

After the plugin call you can use the customVerifyName and customSetName methods as instance methods.

setEncryption(schema, fieldName)

This is a short hand overload of the setEncryption method which auto-generates verify and set method names using the value of the fieldName parameter. Method names are generated adding the PascalCase version of the fieldName to the strings verify and set.

For instance, if fieldName equals 'password' the generated methods are named verifyPassword and setPassword.

Arguments

  • schema - A mongoose-like schema object
  • fieldName - The name of the field to be encrypted

Example

var mongoose = require('mongoose'),
    encryption = require('bcrypt-schema').setEncryption

// Define a schema
// ...

schema.plugin(encryption, 'password')

Package Sidebar

Install

npm i bcrypt-schema

Weekly Downloads

0

Version

0.1.3

License

MIT

Last publish

Collaborators

  • tenevdev