feat(auth): enhance Google authentication validation
- Added validation for token audience, issuer, and expiry in the `googleMobileAuth` method of `AuthController`. - Improved error handling for invalid tokens and expired sessions, ensuring robust authentication flow.
This commit is contained in:
@@ -194,6 +194,21 @@ export default class AuthController extends WorklenzControllerBase {
|
|||||||
const response = await axios.get(`https://oauth2.googleapis.com/tokeninfo?id_token=${idToken}`);
|
const response = await axios.get(`https://oauth2.googleapis.com/tokeninfo?id_token=${idToken}`);
|
||||||
const profile = response.data;
|
const profile = response.data;
|
||||||
|
|
||||||
|
// Validate token audience (client ID)
|
||||||
|
if (profile.aud !== process.env.GOOGLE_CLIENT_ID) {
|
||||||
|
return res.status(400).send(new ServerResponse(false, null, "Invalid token audience"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate token issuer
|
||||||
|
if (!['https://accounts.google.com', 'accounts.google.com'].includes(profile.iss)) {
|
||||||
|
return res.status(400).send(new ServerResponse(false, null, "Invalid token issuer"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check token expiry
|
||||||
|
if (Date.now() >= profile.exp * 1000) {
|
||||||
|
return res.status(400).send(new ServerResponse(false, null, "Token expired"));
|
||||||
|
}
|
||||||
|
|
||||||
if (!profile.email_verified) {
|
if (!profile.email_verified) {
|
||||||
return res.status(400).send(new ServerResponse(false, null, "Email not verified"));
|
return res.status(400).send(new ServerResponse(false, null, "Email not verified"));
|
||||||
}
|
}
|
||||||
@@ -210,7 +225,7 @@ export default class AuthController extends WorklenzControllerBase {
|
|||||||
[profile.sub, profile.email]
|
[profile.sub, profile.email]
|
||||||
);
|
);
|
||||||
|
|
||||||
let user;
|
let user: any;
|
||||||
if (userResult.rowCount) {
|
if (userResult.rowCount) {
|
||||||
// Existing user - login
|
// Existing user - login
|
||||||
user = userResult.rows[0];
|
user = userResult.rows[0];
|
||||||
|
|||||||
Reference in New Issue
Block a user