Merge pull request #88 from christianlouis/copilot/implement-secure-api-key-storage
Add LOGTO_SKIP_SSL_VERIFY setting to support self-signed certificates on Logto instances
This commit is contained in:
@@ -71,10 +71,15 @@ class Settings(BaseSettings):
|
|||||||
# LOGTO_APP_SECRET: the Client Secret of the same application.
|
# LOGTO_APP_SECRET: the Client Secret of the same application.
|
||||||
# LOGTO_REDIRECT_URI (optional): override the default callback URL.
|
# LOGTO_REDIRECT_URI (optional): override the default callback URL.
|
||||||
# Defaults to <base_url>/api/v1/auth/callback.
|
# Defaults to <base_url>/api/v1/auth/callback.
|
||||||
|
# LOGTO_SKIP_SSL_VERIFY (optional): set to false to enable SSL certificate
|
||||||
|
# verification when connecting to the Logto OIDC endpoint.
|
||||||
|
# Defaults to true (verification disabled) to support
|
||||||
|
# self-signed certificates out of the box.
|
||||||
LOGTO_ENDPOINT: Optional[str] = None
|
LOGTO_ENDPOINT: Optional[str] = None
|
||||||
LOGTO_APP_ID: Optional[str] = None
|
LOGTO_APP_ID: Optional[str] = None
|
||||||
LOGTO_APP_SECRET: Optional[str] = None
|
LOGTO_APP_SECRET: Optional[str] = None
|
||||||
LOGTO_REDIRECT_URI: Optional[str] = None
|
LOGTO_REDIRECT_URI: Optional[str] = None
|
||||||
|
LOGTO_SKIP_SSL_VERIFY: bool = True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def logto_configured(self) -> bool:
|
def logto_configured(self) -> bool:
|
||||||
|
|||||||
@@ -12,9 +12,11 @@ Provides:
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import ssl
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
from fastapi import Request, Response
|
from fastapi import Request, Response
|
||||||
from jose import JWTError, jwt
|
from jose import JWTError, jwt
|
||||||
from logto import IdTokenClaims, LogtoClient, LogtoConfig, PersistKey, Storage, UserInfoScope
|
from logto import IdTokenClaims, LogtoClient, LogtoConfig, PersistKey, Storage, UserInfoScope
|
||||||
@@ -38,6 +40,59 @@ _SIGN_IN_SESSION_MAX_AGE = 600 # 10 minutes
|
|||||||
_SESSION_MAX_AGE = 86_400 # 24 hours
|
_SESSION_MAX_AGE = 86_400 # 24 hours
|
||||||
|
|
||||||
|
|
||||||
|
# ── SSL configuration for Logto SDK ──────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_logto_ssl_patch() -> None:
|
||||||
|
"""
|
||||||
|
If ``LOGTO_SKIP_SSL_VERIFY`` is ``True``, monkey-patch ``aiohttp.ClientSession``
|
||||||
|
so that every session created by the Logto SDK uses a non-verifying SSL connector.
|
||||||
|
|
||||||
|
The Logto SDK creates its own ``aiohttp.ClientSession`` objects internally and
|
||||||
|
provides no mechanism to inject an SSL context. Replacing the class at module
|
||||||
|
level is the only way to propagate the setting without forking the SDK.
|
||||||
|
|
||||||
|
**Scope note:** ``aiohttp`` is not used anywhere else in this application – only
|
||||||
|
the Logto SDK pulls it in. If additional code in this repository starts using
|
||||||
|
``aiohttp`` directly, review whether those connections should also skip
|
||||||
|
verification before enabling this setting.
|
||||||
|
|
||||||
|
.. warning::
|
||||||
|
Disabling SSL verification removes protection against man-in-the-middle
|
||||||
|
attacks. Only enable this when connecting to a Logto instance that uses
|
||||||
|
a self-signed certificate that you control.
|
||||||
|
"""
|
||||||
|
if not settings.LOGTO_SKIP_SSL_VERIFY:
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.warning(
|
||||||
|
"LOGTO_SKIP_SSL_VERIFY is enabled – SSL certificate verification for "
|
||||||
|
"Logto OIDC connections is DISABLED. Use this only when your Logto "
|
||||||
|
"instance uses a self-signed certificate. Never enable this in a "
|
||||||
|
"production environment that faces the public internet."
|
||||||
|
)
|
||||||
|
|
||||||
|
ssl_ctx = ssl.create_default_context()
|
||||||
|
ssl_ctx.check_hostname = False
|
||||||
|
ssl_ctx.verify_mode = ssl.CERT_NONE
|
||||||
|
|
||||||
|
_OriginalClientSession = aiohttp.ClientSession
|
||||||
|
|
||||||
|
class _NoVerifyClientSession(_OriginalClientSession): # type: ignore[misc]
|
||||||
|
"""``aiohttp.ClientSession`` subclass that disables SSL verification."""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs) -> None: # type: ignore[override]
|
||||||
|
if "connector" not in kwargs:
|
||||||
|
kwargs["connector"] = aiohttp.TCPConnector(ssl=ssl_ctx)
|
||||||
|
kwargs.setdefault("connector_owner", True)
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
aiohttp.ClientSession = _NoVerifyClientSession # type: ignore[assignment]
|
||||||
|
|
||||||
|
|
||||||
|
_apply_logto_ssl_patch()
|
||||||
|
|
||||||
|
|
||||||
# ── Cookie-backed Logto Storage ───────────────────────────────────────────────
|
# ── Cookie-backed Logto Storage ───────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,16 @@ DMARQ can be configured through:
|
|||||||
| `LOG_LEVEL` | Application logging level | `INFO` | `DEBUG`, `INFO`, `WARNING`, `ERROR` |
|
| `LOG_LEVEL` | Application logging level | `INFO` | `DEBUG`, `INFO`, `WARNING`, `ERROR` |
|
||||||
| `CORS_ORIGINS` | Allowed CORS origins | `http://localhost:8000` | `https://dmarq.example.com` |
|
| `CORS_ORIGINS` | Allowed CORS origins | `http://localhost:8000` | `https://dmarq.example.com` |
|
||||||
|
|
||||||
|
### Logto Authentication Settings
|
||||||
|
|
||||||
|
| Variable | Description | Default | Example |
|
||||||
|
|----------|-------------|---------|---------|
|
||||||
|
| `LOGTO_ENDPOINT` | Base URL of your Logto instance | - | `https://your-tenant.logto.app` |
|
||||||
|
| `LOGTO_APP_ID` | Client ID of the Logto application | - | `your-app-id` |
|
||||||
|
| `LOGTO_APP_SECRET` | Client Secret of the Logto application | - | `your-app-secret` |
|
||||||
|
| `LOGTO_REDIRECT_URI` | Override the OAuth callback URL | Auto-detected | `https://dmarq.example.com/api/v1/auth/callback` |
|
||||||
|
| `LOGTO_SKIP_SSL_VERIFY` | Disable SSL certificate verification for connections to the Logto endpoint. **Only use this when your Logto instance uses a self-signed certificate that you control. Never enable in production environments.** | `true` | `true`, `false` |
|
||||||
|
|
||||||
### IMAP Settings
|
### IMAP Settings
|
||||||
|
|
||||||
| Variable | Description | Default | Example |
|
| Variable | Description | Default | Example |
|
||||||
|
|||||||
Reference in New Issue
Block a user