-
Notifications
You must be signed in to change notification settings - Fork 24
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
added 2 checks for the function. 1. exp check for iaAuthenticatedFact… #224
Changes from all commits
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,6 @@ | ||||||||||
import {getUserFactory} from './getUser'; | ||||||||||
import { removeTrailingSlash } from '../utils/removeTrailingSlash'; | ||||||||||
import { getUserFactory } from './getUser'; | ||||||||||
import jwtDecode from 'jwt-decode'; | ||||||||||
|
||||||||||
/** | ||||||||||
* | ||||||||||
|
@@ -8,5 +10,38 @@ import {getUserFactory} from './getUser'; | |||||||||
*/ | ||||||||||
export const isAuthenticatedFactory = (req, res) => async () => { | ||||||||||
const user = await getUserFactory(req, res)(); | ||||||||||
return Boolean(user); | ||||||||||
}; | ||||||||||
|
||||||||||
if (!user) { | ||||||||||
return false; | ||||||||||
} | ||||||||||
|
||||||||||
const token = req.headers.authorization?.split(' ')[1]; | ||||||||||
if (!token) { | ||||||||||
return false; | ||||||||||
} | ||||||||||
|
||||||||||
try { | ||||||||||
const decoded = jwtDecode(token); | ||||||||||
if (!decoded) { | ||||||||||
return false; | ||||||||||
} | ||||||||||
|
||||||||||
const { exp, iss } = decoded; | ||||||||||
const currentTime = Math.floor(Date.now() / 1000); | ||||||||||
|
||||||||||
// Check expiration | ||||||||||
if (exp < currentTime) { | ||||||||||
return false; | ||||||||||
} | ||||||||||
|
||||||||||
// Verify issuer | ||||||||||
if (iss !== removeTrailingSlash(process.env.KINDE_ISSUER_URL)) { | ||||||||||
return false; | ||||||||||
} | ||||||||||
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Issuer Validation Already Handled The issuer validation is specified in the You can remove the manual issuer check: // Verify issuer
- if (iss !== removeTrailingSlash(process.env.KINDE_ISSUER_URL)) {
- return false;
- } Ensure that the 📝 Committable suggestion
Suggested change
|
||||||||||
|
||||||||||
return true; | ||||||||||
Comment on lines
+24
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Security Issue: JWT Signature Not Verified Currently, the JWT is decoded using Apply this diff to fix the issue: -import jwtDecode from 'jwt-decode';
+import jwt from 'jsonwebtoken';
export const isAuthenticatedFactory = (req, res) => async () => {
const user = await getUserFactory(req, res)();
if (!user) {
return false;
}
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return false;
}
try {
- const decoded = jwtDecode(token);
+ const decoded = jwt.verify(token, publicKey, {
+ issuer: removeTrailingSlash(process.env.KINDE_ISSUER_URL),
+ algorithms: ['RS256'], // Use the appropriate algorithm
+ });
const { exp, iss } = decoded;
const currentTime = Math.floor(Date.now() / 1000);
// Check expiration
if (exp < currentTime) {
return false;
}
// Verify issuer (already handled in jwt.verify options)
return true;
} catch (error) {
- console.error('Token decoding failed:', error);
+ console.error('Token verification failed');
return false;
}
}; Note: You'll need to:
|
||||||||||
} catch (error) { | ||||||||||
console.error('Token decoding failed:', error); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid Logging Sensitive Error Information Logging the entire error object with Apply this diff to fix the issue: - console.error('Token decoding failed:', error);
+ console.error('Token verification failed'); 📝 Committable suggestion
Suggested change
|
||||||||||
return false; | ||||||||||
} | ||||||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Potential Redundancy in Expiration Check
After using
jwt.verify
, the token's expiration is already validated. Thejwt.verify
function will throw an error if the token is expired. Therefore, the manual expiration check may be unnecessary.You can simplify the code by removing the manual expiration check: