diff --git a/auth.middleware.js b/auth.middleware.js index 35dec2f..4b50714 100644 --- a/auth.middleware.js +++ b/auth.middleware.js @@ -1,35 +1,34 @@ -const jwt = require('express-jwt'); -const jwksRsa = require('jwks-rsa'); - -const authServerUrl = 'http://localhost:5000' +const jwt = require('express-jwt') +const jwksRsa = require('jwks-rsa') +const { authorityUrl } = require('./config') const checkJwt = jwt({ secret: jwksRsa.expressJwtSecret({ cache: false, rateLimit: true, jwksRequestsPerMinute: 5, - jwksUri: `http://172.16.31.64:5000/jwks`, + jwksUri: `${authorityUrl}/jwks`, }), audience: 'foo', - issuer: `${authServerUrl}`, + issuer: authorityUrl, algorithms: ['RS256'], }); const readProducts = (req, res, next) => { const user = req.user; - if(user['read:products'] === 'true') { + if (user['read:products'] === 'true') { return next() - } else{ + } else { return res.status(403).send('u dont have read access') } } const editProducts = (req, res, next) => { const user = req.user; - if(user['edit:products'] === 'true') { + if (user['edit:products'] === 'true') { return next() - } else{ + } else { return res.status(403).send('u dont have edit access') } } diff --git a/sampleDate.js b/sampleDate.js index bfd28b1..a9b813d 100644 --- a/sampleDate.js +++ b/sampleDate.js @@ -27,4 +27,4 @@ const sampleProducts = [ 'Zulu', ] -module.exports = {sampleProducts} \ No newline at end of file +module.exports = { sampleProducts } \ No newline at end of file diff --git a/server.example1.js b/server.example1.js index 60a6c69..b51629f 100644 --- a/server.example1.js +++ b/server.example1.js @@ -1,7 +1,7 @@ const express = require('express') const bodyParser = require('body-parser') const { promisify } = require('util') -const { auth, strategies, requiredScopes } = require('express-oauth2-bearer'); +const { auth, strategies, requiredScopes } = require('express-oauth2-bearer') const app = express() app.use(bodyParser.json()); diff --git a/server.js b/server.js index 60c72a2..fdba046 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,9 @@ const express = require('express') const bodyParser = require('body-parser') const { promisify } = require('util') -const { checkJwt, readProducts, editProducts } = require('./auth.middleware'); +const { checkJwt, readProducts, editProducts } = require('./auth.middleware') const { sampleProducts } = require('./sampleDate') +const { port } = require('./config') const app = express() app.use(bodyParser.json()) @@ -34,7 +35,7 @@ app.get('/sample', checkJwt, readProducts, (req, res) => { const limit = Number(req.query.limit) || 5 const offset = Number(req.query.offset) || 0 - const data = sampleProducts.slice(offset, offset+limit) + const data = sampleProducts.slice(offset, offset + limit) return res.status(200).send(data) }) @@ -51,7 +52,6 @@ app.post('/sample', checkJwt, editProducts, (req, res) => { }) const startServer = async () => { - const port = process.env.SERVER_PORT || 3000 await promisify(app.listen).bind(app)(port) console.log(`Listening on port ${port}`) }