aboutsummaryrefslogtreecommitdiffstats
path: root/cloud_mdir_sync/gmail.py
diff options
context:
space:
mode:
authorJason Gunthorpe <jgg@nvidia.com>2020-07-17 14:49:59 -0300
committerJason Gunthorpe <jgg@nvidia.com>2020-07-17 14:49:59 -0300
commit125b7fa027b869b4054b7c3ec880086ff7d06854 (patch)
tree357cbeac360264aa75d8a91dc5e4afe05f155aa6 /cloud_mdir_sync/gmail.py
parent9517ee2ddc30d7328698d316ca1b592044ec0d54 (diff)
downloadcloud_mdir_sync-125b7fa027b869b4054b7c3ec880086ff7d06854.tar.gz
cloud_mdir_sync-125b7fa027b869b4054b7c3ec880086ff7d06854.tar.bz2
cloud_mdir_sync-125b7fa027b869b4054b7c3ec880086ff7d06854.zip
cms-oauth: Check the token lifetime before returning it
If the mail functions are not being used then the ticket will get stale, check the timestamp before returning it and refresh if too old. Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Diffstat (limited to 'cloud_mdir_sync/gmail.py')
-rw-r--r--cloud_mdir_sync/gmail.py4
1 files changed, 3 insertions, 1 deletions
diff --git a/cloud_mdir_sync/gmail.py b/cloud_mdir_sync/gmail.py
index 7ef9478..dee12f5 100644
--- a/cloud_mdir_sync/gmail.py
+++ b/cloud_mdir_sync/gmail.py
@@ -6,6 +6,7 @@ import datetime
import functools
import logging
import secrets
+import time
from typing import Dict, List, Optional, Set
import aiohttp
@@ -278,7 +279,8 @@ class GmailAPI(oauth.Account):
async def get_xoauth2_bytes(self, proto: str) -> Optional[bytes]:
"""Return the xoauth2 byte string for the given protocol to login to
this account."""
- while self.api_token is None:
+ while (self.api_token is None
+ or self.api_token["expires_at"] <= time.time() + 10):
await self.authenticate()
if proto == "SMTP" or proto == "IMAP":