Coverage for sfkit/auth/auth.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-08-07 15:11 -0400

1import os 

2 

3from sfkit.api import get_doc_ref_dict 

4from sfkit.utils import constants 

5from sfkit.utils.helper_functions import condition_or_fail 

6 

7 

8def auth() -> None: 

9 """ 

10 Authenticate a GCP service account from the study with the sfkit CLI. 

11 """ 

12 

13 try: 

14 with open("auth_key.txt", "r") as f: 

15 auth_key = f.readline().rstrip() 

16 except FileNotFoundError: 

17 print("auth_key.txt not found.") 

18 auth_key_path = input("Enter the path to your auth_key.txt file: ") 

19 try: 

20 with open(auth_key_path, "r") as f: 

21 auth_key = f.readline().rstrip() 

22 except FileNotFoundError: 

23 print("auth_key.txt not found. Please download the auth_key.txt file from the sfkit website.") 

24 exit(1) 

25 

26 os.makedirs(constants.SFKIT_DIR, exist_ok=True) 

27 with open(constants.AUTH_KEY, "w") as f: 

28 f.write(auth_key) 

29 

30 try: 

31 doc_ref_dict = get_doc_ref_dict() 

32 assert doc_ref_dict is not None and "title" in doc_ref_dict 

33 except Exception as e: 

34 os.remove(constants.AUTH_KEY) 

35 print("Invalid auth_key.txt file.") 

36 print(e) 

37 condition_or_fail(False, "sfkit auth failed.") 

38 else: 

39 print(f"Successfully authenticated with study {doc_ref_dict['title']}!")