Commit ·
93c7d2b
1
Parent(s): ac12bf9
Add shared password authentication
Browse files- auth.py: APP_PASSWORD env var + login_required decorator
- /login, /logout routes with Flask session
- before_request guard on all routes (bypasses login + static)
- session.permanent = True, 30-day lifetime
- Login page: dark themed, password field, error flash
- Logout link in desktop sidebar and hamburger drawer
- No password set = open access (dev mode)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- auth.py +17 -0
- routes/auth_routes.py +29 -0
- templates/auth/login.html +59 -0
- templates/base.html +17 -1
- web_app.py +20 -1
auth.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""共有パスワード認証モジュール"""
|
| 2 |
+
import os
|
| 3 |
+
from functools import wraps
|
| 4 |
+
from flask import session, redirect, url_for, request
|
| 5 |
+
|
| 6 |
+
APP_PASSWORD = os.environ.get("APP_PASSWORD", "")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def login_required(f):
|
| 10 |
+
@wraps(f)
|
| 11 |
+
def decorated(*args, **kwargs):
|
| 12 |
+
if not APP_PASSWORD:
|
| 13 |
+
return f(*args, **kwargs) # パスワード未設定なら認証スキップ
|
| 14 |
+
if not session.get("authenticated"):
|
| 15 |
+
return redirect(url_for("auth.login", next=request.path))
|
| 16 |
+
return f(*args, **kwargs)
|
| 17 |
+
return decorated
|
routes/auth_routes.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Blueprint, render_template, request, redirect, url_for, session, flash
|
| 2 |
+
from auth import APP_PASSWORD
|
| 3 |
+
|
| 4 |
+
bp = Blueprint("auth", __name__)
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@bp.get("/login")
|
| 8 |
+
def login():
|
| 9 |
+
if session.get("authenticated"):
|
| 10 |
+
return redirect(url_for("employees.index"))
|
| 11 |
+
return render_template("auth/login.html")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@bp.post("/login")
|
| 15 |
+
def login_post():
|
| 16 |
+
pwd = request.form.get("password", "")
|
| 17 |
+
next_url = request.args.get("next") or url_for("employees.index")
|
| 18 |
+
if APP_PASSWORD and pwd == APP_PASSWORD:
|
| 19 |
+
session["authenticated"] = True
|
| 20 |
+
session.permanent = True
|
| 21 |
+
return redirect(next_url)
|
| 22 |
+
flash("パスワードが違います", "error")
|
| 23 |
+
return render_template("auth/login.html", next=next_url)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@bp.get("/logout")
|
| 27 |
+
def logout():
|
| 28 |
+
session.clear()
|
| 29 |
+
return redirect(url_for("auth.login"))
|
templates/auth/login.html
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="ja">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
| 6 |
+
<meta name="theme-color" content="#1e293b">
|
| 7 |
+
<title>ログイン - SDU-Shift</title>
|
| 8 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
|
| 9 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
|
| 10 |
+
<style>
|
| 11 |
+
body { background: #0f172a; min-height: 100vh;
|
| 12 |
+
display: flex; align-items: center; justify-content: center; }
|
| 13 |
+
.login-card { width: 100%; max-width: 360px; border-radius: 16px;
|
| 14 |
+
background: #1e293b; color: white; padding: 2rem; }
|
| 15 |
+
.login-card .logo { font-size: 2.5rem; margin-bottom: .5rem; }
|
| 16 |
+
.login-card h1 { font-size: 1.4rem; font-weight: 700; margin-bottom: .25rem; }
|
| 17 |
+
.login-card p { color: #94a3b8; font-size: .85rem; margin-bottom: 1.5rem; }
|
| 18 |
+
.form-control {
|
| 19 |
+
background: #0f172a; border-color: #334155; color: white;
|
| 20 |
+
border-radius: 10px; padding: .75rem 1rem;
|
| 21 |
+
}
|
| 22 |
+
.form-control:focus { background: #0f172a; color: white;
|
| 23 |
+
border-color: #38bdf8; box-shadow: 0 0 0 3px rgba(56,189,248,.2); }
|
| 24 |
+
.btn-login {
|
| 25 |
+
background: #0ea5e9; border: none; border-radius: 10px;
|
| 26 |
+
padding: .75rem; font-weight: 600; width: 100%; font-size: 1rem;
|
| 27 |
+
}
|
| 28 |
+
.btn-login:hover { background: #0284c7; }
|
| 29 |
+
.alert { border-radius: 10px; font-size: .85rem; }
|
| 30 |
+
</style>
|
| 31 |
+
</head>
|
| 32 |
+
<body>
|
| 33 |
+
<div class="login-card">
|
| 34 |
+
<div class="logo text-center">🍽️</div>
|
| 35 |
+
<h1 class="text-center">SDU-Shift</h1>
|
| 36 |
+
<p class="text-center">飲食店向けシフト管理</p>
|
| 37 |
+
|
| 38 |
+
{% with msgs = get_flashed_messages(with_categories=true) %}
|
| 39 |
+
{% for cat, msg in msgs %}
|
| 40 |
+
<div class="alert alert-danger py-2 mb-3">
|
| 41 |
+
<i class="bi bi-exclamation-circle me-1"></i>{{ msg }}
|
| 42 |
+
</div>
|
| 43 |
+
{% endfor %}
|
| 44 |
+
{% endwith %}
|
| 45 |
+
|
| 46 |
+
<form method="post" action="{{ url_for('auth.login_post') }}{% if request.args.get('next') %}?next={{ request.args.get('next') }}{% endif %}">
|
| 47 |
+
<div class="mb-3">
|
| 48 |
+
<label class="form-label small text-muted">アクセスパスワード</label>
|
| 49 |
+
<input type="password" name="password" class="form-control"
|
| 50 |
+
placeholder="パスワードを入力" autofocus autocomplete="current-password">
|
| 51 |
+
</div>
|
| 52 |
+
<button type="submit" class="btn btn-login text-white">
|
| 53 |
+
<i class="bi bi-box-arrow-in-right me-1"></i>ログイン
|
| 54 |
+
</button>
|
| 55 |
+
</form>
|
| 56 |
+
</div>
|
| 57 |
+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
| 58 |
+
</body>
|
| 59 |
+
</html>
|
templates/base.html
CHANGED
|
@@ -108,8 +108,13 @@
|
|
| 108 |
}
|
| 109 |
.sidebar .brand {
|
| 110 |
color: white; font-weight: 700; font-size: 1.1rem;
|
| 111 |
-
padding: .5rem 1.25rem
|
| 112 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
.sidebar a {
|
| 114 |
display: flex; align-items: center; gap: .75rem;
|
| 115 |
color: #94a3b8; text-decoration: none; padding: .6rem 1.25rem;
|
|
@@ -140,6 +145,9 @@
|
|
| 140 |
<i class="bi {{ icon }}"></i>{{ label }}
|
| 141 |
</a>
|
| 142 |
{% endfor %}
|
|
|
|
|
|
|
|
|
|
| 143 |
</nav>
|
| 144 |
|
| 145 |
<!-- Mobile Bottom Nav -->
|
|
@@ -188,6 +196,14 @@
|
|
| 188 |
</a>
|
| 189 |
{% endfor %}
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
<button class="drawer-close" onclick="closeDrawer()">
|
| 192 |
<i class="bi bi-x-lg me-2"></i>閉じる
|
| 193 |
</button>
|
|
|
|
| 108 |
}
|
| 109 |
.sidebar .brand {
|
| 110 |
color: white; font-weight: 700; font-size: 1.1rem;
|
| 111 |
+
padding: .5rem 1.25rem 1rem; border-bottom: 1px solid #334155;
|
| 112 |
}
|
| 113 |
+
.sidebar .logout-btn {
|
| 114 |
+
margin-top: auto; padding: .6rem 1.25rem;
|
| 115 |
+
color: #64748b; font-size: .85rem; text-decoration: none; display: flex; align-items: center; gap: .5rem;
|
| 116 |
+
}
|
| 117 |
+
.sidebar .logout-btn:hover { color: #f87171; }
|
| 118 |
.sidebar a {
|
| 119 |
display: flex; align-items: center; gap: .75rem;
|
| 120 |
color: #94a3b8; text-decoration: none; padding: .6rem 1.25rem;
|
|
|
|
| 145 |
<i class="bi {{ icon }}"></i>{{ label }}
|
| 146 |
</a>
|
| 147 |
{% endfor %}
|
| 148 |
+
<a href="{{ url_for('auth.logout') }}" class="logout-btn">
|
| 149 |
+
<i class="bi bi-box-arrow-right"></i>ログアウト
|
| 150 |
+
</a>
|
| 151 |
</nav>
|
| 152 |
|
| 153 |
<!-- Mobile Bottom Nav -->
|
|
|
|
| 196 |
</a>
|
| 197 |
{% endfor %}
|
| 198 |
|
| 199 |
+
<a href="{{ url_for('auth.logout') }}"
|
| 200 |
+
class="drawer a" style="color:#f87171;border-top:1px solid #1e3a5f44">
|
| 201 |
+
<i class="bi bi-box-arrow-right" style="color:#f87171"></i>
|
| 202 |
+
<div>
|
| 203 |
+
<div style="color:#f87171">ログアウト</div>
|
| 204 |
+
<div style="font-size:.75rem;color:#64748b;font-weight:400">アクセスを終了する</div>
|
| 205 |
+
</div>
|
| 206 |
+
</a>
|
| 207 |
<button class="drawer-close" onclick="closeDrawer()">
|
| 208 |
<i class="bi bi-x-lg me-2"></i>閉じる
|
| 209 |
</button>
|
web_app.py
CHANGED
|
@@ -3,10 +3,12 @@ import sys
|
|
| 3 |
import os
|
| 4 |
sys.path.insert(0, os.path.dirname(__file__))
|
| 5 |
|
| 6 |
-
from
|
|
|
|
| 7 |
from flask_compress import Compress
|
| 8 |
from cache import cache
|
| 9 |
from db.database import initialize_db
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
def create_app():
|
|
@@ -29,6 +31,23 @@ def create_app():
|
|
| 29 |
except Exception:
|
| 30 |
pass
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
from routes.employees import bp as emp_bp
|
| 33 |
from routes.shifts import bp as shifts_bp
|
| 34 |
from routes.generate import bp as gen_bp
|
|
|
|
| 3 |
import os
|
| 4 |
sys.path.insert(0, os.path.dirname(__file__))
|
| 5 |
|
| 6 |
+
from datetime import timedelta
|
| 7 |
+
from flask import Flask, g, session, redirect, url_for, request
|
| 8 |
from flask_compress import Compress
|
| 9 |
from cache import cache
|
| 10 |
from db.database import initialize_db
|
| 11 |
+
from auth import APP_PASSWORD
|
| 12 |
|
| 13 |
|
| 14 |
def create_app():
|
|
|
|
| 31 |
except Exception:
|
| 32 |
pass
|
| 33 |
|
| 34 |
+
from routes.auth_routes import bp as auth_bp
|
| 35 |
+
app.register_blueprint(auth_bp)
|
| 36 |
+
|
| 37 |
+
# セッション有効期限:30日
|
| 38 |
+
app.permanent_session_lifetime = timedelta(days=30)
|
| 39 |
+
|
| 40 |
+
# 認証チェック(ログイン・静的ファイルは除外)
|
| 41 |
+
@app.before_request
|
| 42 |
+
def require_login():
|
| 43 |
+
if not APP_PASSWORD:
|
| 44 |
+
return # パスワード未設定なら全開放
|
| 45 |
+
exempt = {"auth.login", "auth.login_post", "static"}
|
| 46 |
+
if request.endpoint in exempt:
|
| 47 |
+
return
|
| 48 |
+
if not session.get("authenticated"):
|
| 49 |
+
return redirect(url_for("auth.login", next=request.path))
|
| 50 |
+
|
| 51 |
from routes.employees import bp as emp_bp
|
| 52 |
from routes.shifts import bp as shifts_bp
|
| 53 |
from routes.generate import bp as gen_bp
|