Skip to content

Commit

Permalink
Merge b859d7b into 9e84f5e
Browse files Browse the repository at this point in the history
  • Loading branch information
gmemmy committed Aug 22, 2019
2 parents 9e84f5e + b859d7b commit 4c32989
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ DB_CONFIG_HOST=Your database's host
DB_CONFIG_PORT=Your database's port channel
DATABASE_URL=The production database's URI
DB_CONFIG_DEV=Your development database
JWT_SECRET=Your JWT secret key
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ The mockups for the app design can be viewed [here](https://www.pivotaltracker.c
### REST API Docs
The Api documentation is done using swagger. View [Barefoot Nomad API Documentation](https://)

### JWT Authentication
The API endpoints are being authenticated using [JWT(Json Web Token)](https://jwt.io/).

### Required Features

```
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import swaggerUI from 'swagger-ui-express';
import dotenv from 'dotenv';
import doc from '../doc.json';

dotenv.config();

const port = process.env.PORT || 3000;
const app = express();
app.use(bodyParser.json());
Expand All @@ -18,7 +21,7 @@ app.get('/', (req, res) => res.status(200).send({ message: 'Welcome to Barefoot
app.all('*', (req, res) => res.send({ message: 'route not found' }));

app.listen(port, () => {
console.info(`Server is up and listening on port ${port}`);
console.info(`Server is up and listening on port ${port}`);
});

export default app;
16 changes: 16 additions & 0 deletions src/middlewares/authorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import verifyToken from '../utils/Authentication';

const checkToken = (req, res, next) => {
const token = req.headers['x-access-token'] || req.query.token || req.headers.authorization;
if (token) {
const response = verifyToken(token);
if (response.success) return next();
}
return res.status(401).json({
status: 401,
success: false,
error: 'Unauthorized! no token provided.'
});
};

export default checkToken;
24 changes: 24 additions & 0 deletions src/utils/authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import jwt from 'jsonwebtoken';

export const shuffleToken = (token) => token.split('').reverse().join('');

export const generateToken = (payload, expiresIn = '24h') => {
jwt.sign(payload, process.env.JWT_SECRET, { expiresIn });
return shuffleToken(generateToken);
};

export const verifyToken = (token) => {
const initialToken = shuffleToken(token);
jwt.verify(initialToken, process.env.JWT_SECRET, (err, decoded) => {
if (err) {
return {
success: false,
error: 'Token is invalid.'
};
}
return {
success: true,
decodedPayload: decoded
};
});
};
37 changes: 21 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1705,9 +1705,9 @@ ejs@^2.6.1:
integrity sha512-PcW2a0tyTuPHz3tWyYqtK6r1fZ3gp+3Sop8Ph+ZYN81Ob5rwmbHEzaqs10N3BEsaGTkh/ooniXK+WwszGlc2+Q==

electron-to-chromium@^1.3.191:
version "1.3.230"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.230.tgz#2d0618cb6f724391d5fd0926dde84d6c67cbcda9"
integrity sha512-r0RljY5DZi9RX4v8mjHxJkDWnQe+nsrkGlHtrDF2uvZcvAkw+iglvlQi1794gZhwRtJoDOomMJlDHL2LfXSCZA==
version "1.3.236"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.236.tgz#c7158cbd4d17a3670ac7c9b44da44d752aa193a9"
integrity sha512-LWOvuJ80pLO3FtFqTcGuXB0dxdMtzSCkRmbXdY5mHUvXRQGor3sTVmyfU70aD2yF5i+fbHz52ncWr5T3xUYHlA==

emoji-regex@^7.0.1:
version "7.0.3"
Expand Down Expand Up @@ -1828,9 +1828,9 @@ eslint-config-airbnb-base@^14.0.0:
object.entries "^1.1.0"

eslint-config-prettier@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-6.0.0.tgz#f429a53bde9fc7660e6353910fd996d6284d3c25"
integrity sha512-vDrcCFE3+2ixNT5H83g28bO/uYAwibJxerXPj+E7op4qzBCsAV36QfvdAyVOoNxKAH2Os/e01T/2x++V0LPukA==
version "6.1.0"
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-6.1.0.tgz#e6f678ba367fbd1273998d5510f76f004e9dce7b"
integrity sha512-k9fny9sPjIBQ2ftFTesJV21Rg4R/7a7t7LCtZVrYQiHEp8Nnuk3EGaDmsKSAnsPj0BYcgB2zxzHa2NTkIxcOLg==
dependencies:
get-stdin "^6.0.0"

Expand Down Expand Up @@ -4038,9 +4038,9 @@ p-limit@^1.1.0:
p-try "^1.0.0"

p-limit@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.2.0.tgz#417c9941e6027a9abcba5092dd2904e255b5fbc2"
integrity sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==
version "2.2.1"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.2.1.tgz#aa07a788cc3151c939b5131f63570f0dd2009537"
integrity sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==
dependencies:
p-try "^2.0.0"

Expand Down Expand Up @@ -4401,11 +4401,16 @@ punycode@^2.1.0:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

qs@6.7.0, qs@^6.5.1:
qs@6.7.0:
version "6.7.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==

qs@^6.5.1:
version "6.8.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.8.0.tgz#87b763f0d37ca54200334cd57bb2ef8f68a1d081"
integrity sha512-tPSkj8y92PfZVbinY1n84i1Qdx75lZjMQYx9WZhnkofyxzw2r7Ho39G3/aEvSUdebxpnnM4LZJCtvE/Aq3+s9w==

qs@~6.5.2:
version "6.5.2"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
Expand Down Expand Up @@ -4824,9 +4829,9 @@ sequelize-pool@^2.3.0:
integrity sha512-Ibz08vnXvkZ8LJTiUOxRcj1Ckdn7qafNZ2t59jYHMX1VIebTAOYefWdRYFt6z6+hy52WGthAHAoLc9hvk3onqA==

sequelize@^5.14.0:
version "5.15.0"
resolved "https://registry.yarnpkg.com/sequelize/-/sequelize-5.15.0.tgz#99aeb863709b5c68881ef60eb566801fae149fb1"
integrity sha512-w0jS4dcwLblIo284Yotb96noYrn6PUO92NtCE3VTDZxUgknfJZXfsbpsz+GqhIoE6gW1b8fsppwvk8fQTIPrlA==
version "5.15.1"
resolved "https://registry.yarnpkg.com/sequelize/-/sequelize-5.15.1.tgz#f130ded17e74395ae7f5e265277c99577e895afb"
integrity sha512-DCzzJYvJLMKnyf8G3at2A+yM9M2fSQmTmuOYIpCWM8Gjqx3XfgNTd1NkuyPWFoi1/d1AXQsN2VDPXkPczida8A==
dependencies:
bluebird "^3.5.0"
cls-bluebird "^2.1.0"
Expand Down Expand Up @@ -5539,9 +5544,9 @@ utils-merge@1.0.1:
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=

uuid@^3.2.1, uuid@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131"
integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==
version "3.3.3"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.3.tgz#4568f0216e78760ee1dbf3a4d2cf53e224112866"
integrity sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ==

v8flags@^3.1.1:
version "3.1.3"
Expand Down

0 comments on commit 4c32989

Please sign in to comment.