fixed key to one line

This commit is contained in:
cpu
2025-03-26 06:38:33 +01:00
parent f2de1e55d0
commit 102d2e2748
2 changed files with 32 additions and 21 deletions

30
app.py
View File

@@ -42,7 +42,7 @@ class FlicButtonHandler:
}
# Ensure subscriptions file and directory exist
self.subscriptions_file = os.getenv('SUBSCRIPTIONS_FILE', '/app/subscriptions.json')
self.subscriptions_file = os.getenv('SUBSCRIPTIONS_FILE', 'app/subscriptions.json')
self._ensure_subscriptions_file()
# Load subscriptions
@@ -70,26 +70,30 @@ class FlicButtonHandler:
def _decode_vapid_private_key(self):
"""
Decode and load the VAPID private key from base64 encoded string.
Returns the PEM-formatted private key as a string.
Load the VAPID private key from environment variable.
Handles the \n escaped format from .env file.
"""
try:
# Decode base64 private key
private_key_pem = base64.urlsafe_b64decode(
os.getenv('VAPID_PRIVATE_KEY', '').encode('utf-8')
)
# Get the key from environment
env_key = os.getenv('VAPID_PRIVATE_KEY', '').strip()
# Load private key to validate it
private_key = serialization.load_pem_private_key(
private_key_pem,
# Convert escaped newlines back to actual newlines
private_pem = env_key.replace('\\n', '\n')
# Verify PEM format
if not private_pem.startswith('-----BEGIN PRIVATE KEY-----'):
raise ValueError("Invalid PEM format")
# Validate the key
serialization.load_pem_private_key(
private_pem.encode('utf-8'),
password=None
)
# Return the original PEM string (pywebpush needs this format)
return private_key_pem.decode('utf-8')
return private_pem
except Exception as e:
logger.error(f"Error loading VAPID private key: {e}")
logger.error(f"VAPID key error: {str(e)}")
raise
def load_subscriptions(self) -> List[Dict]: