-
Notifications
You must be signed in to change notification settings - Fork 0
O auth #14
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
base: main
Are you sure you want to change the base?
O auth #14
Conversation
User login User profile creation
router.post('/request', async (req,res,next) => { | ||
res.header('Access-Control-Allow-Origin', 'http://localhost:3000'); | ||
res.header('Referrer-Policy', 'no-referrer-when-downgrade'); | ||
|
||
const redirectUrl='http://127.0.0.1:4000/oauth'; | ||
|
||
const oAuth2Client = new OAuth2Client(process.env.CLIENT_ID, process.env.CLIENT_SECRET,redirectUrl); | ||
|
||
const authorizedUrl = oAuth2Client.generateAuthUrl({ | ||
access_type:'offline', | ||
scope:'https://www.googleapis.com/auth/userinfo.profile openid', | ||
prompt: 'consent' | ||
}); | ||
|
||
res.json({url:authorizedUrl}); | ||
}); |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 11 months ago
To fix the problem, we will introduce rate limiting to the route handler using the express-rate-limit
package. This will ensure that the number of requests to the /request
endpoint is limited, preventing potential DoS attacks.
- Install the
express-rate-limit
package if it is not already installed. - Import the
express-rate-limit
package in thebackend/routes/User/authReq.js
file. - Set up a rate limiter with appropriate configuration (e.g., maximum of 100 requests per 15 minutes).
- Apply the rate limiter to the
/request
endpoint.
-
Copy modified line R6 -
Copy modified lines R8-R13
@@ -5,4 +5,10 @@ | ||
const {OAuth2Client} = require('google-auth-library'); | ||
const rateLimit = require('express-rate-limit'); | ||
|
||
router.post('/request', async (req,res,next) => { | ||
const limiter = rateLimit({ | ||
windowMs: 15 * 60 * 1000, // 15 minutes | ||
max: 100 // limit each IP to 100 requests per windowMs | ||
}); | ||
|
||
router.post('/request', limiter, async (req,res,next) => { | ||
res.header('Access-Control-Allow-Origin', 'http://localhost:3000'); |
router.get('/', async function (req, res) { | ||
const code = req.query.code; | ||
try { | ||
const redirectUrl = 'http://127.0.0.1:4000/oauth'; | ||
const oAuth2Client = new OAuth2Client(process.env.CLIENT_ID, process.env.CLIENT_SECRET,redirectUrl); | ||
|
||
const { tokens } = await oAuth2Client.getToken(code); | ||
console.log('TOKENS: ',tokens); | ||
|
||
oAuth2Client.setCredentials(tokens); | ||
|
||
const userData = await getUserData(tokens.access_token); | ||
|
||
let user = await User.findOne({ email: "dilhariedissanayake@gmail.com" }); | ||
if (!user) { | ||
const newUser = await createUserFromGoogle(userData); | ||
res.redirect(`http://localhost:3000/oauth-callback?user=${encodeURIComponent(JSON.stringify(newUser))}`); | ||
} else { | ||
const token = createToken(user._id); | ||
const userWithToken = { ...user.toObject(), token }; | ||
res.redirect(`http://localhost:3000/oauth-callback?user=${encodeURIComponent(JSON.stringify(userWithToken))}`); | ||
} | ||
} catch(err) { | ||
console.error("Error with signing in with Google:", err); | ||
res.status(500).send("Error with Google authentication"); | ||
} | ||
}); |
Check failure
Code scanning / CodeQL
Missing rate limiting High
a database access
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 11 months ago
To fix the problem, we will introduce rate limiting to the Express application using the express-rate-limit
package. This will involve:
- Installing the
express-rate-limit
package. - Setting up a rate limiter with a reasonable configuration.
- Applying the rate limiter to the specific route handler that performs the database access.
-
Copy modified lines R9-R14 -
Copy modified line R45
@@ -8,2 +8,8 @@ | ||
const User = require("../../models/User_model"); | ||
const rateLimit = require('express-rate-limit'); | ||
|
||
const limiter = rateLimit({ | ||
windowMs: 15 * 60 * 1000, // 15 minutes | ||
max: 100, // limit each IP to 100 requests per windowMs | ||
}); | ||
|
||
@@ -38,3 +44,3 @@ | ||
|
||
router.get('/', async function (req, res) { | ||
router.get('/', limiter, async function (req, res) { | ||
const code = req.query.code; |
const ROLE = "user"; | ||
localStorage.setItem("role", ROLE); | ||
|
||
localStorage.setItem('user', JSON.stringify(userData)); |
Check failure
Code scanning / CodeQL
Clear text storage of sensitive information High
an access to password
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 11 months ago
To fix the problem, we need to ensure that the sensitive information, specifically the password, is encrypted before being stored in localStorage
. We can use the crypto
module from Node.js to encrypt the password. The encrypted password will then be stored instead of the plain text password.
- Import the
crypto
module. - Create an encryption function that uses a secure algorithm (e.g.,
aes-256-ctr
) to encrypt the password. - Modify the code to encrypt the password before storing the user data in
localStorage
.
-
Copy modified lines R3-R8 -
Copy modified line R31
@@ -2,2 +2,8 @@ | ||
import { useNavigate } from 'react-router-dom'; | ||
import crypto from 'crypto'; | ||
|
||
const encrypt = (text, password) => { | ||
const cipher = crypto.createCipher('aes-256-ctr', password); | ||
return cipher.update(text, 'utf8', 'hex') + cipher.final('hex'); | ||
}; | ||
|
||
@@ -24,3 +30,3 @@ | ||
gender: user.gender, | ||
password: user.password, | ||
password: encrypt(user.password, 'your-encryption-key'), | ||
__v: user.__v |
No description provided.