Skip to content

Commit 43635b2

Browse files
committed
fix: use env variables on heroku
1 parent 6d5fe5d commit 43635b2

2 files changed

Lines changed: 45 additions & 26 deletions

File tree

api/auth.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,31 @@ def __init__(self) -> None:
2323
self._load_config()
2424

2525
def _load_config(self) -> None:
26-
"""Load public keys and token expiry from secret.yaml"""
27-
try:
28-
if "JWT_SECRET" in os.environ:
29-
config_path = os.environ["JWT_SECRET"]
30-
else:
31-
config_path = "./secret.yaml"
32-
33-
with open(config_path, encoding="utf-8") as f:
34-
config = yaml.safe_load(f)
35-
36-
# Load public keys for each client
37-
if "clients" in config:
38-
for client_id, client_config in config["clients"].items():
39-
if "public_key" in client_config:
40-
self.public_keys[client_id] = client_config["public_key"]
26+
"""Load public keys and token expiry from secret.yaml or environment variables"""
4127

42-
# Load token expiry time if specified
43-
if "token_expiry_seconds" in config:
44-
self.token_expiry_seconds = config["token_expiry_seconds"]
28+
if os.path.exists("secret.yaml"):
29+
with open("secret.yaml", "r") as f:
30+
config = yaml.safe_load(f)
31+
clients = config.get("JWT_CLIENTS", {})
32+
for client_id, client_info in clients.items():
33+
pub_key = client_info.get("PUB_KEY")
34+
if pub_key:
35+
self.public_keys[client_id] = pub_key
36+
37+
self.token_expiry_seconds = config.get("JWT_TOKEN_EXPIRY_SECONDS", 3600)
38+
else:
39+
# find all env variables satisfy JWT_CLIENT_YOUR_APP_NAME_PUB_KEY
40+
for key, value in os.environ.items():
41+
if key.startswith("JWT_CLIENT_") and key.endswith("_PUB_KEY"):
42+
client_id = key[len("JWT_CLIENT_") : -len("_PUB_KEY")]
43+
self.public_keys[client_id] = value
44+
45+
self.token_expiry_seconds = int(
46+
os.environ.get("JWT_TOKEN_EXPIRY_SECONDS", 3600)
47+
)
4548

46-
except FileNotFoundError:
47-
print("Warning: secret.yaml not found. JWT authentication disabled.")
48-
except Exception as e:
49-
print(f"Warning: Error loading JWT config: {e}")
49+
if not self.public_keys:
50+
print("Warning: No JWT public keys loaded. Authentication may fail.")
5051

5152

5253
jwt_config = JWTConfig()

auth/README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,36 @@ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...
3030

3131
### Step 3: Admin Adds You to the Server
3232

33-
The admin will add your public key to `secret.yaml`:
33+
The admin needs to register your public key on the server. This can be done in two ways:
34+
35+
#### Option A: Using `secret.yaml` (Local/Standard)
36+
37+
Add the public key to `secret.yaml`:
3438

3539
```yaml
36-
clients:
40+
JWT_CLIENTS:
3741
your_app_name:
38-
public_key: |
42+
PUB_KEY: |
3943
-----BEGIN PUBLIC KEY-----
4044
(your public key here)
4145
-----END PUBLIC KEY-----
4246
```
4347
44-
Then they'll restart the server.
48+
#### Option B: Using Environment Variables (Heroku/Cloud)
49+
50+
For platforms like Heroku where files are not persistent, use environment variables:
51+
52+
1. **Single Client Variable**:
53+
Set an environment variable named `JWT_CLIENT_YOUR_APP_NAME_PUB_KEY` (replace `YOUR_APP_NAME` with the actual client ID in uppercase) with the content of the public key.
54+
55+
```bash
56+
# Example for Heroku
57+
heroku config:set JWT_CLIENT_YOUR_APP_NAME_PUB_KEY="-----BEGIN PUBLIC KEY-----
58+
...
59+
-----END PUBLIC KEY-----"
60+
```
61+
62+
Then restart the server.
4563

4664
---
4765

0 commit comments

Comments
 (0)