struggle with simulating token expiring
I am stuggling with simulating the token expiring scenario by setting the tokens.expiry_date manually.
current behaviour:
I set the tokens.expiry_date to 10 seconds from now on. But it doesn't work. When I call oauth2client.revokeToken(<access_token>) method, it revokes access token successfully.
expect behaviour:
After 10 seconds later, when I call oauth2client.revokeToken(<access_token>), it should be throw an token expired error.
oauth callback code snippet:
router.get("/callback", async (req, res, next) => {
const { code: authorizationCode, error } = req.query;
if (!authorizationCode || error) {
req.app.locals.errorMessage = error;
return next(error);
}
let tokens;
try {
tokens = (await oauth2Client.getToken(authorizationCode)).tokens;
console.log(
`original tokens expiry date: ${tokens.expiry_date},${new Date(tokens.expiry_date as number).toLocaleString()}`,
);
if (tokens.expiry_date) {
// For testing token expire
const expiryDate = moment(new Date(tokens.expiry_date))
.subtract(59, "m")
.subtract(50, "s")
.toDate()
.getTime();
console.log(`test expiry date: ${expiryDate}, ${new Date(expiryDate).toLocaleString()}`);
tokens.expiry_date = expiryDate;
}
oauth2Client.setCredentials(tokens);
} catch (error) {
console.error("oauth2Client get token failed.");
return next(error);
}
let userInfo;
try {
userInfo = await OAuth2Service.getUserInfo();
console.log("userInfo: ", userInfo);
} catch (error) {
console.error("OAuth2Service get user info failed.");
return next(error);
}
lowdb
.get("oauth_clients")
.push({ ...tokens, ...userInfo })
.write();
res.redirect("/");
});
revoke access token codes snippet:
router.get("/revoke-access-token", async (req, res, next) => {
const googleAccount: any = lowdb
.get("oauth_clients")
.find({ email: (req as any).user.email })
.value();
console.log("googleAccount: ", googleAccount);
try {
await oauth2Client.revokeToken(googleAccount.access_token);
console.log("revoke access token successfully.");
lowdb
.get("oauth_clients")
.remove({ email: (req as any).user.email })
.write();
res.redirect("/");
} catch (error) {
console.error("revoke access token failed.");
next(error);
}
});
How can I simulating token expiring scenario? I don't want to wait one hour for access token expiring. Beside, the docs says the oauth2client will refresh the access token automaticlly when the access token will expired?
@mrdulin is the goal to write tests around the fresh token scenario? If you're writing integration tests, I don't believe that setting expiry_date will have the effect you desire, expiry_date indicates when a refresh will be required by the upstream authentication service, changing the value when saving won't make it become invalid sooner (at least from the perspective of the upstream service).
If the goal is writing unit tests, I'd recommend potentially using a library like nock and faking the expired token error.
It's also worth noting that google-auth-library-nodejs will seamlessly try to refresh your credentials for you, after expiry -- it might be potentially be overkill to test this contract in your tests.
If you do want to test the contract however, a combination of faking the appropriate upstream responses with nock, and passing an expired token date, should do the trick for you 👍 let us know.
Hi @mrdulin, instead of revoking access for access_token try revoke access for refresh_token (oauth2Client.revokeToken(googleAccount.refresh_token). Also, you can try to set credentials manually.
OAuth callback code snippet:
app.get('/callback', (req, res) =>{
try {
var newExpiryDate = moment(new Date(token.expiry_date))
.subtract(59,"m")
.subtract(50,"s")
.toDate()
.getTime();
token.expiry_date = newExpiryDate;
oauth2Client.credentials = {
access_token : token.access_token,
refresh_token : token.refresh_token,
token_type : "Bearer",
expiry_date : newExpiryDate,
}
//oauth2Client.setCredentials(token);
} catch (error) {
console.log(error);
}
res.redirect("/");
});
Revoke access code snippet
app.get('/revoke', (req, res) => {
try {
oauth2Client.revokeToken(token.refresh_token, (response, error)=>{
if(error) console.error(error);
console.log(response);
})
console.log("revoke token successfully.");
res.redirect("/");
} catch (error) {
console.error("revoke token failed.");
}
});
@mrdulin did you figure out an approach for testing? either with integration tests and revoking tokens like @vishald123 suggests, or with mocking?
Greetings! We haven't heard back in a while, so we're going to go ahead and close this one out. Please do let us know if you have any other questions!