|
1 | | -def authenticate(): |
| 1 | +import os |
| 2 | +import getpass |
| 3 | +from .internal import token_utils |
| 4 | +from .api.auth import login as auth_login |
| 5 | + |
| 6 | +def authenticate(username=None, interaction_enabled=True): |
2 | 7 | """ |
3 | | - Returns the authentication token for the current user. |
| 8 | + Authenticates using secret, environment variables or prompt. |
| 9 | +
|
| 10 | + Args: |
| 11 | + username (str, optional): Username to use for login. |
| 12 | + interaction_enabled (bool, optional): Whether interactive steps are enabled. |
4 | 13 |
|
5 | | - This is a placeholder implementation. |
| 14 | + Returns: |
| 15 | + str: The authentication token. |
6 | 16 | """ |
7 | | - return "dummy_token" |
| 17 | + |
| 18 | + # Check if already authenticated |
| 19 | + if is_authenticated(username): |
| 20 | + token, _ = token_utils.get_active_token() |
| 21 | + return token |
| 22 | + |
| 23 | + # Check environment variables |
| 24 | + if authenticated_with_environment_variable(username): |
| 25 | + token, _ = token_utils.get_active_token() |
| 26 | + return token |
| 27 | + |
| 28 | + # Prompt user |
| 29 | + if interaction_enabled: |
| 30 | + if prompt_login(username): |
| 31 | + token, _ = token_utils.get_active_token() |
| 32 | + return token |
| 33 | + |
| 34 | + # If we get here, authentication failed or was skipped |
| 35 | + # Matlab doesn't return anything or returns existing token (which might be empty/invalid?) |
| 36 | + # But here we want to return a valid token or raise error? |
| 37 | + # Matlab returns token found at the end. |
| 38 | + |
| 39 | + token, _ = token_utils.get_active_token() |
| 40 | + if not token: |
| 41 | + # If no token, maybe we should raise an error if interaction was enabled but failed? |
| 42 | + # Or just return None/empty string? |
| 43 | + # Matlab doesn't explicitly raise error in the main function, but prompt might. |
| 44 | + pass |
| 45 | + |
| 46 | + return token |
| 47 | + |
| 48 | +def is_authenticated(username=None): |
| 49 | + token, _ = token_utils.get_active_token() |
| 50 | + if not token: |
| 51 | + return False |
| 52 | + |
| 53 | + if username: |
| 54 | + decoded_token = token_utils.decode_jwt(token) |
| 55 | + if decoded_token.get('email') != username: |
| 56 | + return False |
| 57 | + |
| 58 | + return True |
| 59 | + |
| 60 | +def authenticated_with_environment_variable(requested_username=None): |
| 61 | + username = os.environ.get("NDI_CLOUD_USERNAME") |
| 62 | + password = os.environ.get("NDI_CLOUD_PASSWORD") |
| 63 | + |
| 64 | + if username and password: |
| 65 | + if requested_username and username != requested_username: |
| 66 | + return False |
| 67 | + return perform_login(username, password) |
| 68 | + return False |
| 69 | + |
| 70 | +def prompt_login(username=None): |
| 71 | + if not username: |
| 72 | + username = input("Enter NDI Cloud Email: ") |
| 73 | + |
| 74 | + password = getpass.getpass(f"Enter Password for {username}: ") |
| 75 | + |
| 76 | + return perform_login(username, password) |
| 77 | + |
| 78 | +def perform_login(username, password): |
| 79 | + success, answer, response, _ = auth_login.login(username, password) |
| 80 | + |
| 81 | + if success: |
| 82 | + token = answer.get('token') |
| 83 | + # Handle organizations. Structure might be user -> organizations -> id? |
| 84 | + # Matlab: answer.user.organizations.id |
| 85 | + # We need to be careful about structure. |
| 86 | + # Let's try to find it. |
| 87 | + user = answer.get('user', {}) |
| 88 | + organizations = user.get('organizations', []) |
| 89 | + organization_id = '' |
| 90 | + |
| 91 | + # If organizations is a list, take first? Or checks for specific one? |
| 92 | + # Matlab code: answer.user.organizations.id. This suggests organizations is a struct (dict), not a list? |
| 93 | + # Or maybe it is a list of structs and Matlab auto-vectorizes? |
| 94 | + # But commonly in these APIs it's a list. |
| 95 | + # If it's a list, we might pick the first one. |
| 96 | + |
| 97 | + if isinstance(organizations, list) and len(organizations) > 0: |
| 98 | + organization_id = organizations[0].get('id') |
| 99 | + elif isinstance(organizations, dict): |
| 100 | + organization_id = organizations.get('id') |
| 101 | + |
| 102 | + os.environ['NDI_CLOUD_TOKEN'] = token |
| 103 | + if organization_id: |
| 104 | + os.environ['NDI_CLOUD_ORGANIZATION_ID'] = organization_id |
| 105 | + |
| 106 | + return True |
| 107 | + else: |
| 108 | + print(f"Authentication failed: {response.status_code} {response.reason}") |
| 109 | + # print body? |
| 110 | + return False |
0 commit comments