-
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
Araan Branco
committed
Mar 10, 2017
1 parent
6c63c60
commit 9e8c8b5
Showing
12 changed files
with
195 additions
and
3 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,20 @@ | ||
|
||
import Boom from 'boom'; | ||
import * as db from '../helpers/db'; | ||
|
||
export const login = async(request, reply) => { | ||
const { User } = db.connect('sofanerd').connection.models; | ||
let { username, password } = request.payload; | ||
let user = await User.findOne({ | ||
username | ||
}); | ||
|
||
if(!user || !await user.checkPassword(password)) { | ||
return reply(Boom.unauthorized('Credentials invalid!')); | ||
} | ||
|
||
reply({ | ||
token: user.getToken(), | ||
user | ||
}); | ||
}; |
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,15 @@ | ||
|
||
import Boom from 'boom'; | ||
import * as db from '../helpers/db'; | ||
|
||
export const create = async(request, reply) => { | ||
const { User } = db.connect('sofanerd').connection.models; | ||
|
||
let user = new User(request.payload); | ||
user = await user.save(); | ||
|
||
reply({ | ||
token: user.getToken(), | ||
user | ||
}); | ||
}; |
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,13 @@ | ||
|
||
import bcrypt from 'bcrypt'; | ||
import Bluebird from 'bluebird'; | ||
|
||
// Generate Hash with Bcrypt | ||
export const hash = (value) => bcrypt.hashSync(value, process.env.NODE_BCRYPT_SALT); | ||
|
||
// Compare hashs passwords | ||
export const compare = async (password, hash) => { | ||
console.log(password, hash); | ||
let compareAsync = Bluebird.promisify(bcrypt.compare); | ||
return await compareAsync(password, hash); | ||
}; |
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,14 @@ | ||
|
||
import jwt from 'jsonwebtoken'; | ||
|
||
export const generate = (payload, audience) => { | ||
let aud = audience ? audience : 'api'; | ||
return jwt.sign(payload, process.env.NODE_JWT_SALT, { | ||
algorithm: 'HS256', | ||
audience: aud | ||
}); | ||
}; | ||
|
||
export const decode = (token) => jwt.decode(token, { complete: true }); | ||
|
||
export const verify = (token, options) => jwt.verify(token, process.env.NODE_JWT_SALT, options); |
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
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
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
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 |
---|---|---|
@@ -1,9 +1,34 @@ | ||
|
||
import Debug from 'debug'; | ||
import Boom from 'boom'; | ||
import _ from 'lodash'; | ||
import schema from './schemas/user'; | ||
import * as jwt from '../helpers/jwt'; | ||
import { compare } from '../helpers/bcrypt'; | ||
|
||
const debug = Debug('sofanerd.models.user'); | ||
|
||
export const model = (connection) => { | ||
|
||
// Generate Token JWT for the user | ||
schema.methods.getToken = function({ aud = 'api' } = {}) { | ||
let data = _.pick(this.toObject(), '_id'); | ||
return jwt.generate(data, aud); | ||
}; | ||
|
||
// Check Password | ||
schema.methods.checkPassword = async function(password) { | ||
if(!this.password) { | ||
throw Boom.badRequest('User not have password'); | ||
} | ||
|
||
const comparePassword = await compare(password, this.password); | ||
if(!comparePassword) { | ||
throw Boom.badRequest('Password not match'); | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
return connection.model('User', schema); | ||
}; |
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
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,47 @@ | ||
|
||
import Debug from 'debug'; | ||
import Boom from 'boom'; | ||
import * as db from '../helpers/db'; | ||
|
||
const debug = Debug('sofanerd.authStrategy'); | ||
|
||
const register = (server, options, next) => { | ||
try { | ||
debug('Inicialize plugins'); | ||
|
||
server.auth.strategy('token-api', 'jwt', { | ||
key: process.env.NODE_JWT_SALT, | ||
async validateFunc(decoded, request, reply) { | ||
const { User } = db.connect('sofanerd').connection.models; | ||
|
||
let user = await User.findOne({ | ||
_id: decoded._id | ||
}); | ||
|
||
if(!user) { | ||
return reply(Boom.notFound('User not found'), false); | ||
} else { | ||
return reply(null, true, user); | ||
} | ||
}, | ||
verifyOptions: { | ||
algorithms: ['HS256'], | ||
audience: 'api' | ||
} | ||
}); | ||
|
||
debug('Plugins registered'); | ||
next(); | ||
} catch (err) { | ||
debug(err); | ||
throw err; | ||
} | ||
}; | ||
|
||
register.attributes = { | ||
name: 'auth' | ||
}; | ||
|
||
export default { | ||
register | ||
}; |
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,22 @@ | ||
|
||
import Debug from 'debug'; | ||
import Joi from 'joi'; | ||
import * as Controller from '../controllers/auth'; | ||
|
||
const debug = Debug('sofanerd.routes.auth'); | ||
|
||
export default [ | ||
{ | ||
method: 'POST', | ||
path: '/api/auth', | ||
config: { | ||
validate: { | ||
payload: { | ||
username: Joi.string().trim().lowercase().required(), | ||
password: Joi.string().trim().token().min(5).max(40).required() | ||
} | ||
} | ||
}, | ||
handler: Controller.login | ||
} | ||
]; |
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 @@ | ||
|
||
import Debug from 'debug'; | ||
import Joi from 'joi'; | ||
import * as Controller from '../controllers/user'; | ||
|
||
const debug = Debug('sofanerd.routes.auth'); | ||
|
||
const userCreatePayloadValidation = Joi.object({ | ||
username: Joi.string().trim().lowercase().min(3).max(20).required(), | ||
name: Joi.string().min(1).max(50).required(), | ||
email: Joi.string().email().min(1).max(40).required(), | ||
password: Joi.string().token().min(6).max(40).required() | ||
}); | ||
|
||
export default [ | ||
{ | ||
method: 'POST', | ||
path: '/api/user', | ||
config: { | ||
validate: { | ||
payload: userCreatePayloadValidation | ||
} | ||
}, | ||
handler: Controller.create | ||
} | ||
]; |