Commit ·
a70779e
1
Parent(s): 8f7373f
fix: リバースプロキシ環境での OAuth2 insecure_transport エラーを回避するため ProxyFix の適用と HTTPS スキームの強制処理を追加
Browse files- routes/settings_routes.py +14 -3
- web_app.py +5 -0
routes/settings_routes.py
CHANGED
|
@@ -113,7 +113,10 @@ def google_login():
|
|
| 113 |
flash("Google Client ID または Client Secret が設定されていません。", "error")
|
| 114 |
return redirect(url_for("settings.index"))
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
| 117 |
flow = get_oauth_credentials(client_id, client_secret, redirect_uri)
|
| 118 |
authorization_url, state = flow.authorization_url(
|
| 119 |
access_type='offline',
|
|
@@ -132,11 +135,19 @@ def google_callback():
|
|
| 132 |
return redirect(url_for("settings.index"))
|
| 133 |
|
| 134 |
client_id, client_secret = _get_google_client_credentials()
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
flow = get_oauth_credentials(client_id, client_secret, redirect_uri)
|
| 138 |
try:
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
creds = flow.credentials
|
| 141 |
repo.save_google_token(creds.to_json())
|
| 142 |
session.pop('google_oauth_state', None)
|
|
|
|
| 113 |
flash("Google Client ID または Client Secret が設定されていません。", "error")
|
| 114 |
return redirect(url_for("settings.index"))
|
| 115 |
|
| 116 |
+
is_dev = os.environ.get("FLASK_ENV") == "development" or os.environ.get("FLASK_DEBUG") == "1"
|
| 117 |
+
scheme = "http" if is_dev else "https"
|
| 118 |
+
redirect_uri = url_for("settings.google_callback", _external=True, _scheme=scheme)
|
| 119 |
+
|
| 120 |
flow = get_oauth_credentials(client_id, client_secret, redirect_uri)
|
| 121 |
authorization_url, state = flow.authorization_url(
|
| 122 |
access_type='offline',
|
|
|
|
| 135 |
return redirect(url_for("settings.index"))
|
| 136 |
|
| 137 |
client_id, client_secret = _get_google_client_credentials()
|
| 138 |
+
is_dev = os.environ.get("FLASK_ENV") == "development" or os.environ.get("FLASK_DEBUG") == "1"
|
| 139 |
+
scheme = "http" if is_dev else "https"
|
| 140 |
+
redirect_uri = url_for("settings.google_callback", _external=True, _scheme=scheme)
|
| 141 |
|
| 142 |
flow = get_oauth_credentials(client_id, client_secret, redirect_uri)
|
| 143 |
try:
|
| 144 |
+
# プロキシ環境により request.url が http で入ってくる場合があるため、
|
| 145 |
+
# oauthlib での insecure_transport エラーを回避するために https に強制置換する
|
| 146 |
+
auth_response_url = request.url
|
| 147 |
+
if not is_dev and auth_response_url.startswith("http://"):
|
| 148 |
+
auth_response_url = auth_response_url.replace("http://", "https://", 1)
|
| 149 |
+
|
| 150 |
+
flow.fetch_token(authorization_response=auth_response_url)
|
| 151 |
creds = flow.credentials
|
| 152 |
repo.save_google_token(creds.to_json())
|
| 153 |
session.pop('google_oauth_state', None)
|
web_app.py
CHANGED
|
@@ -14,6 +14,11 @@ from auth import APP_PASSWORD, SESSION_VERSION
|
|
| 14 |
|
| 15 |
def create_app():
|
| 16 |
app = Flask(__name__, template_folder="templates", static_folder="static")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
app.secret_key = os.environ.get("SECRET_KEY", "dev-secret-key-change-in-prod")
|
| 18 |
Compress(app)
|
| 19 |
cache.init_app(app, config={"CACHE_TYPE": "SimpleCache", "CACHE_DEFAULT_TIMEOUT": 180})
|
|
|
|
| 14 |
|
| 15 |
def create_app():
|
| 16 |
app = Flask(__name__, template_folder="templates", static_folder="static")
|
| 17 |
+
|
| 18 |
+
# リバースプロキシ背後でのプロキシヘッダー(X-Forwarded-Protoなど)を正しく認識させる
|
| 19 |
+
from werkzeug.middleware.proxy_fix import ProxyFix
|
| 20 |
+
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
|
| 21 |
+
|
| 22 |
app.secret_key = os.environ.get("SECRET_KEY", "dev-secret-key-change-in-prod")
|
| 23 |
Compress(app)
|
| 24 |
cache.init_app(app, config={"CACHE_TYPE": "SimpleCache", "CACHE_DEFAULT_TIMEOUT": 180})
|