Skip to content

Commit

Permalink
fix: hash the password
Browse files Browse the repository at this point in the history
Signed-off-by: jannyHou <[email protected]>
  • Loading branch information
jannyHou committed Jan 30, 2019
1 parent 1d705b7 commit 6d993b0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
16 changes: 14 additions & 2 deletions src/utils/user.authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {promisify} from 'util';
import * as isemail from 'isemail';
import {HttpErrors} from '@loopback/rest';
import {UserProfile} from '@loopback/authentication';
import {compare} from 'bcryptjs';
const compareAsync = promisify(compare);
const jwt = require('jsonwebtoken');
const signAsync = promisify(jwt.sign);
const verifyAsync = promisify(jwt.verify);
Expand All @@ -18,11 +20,21 @@ export async function getAccessTokenForUser(
userRepository: UserRepository,
credentials: Credentials,
): Promise<string> {
console.log('getAccessTokenForUser', credentials.password);
const foundUser = await userRepository.findOne({
where: {email: credentials.email, password: credentials.password},
where: {email: credentials.email},
});
if (!foundUser) {
throw new HttpErrors.Unauthorized('Wrong credentials!');
throw new HttpErrors.Unauthorized(
`User with email ${credentials.email} not found`,
);
}
const passwordMatched = await compareAsync(
credentials.password,
foundUser.password,
);
if (!passwordMatched) {
throw new HttpErrors.Unauthorized('The credential is not correct!');
}

const currentUser = _.pick(toJSON(foundUser), ['id', 'email', 'firstName']);
Expand Down
27 changes: 17 additions & 10 deletions test/acceptance/user.controller.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import {setupApplication} from './helper';
import {createRecommendationServer} from '../../recommender';
import {Server} from 'http';
import * as _ from 'lodash';
import {promisify} from 'util';
import {hash} from 'bcryptjs';
import {getAccessTokenForUser} from '../../src/utils/user.authentication';
const recommendations = require('../../recommender/recommendations.json');
const hashAsync = promisify(hash);

describe('UserController', () => {
let app: ShoppingApplication;
Expand Down Expand Up @@ -135,36 +138,40 @@ describe('UserController', () => {
});

describe('authentication functions', () => {
// TODO: fix storing the plain password in the following issue:
// https://github.com/strongloop/loopback-next/issues/1996
let plainPassword: string;

before('create new user', async () => {
plainPassword = user.password;
// Salt + Hash Password
user.password = await hashAsync(user.password, 10);
});

it('login returns a valid token', async () => {
const newUser = await userRepo.create(user);
await client
.post('/users/login')
.send({email: newUser.email, password: newUser.password})
.send({email: newUser.email, password: plainPassword})
.expect(200)
.then(getToken);
.then(verifyToken);

function getToken(res: Response) {
function verifyToken(res: Response) {
const token = res.body.token;
expect(token).to.not.be.empty();
}
});

it('login returns an error when invalid credentials are used', async () => {
const newUser = await userRepo.create(user);
newUser.password = 'wrong password';
await client
.post('/users/login')
.send({email: newUser.email, password: newUser.password})
.send({email: user.email, password: 'wrongpassword'})
.expect(401);
});

it('/me returns the current user', async () => {
const newUser = await userRepo.create(user);
const token = await getAccessTokenForUser(userRepo, {
email: newUser.email,
password: newUser.password,
password: plainPassword,
});

newUser.id = newUser.id.toString();
Expand All @@ -180,7 +187,7 @@ describe('UserController', () => {
const newUser = await userRepo.create(user);
await getAccessTokenForUser(userRepo, {
email: newUser.email,
password: newUser.password,
password: plainPassword,
});

await client.get('/users/me').expect(401);
Expand Down
4 changes: 4 additions & 0 deletions test/unit/utils.authentication.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
import {UserRepository, OrderRepository} from '../../src/repositories';
import {User} from '../../src/models';
import * as _ from 'lodash';
import {promisify} from 'util';
import {hash} from 'bcryptjs';
const hashAsync = promisify(hash);
const SECRET = 'secretforjwt';

describe('authentication', () => {
Expand All @@ -27,6 +30,7 @@ describe('authentication', () => {
let newUser: User;

before('create user', async () => {
user.password = await hashAsync(user.password, 10);
newUser = await userRepo.create(user);
});

Expand Down

0 comments on commit 6d993b0

Please sign in to comment.