askrobots / dbbasic-accounts
Public
Unix-style user accounts (passwd, groups, permissions) with a web-friendly Python API.
flask_example.py · 13.8 KB · 398 lines
| 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Complete Flask app with dbbasic-passwd authentication |
| 4 | |
| 5 | Replaces Flask-Login + SQLAlchemy with simple TSV files. |
| 6 | """ |
| 7 | |
| 8 | from flask import Flask, request, session, redirect, url_for, render_template_string |
| 9 | from functools import wraps |
| 10 | from dbbasic_passwd import PasswdDB |
| 11 | |
| 12 | app = Flask(__name__) |
| 13 | app.secret_key = 'change-this-in-production' # Use secrets.token_hex(16) in production |
| 14 | |
| 15 | # Initialize auth database |
| 16 | passwd = PasswdDB('./etc') |
| 17 | |
| 18 | # Create demo users if database is empty |
| 19 | if not passwd.list_users(): |
| 20 | passwd.useradd('admin', password='admin123', fullname='Admin User', groups=['admins']) |
| 21 | passwd.useradd('editor', password='editor123', fullname='Editor User', groups=['editors']) |
| 22 | passwd.useradd('user', password='user123', fullname='Regular User') |
| 23 | print("Created demo users: admin/admin123, editor/editor123, user/user123") |
| 24 | |
| 25 | |
| 26 | # Auth decorators |
| 27 | def login_required(f): |
| 28 | """Require user to be logged in""" |
| 29 | @wraps(f) |
| 30 | def decorated_function(*args, **kwargs): |
| 31 | if 'username' not in session: |
| 32 | return redirect(url_for('login')) |
| 33 | return f(*args, **kwargs) |
| 34 | return decorated_function |
| 35 | |
| 36 | |
| 37 | def admin_required(f): |
| 38 | """Require user to be in 'admins' group""" |
| 39 | @wraps(f) |
| 40 | def decorated_function(*args, **kwargs): |
| 41 | if 'username' not in session: |
| 42 | return redirect(url_for('login')) |
| 43 | |
| 44 | groups = passwd.groups(session['username']) |
| 45 | if 'admins' not in groups: |
| 46 | return 'Forbidden: Admin access required', 403 |
| 47 | |
| 48 | return f(*args, **kwargs) |
| 49 | return decorated_function |
| 50 | |
| 51 | |
| 52 | def get_current_user(): |
| 53 | """Get current logged-in user""" |
| 54 | if 'username' in session: |
| 55 | return passwd.getuser(session['username']) |
| 56 | return None |
| 57 | |
| 58 | |
| 59 | # Routes |
| 60 | @app.route('/') |
| 61 | def index(): |
| 62 | user = get_current_user() |
| 63 | return render_template_string(''' |
| 64 | <!DOCTYPE html> |
| 65 | <html> |
| 66 | <head> |
| 67 | <title>dbbasic-passwd Flask Demo</title> |
| 68 | <style> |
| 69 | body { font-family: sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; } |
| 70 | .user-info { background: #f0f0f0; padding: 20px; border-radius: 5px; margin-bottom: 20px; } |
| 71 | .links { margin-top: 20px; } |
| 72 | .links a { display: inline-block; margin-right: 15px; padding: 10px 15px; |
| 73 | background: #007bff; color: white; text-decoration: none; border-radius: 3px; } |
| 74 | .links a:hover { background: #0056b3; } |
| 75 | .logout { background: #dc3545 !important; } |
| 76 | .logout:hover { background: #c82333 !important; } |
| 77 | </style> |
| 78 | </head> |
| 79 | <body> |
| 80 | <h1>dbbasic-passwd Flask Demo</h1> |
| 81 | |
| 82 | {% if user %} |
| 83 | <div class="user-info"> |
| 84 | <h2>Logged in as {{ user.fullname }}</h2> |
| 85 | <p><strong>Username:</strong> {{ user.username }}</p> |
| 86 | <p><strong>UID:</strong> {{ user.uid }}</p> |
| 87 | <p><strong>Groups:</strong> {{ groups|join(', ') }}</p> |
| 88 | </div> |
| 89 | |
| 90 | <div class="links"> |
| 91 | <a href="{{ url_for('dashboard') }}">Dashboard</a> |
| 92 | <a href="{{ url_for('profile') }}">Profile</a> |
| 93 | {% if 'admins' in groups %} |
| 94 | <a href="{{ url_for('admin') }}">Admin Panel</a> |
| 95 | {% endif %} |
| 96 | <a href="{{ url_for('logout') }}" class="logout">Logout</a> |
| 97 | </div> |
| 98 | {% else %} |
| 99 | <p>Welcome! Please <a href="{{ url_for('login') }}">login</a> to continue.</p> |
| 100 | |
| 101 | <h3>Demo accounts:</h3> |
| 102 | <ul> |
| 103 | <li><strong>admin</strong> / admin123 (has admin access)</li> |
| 104 | <li><strong>editor</strong> / editor123 (has editor access)</li> |
| 105 | <li><strong>user</strong> / user123 (regular user)</li> |
| 106 | </ul> |
| 107 | {% endif %} |
| 108 | </body> |
| 109 | </html> |
| 110 | ''', user=user, groups=passwd.groups(user.username) if user else []) |
| 111 | |
| 112 | |
| 113 | @app.route('/login', methods=['GET', 'POST']) |
| 114 | def login(): |
| 115 | if request.method == 'POST': |
| 116 | username = request.form.get('username') |
| 117 | password = request.form.get('password') |
| 118 | |
| 119 | # Authenticate with dbbasic-passwd |
| 120 | user = passwd.authenticate(username, password) |
| 121 | |
| 122 | if user: |
| 123 | # Login successful - store in session |
| 124 | session['username'] = user.username |
| 125 | session['uid'] = user.uid |
| 126 | return redirect(url_for('index')) |
| 127 | else: |
| 128 | error = 'Invalid username or password' |
| 129 | return render_template_string(LOGIN_TEMPLATE, error=error) |
| 130 | |
| 131 | return render_template_string(LOGIN_TEMPLATE, error=None) |
| 132 | |
| 133 | |
| 134 | @app.route('/logout') |
| 135 | def logout(): |
| 136 | session.clear() |
| 137 | return redirect(url_for('index')) |
| 138 | |
| 139 | |
| 140 | @app.route('/dashboard') |
| 141 | @login_required |
| 142 | def dashboard(): |
| 143 | user = get_current_user() |
| 144 | groups = passwd.groups(user.username) |
| 145 | |
| 146 | return render_template_string(''' |
| 147 | <!DOCTYPE html> |
| 148 | <html> |
| 149 | <head> |
| 150 | <title>Dashboard</title> |
| 151 | <style> |
| 152 | body { font-family: sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; } |
| 153 | .card { background: #f8f9fa; padding: 20px; border-radius: 5px; margin-bottom: 20px; } |
| 154 | </style> |
| 155 | </head> |
| 156 | <body> |
| 157 | <h1>Dashboard</h1> |
| 158 | |
| 159 | <div class="card"> |
| 160 | <h2>Welcome, {{ user.fullname }}!</h2> |
| 161 | <p>This is your dashboard. Only logged-in users can see this page.</p> |
| 162 | <p><strong>Your groups:</strong> {{ groups|join(', ') }}</p> |
| 163 | </div> |
| 164 | |
| 165 | <p><a href="{{ url_for('index') }}">← Back to home</a></p> |
| 166 | </body> |
| 167 | </html> |
| 168 | ''', user=user, groups=groups) |
| 169 | |
| 170 | |
| 171 | @app.route('/profile') |
| 172 | @login_required |
| 173 | def profile(): |
| 174 | user = get_current_user() |
| 175 | groups = passwd.groups(user.username) |
| 176 | |
| 177 | return render_template_string(''' |
| 178 | <!DOCTYPE html> |
| 179 | <html> |
| 180 | <head> |
| 181 | <title>Profile</title> |
| 182 | <style> |
| 183 | body { font-family: sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; } |
| 184 | table { width: 100%; border-collapse: collapse; } |
| 185 | td { padding: 10px; border-bottom: 1px solid #ddd; } |
| 186 | td:first-child { font-weight: bold; width: 200px; } |
| 187 | </style> |
| 188 | </head> |
| 189 | <body> |
| 190 | <h1>User Profile</h1> |
| 191 | |
| 192 | <table> |
| 193 | <tr><td>Username</td><td>{{ user.username }}</td></tr> |
| 194 | <tr><td>Full Name</td><td>{{ user.fullname }}</td></tr> |
| 195 | <tr><td>UID</td><td>{{ user.uid }}</td></tr> |
| 196 | <tr><td>GID</td><td>{{ user.gid }}</td></tr> |
| 197 | <tr><td>Home Directory</td><td>{{ user.homedir }}</td></tr> |
| 198 | <tr><td>Shell</td><td>{{ user.shell }}</td></tr> |
| 199 | <tr><td>Groups</td><td>{{ groups|join(', ') }}</td></tr> |
| 200 | <tr><td>Created</td><td>{{ user.created }}</td></tr> |
| 201 | </table> |
| 202 | |
| 203 | <p><a href="{{ url_for('change_password') }}">Change Password</a></p> |
| 204 | <p><a href="{{ url_for('index') }}">← Back to home</a></p> |
| 205 | </body> |
| 206 | </html> |
| 207 | ''', user=user, groups=groups) |
| 208 | |
| 209 | |
| 210 | @app.route('/change-password', methods=['GET', 'POST']) |
| 211 | @login_required |
| 212 | def change_password(): |
| 213 | user = get_current_user() |
| 214 | |
| 215 | if request.method == 'POST': |
| 216 | current_password = request.form.get('current_password') |
| 217 | new_password = request.form.get('new_password') |
| 218 | confirm_password = request.form.get('confirm_password') |
| 219 | |
| 220 | # Verify current password |
| 221 | if not passwd.authenticate(user.username, current_password): |
| 222 | error = 'Current password is incorrect' |
| 223 | elif new_password != confirm_password: |
| 224 | error = 'New passwords do not match' |
| 225 | elif len(new_password) < 6: |
| 226 | error = 'Password must be at least 6 characters' |
| 227 | else: |
| 228 | # Change password |
| 229 | passwd.passwd(user.username, new_password) |
| 230 | return render_template_string(''' |
| 231 | <!DOCTYPE html> |
| 232 | <html> |
| 233 | <head><title>Password Changed</title></head> |
| 234 | <body style="font-family: sans-serif; max-width: 800px; margin: 50px auto; padding: 20px;"> |
| 235 | <h1>Password Changed</h1> |
| 236 | <p>Your password has been changed successfully.</p> |
| 237 | <p><a href="{{ url_for('profile') }}">← Back to profile</a></p> |
| 238 | </body> |
| 239 | </html> |
| 240 | ''') |
| 241 | |
| 242 | return render_template_string(CHANGE_PASSWORD_TEMPLATE, error=error) |
| 243 | |
| 244 | return render_template_string(CHANGE_PASSWORD_TEMPLATE, error=None) |
| 245 | |
| 246 | |
| 247 | @app.route('/admin') |
| 248 | @admin_required |
| 249 | def admin(): |
| 250 | users = passwd.list_users() |
| 251 | groups = passwd.list_groups() |
| 252 | |
| 253 | return render_template_string(''' |
| 254 | <!DOCTYPE html> |
| 255 | <html> |
| 256 | <head> |
| 257 | <title>Admin Panel</title> |
| 258 | <style> |
| 259 | body { font-family: sans-serif; max-width: 1000px; margin: 50px auto; padding: 20px; } |
| 260 | table { width: 100%; border-collapse: collapse; margin-bottom: 30px; } |
| 261 | th, td { padding: 10px; border: 1px solid #ddd; text-align: left; } |
| 262 | th { background: #007bff; color: white; } |
| 263 | .danger { background: #dc3545; color: white; padding: 5px 10px; |
| 264 | text-decoration: none; border-radius: 3px; } |
| 265 | </style> |
| 266 | </head> |
| 267 | <body> |
| 268 | <h1>Admin Panel</h1> |
| 269 | <p><em>Only users in 'admins' group can access this page.</em></p> |
| 270 | |
| 271 | <h2>Users</h2> |
| 272 | <table> |
| 273 | <tr> |
| 274 | <th>Username</th> |
| 275 | <th>Full Name</th> |
| 276 | <th>UID</th> |
| 277 | <th>Groups</th> |
| 278 | <th>Created</th> |
| 279 | </tr> |
| 280 | {% for user in users %} |
| 281 | <tr> |
| 282 | <td>{{ user.username }}</td> |
| 283 | <td>{{ user.fullname }}</td> |
| 284 | <td>{{ user.uid }}</td> |
| 285 | <td>{{ user_groups[user.username]|join(', ') }}</td> |
| 286 | <td>{{ user.created }}</td> |
| 287 | </tr> |
| 288 | {% endfor %} |
| 289 | </table> |
| 290 | |
| 291 | <h2>Groups</h2> |
| 292 | <table> |
| 293 | <tr> |
| 294 | <th>Group Name</th> |
| 295 | <th>GID</th> |
| 296 | <th>Members</th> |
| 297 | </tr> |
| 298 | {% for group in groups %} |
| 299 | <tr> |
| 300 | <td>{{ group.groupname }}</td> |
| 301 | <td>{{ group.gid }}</td> |
| 302 | <td>{{ group.members|join(', ') if group.members else '(empty)' }}</td> |
| 303 | </tr> |
| 304 | {% endfor %} |
| 305 | </table> |
| 306 | |
| 307 | <p><a href="{{ url_for('index') }}">← Back to home</a></p> |
| 308 | </body> |
| 309 | </html> |
| 310 | ''', users=users, groups=groups, |
| 311 | user_groups={u.username: passwd.groups(u.username) for u in users}) |
| 312 | |
| 313 | |
| 314 | # HTML Templates |
| 315 | LOGIN_TEMPLATE = ''' |
| 316 | <!DOCTYPE html> |
| 317 | <html> |
| 318 | <head> |
| 319 | <title>Login</title> |
| 320 | <style> |
| 321 | body { font-family: sans-serif; max-width: 400px; margin: 100px auto; padding: 20px; } |
| 322 | form { background: #f8f9fa; padding: 30px; border-radius: 5px; } |
| 323 | input { width: 100%; padding: 10px; margin: 10px 0; box-sizing: border-box; } |
| 324 | button { width: 100%; padding: 12px; background: #007bff; color: white; |
| 325 | border: none; border-radius: 3px; cursor: pointer; font-size: 16px; } |
| 326 | button:hover { background: #0056b3; } |
| 327 | .error { color: #dc3545; padding: 10px; background: #f8d7da; border-radius: 3px; margin-bottom: 15px; } |
| 328 | </style> |
| 329 | </head> |
| 330 | <body> |
| 331 | <h1>Login</h1> |
| 332 | |
| 333 | {% if error %} |
| 334 | <div class="error">{{ error }}</div> |
| 335 | {% endif %} |
| 336 | |
| 337 | <form method="POST"> |
| 338 | <input type="text" name="username" placeholder="Username" required autofocus> |
| 339 | <input type="password" name="password" placeholder="Password" required> |
| 340 | <button type="submit">Login</button> |
| 341 | </form> |
| 342 | |
| 343 | <p style="text-align: center; margin-top: 20px;"> |
| 344 | <a href="{{ url_for('index') }}">← Back to home</a> |
| 345 | </p> |
| 346 | </body> |
| 347 | </html> |
| 348 | ''' |
| 349 | |
| 350 | CHANGE_PASSWORD_TEMPLATE = ''' |
| 351 | <!DOCTYPE html> |
| 352 | <html> |
| 353 | <head> |
| 354 | <title>Change Password</title> |
| 355 | <style> |
| 356 | body { font-family: sans-serif; max-width: 400px; margin: 100px auto; padding: 20px; } |
| 357 | form { background: #f8f9fa; padding: 30px; border-radius: 5px; } |
| 358 | input { width: 100%; padding: 10px; margin: 10px 0; box-sizing: border-box; } |
| 359 | button { width: 100%; padding: 12px; background: #007bff; color: white; |
| 360 | border: none; border-radius: 3px; cursor: pointer; font-size: 16px; } |
| 361 | button:hover { background: #0056b3; } |
| 362 | .error { color: #dc3545; padding: 10px; background: #f8d7da; border-radius: 3px; margin-bottom: 15px; } |
| 363 | </style> |
| 364 | </head> |
| 365 | <body> |
| 366 | <h1>Change Password</h1> |
| 367 | |
| 368 | {% if error %} |
| 369 | <div class="error">{{ error }}</div> |
| 370 | {% endif %} |
| 371 | |
| 372 | <form method="POST"> |
| 373 | <input type="password" name="current_password" placeholder="Current Password" required autofocus> |
| 374 | <input type="password" name="new_password" placeholder="New Password" required> |
| 375 | <input type="password" name="confirm_password" placeholder="Confirm New Password" required> |
| 376 | <button type="submit">Change Password</button> |
| 377 | </form> |
| 378 | |
| 379 | <p style="text-align: center; margin-top: 20px;"> |
| 380 | <a href="{{ url_for('profile') }}">← Cancel</a> |
| 381 | </p> |
| 382 | </body> |
| 383 | </html> |
| 384 | ''' |
| 385 | |
| 386 | |
| 387 | if __name__ == '__main__': |
| 388 | print("\n" + "="*60) |
| 389 | print("Flask app with dbbasic-passwd authentication") |
| 390 | print("="*60) |
| 391 | print("\nDemo users:") |
| 392 | print(" - admin/admin123 (has admin access)") |
| 393 | print(" - editor/editor123 (has editor access)") |
| 394 | print(" - user/user123 (regular user)") |
| 395 | print("\nRunning on http://127.0.0.1:5000") |
| 396 | print("="*60 + "\n") |
| 397 | |
| 398 | app.run(debug=True, port=5000) |