Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/get protected data #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions auth.middleware.js
Original file line number Diff line number Diff line change
@@ -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')
}
}
Expand Down
2 changes: 1 addition & 1 deletion sampleDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ const sampleProducts = [
'Zulu',
]

module.exports = {sampleProducts}
module.exports = { sampleProducts }
2 changes: 1 addition & 1 deletion server.example1.js
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
6 changes: 3 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
@@ -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())
Expand Down Expand Up @@ -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)
})
Expand All @@ -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}`)
}
Expand Down