aboutsummaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--cloud_mdir_sync/gmail.py4
-rw-r--r--cloud_mdir_sync/office365.py4
2 files changed, 6 insertions, 2 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":
diff --git a/cloud_mdir_sync/office365.py b/cloud_mdir_sync/office365.py
index 69a4462..32ae45a 100644
--- a/cloud_mdir_sync/office365.py
+++ b/cloud_mdir_sync/office365.py
@@ -7,6 +7,7 @@ import logging
import os
import pickle
import secrets
+import time
from typing import Any, Dict, Optional, Union
import aiohttp
@@ -487,7 +488,8 @@ class GraphAPI(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.owa_token is None:
+ while (self.owa_token is None
+ or self.owa_token["expires_at"] <= time.time() + 10):
await self.authenticate()
if proto == "SMTP" or proto == "IMAP":