implement lifetime

This commit is contained in:
Valentin Boettcher 2024-04-05 20:16:31 -04:00
parent 057d343656
commit c533eb92d7
No known key found for this signature in database
GPG key ID: E034E12B7AF56ACE
4 changed files with 14 additions and 4 deletions

View file

@ -20,4 +20,5 @@ def get_config(profile):
Scopes = config_data["Scopes"],
RefreshTokenFileName = cache_path / "imap_smtp_refresh_token",
AccessTokenFileName = cache_path / "imap_smtp_access_token",
Authority = config_data["Authority"] or None)
Authority = config_data["Authority"] or None,
Timeout = config_data.get("Timeout", 60 * 60))

View file

@ -2,7 +2,7 @@
ClientId = "08162f7c-0fd2-4200-a84a-f25a4db0b584"
ClientSecret = "TxRBilcHdC6WGBee]fs?QR:SJ8nI[g82"
Scopes = ['https://outlook.office.com/IMAP.AccessAsUser.All','https://outlook.office.com/SMTP.Send']
Authority = false
Timeout = 3600
[mcgill]

View file

@ -25,6 +25,7 @@ in
ClientSecret = "TxRBilcHdC6WGBee]fs?QR:SJ8nI[g82"
Scopes = ['https://outlook.office.com/IMAP.AccessAsUser.All','https://outlook.office.com/SMTP.Send']
Authority = false
Timeout = 3600
'';
};
};

View file

@ -2,10 +2,10 @@
from msal import ConfidentialClientApplication, SerializableTokenCache
import config
import sys
import os
import time
print_access_token = True
# get first command line argument
if len(sys.argv) > 1:
profile = sys.argv[1]
@ -18,10 +18,18 @@ profile_config = config.get_config(profile)
cache = SerializableTokenCache()
app = ConfidentialClientApplication(profile_config.ClientId, client_credential=profile_config.ClientSecret, token_cache=cache, authority=profile_config.Authority)
if profile_config.AccessTokenFileName.exists() and print_access_token:
st = os.stat(profile_config.AccessTokenFileName)
if (time.time()-st.st_mtime) < profile_config.Timeout:
with open(profile_config.AccessTokenFileName, 'r') as f:
print(f.read())
sys.exit(0)
# check if file exists and error out if it doesn't
try:
old_refresh_token = open(profile_config.RefreshTokenFileName,'r').read()
except FileNotFoundError:
sys.exit("Please get the initial token by running `o365-get-token` first.")