diff --git a/config.py b/config.py index 8638704..6a18fb1 100644 --- a/config.py +++ b/config.py @@ -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)) diff --git a/config.toml b/config.toml index 86d1871..82c4652 100644 --- a/config.toml +++ b/config.toml @@ -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] diff --git a/nix/hm-module.nix b/nix/hm-module.nix index 8f7a31f..1565a38 100644 --- a/nix/hm-module.nix +++ b/nix/hm-module.nix @@ -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 ''; }; }; diff --git a/refresh_token.py b/refresh_token.py index 41d165a..1245303 100644 --- a/refresh_token.py +++ b/refresh_token.py @@ -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.")