devadmin 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- devadmin/__init__.py +19 -0
- devadmin/__main__.py +5 -0
- devadmin/adapters/__init__.py +0 -0
- devadmin/adapters/base.py +175 -0
- devadmin/adapters/tortoise/__init__.py +81 -0
- devadmin/adapters/tortoise/introspect.py +209 -0
- devadmin/adapters/tortoise/models.py +34 -0
- devadmin/adapters/tortoise/queries.py +330 -0
- devadmin/adapters/tortoise/related.py +215 -0
- devadmin/adapters/tortoise/store.py +156 -0
- devadmin/auth/__init__.py +16 -0
- devadmin/auth/csrf.py +18 -0
- devadmin/auth/ip.py +28 -0
- devadmin/auth/login.py +149 -0
- devadmin/auth/session.py +94 -0
- devadmin/cli.py +314 -0
- devadmin/config.py +34 -0
- devadmin/core/__init__.py +0 -0
- devadmin/core/binding.py +609 -0
- devadmin/core/forms.py +475 -0
- devadmin/core/listing.py +400 -0
- devadmin/core/options.py +99 -0
- devadmin/core/paths.py +39 -0
- devadmin/core/site.py +288 -0
- devadmin/core/values.py +122 -0
- devadmin/history/__init__.py +38 -0
- devadmin/history/diff.py +109 -0
- devadmin/history/encoding.py +120 -0
- devadmin/history/records.py +40 -0
- devadmin/history/render.py +129 -0
- devadmin/history/service.py +256 -0
- devadmin/web/__init__.py +0 -0
- devadmin/web/app.py +106 -0
- devadmin/web/deps.py +275 -0
- devadmin/web/guard.py +178 -0
- devadmin/web/static/autocomplete.js +231 -0
- devadmin/web/static/devadmin.css +1645 -0
- devadmin/web/static/devadmin.js +129 -0
- devadmin/web/static/htmx.min.js +1 -0
- devadmin/web/static/icons/check.svg +1 -0
- devadmin/web/static/icons/clock.svg +1 -0
- devadmin/web/static/icons/cross.svg +1 -0
- devadmin/web/static/icons/down.svg +1 -0
- devadmin/web/static/icons/eye.svg +1 -0
- devadmin/web/static/icons/lock.svg +1 -0
- devadmin/web/static/icons/pencil.svg +1 -0
- devadmin/web/static/icons/plus.svg +1 -0
- devadmin/web/static/icons/right.svg +1 -0
- devadmin/web/static/icons/trash.svg +1 -0
- devadmin/web/static/icons/up.svg +1 -0
- devadmin/web/templates/_field.html +35 -0
- devadmin/web/templates/_history.html +19 -0
- devadmin/web/templates/_pagination.html +8 -0
- devadmin/web/templates/action_confirm.html +26 -0
- devadmin/web/templates/base.html +63 -0
- devadmin/web/templates/delete.html +62 -0
- devadmin/web/templates/error.html +9 -0
- devadmin/web/templates/form.html +34 -0
- devadmin/web/templates/history.html +59 -0
- devadmin/web/templates/index.html +49 -0
- devadmin/web/templates/list.html +88 -0
- devadmin/web/templates/login.html +15 -0
- devadmin/web/templates/recent.html +25 -0
- devadmin/web/views_auth.py +146 -0
- devadmin/web/views_delete.py +774 -0
- devadmin/web/views_form.py +551 -0
- devadmin/web/views_history.py +261 -0
- devadmin/web/views_list.py +246 -0
- devadmin-0.1.0.dist-info/METADATA +702 -0
- devadmin-0.1.0.dist-info/RECORD +73 -0
- devadmin-0.1.0.dist-info/WHEEL +4 -0
- devadmin-0.1.0.dist-info/entry_points.txt +3 -0
- devadmin-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{% extends "base.html" %}
|
|
2
|
+
{% import "_history.html" as history %}
|
|
3
|
+
{% macro shown(value) -%}
|
|
4
|
+
{%- if value.more -%}
|
|
5
|
+
<span class="value">{{ value.head }}…</span><details class="value-more"><summary>show all</summary><span class="value">{{ value.full }}</span></details>
|
|
6
|
+
{%- else -%}
|
|
7
|
+
<span class="value">{{ value.full }}</span>
|
|
8
|
+
{%- endif -%}
|
|
9
|
+
{%- endmacro %}
|
|
10
|
+
{% macro field_label(row) -%}
|
|
11
|
+
{{ row.label }}{% if row.removed %} <span class="removed">(removed)</span>{% endif %}
|
|
12
|
+
{%- endmacro %}
|
|
13
|
+
{% block title %}History · {{ object_text }}{% endblock %}
|
|
14
|
+
{% block breadcrumbs %}<a href="{{ url() }}">Home</a> <span class="crumb-sep" aria-hidden="true"></span> <span>{{ admin.group }}</span> <span class="crumb-sep" aria-hidden="true"></span> <a href="{{ url('m', key, '') }}">{{ admin.label_plural }}</a> <span class="crumb-sep" aria-hidden="true"></span> {% if object_link %}<a href="{{ object_link }}">{{ object_text }}</a>{% else %}<span>{{ object_text }}</span>{% endif %} <span class="crumb-sep" aria-hidden="true"></span> <span>History</span>{% endblock %}
|
|
15
|
+
{% block content %}
|
|
16
|
+
<div class="page-head">
|
|
17
|
+
<h1>History of {{ admin.label|lower }} {{ object_text }}</h1>
|
|
18
|
+
</div>
|
|
19
|
+
<p class="object-status">
|
|
20
|
+
<span class="muted">Key: {{ object_key }}.</span>
|
|
21
|
+
{% if object_state == "loaded" %}<a href="{{ object_link }}">Open this {{ admin.label|lower }}</a>{% elif object_state == "gone" %}This object no longer exists.{% else %}This object could not be loaded.{% endif %}
|
|
22
|
+
</p>
|
|
23
|
+
{% for entry in entries %}
|
|
24
|
+
<article class="entry">
|
|
25
|
+
{{ history.entry_head(entry) }}
|
|
26
|
+
{% for change in entry.changes %}
|
|
27
|
+
{% if change.note %}<p class="entry-note">{{ change.note }}</p>{% endif %}
|
|
28
|
+
{% if change.rows %}
|
|
29
|
+
<div class="table-wrap">
|
|
30
|
+
{% if change.op == "delete" %}
|
|
31
|
+
<table class="diff snapshot">
|
|
32
|
+
<thead><tr><th scope="col">Field</th><th scope="col">Value before the delete</th></tr></thead>
|
|
33
|
+
<tbody>
|
|
34
|
+
{% for row in change.rows %}
|
|
35
|
+
<tr class="diff-row"><th scope="row">{{ field_label(row) }}</th>{% if row.sensitive %}<td class="sensitive">hidden</td>{% else %}<td>{{ shown(row.old) }}</td>{% endif %}</tr>
|
|
36
|
+
{% endfor %}
|
|
37
|
+
</tbody>
|
|
38
|
+
</table>
|
|
39
|
+
{% else %}
|
|
40
|
+
<table class="diff">
|
|
41
|
+
<thead><tr><th scope="col">Field</th><th scope="col">Old</th><th scope="col">New</th></tr></thead>
|
|
42
|
+
<tbody>
|
|
43
|
+
{% for row in change.rows %}
|
|
44
|
+
<tr class="diff-row"><th scope="row">{{ field_label(row) }}</th>{% if row.sensitive %}<td class="sensitive" colspan="2">changed</td>{% else %}<td class="old">{{ shown(row.old) }}</td><td class="new">{{ shown(row.new) }}</td>{% endif %}</tr>
|
|
45
|
+
{% endfor %}
|
|
46
|
+
</tbody>
|
|
47
|
+
</table>
|
|
48
|
+
{% endif %}
|
|
49
|
+
</div>
|
|
50
|
+
{% else %}
|
|
51
|
+
<p class="muted">No field values were recorded.</p>
|
|
52
|
+
{% endif %}
|
|
53
|
+
{% endfor %}
|
|
54
|
+
</article>
|
|
55
|
+
{% else %}
|
|
56
|
+
<p class="muted">{{ "No history recorded." if page == 1 else "No entries on this page." }}</p>
|
|
57
|
+
{% endfor %}
|
|
58
|
+
{{ history.pages(page, prev_link, next_link) }}
|
|
59
|
+
{% endblock %}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{% extends "base.html" %}
|
|
2
|
+
{% block title %}Home{% endblock %}
|
|
3
|
+
{% block breadcrumbs %}<span>Home</span>{% endblock %}
|
|
4
|
+
{% block content %}
|
|
5
|
+
<div class="page-head">
|
|
6
|
+
<h1>Site administration</h1>
|
|
7
|
+
</div>
|
|
8
|
+
<div class="dashboard">
|
|
9
|
+
<div class="index-groups">
|
|
10
|
+
{% for group, admins in index_groups %}
|
|
11
|
+
<section class="module">
|
|
12
|
+
<h2>{{ group or "Models" }}</h2>
|
|
13
|
+
<ul class="model-list">
|
|
14
|
+
{% for item, can_add in admins %}
|
|
15
|
+
<li>
|
|
16
|
+
<a class="model-name" href="{{ url('m', item.info.key, '') }}">{{ item.label_plural }}</a>
|
|
17
|
+
{% if can_add %}<a class="row-action add" href="{{ url('m', item.info.key, 'add') }}">Add</a>{% endif %}
|
|
18
|
+
{% if item.read_only %}<a class="row-action view" href="{{ url('m', item.info.key, '') }}">View</a>{% else %}<a class="row-action change" href="{{ url('m', item.info.key, '') }}">Change</a>{% endif %}
|
|
19
|
+
</li>
|
|
20
|
+
{% endfor %}
|
|
21
|
+
</ul>
|
|
22
|
+
</section>
|
|
23
|
+
{% else %}
|
|
24
|
+
<p class="muted">There are no models you can view.</p>
|
|
25
|
+
{% endfor %}
|
|
26
|
+
</div>
|
|
27
|
+
<aside class="module recent-module" aria-label="Recent actions">
|
|
28
|
+
<h2>Recent actions</h2>
|
|
29
|
+
{% if recent %}
|
|
30
|
+
<ol class="recent-list">
|
|
31
|
+
{% for entry in recent %}
|
|
32
|
+
<li class="recent-item kind-{{ entry.kind }}">
|
|
33
|
+
{% for line in entry.lines %}
|
|
34
|
+
<p class="recent-line op-{{ line.op }}">{% if line.link %}<a href="{{ line.link }}">{{ line.text }}</a>{% else %}<span class="object-key">{{ line.text }}</span>{% endif %} <span class="model">{{ line.model }}</span></p>
|
|
35
|
+
{% else %}
|
|
36
|
+
<p class="recent-line op-{{ entry.kind }}"><span>{{ entry.summary or entry.kind }}</span></p>
|
|
37
|
+
{% endfor %}
|
|
38
|
+
{% if entry.more %}<p class="recent-more">and {{ entry.more }} more</p>{% endif %}
|
|
39
|
+
<p class="recent-meta"><time datetime="{{ entry.iso }}">{{ entry.date }}</time> <span>{{ entry.actor }}</span></p>
|
|
40
|
+
</li>
|
|
41
|
+
{% endfor %}
|
|
42
|
+
</ol>
|
|
43
|
+
{% else %}
|
|
44
|
+
<p class="module-empty">Nothing has been changed through the panel yet.</p>
|
|
45
|
+
{% endif %}
|
|
46
|
+
<a class="module-foot" href="{{ url('history') }}">All actions</a>
|
|
47
|
+
</aside>
|
|
48
|
+
</div>
|
|
49
|
+
{% endblock %}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
{% extends "base.html" %}
|
|
2
|
+
{% macro cell_body(cell) -%}
|
|
3
|
+
{%- if cell.boolean is true -%}<span class="bool yes"><span class="visually-hidden">Yes</span></span>
|
|
4
|
+
{%- elif cell.boolean is false -%}<span class="bool no"><span class="visually-hidden">No</span></span>
|
|
5
|
+
{%- else -%}{{ cell.text }}{%- endif -%}
|
|
6
|
+
{%- endmacro %}
|
|
7
|
+
{% block title %}{{ admin.label_plural }}{% endblock %}
|
|
8
|
+
{% block breadcrumbs %}<a href="{{ url() }}">Home</a> <span class="crumb-sep" aria-hidden="true"></span> <span>{{ admin.group }}</span> <span class="crumb-sep" aria-hidden="true"></span> <span>{{ admin.label_plural }}</span>{% endblock %}
|
|
9
|
+
{% block content %}
|
|
10
|
+
<div class="page-head">
|
|
11
|
+
<h1>{{ admin.label_plural }}{% if admin.read_only %} <span class="badge">(read-only)</span>{% endif %}</h1>
|
|
12
|
+
{% if can_add %}<a class="button primary add" href="{{ url('m', key, 'add') }}">Add {{ admin.label|lower }}</a>{% endif %}
|
|
13
|
+
</div>
|
|
14
|
+
<div class="toolbar">
|
|
15
|
+
{% if admin.search_fields %}
|
|
16
|
+
<form class="search" method="get" action="{{ url('m', key, '') }}" role="search">
|
|
17
|
+
{% for name, value in links.hidden_for_search() %}<input type="hidden" name="{{ name }}" value="{{ value }}">{% endfor %}
|
|
18
|
+
<input type="search" name="q" value="{{ search }}" placeholder="Search by {{ search_hint }}" aria-label="Search {{ admin.label_plural|lower }}">
|
|
19
|
+
<button type="submit" class="button">Search</button>{% if search %} <a class="clear-search" href="{{ url('m', key, '') }}">Clear</a>{% endif %}
|
|
20
|
+
</form>
|
|
21
|
+
{% endif %}
|
|
22
|
+
</div>
|
|
23
|
+
<div class="changelist{% if filters %} has-filters{% endif %}">
|
|
24
|
+
<div class="changelist-main">
|
|
25
|
+
{% if action_error %}<div class="form-errors" role="alert"><p>{{ action_error }}</p></div>{% endif %}
|
|
26
|
+
{% set selectable = actions|length > 0 %}
|
|
27
|
+
{% if selectable %}
|
|
28
|
+
<form class="actions-form" method="post" action="{{ url('m', key, 'action') }}" hx-boost="false">
|
|
29
|
+
<input type="hidden" name="_csrf" value="{{ csrf }}">
|
|
30
|
+
<input type="hidden" name="list_query" value="{{ list_query }}">
|
|
31
|
+
<div class="action-bar">
|
|
32
|
+
<label for="action-choice">Action</label>
|
|
33
|
+
<select id="action-choice" name="action">
|
|
34
|
+
{% for choice in actions %}<option value="{{ choice.name }}">{{ choice.description }}</option>{% endfor %}
|
|
35
|
+
</select>
|
|
36
|
+
<button type="submit" class="button">Run</button>
|
|
37
|
+
</div>
|
|
38
|
+
{% endif %}
|
|
39
|
+
<div class="table-wrap">
|
|
40
|
+
<table class="rows">
|
|
41
|
+
<thead>
|
|
42
|
+
<tr>
|
|
43
|
+
{% if selectable %}<th scope="col" class="select"><input type="checkbox" class="select-all" aria-label="Select all rows on this page" hidden></th>{% endif %}
|
|
44
|
+
{% for header in headers %}
|
|
45
|
+
<th scope="col"{% if header.direction %} aria-sort="{{ 'ascending' if header.direction == 'asc' else 'descending' }}"{% endif %}>
|
|
46
|
+
{% if header.link %}<a href="{{ header.link }}">{{ header.label }}{% if header.direction %} <span class="arrow {{ header.direction }}" aria-hidden="true"></span>{% endif %}</a>{% else %}{{ header.label }}{% endif %}
|
|
47
|
+
</th>
|
|
48
|
+
{% endfor %}
|
|
49
|
+
</tr>
|
|
50
|
+
</thead>
|
|
51
|
+
<tbody>
|
|
52
|
+
{% for row in rows %}
|
|
53
|
+
<tr>
|
|
54
|
+
{% if selectable %}<td class="select"><input type="checkbox" name="selected" value="{{ row.pk }}" aria-label="Select row {{ row.pk }}"></td>{% endif %}
|
|
55
|
+
{% for cell in row.cells %}
|
|
56
|
+
<td>{% if cell.link %}<a href="{{ row.link }}">{{ cell_body(cell) }}</a>{% else %}{{ cell_body(cell) }}{% endif %}</td>
|
|
57
|
+
{% endfor %}
|
|
58
|
+
</tr>
|
|
59
|
+
{% else %}
|
|
60
|
+
<tr><td class="empty" colspan="{{ headers|length + (1 if selectable else 0) }}">{{ "No rows match." if narrowed else "This table is empty." }}</td></tr>
|
|
61
|
+
{% endfor %}
|
|
62
|
+
</tbody>
|
|
63
|
+
</table>
|
|
64
|
+
</div>
|
|
65
|
+
{% if selectable %}</form>{% endif %}
|
|
66
|
+
{% include "_pagination.html" %}
|
|
67
|
+
</div>
|
|
68
|
+
{% if filters %}
|
|
69
|
+
<aside class="filters-panel module" aria-label="Filters">
|
|
70
|
+
<h2>Filter</h2>
|
|
71
|
+
{% for item in filters %}
|
|
72
|
+
<div class="filter">
|
|
73
|
+
<h3 class="filter-title">By {{ item.title|lower }}</h3>
|
|
74
|
+
{% if item.options %}
|
|
75
|
+
<ul>
|
|
76
|
+
{% for option in item.options %}<li>{% if option.active %}<a href="{{ option.link }}" class="active" aria-current="true">{{ option.label }}</a>{% else %}<a href="{{ option.link }}">{{ option.label }}</a>{% endif %}</li>{% endfor %}
|
|
77
|
+
</ul>
|
|
78
|
+
{% elif item.current is not none %}
|
|
79
|
+
<p><span class="filter-value">{{ item.current }}</span> <a href="{{ item.clear }}">Clear</a></p>
|
|
80
|
+
{% else %}
|
|
81
|
+
<p class="muted">all</p>
|
|
82
|
+
{% endif %}
|
|
83
|
+
</div>
|
|
84
|
+
{% endfor %}
|
|
85
|
+
</aside>
|
|
86
|
+
{% endif %}
|
|
87
|
+
</div>
|
|
88
|
+
{% endblock %}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{% extends "base.html" %}
|
|
2
|
+
{% block title %}Log in{% endblock %}
|
|
3
|
+
{% block content %}
|
|
4
|
+
<form class="card login" method="post" action="{{ url('login') }}">
|
|
5
|
+
<h1>Log in</h1>
|
|
6
|
+
{% if error %}<p class="error" role="alert">{{ error }}</p>{% endif %}
|
|
7
|
+
<input type="hidden" name="_csrf" value="{{ login_csrf }}">
|
|
8
|
+
<input type="hidden" name="next" value="{{ next }}">
|
|
9
|
+
<label for="login-field">Email</label>
|
|
10
|
+
<input id="login-field" type="text" name="login" value="{{ login }}" autocomplete="username" required autofocus>
|
|
11
|
+
<label for="password-field">Password</label>
|
|
12
|
+
<input id="password-field" type="password" name="password" autocomplete="current-password" required>
|
|
13
|
+
<button type="submit" class="button primary">Log in</button>
|
|
14
|
+
</form>
|
|
15
|
+
{% endblock %}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{% extends "base.html" %}
|
|
2
|
+
{% import "_history.html" as history %}
|
|
3
|
+
{% block title %}Recent actions{% endblock %}
|
|
4
|
+
{% block breadcrumbs %}<a href="{{ url() }}">Home</a> <span class="crumb-sep" aria-hidden="true"></span> <span>Recent actions</span>{% endblock %}
|
|
5
|
+
{% block content %}
|
|
6
|
+
<div class="page-head">
|
|
7
|
+
<h1>Recent actions</h1>
|
|
8
|
+
</div>
|
|
9
|
+
{% for entry in entries %}
|
|
10
|
+
<article class="entry">
|
|
11
|
+
{{ history.entry_head(entry) }}
|
|
12
|
+
{% if entry.lines %}
|
|
13
|
+
<ul class="change-lines">
|
|
14
|
+
{% for line in entry.lines %}
|
|
15
|
+
<li class="change-line op-{{ line.op }}"><span class="op">{{ line.op }}</span> <span class="model">{{ line.model }}</span> {% if line.link %}<a href="{{ line.link }}">{{ line.text }}</a>{% else %}<span class="object-key">{{ line.text }}</span>{% endif %}</li>
|
|
16
|
+
{% endfor %}
|
|
17
|
+
{% if entry.more %}<li class="change-more">and {{ entry.more }} more</li>{% endif %}
|
|
18
|
+
</ul>
|
|
19
|
+
{% endif %}
|
|
20
|
+
</article>
|
|
21
|
+
{% else %}
|
|
22
|
+
<p class="muted">{{ "No actions recorded." if page == 1 else "No actions on this page." }}</p>
|
|
23
|
+
{% endfor %}
|
|
24
|
+
{{ history.pages(page, prev_link, next_link) }}
|
|
25
|
+
{% endblock %}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import hmac
|
|
3
|
+
import logging
|
|
4
|
+
import re
|
|
5
|
+
import secrets
|
|
6
|
+
from typing import Any
|
|
7
|
+
from urllib.parse import unquote, urlsplit
|
|
8
|
+
|
|
9
|
+
from fastapi import APIRouter
|
|
10
|
+
from itsdangerous import BadData, URLSafeTimedSerializer
|
|
11
|
+
from starlette.requests import Request
|
|
12
|
+
from starlette.responses import RedirectResponse, Response
|
|
13
|
+
|
|
14
|
+
from devadmin.auth import SESSION_COOKIE, authenticate, new_session, password_fingerprint
|
|
15
|
+
from devadmin.history.records import ActionRecord
|
|
16
|
+
from devadmin.web.deps import (
|
|
17
|
+
client_address,
|
|
18
|
+
ctx_of,
|
|
19
|
+
current_session,
|
|
20
|
+
delete_cookie,
|
|
21
|
+
mount_path,
|
|
22
|
+
panel_of,
|
|
23
|
+
render,
|
|
24
|
+
set_cookie,
|
|
25
|
+
url,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
logger = logging.getLogger("devadmin")
|
|
29
|
+
|
|
30
|
+
router = APIRouter()
|
|
31
|
+
|
|
32
|
+
LOGIN_CSRF_COOKIE = "devadmin_csrf"
|
|
33
|
+
LOGIN_CSRF_SECONDS = 3600
|
|
34
|
+
WRONG_LOGIN = "Wrong email or password."
|
|
35
|
+
TOO_MANY_ATTEMPTS = "Too many attempts. Try again later."
|
|
36
|
+
FORM_EXPIRED = "The form has expired. Please try again."
|
|
37
|
+
_TOKEN = re.compile(r"[0-9a-f]{64}")
|
|
38
|
+
_ACTOR_REPR_LENGTH = 255
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _serializer(request: Request) -> URLSafeTimedSerializer:
|
|
42
|
+
return URLSafeTimedSerializer(
|
|
43
|
+
panel_of(request).config.secret_key,
|
|
44
|
+
salt="devadmin.login-csrf",
|
|
45
|
+
signer_kwargs={"digest_method": hashlib.sha256},
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _login_token(request: Request) -> str | None:
|
|
50
|
+
value = request.cookies.get(LOGIN_CSRF_COOKIE)
|
|
51
|
+
if not value:
|
|
52
|
+
return None
|
|
53
|
+
try:
|
|
54
|
+
token = _serializer(request).loads(value, max_age=LOGIN_CSRF_SECONDS)
|
|
55
|
+
except (BadData, UnicodeError):
|
|
56
|
+
return None
|
|
57
|
+
return token if isinstance(token, str) and _TOKEN.fullmatch(token) else None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _login_page(request: Request, status_code: int = 200, error: str = "", login: str = "", next: str = "") -> Response:
|
|
61
|
+
token = _login_token(request) or secrets.token_hex(32)
|
|
62
|
+
response = render(request, "login.html", status_code, error=error, login=login, next=next, login_csrf=token)
|
|
63
|
+
set_cookie(response, request, LOGIN_CSRF_COOKIE, _serializer(request).dumps(token), LOGIN_CSRF_SECONDS)
|
|
64
|
+
return response
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _field(form: Any, name: str) -> str:
|
|
68
|
+
value = form.get(name)
|
|
69
|
+
return value if isinstance(value, str) else ""
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def safe_next(request: Request, target: str) -> str | None:
|
|
73
|
+
if not target or not target.isprintable() or "\\" in target or not target.startswith("/"):
|
|
74
|
+
return None
|
|
75
|
+
if target.startswith("//"):
|
|
76
|
+
return None
|
|
77
|
+
parts = urlsplit(target)
|
|
78
|
+
if parts.scheme or parts.netloc:
|
|
79
|
+
return None
|
|
80
|
+
root = mount_path(request)
|
|
81
|
+
if root and parts.path != root and not parts.path.startswith(root + "/"):
|
|
82
|
+
return None
|
|
83
|
+
if any(segment in (".", "..") for segment in unquote(parts.path).split("/")):
|
|
84
|
+
return None
|
|
85
|
+
return target
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
async def _record(request: Request, kind: str, actor_id: str | None, actor_repr: str) -> None:
|
|
89
|
+
action = ActionRecord(
|
|
90
|
+
actor_id=actor_id,
|
|
91
|
+
actor_repr=actor_repr[:_ACTOR_REPR_LENGTH],
|
|
92
|
+
kind=kind,
|
|
93
|
+
summary="",
|
|
94
|
+
source="panel",
|
|
95
|
+
ip=client_address(request),
|
|
96
|
+
meta={},
|
|
97
|
+
)
|
|
98
|
+
try:
|
|
99
|
+
await panel_of(request).adapter.history.insert(action, [])
|
|
100
|
+
except Exception:
|
|
101
|
+
logger.exception("could not record the %s action", kind)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@router.get("/login")
|
|
105
|
+
async def login_form(request: Request) -> Response:
|
|
106
|
+
_, actor = await current_session(request)
|
|
107
|
+
if actor is not None:
|
|
108
|
+
return RedirectResponse(url(request), status_code=303)
|
|
109
|
+
return _login_page(request, next=request.query_params.get("next", ""))
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@router.post("/login")
|
|
113
|
+
async def login(request: Request) -> Response:
|
|
114
|
+
panel = panel_of(request)
|
|
115
|
+
config = panel.config
|
|
116
|
+
form = ctx_of(request).form
|
|
117
|
+
login_value, password, next_value = _field(form, "login"), _field(form, "password"), _field(form, "next")
|
|
118
|
+
expected = _login_token(request)
|
|
119
|
+
submitted = _field(form, "_csrf")
|
|
120
|
+
if expected is None or not hmac.compare_digest(
|
|
121
|
+
expected.encode(), submitted.encode("utf-8", "surrogatepass")
|
|
122
|
+
):
|
|
123
|
+
return _login_page(request, 403, FORM_EXPIRED, login_value, next_value)
|
|
124
|
+
typed_login = login_value.strip()
|
|
125
|
+
limiter = config.login_rate_limiter
|
|
126
|
+
if limiter is not None and await limiter(request, typed_login.casefold()) is False:
|
|
127
|
+
return _login_page(request, 429, TOO_MANY_ATTEMPTS, login_value, next_value)
|
|
128
|
+
user = await authenticate(config, login_value, password)
|
|
129
|
+
pwd = None if user is None else password_fingerprint(config.secret_key, getattr(user, config.password_field))
|
|
130
|
+
if pwd is None:
|
|
131
|
+
await _record(request, "login_failed", None, typed_login)
|
|
132
|
+
return _login_page(request, 401, WRONG_LOGIN, login_value, next_value)
|
|
133
|
+
adapter = panel.adapter
|
|
134
|
+
pk = adapter.pk_of(user)
|
|
135
|
+
response = RedirectResponse(safe_next(request, next_value) or url(request), status_code=303)
|
|
136
|
+
set_cookie(response, request, SESSION_COOKIE, panel.signer.dumps(new_session(pk, pwd)), config.session_seconds)
|
|
137
|
+
delete_cookie(response, request, LOGIN_CSRF_COOKIE)
|
|
138
|
+
await _record(request, "login", str(pk), adapter.repr_of(user))
|
|
139
|
+
return response
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@router.post("/logout")
|
|
143
|
+
async def logout(request: Request) -> Response:
|
|
144
|
+
response = RedirectResponse(url(request, "login"), status_code=303)
|
|
145
|
+
delete_cookie(response, request, SESSION_COOKIE)
|
|
146
|
+
return response
|