Skip to content

Commit

Permalink
fix(login): use bcrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Aug 7, 2017
1 parent 7accca3 commit 9d69ea7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -3,4 +3,8 @@
1. `git clone git@github.com:RisingStack/nodehero-authentication.git`
2. `cd nodehero-authentication`
3. `npm install`
4. `npm start`
4. `REDIS_STORE_URI=redis://localhost REDIS_STORE_SECRET=my-strong-secret npm start`

## Pre requirements

- Running [Redis](https://redis.io/) database
31 changes: 24 additions & 7 deletions app/authentication/init.js
@@ -1,11 +1,18 @@
const passport = require('passport')
const bcrypt = require('bcrypt')
const LocalStrategy = require('passport-local').Strategy

const authenticationMiddleware = require('./middleware')

// Generate Password
const saltRounds = 10
const myPlaintextPassword = 'my-password'
const salt = bcrypt.genSaltSync(saltRounds)
const passwordHash = bcrypt.hashSync(myPlaintextPassword, salt)

const user = {
username: 'test-user',
password: 'test-password',
passwordHash,
id: 1
}

Expand All @@ -26,18 +33,28 @@ passport.deserializeUser(function (username, cb) {

function initPassport () {
passport.use(new LocalStrategy(
function(username, password, done) {
findUser(username, function (err, user) {
(username, password, done) => {
findUser(username, (err, user) => {
if (err) {
return done(err)
}

// User not found
if (!user) {
console.log('User not found')
return done(null, false)
}
if (password !== user.password ) {
return done(null, false)
}
return done(null, user)

// Always use hashed passwords and fixed time comparison
bcrypt.compare(password, user.passwordHash, (err, isValid) => {
if (err) {
return done(err)
}
if (!isValid) {
return done(null, false)
}
return done(null, user)
})
})
}
))
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -19,6 +19,7 @@
},
"homepage": "https://github.com/RisingStack/nodehero-authentication#readme",
"dependencies": {
"bcrypt": "1.0.2",
"body-parser": "1.15.1",
"connect-redis": "3.0.2",
"express": "4.13.4",
Expand Down

1 comment on commit 9d69ea7

@RoelRoel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Please sign in to comment.