diff --git a/config.py b/config.py index e01d259..7b1d4ac 100644 --- a/config.py +++ b/config.py @@ -18,10 +18,9 @@ def get_config(profile): ClientId = config_data["ClientId"], ClientSecret = config_data["ClientSecret"], Scopes = config_data["Scopes"], - RefreshTokenFileName = cache_path / "imap_smtp_refresh_token", - AccessTokenFileName = cache_path / "imap_smtp_access_token", + CacheFile = cache_path / "cache.json", Authority = config_data["Authority"] or None, - Timeout = config_data.get("Timeout", 60 * 60)) + ) if __name__ == "__main__": if len(sys.argv) < 3: diff --git a/config.toml b/config.toml index 82c4652..f59fc46 100644 --- a/config.toml +++ b/config.toml @@ -3,6 +3,5 @@ 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/get_token.py b/get_token.py index 296cdf4..ff6cbfb 100755 --- a/get_token.py +++ b/get_token.py @@ -7,7 +7,7 @@ import sys import threading import urllib.parse import webbrowser - +import json if len(sys.argv) > 1: profile = sys.argv[1] else: @@ -78,10 +78,8 @@ if 'error' in token: print(token) sys.exit("Failed to get access token") -with open(profile_config.RefreshTokenFileName, 'w') as f: - print(f'Refresh token acquired, writing to file {profile_config.RefreshTokenFileName}') - f.write(token['refresh_token']) -with open(profile_config.AccessTokenFileName, 'w') as f: - print(f'Access token acquired, writing to file {profile_config.AccessTokenFileName}') - f.write(token['access_token']) +with open(profile_config.CacheFile, 'w') as f: + json.dump({'refresh_token': token['refresh_token'], + 'expires_in': token['expires_in'], + 'access_token': token['access_token']}, f) diff --git a/refresh_token.py b/refresh_token.py index 1245303..2db519e 100644 --- a/refresh_token.py +++ b/refresh_token.py @@ -4,7 +4,7 @@ import config import sys import os import time - +import json print_access_token = True if len(sys.argv) > 1: @@ -18,34 +18,29 @@ 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: +if not profile_config.CacheFile.exists(): sys.exit("Please get the initial token by running `o365-get-token` first.") +token_cache = json.loads(profile_config.CacheFile.read_text()) -token = app.acquire_token_by_refresh_token(old_refresh_token,profile_config.Scopes) +st = os.stat(profile_config.CacheFile) +if (time.time()-st.st_mtime) < token_cache["expires_in"]: + print(token_cache["access_token"]) + sys.exit(0) + + +token = app.acquire_token_by_refresh_token(token_cache["refresh_token"],profile_config.Scopes) if 'error' in token: print(token) sys.exit("Failed to get access token") -# you're supposed to save the old refresh token each time -with open(profile_config.RefreshTokenFileName, 'w') as f: - #f.write(cache.find('RefreshToken')[0]['secret']) - f.write(token['refresh_token']) -with open(profile_config.AccessTokenFileName, 'w') as f: - f.write(token['access_token']) +# you're supposed to save the old refresh token each time +with open(profile_config.CacheFile, 'w') as f: + json.dump({'refresh_token': token['refresh_token'], + 'expires_in': token['expires_in'], + 'access_token': token['access_token']}, f) if print_access_token: print(token['access_token'])