-
Notifications
You must be signed in to change notification settings - Fork 2
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
Finished user managment #92
Changes from 17 commits
1973385
a65f6e5
e9ad477
cbb7d35
32b7a62
dd1c1fe
24931ad
24f10df
490c2ba
62d7d76
46ca537
257db61
57b44f3
22dba52
bb17d39
72850e7
51ff839
de0e44d
7d79eb4
975cca0
19a68d3
e27f842
1b05925
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
const swaggerUi = require('swagger-ui-express'); | ||
const fs = require("fs") | ||
const YAML = require('yaml') | ||
|
||
const jwt = require('jsonwebtoken'); | ||
const app = express(); | ||
const port = 8000; | ||
|
||
|
@@ -47,7 +47,7 @@ | |
} | ||
}); | ||
|
||
app.get('/questions', async (req, res) => { | ||
app.get('/questions', verifyToken, async (req, res) => { | ||
try { | ||
|
||
// Forward the question request to the quetion service | ||
|
@@ -74,6 +74,36 @@ | |
}); | ||
|
||
|
||
app.get('/questions/:lang/:amount', verifyToken, async (req, res) => { | ||
try { | ||
const lang = req.params.lang.toString(); | ||
const amount = req.params.amount.toString(); | ||
// Forward the question request to the quetion service | ||
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang + '/' + amount); | ||
|
||
res.json(questionResponse.data); | ||
} catch (error) { | ||
|
||
res.status(error.response.status).json({ error: error.response.data.error }); | ||
} | ||
}); | ||
|
||
app.get('/questions/:lang/:amount/:type', verifyToken, async (req, res) => { | ||
try { | ||
const lang = req.params.lang.toString(); | ||
const amount = req.params.amount.toString(); | ||
const type = req.params.type.toString(); | ||
// Forward the question request to the quetion service | ||
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang + '/' + amount + '/' + type); | ||
|
||
res.json(questionResponse.data); | ||
} catch (error) { | ||
|
||
res.status(error.response.status).json({ error: error.response.data.error }); | ||
} | ||
}); | ||
|
||
|
||
app.get('/questions/:lang/:amount', async (req, res) => { | ||
try { | ||
const lang = req.params.lang.toString(); | ||
|
@@ -88,11 +118,11 @@ | |
} | ||
}); | ||
|
||
app.get('/questions/:lang', async (req, res) => { | ||
app.get('/questions/:lang', verifyToken, async (req, res) => { | ||
try { | ||
const lang = req.params.lang.toString(); | ||
// Forward the question request to the quetion service | ||
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang); | ||
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang.toString()); | ||
Check warning Code scanning / SonarCloud Server-side requests should not be vulnerable to forging attacks Medium
Change this code to not construct the URL from user-controlled data. See more on SonarCloud
|
||
|
||
res.json(questionResponse.data); | ||
} catch (error) { | ||
|
@@ -101,7 +131,8 @@ | |
} | ||
}); | ||
|
||
app.post('/record', async(req, res) => { | ||
app.post('/record', verifyToken, async(req, res) => { | ||
console.log("in") | ||
try { | ||
// Forward the record request to the record service | ||
const recordResponse = await axios.post(recordServiceUrl+'/record', req.body); | ||
|
@@ -111,7 +142,28 @@ | |
} | ||
}); | ||
|
||
app.get('/record/ranking/top10', async(req, res)=>{ | ||
app.get('/record/ranking/top10', verifyToken, async(req, res)=>{ | ||
try { | ||
// Forward the record request to the record service | ||
const recordResponse = await axios.get(recordServiceUrl + '/record/ranking/top10'); | ||
res.json(recordResponse.data); | ||
} catch (error) { | ||
res.send(error); | ||
} | ||
}); | ||
|
||
app.get('/record/ranking/:user', verifyToken, async(req, res)=>{ | ||
try { | ||
const user = req.params.user; | ||
// Forward the record request to the record service | ||
const recordResponse = await axios.get(recordServiceUrl + '/record/ranking/' + user); | ||
Check warning Code scanning / SonarCloud Server-side requests should not be vulnerable to forging attacks Medium
Change this code to not construct the URL from user-controlled data. See more on SonarCloud
|
||
res.json(recordResponse.data); | ||
} catch (error) { | ||
res.send(error); | ||
} | ||
}); | ||
|
||
app.get('/record/ranking/top10', verifyToken, async(req, res)=>{ | ||
try { | ||
// Forward the record request to the record service | ||
const recordResponse = await axios.get(recordServiceUrl + '/record/ranking/top10'); | ||
|
@@ -132,7 +184,7 @@ | |
} | ||
}); | ||
|
||
app.get('/record/:user', async(req, res)=>{ | ||
app.get('/record/:user', verifyToken, async(req, res)=>{ | ||
try { | ||
const user = req.params.user; | ||
// Forward the record request to the record service | ||
|
@@ -159,4 +211,23 @@ | |
console.log(`Gateway Service listening at http://localhost:${port}`); | ||
}); | ||
|
||
function verifyToken(req, res, next) { | ||
// Get the token from the request headers | ||
const token = req.headers['token'] || req.body.token || req.query.token; | ||
|
||
// Verify if the token is valid | ||
jwt.verify(token, (process.env.JWT_KEY??'my-key'), (err, decoded) => { | ||
if (err) { | ||
// Token is not valid | ||
res.status(403).json({authorized: false, | ||
error: 'Invalid token or outdated'}); | ||
} else { | ||
// Token is valid | ||
req.decodedToken = decoded; | ||
// Call next() to proceed to the next middleware or route handler | ||
next(); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = server |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Check warning
Code scanning / SonarCloud
Server-side requests should not be vulnerable to forging attacks Medium