-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
82 lines (67 loc) · 2.6 KB
/
auth.py
File metadata and controls
82 lines (67 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Authentication Helper Module for Stockwatch
Loads username and password from secrets.json file.
"""
import json
import os
from typing import Dict, Any
def load_stockwatch_auth() -> Dict[str, str]:
"""
Load Stockwatch username and password from secrets.json file or environment variables.
Priority:
1. Environment variables (STOCKWATCH_USERNAME, STOCKWATCH_PASSWORD) - for CI/CD
2. secrets.json file - for local development
Returns:
Dictionary with 'username' and 'password' keys
Raises:
FileNotFoundError: If secrets.json file doesn't exist and env vars not set
json.JSONDecodeError: If secrets.json contains invalid JSON
ValueError: If credentials are missing or invalid
"""
# First, try environment variables (for GitHub Actions)
username = os.getenv("STOCKWATCH_USERNAME")
password = os.getenv("STOCKWATCH_PASSWORD")
if username and password:
return {
"username": username,
"password": password
}
# Fall back to secrets.json file (for local development)
secrets_file = "secrets.json"
# Check if file exists
if not os.path.isfile(secrets_file):
raise FileNotFoundError(
f"Authentication file '{secrets_file}' not found and environment variables not set.\n"
f"Please either:\n"
f" 1. Set STOCKWATCH_USERNAME and STOCKWATCH_PASSWORD environment variables, or\n"
f" 2. Create {secrets_file} with your Stockwatch username and password:\n"
f' {{\n'
f' "username": "your_username",\n'
f' "password": "your_password"\n'
f' }}'
)
# Read and parse JSON file
try:
with open(secrets_file, 'r', encoding='utf-8') as f:
auth_data = json.load(f)
except json.JSONDecodeError as e:
raise json.JSONDecodeError(
f"Invalid JSON in {secrets_file}: {e.msg}",
e.doc,
e.pos
)
# Validate that we have username and password
if not isinstance(auth_data, dict):
raise ValueError(
f"{secrets_file} must contain a JSON object (dictionary).\n"
f"Found: {type(auth_data).__name__}"
)
if "username" not in auth_data or "password" not in auth_data:
raise ValueError(
f"{secrets_file} must contain 'username' and 'password' fields:\n"
f' {{\n'
f' "username": "your_username",\n'
f' "password": "your_password"\n'
f' }}'
)
return auth_data