confsync-server 0.1.0__tar.gz → 0.2.0__tar.gz
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.
- {confsync_server-0.1.0 → confsync_server-0.2.0}/PKG-INFO +1 -1
- {confsync_server-0.1.0 → confsync_server-0.2.0}/confsync_server/__init__.py +1 -1
- {confsync_server-0.1.0 → confsync_server-0.2.0}/confsync_server/app.py +148 -36
- confsync_server-0.2.0/confsync_server/auth.py +144 -0
- {confsync_server-0.1.0 → confsync_server-0.2.0}/confsync_server/cli.py +51 -3
- {confsync_server-0.1.0 → confsync_server-0.2.0}/confsync_server/config.py +19 -1
- {confsync_server-0.1.0 → confsync_server-0.2.0}/confsync_server/db.py +64 -5
- {confsync_server-0.1.0 → confsync_server-0.2.0}/tests/test_api.py +8 -0
- confsync_server-0.2.0/tests/test_backup.py +72 -0
- confsync_server-0.2.0/tests/test_gitea.py +265 -0
- confsync_server-0.1.0/confsync_server/auth.py +0 -77
- {confsync_server-0.1.0 → confsync_server-0.2.0}/.gitignore +0 -0
- {confsync_server-0.1.0 → confsync_server-0.2.0}/README.md +0 -0
- {confsync_server-0.1.0 → confsync_server-0.2.0}/confsync_server/__main__.py +0 -0
- {confsync_server-0.1.0 → confsync_server-0.2.0}/confsync_server/crypto.py +0 -0
- {confsync_server-0.1.0 → confsync_server-0.2.0}/confsync_server/service.py +0 -0
- {confsync_server-0.1.0 → confsync_server-0.2.0}/pyproject.toml +0 -0
- {confsync_server-0.1.0 → confsync_server-0.2.0}/tests/test_auth.py +0 -0
- {confsync_server-0.1.0 → confsync_server-0.2.0}/tests/test_crypto.py +0 -0
|
@@ -10,11 +10,12 @@ from starlette.requests import Request
|
|
|
10
10
|
from starlette.responses import JSONResponse, PlainTextResponse, RedirectResponse, Response
|
|
11
11
|
from starlette.routing import Route
|
|
12
12
|
|
|
13
|
-
from confsync_server import auth, crypto, db
|
|
13
|
+
from confsync_server import __version__, auth, crypto, db
|
|
14
14
|
from confsync_server.config import ServerSettings, load_settings
|
|
15
15
|
|
|
16
16
|
SESSION_COOKIE = "confsync_session"
|
|
17
17
|
MAX_CONTENT_BYTES = 4 * 1024 * 1024
|
|
18
|
+
GITEA_TOKEN_CACHE_TTL = 5 * 60 # re-verify Gitea API tokens at most this often
|
|
18
19
|
|
|
19
20
|
# ── rendering ─────────────────────────────────────────────────────
|
|
20
21
|
|
|
@@ -43,6 +44,7 @@ code.key { background: #fff8c5; padding: .4rem .6rem; border-radius: 6px;
|
|
|
43
44
|
border-radius: 6px; margin-bottom: 1rem; }
|
|
44
45
|
.muted { color: #656d76; font-size: .9em; }
|
|
45
46
|
form.inline { display: inline; }
|
|
47
|
+
p .btn { margin-right: .5rem; }
|
|
46
48
|
"""
|
|
47
49
|
|
|
48
50
|
|
|
@@ -127,14 +129,65 @@ def _require_web_user(request: Request):
|
|
|
127
129
|
return user, None
|
|
128
130
|
|
|
129
131
|
|
|
130
|
-
def _require_api_user(request: Request):
|
|
131
|
-
"""Returns (user, response). response is a 401 JSON when unauthorized.
|
|
132
|
+
async def _require_api_user(request: Request):
|
|
133
|
+
"""Returns (user, response). response is a 401/403 JSON when unauthorized.
|
|
134
|
+
|
|
135
|
+
Bearer tokens are either a confsync API key (``cs_…``) or, when Gitea is
|
|
136
|
+
configured, a Gitea access token verified against the Gitea instance."""
|
|
132
137
|
header = request.headers.get("Authorization", "")
|
|
133
138
|
if not header.startswith("Bearer "):
|
|
134
139
|
return None, JSONResponse({"error": "missing bearer token"}, status_code=401)
|
|
140
|
+
token = header[len("Bearer "):]
|
|
141
|
+
s = _settings(request)
|
|
142
|
+
if not token.startswith(auth.KEY_PREFIX):
|
|
143
|
+
if not s.gitea_configured:
|
|
144
|
+
return None, JSONResponse({"error": "invalid or revoked API key"}, status_code=401)
|
|
145
|
+
return await _gitea_token_user(request, token)
|
|
135
146
|
conn = _open_db(request)
|
|
136
147
|
try:
|
|
137
|
-
user = db.get_user_by_api_key(conn,
|
|
148
|
+
user = db.get_user_by_api_key(conn, token)
|
|
149
|
+
finally:
|
|
150
|
+
conn.close()
|
|
151
|
+
if user is None:
|
|
152
|
+
return None, JSONResponse({"error": "invalid or revoked API key"}, status_code=401)
|
|
153
|
+
return user, None
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
async def _gitea_token_user(request: Request, token: str):
|
|
157
|
+
"""Authenticate a Gitea access token: verify it against the Gitea API,
|
|
158
|
+
gate the login on the allowlist, and map it to a confsync user
|
|
159
|
+
(auto-provisioned on first use). Verified tokens are cached briefly, so a
|
|
160
|
+
token revoked in Gitea stays valid for at most GITEA_TOKEN_CACHE_TTL."""
|
|
161
|
+
s = _settings(request)
|
|
162
|
+
cache = request.app.state.gitea_token_cache
|
|
163
|
+
token_hash = db.hash_token(token)
|
|
164
|
+
cached = cache.get(token_hash)
|
|
165
|
+
if cached is not None and cached[1] > time.time():
|
|
166
|
+
user_id = cached[0]
|
|
167
|
+
else:
|
|
168
|
+
try:
|
|
169
|
+
profile = await auth.fetch_gitea_user(s.gitea_url, token)
|
|
170
|
+
except ValueError as exc:
|
|
171
|
+
return None, JSONResponse({"error": str(exc)}, status_code=401)
|
|
172
|
+
conn = _open_db(request)
|
|
173
|
+
try:
|
|
174
|
+
if not db.is_login_allowed(conn, profile["login"]):
|
|
175
|
+
return None, JSONResponse(
|
|
176
|
+
{"error": f"Gitea user '{profile['login']}' is not on the allowlist"},
|
|
177
|
+
status_code=403,
|
|
178
|
+
)
|
|
179
|
+
user = db.upsert_gitea_user(conn, profile["id"], profile["login"],
|
|
180
|
+
profile["name"], profile["avatar_url"])
|
|
181
|
+
finally:
|
|
182
|
+
conn.close()
|
|
183
|
+
user_id = user["id"]
|
|
184
|
+
if len(cache) > 1024:
|
|
185
|
+
cache.clear()
|
|
186
|
+
cache[token_hash] = (user_id, time.time() + GITEA_TOKEN_CACHE_TTL)
|
|
187
|
+
|
|
188
|
+
conn = _open_db(request)
|
|
189
|
+
try:
|
|
190
|
+
user = db.get_user_by_id(conn, user_id)
|
|
138
191
|
finally:
|
|
139
192
|
conn.close()
|
|
140
193
|
if user is None:
|
|
@@ -169,13 +222,19 @@ def _flash_error(request: Request) -> dict:
|
|
|
169
222
|
async def login_page(request: Request) -> Response:
|
|
170
223
|
if _session_user(request) is not None:
|
|
171
224
|
return RedirectResponse("/", status_code=303)
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
225
|
+
s = _settings(request)
|
|
226
|
+
buttons = []
|
|
227
|
+
if s.github_client_id:
|
|
228
|
+
buttons.append('<a class="btn" href="/auth/github/login">Sign in with GitHub</a>')
|
|
229
|
+
if s.gitea_configured:
|
|
230
|
+
buttons.append('<a class="btn" href="/auth/gitea/login">Sign in with Gitea</a>')
|
|
231
|
+
if not buttons:
|
|
232
|
+
body = ("<p class='error'>No login provider is configured on this server "
|
|
233
|
+
"(missing github.client_id or gitea.url + gitea.client_id).</p>")
|
|
175
234
|
return _page("Login", body)
|
|
176
|
-
body = """
|
|
235
|
+
body = f"""
|
|
177
236
|
<h1>Sign in</h1>
|
|
178
|
-
<p
|
|
237
|
+
<p>{''.join(buttons)}</p>
|
|
179
238
|
"""
|
|
180
239
|
return _page("Login", body)
|
|
181
240
|
|
|
@@ -192,47 +251,90 @@ async def github_login(request: Request) -> Response:
|
|
|
192
251
|
return RedirectResponse(url)
|
|
193
252
|
|
|
194
253
|
|
|
195
|
-
async def
|
|
254
|
+
async def gitea_login(request: Request) -> Response:
|
|
196
255
|
s = _settings(request)
|
|
256
|
+
if not s.gitea_configured:
|
|
257
|
+
return _page("Error", "", error="Gitea OAuth is not configured.")
|
|
258
|
+
state = auth.generate_oauth_state()
|
|
259
|
+
request.app.state.oauth_states.add(state)
|
|
260
|
+
url = auth.build_gitea_authorize_url(
|
|
261
|
+
s.gitea_url, s.gitea_client_id, f"{s.external_url}/auth/gitea/callback", state
|
|
262
|
+
)
|
|
263
|
+
return RedirectResponse(url)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def _check_oauth_state(request: Request) -> str | None:
|
|
267
|
+
"""Returns an error message when the OAuth state is missing/unknown."""
|
|
197
268
|
state = request.query_params.get("state", "")
|
|
198
|
-
code = request.query_params.get("code", "")
|
|
199
269
|
states = request.app.state.oauth_states
|
|
200
270
|
if not state or state not in states:
|
|
201
|
-
return
|
|
271
|
+
return "Invalid OAuth state; try logging in again."
|
|
202
272
|
states.discard(state)
|
|
203
|
-
|
|
204
|
-
return _page("Error", "", error="Missing OAuth code from GitHub.")
|
|
273
|
+
return None
|
|
205
274
|
|
|
206
|
-
try:
|
|
207
|
-
token = await auth.exchange_code(
|
|
208
|
-
s.github_client_id, s.github_client_secret, code,
|
|
209
|
-
f"{s.external_url}/auth/github/callback",
|
|
210
|
-
)
|
|
211
|
-
profile = await auth.fetch_github_user(token)
|
|
212
|
-
except ValueError as exc:
|
|
213
|
-
return _page("Error", "", error=str(exc))
|
|
214
275
|
|
|
276
|
+
def _login_profile(request: Request, provider: str, profile: dict, upsert) -> Response:
|
|
277
|
+
"""Allowlist-check an OAuth profile, upsert the user, start a session."""
|
|
215
278
|
conn = _open_db(request)
|
|
216
279
|
try:
|
|
217
280
|
if not db.is_login_allowed(conn, profile["login"]):
|
|
218
281
|
return _page("Access denied", "",
|
|
219
|
-
error=f"
|
|
220
|
-
user =
|
|
221
|
-
|
|
282
|
+
error=f"{provider} user '{profile['login']}' is not on the allowlist.")
|
|
283
|
+
user = upsert(conn, profile["id"], profile["login"],
|
|
284
|
+
profile["name"], profile["avatar_url"])
|
|
222
285
|
session_token = auth.generate_session_token()
|
|
223
286
|
db.create_session(conn, user["id"], session_token)
|
|
224
287
|
finally:
|
|
225
288
|
conn.close()
|
|
226
|
-
|
|
227
289
|
resp = RedirectResponse("/", status_code=303)
|
|
228
290
|
resp.set_cookie(
|
|
229
291
|
SESSION_COOKIE, session_token,
|
|
230
292
|
max_age=db.SESSION_TTL, httponly=True, samesite="lax",
|
|
231
|
-
secure=
|
|
293
|
+
secure=_settings(request).external_url.startswith("https://"),
|
|
232
294
|
)
|
|
233
295
|
return resp
|
|
234
296
|
|
|
235
297
|
|
|
298
|
+
async def github_callback(request: Request) -> Response:
|
|
299
|
+
s = _settings(request)
|
|
300
|
+
if (err := _check_oauth_state(request)) is not None:
|
|
301
|
+
return _page("Error", "", error=err)
|
|
302
|
+
code = request.query_params.get("code", "")
|
|
303
|
+
if not code:
|
|
304
|
+
return _page("Error", "", error="Missing OAuth code from GitHub.")
|
|
305
|
+
|
|
306
|
+
try:
|
|
307
|
+
token = await auth.exchange_code(
|
|
308
|
+
s.github_client_id, s.github_client_secret, code,
|
|
309
|
+
f"{s.external_url}/auth/github/callback",
|
|
310
|
+
)
|
|
311
|
+
profile = await auth.fetch_github_user(token)
|
|
312
|
+
except ValueError as exc:
|
|
313
|
+
return _page("Error", "", error=str(exc))
|
|
314
|
+
|
|
315
|
+
return _login_profile(request, "GitHub", profile, db.upsert_user)
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
async def gitea_callback(request: Request) -> Response:
|
|
319
|
+
s = _settings(request)
|
|
320
|
+
if (err := _check_oauth_state(request)) is not None:
|
|
321
|
+
return _page("Error", "", error=err)
|
|
322
|
+
code = request.query_params.get("code", "")
|
|
323
|
+
if not code:
|
|
324
|
+
return _page("Error", "", error="Missing OAuth code from Gitea.")
|
|
325
|
+
|
|
326
|
+
try:
|
|
327
|
+
token = await auth.exchange_gitea_code(
|
|
328
|
+
s.gitea_url, s.gitea_client_id, s.gitea_client_secret, code,
|
|
329
|
+
f"{s.external_url}/auth/gitea/callback",
|
|
330
|
+
)
|
|
331
|
+
profile = await auth.fetch_gitea_user(s.gitea_url, token)
|
|
332
|
+
except ValueError as exc:
|
|
333
|
+
return _page("Error", "", error=str(exc))
|
|
334
|
+
|
|
335
|
+
return _login_profile(request, "Gitea", profile, db.upsert_gitea_user)
|
|
336
|
+
|
|
337
|
+
|
|
236
338
|
async def logout(request: Request) -> Response:
|
|
237
339
|
token = request.cookies.get(SESSION_COOKIE)
|
|
238
340
|
if token:
|
|
@@ -489,11 +591,11 @@ async def web_admin(request: Request) -> Response:
|
|
|
489
591
|
for login in allowed
|
|
490
592
|
)
|
|
491
593
|
body = f"""
|
|
492
|
-
<h1>Admin — allowed
|
|
594
|
+
<h1>Admin — allowed logins</h1>
|
|
493
595
|
<table><tr><th>Login</th><th></th></tr>{rows}</table>
|
|
494
596
|
<h2>Add login</h2>
|
|
495
597
|
<form method="post" action="/admin/allow">
|
|
496
|
-
<input type="text" name="login" placeholder="
|
|
598
|
+
<input type="text" name="login" placeholder="username (GitHub or Gitea)" required>
|
|
497
599
|
<button class="primary" type="submit">Allow</button>
|
|
498
600
|
</form>
|
|
499
601
|
"""
|
|
@@ -535,15 +637,21 @@ async def web_admin_disallow(request: Request) -> Response:
|
|
|
535
637
|
|
|
536
638
|
# ── REST API ──────────────────────────────────────────────────────
|
|
537
639
|
|
|
640
|
+
async def api_version(request: Request) -> Response:
|
|
641
|
+
"""Public server version — lets clients/operators check what is running
|
|
642
|
+
before authenticating (used by the upgrade procedure)."""
|
|
643
|
+
return JSONResponse({"server": __version__})
|
|
644
|
+
|
|
645
|
+
|
|
538
646
|
async def api_whoami(request: Request) -> Response:
|
|
539
|
-
user, resp = _require_api_user(request)
|
|
647
|
+
user, resp = await _require_api_user(request)
|
|
540
648
|
if resp:
|
|
541
649
|
return resp
|
|
542
650
|
return JSONResponse({"login": user["login"], "name": user["name"]})
|
|
543
651
|
|
|
544
652
|
|
|
545
653
|
async def api_list(request: Request) -> Response:
|
|
546
|
-
user, resp = _require_api_user(request)
|
|
654
|
+
user, resp = await _require_api_user(request)
|
|
547
655
|
if resp:
|
|
548
656
|
return resp
|
|
549
657
|
conn = _open_db(request)
|
|
@@ -559,7 +667,7 @@ async def api_list(request: Request) -> Response:
|
|
|
559
667
|
|
|
560
668
|
|
|
561
669
|
async def api_get(request: Request) -> Response:
|
|
562
|
-
user, resp = _require_api_user(request)
|
|
670
|
+
user, resp = await _require_api_user(request)
|
|
563
671
|
if resp:
|
|
564
672
|
return resp
|
|
565
673
|
app, name = request.path_params["app"], request.path_params["name"]
|
|
@@ -577,7 +685,7 @@ async def api_get(request: Request) -> Response:
|
|
|
577
685
|
|
|
578
686
|
|
|
579
687
|
async def api_put(request: Request) -> Response:
|
|
580
|
-
user, resp = _require_api_user(request)
|
|
688
|
+
user, resp = await _require_api_user(request)
|
|
581
689
|
if resp:
|
|
582
690
|
return resp
|
|
583
691
|
app, name = request.path_params["app"], request.path_params["name"]
|
|
@@ -595,7 +703,7 @@ async def api_put(request: Request) -> Response:
|
|
|
595
703
|
|
|
596
704
|
|
|
597
705
|
async def api_delete(request: Request) -> Response:
|
|
598
|
-
user, resp = _require_api_user(request)
|
|
706
|
+
user, resp = await _require_api_user(request)
|
|
599
707
|
if resp:
|
|
600
708
|
return resp
|
|
601
709
|
app, name = request.path_params["app"], request.path_params["name"]
|
|
@@ -610,7 +718,7 @@ async def api_delete(request: Request) -> Response:
|
|
|
610
718
|
|
|
611
719
|
|
|
612
720
|
async def api_history(request: Request) -> Response:
|
|
613
|
-
user, resp = _require_api_user(request)
|
|
721
|
+
user, resp = await _require_api_user(request)
|
|
614
722
|
if resp:
|
|
615
723
|
return resp
|
|
616
724
|
app, name = request.path_params["app"], request.path_params["name"]
|
|
@@ -626,7 +734,7 @@ async def api_history(request: Request) -> Response:
|
|
|
626
734
|
|
|
627
735
|
|
|
628
736
|
async def api_history_get(request: Request) -> Response:
|
|
629
|
-
user, resp = _require_api_user(request)
|
|
737
|
+
user, resp = await _require_api_user(request)
|
|
630
738
|
if resp:
|
|
631
739
|
return resp
|
|
632
740
|
app, name = request.path_params["app"], request.path_params["name"]
|
|
@@ -665,6 +773,8 @@ def create_app(settings: ServerSettings | None = None) -> Starlette:
|
|
|
665
773
|
Route("/login", login_page),
|
|
666
774
|
Route("/auth/github/login", github_login),
|
|
667
775
|
Route("/auth/github/callback", github_callback),
|
|
776
|
+
Route("/auth/gitea/login", gitea_login),
|
|
777
|
+
Route("/auth/gitea/callback", gitea_callback),
|
|
668
778
|
Route("/logout", logout),
|
|
669
779
|
Route("/keys", web_keys),
|
|
670
780
|
Route("/keys/create", web_key_create, methods=["POST"]),
|
|
@@ -677,6 +787,7 @@ def create_app(settings: ServerSettings | None = None) -> Starlette:
|
|
|
677
787
|
Route("/docs/{app}/{name:path}/delete", web_doc_delete, methods=["POST"]),
|
|
678
788
|
Route("/docs/{app}/{name:path}", web_doc_view),
|
|
679
789
|
Route("/docs/{app}/{name:path}", web_doc_save, methods=["POST"]),
|
|
790
|
+
Route("/api/v1/version", api_version),
|
|
680
791
|
Route("/api/v1/whoami", api_whoami),
|
|
681
792
|
Route("/api/v1/documents", api_list),
|
|
682
793
|
Route("/api/v1/documents/{app}/{name:path}/history/{version:int}", api_history_get),
|
|
@@ -690,4 +801,5 @@ def create_app(settings: ServerSettings | None = None) -> Starlette:
|
|
|
690
801
|
app.state.settings = settings
|
|
691
802
|
app.state.master_key = master_key
|
|
692
803
|
app.state.oauth_states = set()
|
|
804
|
+
app.state.gitea_token_cache = {}
|
|
693
805
|
return app
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""GitHub/Gitea OAuth flows and token/API-key generation helpers."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import secrets
|
|
5
|
+
import string
|
|
6
|
+
from urllib.parse import urlencode
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
|
|
10
|
+
GITHUB_AUTHORIZE_URL = "https://github.com/login/oauth/authorize"
|
|
11
|
+
GITHUB_TOKEN_URL = "https://github.com/login/oauth/access_token"
|
|
12
|
+
GITHUB_USER_URL = "https://api.github.com/user"
|
|
13
|
+
|
|
14
|
+
GITEA_AUTHORIZE_PATH = "/login/oauth/authorize"
|
|
15
|
+
GITEA_TOKEN_PATH = "/login/oauth/access_token"
|
|
16
|
+
GITEA_USER_PATH = "/api/v1/user"
|
|
17
|
+
GITEA_SCOPE = "read:user"
|
|
18
|
+
|
|
19
|
+
_KEY_ALPHABET = string.ascii_letters + string.digits
|
|
20
|
+
KEY_PREFIX = "cs_"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def generate_api_key() -> str:
|
|
24
|
+
"""New API key: ``cs_`` + 48 random base62 chars. Plaintext is shown to
|
|
25
|
+
the user once; only its sha256 is stored server-side."""
|
|
26
|
+
return KEY_PREFIX + "".join(secrets.choice(_KEY_ALPHABET) for _ in range(48))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def generate_session_token() -> str:
|
|
30
|
+
return secrets.token_urlsafe(32)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def generate_oauth_state() -> str:
|
|
34
|
+
return secrets.token_urlsafe(24)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def build_authorize_url(client_id: str, redirect_uri: str, state: str) -> str:
|
|
38
|
+
return (
|
|
39
|
+
f"{GITHUB_AUTHORIZE_URL}?client_id={client_id}"
|
|
40
|
+
f"&redirect_uri={redirect_uri}&state={state}&scope=read:user"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
async def exchange_code(client_id: str, client_secret: str, code: str,
|
|
45
|
+
redirect_uri: str) -> str:
|
|
46
|
+
"""Exchange an OAuth code for a GitHub access token."""
|
|
47
|
+
async with httpx.AsyncClient(timeout=20) as client:
|
|
48
|
+
resp = await client.post(
|
|
49
|
+
GITHUB_TOKEN_URL,
|
|
50
|
+
data={
|
|
51
|
+
"client_id": client_id,
|
|
52
|
+
"client_secret": client_secret,
|
|
53
|
+
"code": code,
|
|
54
|
+
"redirect_uri": redirect_uri,
|
|
55
|
+
},
|
|
56
|
+
headers={"Accept": "application/json"},
|
|
57
|
+
)
|
|
58
|
+
data = resp.json()
|
|
59
|
+
token = data.get("access_token")
|
|
60
|
+
if not token:
|
|
61
|
+
raise ValueError(f"GitHub token exchange failed: {data.get('error_description') or data}")
|
|
62
|
+
return token
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
async def fetch_github_user(access_token: str) -> dict:
|
|
66
|
+
"""Return the GitHub profile: {id, login, name, avatar_url}."""
|
|
67
|
+
async with httpx.AsyncClient(timeout=20) as client:
|
|
68
|
+
resp = await client.get(
|
|
69
|
+
GITHUB_USER_URL,
|
|
70
|
+
headers={
|
|
71
|
+
"Authorization": f"Bearer {access_token}",
|
|
72
|
+
"Accept": "application/vnd.github+json",
|
|
73
|
+
},
|
|
74
|
+
)
|
|
75
|
+
if resp.status_code != 200:
|
|
76
|
+
raise ValueError(f"GitHub user fetch failed: HTTP {resp.status_code}")
|
|
77
|
+
data = resp.json()
|
|
78
|
+
return {
|
|
79
|
+
"id": int(data["id"]),
|
|
80
|
+
"login": str(data["login"]),
|
|
81
|
+
"name": str(data.get("name") or ""),
|
|
82
|
+
"avatar_url": str(data.get("avatar_url") or ""),
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def build_gitea_authorize_url(gitea_url: str, client_id: str, redirect_uri: str,
|
|
87
|
+
state: str) -> str:
|
|
88
|
+
query = urlencode({
|
|
89
|
+
"client_id": client_id,
|
|
90
|
+
"redirect_uri": redirect_uri,
|
|
91
|
+
"response_type": "code",
|
|
92
|
+
"state": state,
|
|
93
|
+
"scope": GITEA_SCOPE,
|
|
94
|
+
})
|
|
95
|
+
return f"{gitea_url}{GITEA_AUTHORIZE_PATH}?{query}"
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
async def exchange_gitea_code(gitea_url: str, client_id: str, client_secret: str,
|
|
99
|
+
code: str, redirect_uri: str) -> str:
|
|
100
|
+
"""Exchange an OAuth code for a Gitea access token."""
|
|
101
|
+
async with httpx.AsyncClient(timeout=20) as client:
|
|
102
|
+
resp = await client.post(
|
|
103
|
+
f"{gitea_url}{GITEA_TOKEN_PATH}",
|
|
104
|
+
json={
|
|
105
|
+
"client_id": client_id,
|
|
106
|
+
"client_secret": client_secret,
|
|
107
|
+
"code": code,
|
|
108
|
+
"grant_type": "authorization_code",
|
|
109
|
+
"redirect_uri": redirect_uri,
|
|
110
|
+
},
|
|
111
|
+
)
|
|
112
|
+
data = resp.json()
|
|
113
|
+
token = data.get("access_token")
|
|
114
|
+
if resp.status_code != 200 or not token:
|
|
115
|
+
raise ValueError(
|
|
116
|
+
f"Gitea token exchange failed: {data.get('error_description') or data.get('error') or data}"
|
|
117
|
+
)
|
|
118
|
+
return token
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
async def fetch_gitea_user(gitea_url: str, access_token: str) -> dict:
|
|
122
|
+
"""Return the Gitea profile: {id, login, name, avatar_url}.
|
|
123
|
+
|
|
124
|
+
Also used to verify plain Gitea access tokens (personal tokens) for API
|
|
125
|
+
auth: a non-200 response means the token is invalid or revoked."""
|
|
126
|
+
async with httpx.AsyncClient(timeout=20) as client:
|
|
127
|
+
resp = await client.get(
|
|
128
|
+
f"{gitea_url}{GITEA_USER_PATH}",
|
|
129
|
+
headers={"Authorization": f"token {access_token}"},
|
|
130
|
+
)
|
|
131
|
+
if resp.status_code == 401:
|
|
132
|
+
raise ValueError("Gitea rejected the access token (invalid or revoked).")
|
|
133
|
+
if resp.status_code != 200:
|
|
134
|
+
raise ValueError(f"Gitea user fetch failed: HTTP {resp.status_code}")
|
|
135
|
+
data = resp.json()
|
|
136
|
+
avatar = str(data.get("avatar_url") or "")
|
|
137
|
+
if avatar and not avatar.startswith(("http://", "https://")):
|
|
138
|
+
avatar = f"{gitea_url}/{avatar.lstrip('/')}"
|
|
139
|
+
return {
|
|
140
|
+
"id": int(data["id"]),
|
|
141
|
+
"login": str(data["login"]),
|
|
142
|
+
"name": str(data.get("full_name") or ""),
|
|
143
|
+
"avatar_url": avatar,
|
|
144
|
+
}
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
"""confsync-server CLI: serve / service."""
|
|
1
|
+
"""confsync-server CLI: serve / service / bootstrap / backup."""
|
|
2
2
|
from __future__ import annotations
|
|
3
3
|
|
|
4
4
|
import argparse
|
|
5
|
+
import os
|
|
6
|
+
import shutil
|
|
7
|
+
import sqlite3
|
|
5
8
|
import sys
|
|
9
|
+
import time
|
|
6
10
|
|
|
7
11
|
from confsync_server import __version__
|
|
8
12
|
from confsync_server.config import load_settings
|
|
@@ -53,6 +57,43 @@ def cmd_bootstrap(args: argparse.Namespace) -> None:
|
|
|
53
57
|
print(f"API key (shown once): {key}")
|
|
54
58
|
|
|
55
59
|
|
|
60
|
+
def cmd_backup(args: argparse.Namespace) -> None:
|
|
61
|
+
"""Hot backup: consistent sqlite snapshot of server.db + the master key.
|
|
62
|
+
Run this before upgrading the server (see MIGRATION.md)."""
|
|
63
|
+
settings = load_settings()
|
|
64
|
+
if not os.path.exists(settings.db_path):
|
|
65
|
+
print(f"error: no database at {settings.db_path} — is CONFSYNC_HOME correct?",
|
|
66
|
+
file=sys.stderr)
|
|
67
|
+
sys.exit(1)
|
|
68
|
+
out = args.out or os.path.join(
|
|
69
|
+
settings.home, "backups", time.strftime("confsync-%Y%m%d-%H%M%S")
|
|
70
|
+
)
|
|
71
|
+
os.makedirs(out, exist_ok=True)
|
|
72
|
+
|
|
73
|
+
db_out = os.path.join(out, "server.db")
|
|
74
|
+
src = sqlite3.connect(settings.db_path)
|
|
75
|
+
dst = sqlite3.connect(db_out)
|
|
76
|
+
try:
|
|
77
|
+
src.backup(dst) # online backup API: safe while the server is running
|
|
78
|
+
finally:
|
|
79
|
+
src.close()
|
|
80
|
+
dst.close()
|
|
81
|
+
os.chmod(db_out, 0o600)
|
|
82
|
+
|
|
83
|
+
key_path = os.path.join(settings.home, "master.key")
|
|
84
|
+
if os.path.exists(key_path):
|
|
85
|
+
key_out = os.path.join(out, "master.key")
|
|
86
|
+
shutil.copyfile(key_path, key_out)
|
|
87
|
+
os.chmod(key_out, 0o600)
|
|
88
|
+
print(f"backup written to {out} (server.db + master.key)")
|
|
89
|
+
else:
|
|
90
|
+
print(f"backup written to {out} (server.db)")
|
|
91
|
+
print("note: master key comes from $CONFSYNC_MASTER_KEY — "
|
|
92
|
+
"back it up separately (secret manager).")
|
|
93
|
+
print(f"restore: stop confsync-server, copy the files back into "
|
|
94
|
+
f"{settings.home}, start again.")
|
|
95
|
+
|
|
96
|
+
|
|
56
97
|
def main() -> None:
|
|
57
98
|
parser = argparse.ArgumentParser(
|
|
58
99
|
prog="confsync-server",
|
|
@@ -71,10 +112,15 @@ def main() -> None:
|
|
|
71
112
|
|
|
72
113
|
boot = sub.add_parser(
|
|
73
114
|
"bootstrap",
|
|
74
|
-
help="Create a user + API key without
|
|
75
|
-
boot.add_argument("--login", required=True, help="
|
|
115
|
+
help="Create a user + API key without OAuth (adopts the real provider id on first login)")
|
|
116
|
+
boot.add_argument("--login", required=True, help="login name of the user (GitHub/Gitea)")
|
|
76
117
|
boot.add_argument("--name", default="bootstrap", help="API key name")
|
|
77
118
|
|
|
119
|
+
backup = sub.add_parser(
|
|
120
|
+
"backup",
|
|
121
|
+
help="Hot backup of server.db + master key (run before upgrades)")
|
|
122
|
+
backup.add_argument("--out", help="Backup directory (default: $CONFSYNC_HOME/backups/<timestamp>)")
|
|
123
|
+
|
|
78
124
|
args = parser.parse_args()
|
|
79
125
|
if args.group == "serve":
|
|
80
126
|
cmd_serve(args)
|
|
@@ -82,6 +128,8 @@ def main() -> None:
|
|
|
82
128
|
cmd_service(args)
|
|
83
129
|
elif args.group == "bootstrap":
|
|
84
130
|
cmd_bootstrap(args)
|
|
131
|
+
elif args.group == "backup":
|
|
132
|
+
cmd_backup(args)
|
|
85
133
|
else:
|
|
86
134
|
parser.print_help()
|
|
87
135
|
sys.exit(0)
|
|
@@ -24,19 +24,27 @@ class ServerSettings:
|
|
|
24
24
|
external_url: str = "" # e.g. https://confsync.example.com (OAuth callback base)
|
|
25
25
|
github_client_id: str = ""
|
|
26
26
|
github_client_secret: str = ""
|
|
27
|
-
|
|
27
|
+
gitea_url: str = "" # self-hosted Gitea base URL, e.g. https://git.example.com
|
|
28
|
+
gitea_client_id: str = ""
|
|
29
|
+
gitea_client_secret: str = ""
|
|
30
|
+
allowed_users: list[str] = field(default_factory=list) # provider logins
|
|
28
31
|
home: str = ""
|
|
29
32
|
|
|
30
33
|
@property
|
|
31
34
|
def db_path(self) -> str:
|
|
32
35
|
return os.path.join(self.home, "server.db")
|
|
33
36
|
|
|
37
|
+
@property
|
|
38
|
+
def gitea_configured(self) -> bool:
|
|
39
|
+
return bool(self.gitea_url and self.gitea_client_id)
|
|
40
|
+
|
|
34
41
|
|
|
35
42
|
def load_settings(home: str | None = None) -> ServerSettings:
|
|
36
43
|
"""Load settings from $CONFSYNC_HOME/server.yaml with env overrides.
|
|
37
44
|
|
|
38
45
|
Env overrides: CONFSYNC_HOST, CONFSYNC_PORT, CONFSYNC_EXTERNAL_URL,
|
|
39
46
|
CONFSYNC_GITHUB_CLIENT_ID, CONFSYNC_GITHUB_CLIENT_SECRET,
|
|
47
|
+
CONFSYNC_GITEA_URL, CONFSYNC_GITEA_CLIENT_ID, CONFSYNC_GITEA_CLIENT_SECRET,
|
|
40
48
|
CONFSYNC_ALLOWED_USERS (comma-separated).
|
|
41
49
|
"""
|
|
42
50
|
import yaml
|
|
@@ -52,6 +60,7 @@ def load_settings(home: str | None = None) -> ServerSettings:
|
|
|
52
60
|
raw = loaded or {}
|
|
53
61
|
|
|
54
62
|
gh = raw.get("github", {}) or {}
|
|
63
|
+
gt = raw.get("gitea", {}) or {}
|
|
55
64
|
|
|
56
65
|
s = ServerSettings(
|
|
57
66
|
host=str(raw.get("host", "127.0.0.1")),
|
|
@@ -59,6 +68,9 @@ def load_settings(home: str | None = None) -> ServerSettings:
|
|
|
59
68
|
external_url=str(raw.get("external_url", "")).rstrip("/"),
|
|
60
69
|
github_client_id=str(gh.get("client_id", "")),
|
|
61
70
|
github_client_secret=str(gh.get("client_secret", "")),
|
|
71
|
+
gitea_url=str(gt.get("url", "")).rstrip("/"),
|
|
72
|
+
gitea_client_id=str(gt.get("client_id", "")),
|
|
73
|
+
gitea_client_secret=str(gt.get("client_secret", "")),
|
|
62
74
|
allowed_users=[str(u) for u in (raw.get("allowed_users", []) or [])],
|
|
63
75
|
home=home,
|
|
64
76
|
)
|
|
@@ -74,6 +86,12 @@ def load_settings(home: str | None = None) -> ServerSettings:
|
|
|
74
86
|
s.github_client_id = env["CONFSYNC_GITHUB_CLIENT_ID"]
|
|
75
87
|
if env.get("CONFSYNC_GITHUB_CLIENT_SECRET"):
|
|
76
88
|
s.github_client_secret = env["CONFSYNC_GITHUB_CLIENT_SECRET"]
|
|
89
|
+
if env.get("CONFSYNC_GITEA_URL"):
|
|
90
|
+
s.gitea_url = env["CONFSYNC_GITEA_URL"].rstrip("/")
|
|
91
|
+
if env.get("CONFSYNC_GITEA_CLIENT_ID"):
|
|
92
|
+
s.gitea_client_id = env["CONFSYNC_GITEA_CLIENT_ID"]
|
|
93
|
+
if env.get("CONFSYNC_GITEA_CLIENT_SECRET"):
|
|
94
|
+
s.gitea_client_secret = env["CONFSYNC_GITEA_CLIENT_SECRET"]
|
|
77
95
|
if env.get("CONFSYNC_ALLOWED_USERS"):
|
|
78
96
|
s.allowed_users = [u.strip() for u in env["CONFSYNC_ALLOWED_USERS"].split(",") if u.strip()]
|
|
79
97
|
|
|
@@ -7,6 +7,7 @@ from __future__ import annotations
|
|
|
7
7
|
|
|
8
8
|
import hashlib
|
|
9
9
|
import os
|
|
10
|
+
import secrets
|
|
10
11
|
import sqlite3
|
|
11
12
|
import time
|
|
12
13
|
|
|
@@ -14,6 +15,7 @@ SCHEMA = """\
|
|
|
14
15
|
CREATE TABLE IF NOT EXISTS users (
|
|
15
16
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
16
17
|
github_id INTEGER NOT NULL UNIQUE,
|
|
18
|
+
gitea_id INTEGER,
|
|
17
19
|
login TEXT NOT NULL UNIQUE,
|
|
18
20
|
name TEXT NOT NULL DEFAULT '',
|
|
19
21
|
avatar_url TEXT NOT NULL DEFAULT '',
|
|
@@ -68,6 +70,12 @@ def hash_token(token: str) -> str:
|
|
|
68
70
|
return hashlib.sha256(token.encode()).hexdigest()
|
|
69
71
|
|
|
70
72
|
|
|
73
|
+
def _placeholder_remote_id() -> int:
|
|
74
|
+
"""Negative random id standing in for a provider id the user hasn't
|
|
75
|
+
logged in with yet (see create_bootstrap_user / upsert_gitea_user)."""
|
|
76
|
+
return -(secrets.randbelow(2**62) + 1)
|
|
77
|
+
|
|
78
|
+
|
|
71
79
|
def connect(db_path: str) -> sqlite3.Connection:
|
|
72
80
|
os.makedirs(os.path.dirname(os.path.abspath(db_path)), exist_ok=True)
|
|
73
81
|
conn = sqlite3.connect(db_path)
|
|
@@ -75,15 +83,32 @@ def connect(db_path: str) -> sqlite3.Connection:
|
|
|
75
83
|
conn.execute("PRAGMA journal_mode=WAL")
|
|
76
84
|
conn.execute("PRAGMA foreign_keys=ON")
|
|
77
85
|
conn.executescript(SCHEMA)
|
|
86
|
+
_migrate(conn)
|
|
78
87
|
return conn
|
|
79
88
|
|
|
80
89
|
|
|
90
|
+
def _migrate(conn: sqlite3.Connection) -> None:
|
|
91
|
+
"""Schema migrations for databases created before Gitea support."""
|
|
92
|
+
cols = {r["name"] for r in conn.execute("PRAGMA table_info(users)")}
|
|
93
|
+
if "gitea_id" not in cols:
|
|
94
|
+
conn.execute("ALTER TABLE users ADD COLUMN gitea_id INTEGER")
|
|
95
|
+
conn.execute(
|
|
96
|
+
"CREATE UNIQUE INDEX IF NOT EXISTS idx_users_gitea_id"
|
|
97
|
+
" ON users (gitea_id) WHERE gitea_id IS NOT NULL"
|
|
98
|
+
)
|
|
99
|
+
conn.commit()
|
|
100
|
+
|
|
101
|
+
|
|
81
102
|
# ── users / allowlist ─────────────────────────────────────────────
|
|
82
103
|
|
|
83
104
|
def get_user_by_github_id(conn: sqlite3.Connection, github_id: int) -> sqlite3.Row | None:
|
|
84
105
|
return conn.execute("SELECT * FROM users WHERE github_id = ?", (github_id,)).fetchone()
|
|
85
106
|
|
|
86
107
|
|
|
108
|
+
def get_user_by_gitea_id(conn: sqlite3.Connection, gitea_id: int) -> sqlite3.Row | None:
|
|
109
|
+
return conn.execute("SELECT * FROM users WHERE gitea_id = ?", (gitea_id,)).fetchone()
|
|
110
|
+
|
|
111
|
+
|
|
87
112
|
def get_user_by_id(conn: sqlite3.Connection, user_id: int) -> sqlite3.Row | None:
|
|
88
113
|
return conn.execute("SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
|
|
89
114
|
|
|
@@ -143,12 +168,46 @@ def upsert_user(conn: sqlite3.Connection, github_id: int, login: str,
|
|
|
143
168
|
return get_user_by_id(conn, cur.lastrowid)
|
|
144
169
|
|
|
145
170
|
|
|
171
|
+
def upsert_gitea_user(conn: sqlite3.Connection, gitea_id: int, login: str,
|
|
172
|
+
name: str, avatar_url: str) -> sqlite3.Row:
|
|
173
|
+
"""Insert or refresh a user from their Gitea profile (mirror of
|
|
174
|
+
upsert_user). A user row whose login matches but has no gitea_id yet
|
|
175
|
+
(CLI-bootstrapped or GitHub-only) adopts the gitea_id, keeping its
|
|
176
|
+
documents and API keys."""
|
|
177
|
+
existing = get_user_by_gitea_id(conn, gitea_id)
|
|
178
|
+
now = time.time()
|
|
179
|
+
if existing:
|
|
180
|
+
conn.execute(
|
|
181
|
+
"UPDATE users SET login = ?, name = ?, avatar_url = ? WHERE id = ?",
|
|
182
|
+
(login, name, avatar_url, existing["id"]),
|
|
183
|
+
)
|
|
184
|
+
conn.commit()
|
|
185
|
+
return get_user_by_id(conn, existing["id"])
|
|
186
|
+
by_login = conn.execute("SELECT * FROM users WHERE login = ?", (login,)).fetchone()
|
|
187
|
+
if by_login is not None and by_login["gitea_id"] is None:
|
|
188
|
+
conn.execute(
|
|
189
|
+
"UPDATE users SET gitea_id = ?, name = ?, avatar_url = ? WHERE id = ?",
|
|
190
|
+
(gitea_id, name, avatar_url, by_login["id"]),
|
|
191
|
+
)
|
|
192
|
+
conn.commit()
|
|
193
|
+
return get_user_by_id(conn, by_login["id"])
|
|
194
|
+
is_admin = 1 if count_users(conn) == 0 else 0
|
|
195
|
+
cur = conn.execute(
|
|
196
|
+
"INSERT INTO users (github_id, gitea_id, login, name, avatar_url, is_admin, created_at)"
|
|
197
|
+
" VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
198
|
+
(_placeholder_remote_id(), gitea_id, login, name, avatar_url, is_admin, now),
|
|
199
|
+
)
|
|
200
|
+
if is_admin:
|
|
201
|
+
conn.execute("INSERT OR IGNORE INTO allowed_logins (login) VALUES (?)", (login,))
|
|
202
|
+
conn.commit()
|
|
203
|
+
return get_user_by_id(conn, cur.lastrowid)
|
|
204
|
+
|
|
205
|
+
|
|
146
206
|
def create_bootstrap_user(conn: sqlite3.Connection, login: str) -> sqlite3.Row:
|
|
147
207
|
"""Create a CLI-only user (placeholder negative github_id) so an API key
|
|
148
|
-
can be issued before
|
|
149
|
-
adopts the real
|
|
150
|
-
|
|
151
|
-
|
|
208
|
+
can be issued before OAuth is set up. On first OAuth login (GitHub or
|
|
209
|
+
Gitea) the row adopts the real provider id (see upsert_user /
|
|
210
|
+
upsert_gitea_user)."""
|
|
152
211
|
existing = conn.execute("SELECT * FROM users WHERE login = ?", (login,)).fetchone()
|
|
153
212
|
if existing is not None:
|
|
154
213
|
return existing
|
|
@@ -156,7 +215,7 @@ def create_bootstrap_user(conn: sqlite3.Connection, login: str) -> sqlite3.Row:
|
|
|
156
215
|
cur = conn.execute(
|
|
157
216
|
"INSERT INTO users (github_id, login, name, avatar_url, is_admin, created_at)"
|
|
158
217
|
" VALUES (?, ?, '', '', ?, ?)",
|
|
159
|
-
(
|
|
218
|
+
(_placeholder_remote_id(), login, is_admin, time.time()),
|
|
160
219
|
)
|
|
161
220
|
conn.execute("INSERT OR IGNORE INTO allowed_logins (login) VALUES (?)", (login,))
|
|
162
221
|
conn.commit()
|
|
@@ -132,3 +132,11 @@ def test_web_requires_login(server):
|
|
|
132
132
|
resp = client.get("/", follow_redirects=False)
|
|
133
133
|
assert resp.status_code == 303
|
|
134
134
|
assert resp.headers["location"] == "/login"
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def test_version_endpoint_is_public(server):
|
|
138
|
+
client, _ = server
|
|
139
|
+
from confsync_server import __version__
|
|
140
|
+
resp = client.get("/api/v1/version") # no auth header
|
|
141
|
+
assert resp.status_code == 200
|
|
142
|
+
assert resp.json() == {"server": __version__}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""confsync-server backup command: hot sqlite snapshot + master key copy."""
|
|
2
|
+
import os
|
|
3
|
+
import sqlite3
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from confsync_server import db
|
|
8
|
+
from confsync_server.cli import cmd_backup
|
|
9
|
+
from confsync_server.config import load_settings
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_backup_captures_db_and_key(tmp_path, monkeypatch, capsys):
|
|
13
|
+
home = tmp_path / "home"
|
|
14
|
+
monkeypatch.setenv("CONFSYNC_HOME", str(home))
|
|
15
|
+
settings = load_settings()
|
|
16
|
+
|
|
17
|
+
conn = db.connect(settings.db_path)
|
|
18
|
+
db.upsert_user(conn, 1001, "alice", "Alice", "")
|
|
19
|
+
conn.close()
|
|
20
|
+
(home / "master.key").write_bytes(b"k" * 32)
|
|
21
|
+
|
|
22
|
+
out = tmp_path / "backup"
|
|
23
|
+
cmd_backup(type("Args", (), {"out": str(out)})())
|
|
24
|
+
|
|
25
|
+
# db snapshot is a consistent, usable copy
|
|
26
|
+
snap = sqlite3.connect(str(out / "server.db"))
|
|
27
|
+
snap.row_factory = sqlite3.Row
|
|
28
|
+
row = snap.execute("SELECT login FROM users").fetchone()
|
|
29
|
+
snap.close()
|
|
30
|
+
assert row["login"] == "alice"
|
|
31
|
+
|
|
32
|
+
# master key copied with restrictive permissions
|
|
33
|
+
assert (out / "master.key").read_bytes() == b"k" * 32
|
|
34
|
+
mode = os.stat(out / "master.key").st_mode & 0o777
|
|
35
|
+
assert mode == 0o600
|
|
36
|
+
|
|
37
|
+
assert "backup written to" in capsys.readouterr().out
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_backup_notes_env_master_key(tmp_path, monkeypatch, capsys):
|
|
41
|
+
home = tmp_path / "home"
|
|
42
|
+
home.mkdir()
|
|
43
|
+
monkeypatch.setenv("CONFSYNC_HOME", str(home))
|
|
44
|
+
monkeypatch.setenv("CONFSYNC_MASTER_KEY",
|
|
45
|
+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB")
|
|
46
|
+
|
|
47
|
+
db.connect(load_settings().db_path).close()
|
|
48
|
+
cmd_backup(type("Args", (), {"out": str(tmp_path / "bk")})())
|
|
49
|
+
|
|
50
|
+
out = capsys.readouterr().out
|
|
51
|
+
assert "back it up separately" in out
|
|
52
|
+
assert not (tmp_path / "bk" / "master.key").exists()
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_backup_default_path_is_timestamped(tmp_path, monkeypatch, capsys):
|
|
56
|
+
monkeypatch.setenv("CONFSYNC_HOME", str(tmp_path / "home"))
|
|
57
|
+
settings = load_settings()
|
|
58
|
+
db.connect(settings.db_path).close()
|
|
59
|
+
|
|
60
|
+
cmd_backup(type("Args", (), {"out": None})())
|
|
61
|
+
|
|
62
|
+
backups = list((tmp_path / "home" / "backups").iterdir())
|
|
63
|
+
assert len(backups) == 1
|
|
64
|
+
assert backups[0].name.startswith("confsync-")
|
|
65
|
+
assert (backups[0] / "server.db").exists()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_backup_missing_db_exits(tmp_path, monkeypatch, capsys):
|
|
69
|
+
monkeypatch.setenv("CONFSYNC_HOME", str(tmp_path / "empty"))
|
|
70
|
+
with pytest.raises(SystemExit):
|
|
71
|
+
cmd_backup(type("Args", (), {"out": None})())
|
|
72
|
+
assert "no database" in capsys.readouterr().err
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"""Gitea login: OAuth web flow, access-token API auth, db upsert/migration."""
|
|
2
|
+
import sqlite3
|
|
3
|
+
from urllib.parse import parse_qs, urlparse
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
from starlette.testclient import TestClient
|
|
7
|
+
|
|
8
|
+
from confsync_server import auth, db
|
|
9
|
+
from confsync_server.app import create_app
|
|
10
|
+
from confsync_server.config import ServerSettings, load_settings
|
|
11
|
+
|
|
12
|
+
GITEA_URL = "http://git.example.com"
|
|
13
|
+
GITEA_PROFILE = {"id": 7, "login": "gitea-user", "name": "Gitea User",
|
|
14
|
+
"avatar_url": f"{GITEA_URL}/avatars/7"}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@pytest.fixture()
|
|
18
|
+
def gitea_server(tmp_path):
|
|
19
|
+
settings = ServerSettings(
|
|
20
|
+
home=str(tmp_path),
|
|
21
|
+
external_url="http://testserver",
|
|
22
|
+
github_client_id="gh-id",
|
|
23
|
+
gitea_url=GITEA_URL,
|
|
24
|
+
gitea_client_id="gt-id",
|
|
25
|
+
gitea_client_secret="gt-secret",
|
|
26
|
+
)
|
|
27
|
+
app = create_app(settings)
|
|
28
|
+
with TestClient(app) as client:
|
|
29
|
+
yield client
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _patch_fetch(monkeypatch, token="good-token", profile=GITEA_PROFILE):
|
|
33
|
+
async def fake_fetch(gitea_url, access_token):
|
|
34
|
+
if access_token != token:
|
|
35
|
+
raise ValueError("Gitea rejected the access token (invalid or revoked).")
|
|
36
|
+
return dict(profile)
|
|
37
|
+
monkeypatch.setattr(auth, "fetch_gitea_user", fake_fetch)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _oauth_state(client) -> str:
|
|
41
|
+
resp = client.get("/auth/gitea/login", follow_redirects=False)
|
|
42
|
+
assert resp.status_code == 307
|
|
43
|
+
loc = resp.headers["location"]
|
|
44
|
+
assert loc.startswith(f"{GITEA_URL}/login/oauth/authorize")
|
|
45
|
+
return parse_qs(urlparse(loc).query)["state"][0]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# ── settings ──────────────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
def test_settings_gitea_yaml_and_env(tmp_path, monkeypatch):
|
|
51
|
+
home = tmp_path / "home"
|
|
52
|
+
home.mkdir()
|
|
53
|
+
(home / "server.yaml").write_text(
|
|
54
|
+
"gitea:\n url: https://git.example.com/\n client_id: id1\n client_secret: sec1\n"
|
|
55
|
+
)
|
|
56
|
+
monkeypatch.setenv("CONFSYNC_HOME", str(home))
|
|
57
|
+
s = load_settings()
|
|
58
|
+
assert s.gitea_url == "https://git.example.com" # trailing slash stripped
|
|
59
|
+
assert s.gitea_client_id == "id1"
|
|
60
|
+
assert s.gitea_configured
|
|
61
|
+
|
|
62
|
+
monkeypatch.setenv("CONFSYNC_GITEA_CLIENT_ID", "id2")
|
|
63
|
+
s = load_settings()
|
|
64
|
+
assert s.gitea_client_id == "id2"
|
|
65
|
+
|
|
66
|
+
# a home without gitea config is not configured
|
|
67
|
+
bare = tmp_path / "bare"
|
|
68
|
+
bare.mkdir()
|
|
69
|
+
monkeypatch.setenv("CONFSYNC_HOME", str(bare))
|
|
70
|
+
assert not load_settings().gitea_configured
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_build_gitea_authorize_url():
|
|
74
|
+
url = auth.build_gitea_authorize_url(
|
|
75
|
+
GITEA_URL, "cid", "https://cb.example.com/cb", "st ate"
|
|
76
|
+
)
|
|
77
|
+
parts = urlparse(url)
|
|
78
|
+
assert f"{parts.scheme}://{parts.netloc}{parts.path}" == f"{GITEA_URL}/login/oauth/authorize"
|
|
79
|
+
assert parse_qs(parts.query) == {
|
|
80
|
+
"client_id": ["cid"],
|
|
81
|
+
"redirect_uri": ["https://cb.example.com/cb"],
|
|
82
|
+
"response_type": ["code"],
|
|
83
|
+
"state": ["st ate"],
|
|
84
|
+
"scope": ["read:user"],
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# ── db ────────────────────────────────────────────────────────────
|
|
89
|
+
|
|
90
|
+
def test_gitea_first_user_becomes_admin(tmp_path):
|
|
91
|
+
conn = db.connect(str(tmp_path / "t.db"))
|
|
92
|
+
assert db.is_login_allowed(conn, "anyone") # empty db → bootstrap open
|
|
93
|
+
user = db.upsert_gitea_user(conn, 7, "gitea-user", "Gitea User", "")
|
|
94
|
+
assert user["is_admin"] == 1
|
|
95
|
+
assert user["gitea_id"] == 7
|
|
96
|
+
assert user["github_id"] < 0 # placeholder until a GitHub login links it
|
|
97
|
+
|
|
98
|
+
# refresh by gitea_id updates the profile, no duplicate row
|
|
99
|
+
again = db.upsert_gitea_user(conn, 7, "gitea-user", "Renamed", "")
|
|
100
|
+
assert again["id"] == user["id"]
|
|
101
|
+
assert again["name"] == "Renamed"
|
|
102
|
+
assert db.count_users(conn) == 1
|
|
103
|
+
conn.close()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_bootstrap_user_adopts_gitea_id(tmp_path):
|
|
107
|
+
conn = db.connect(str(tmp_path / "t.db"))
|
|
108
|
+
boot = db.create_bootstrap_user(conn, "carol")
|
|
109
|
+
user = db.upsert_gitea_user(conn, 3003, "carol", "Carol", "")
|
|
110
|
+
assert user["id"] == boot["id"]
|
|
111
|
+
assert user["gitea_id"] == 3003
|
|
112
|
+
assert db.count_users(conn) == 1
|
|
113
|
+
conn.close()
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def test_gitea_id_is_unique(tmp_path):
|
|
117
|
+
conn = db.connect(str(tmp_path / "t.db"))
|
|
118
|
+
db.upsert_gitea_user(conn, 9, "a", "A", "")
|
|
119
|
+
db.allow_login(conn, "b")
|
|
120
|
+
db.upsert_gitea_user(conn, 10, "b", "B", "")
|
|
121
|
+
with pytest.raises(sqlite3.IntegrityError):
|
|
122
|
+
conn.execute("UPDATE users SET gitea_id = 9 WHERE login = 'b'")
|
|
123
|
+
conn.close()
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def test_migration_adds_gitea_id_to_existing_db(tmp_path):
|
|
127
|
+
path = str(tmp_path / "old.db")
|
|
128
|
+
conn = sqlite3.connect(path)
|
|
129
|
+
conn.executescript("""\
|
|
130
|
+
CREATE TABLE users (
|
|
131
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
132
|
+
github_id INTEGER NOT NULL UNIQUE,
|
|
133
|
+
login TEXT NOT NULL UNIQUE,
|
|
134
|
+
name TEXT NOT NULL DEFAULT '',
|
|
135
|
+
avatar_url TEXT NOT NULL DEFAULT '',
|
|
136
|
+
is_admin INTEGER NOT NULL DEFAULT 0,
|
|
137
|
+
created_at REAL NOT NULL
|
|
138
|
+
);
|
|
139
|
+
INSERT INTO users (github_id, login, created_at) VALUES (1, 'old', 100);
|
|
140
|
+
""")
|
|
141
|
+
conn.commit()
|
|
142
|
+
conn.close()
|
|
143
|
+
|
|
144
|
+
conn = db.connect(path) # connect() applies the migration
|
|
145
|
+
cols = {r["name"] for r in conn.execute("PRAGMA table_info(users)")}
|
|
146
|
+
assert "gitea_id" in cols
|
|
147
|
+
# existing GitHub-only row links to the gitea account with the same login
|
|
148
|
+
user = db.upsert_gitea_user(conn, 55, "old", "Old", "")
|
|
149
|
+
assert user["id"] == 1
|
|
150
|
+
assert user["github_id"] == 1 and user["gitea_id"] == 55
|
|
151
|
+
conn.close()
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
# ── web: OAuth login ──────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
def test_login_page_shows_both_providers(gitea_server):
|
|
157
|
+
resp = gitea_server.get("/login")
|
|
158
|
+
assert resp.status_code == 200
|
|
159
|
+
assert "Sign in with GitHub" in resp.text
|
|
160
|
+
assert "Sign in with Gitea" in resp.text
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def test_gitea_login_redirects_with_state(gitea_server):
|
|
164
|
+
resp = gitea_server.get("/auth/gitea/login", follow_redirects=False)
|
|
165
|
+
assert resp.status_code == 307
|
|
166
|
+
q = parse_qs(urlparse(resp.headers["location"]).query)
|
|
167
|
+
assert q["client_id"] == ["gt-id"]
|
|
168
|
+
assert q["redirect_uri"] == ["http://testserver/auth/gitea/callback"]
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def test_gitea_oauth_callback_logs_in(gitea_server, monkeypatch):
|
|
172
|
+
async def fake_exchange(gitea_url, client_id, client_secret, code, redirect_uri):
|
|
173
|
+
assert (client_id, client_secret) == ("gt-id", "gt-secret")
|
|
174
|
+
assert redirect_uri == "http://testserver/auth/gitea/callback"
|
|
175
|
+
return "oauth-token"
|
|
176
|
+
|
|
177
|
+
_patch_fetch(monkeypatch, token="oauth-token")
|
|
178
|
+
monkeypatch.setattr(auth, "exchange_gitea_code", fake_exchange)
|
|
179
|
+
|
|
180
|
+
state = _oauth_state(gitea_server)
|
|
181
|
+
resp = gitea_server.get(f"/auth/gitea/callback?code=c&state={state}")
|
|
182
|
+
assert resp.status_code == 200 # followed the redirect to /
|
|
183
|
+
assert "gitea-user" in resp.text # nav shows the logged-in user
|
|
184
|
+
|
|
185
|
+
conn = db.connect(gitea_server.app.state.settings.db_path)
|
|
186
|
+
row = conn.execute("SELECT * FROM users WHERE login = 'gitea-user'").fetchone()
|
|
187
|
+
conn.close()
|
|
188
|
+
assert row["gitea_id"] == 7 and row["is_admin"] == 1
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def test_gitea_oauth_callback_rejects_bad_state(gitea_server):
|
|
192
|
+
resp = gitea_server.get("/auth/gitea/callback?code=c&state=bogus")
|
|
193
|
+
assert "Invalid OAuth state" in resp.text
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def test_gitea_oauth_denied_when_not_allowlisted(gitea_server, monkeypatch):
|
|
197
|
+
conn = db.connect(gitea_server.app.state.settings.db_path)
|
|
198
|
+
db.upsert_user(conn, 1001, "admin", "Admin", "") # users exist → allowlist applies
|
|
199
|
+
conn.close()
|
|
200
|
+
|
|
201
|
+
async def fake_exchange(gitea_url, client_id, client_secret, code, redirect_uri):
|
|
202
|
+
return "oauth-token"
|
|
203
|
+
|
|
204
|
+
_patch_fetch(monkeypatch, token="oauth-token")
|
|
205
|
+
monkeypatch.setattr(auth, "exchange_gitea_code", fake_exchange)
|
|
206
|
+
|
|
207
|
+
state = _oauth_state(gitea_server)
|
|
208
|
+
resp = gitea_server.get(f"/auth/gitea/callback?code=c&state={state}")
|
|
209
|
+
assert "not on the allowlist" in resp.text
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
# ── REST API: Gitea access tokens ─────────────────────────────────
|
|
213
|
+
|
|
214
|
+
def test_api_accepts_gitea_token_with_cache(gitea_server, monkeypatch):
|
|
215
|
+
calls = {"gitea_hits": 0}
|
|
216
|
+
|
|
217
|
+
async def fake_fetch(gitea_url, access_token):
|
|
218
|
+
calls["gitea_hits"] += 1
|
|
219
|
+
if access_token != "gt-token":
|
|
220
|
+
raise ValueError("Gitea rejected the access token (invalid or revoked).")
|
|
221
|
+
return dict(GITEA_PROFILE)
|
|
222
|
+
|
|
223
|
+
monkeypatch.setattr(auth, "fetch_gitea_user", fake_fetch)
|
|
224
|
+
h = {"Authorization": "Bearer gt-token"}
|
|
225
|
+
|
|
226
|
+
resp = gitea_server.get("/api/v1/whoami", headers=h)
|
|
227
|
+
assert resp.status_code == 200
|
|
228
|
+
assert resp.json() == {"login": "gitea-user", "name": "Gitea User"}
|
|
229
|
+
|
|
230
|
+
# second call is served from the cache — Gitea hit only once
|
|
231
|
+
assert gitea_server.get("/api/v1/documents", headers=h).status_code == 200
|
|
232
|
+
assert calls["gitea_hits"] == 1
|
|
233
|
+
|
|
234
|
+
# the token drives the full document lifecycle
|
|
235
|
+
gitea_server.put("/api/v1/documents/app/cfg", json={"content": "x"}, headers=h)
|
|
236
|
+
resp = gitea_server.get("/api/v1/documents/app/cfg", headers=h)
|
|
237
|
+
assert resp.json()["content"] == "x"
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def test_api_rejects_invalid_gitea_token(gitea_server, monkeypatch):
|
|
241
|
+
_patch_fetch(monkeypatch, token="good-token")
|
|
242
|
+
resp = gitea_server.get("/api/v1/whoami",
|
|
243
|
+
headers={"Authorization": "Bearer wrong"})
|
|
244
|
+
assert resp.status_code == 401
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def test_api_gitea_token_respects_allowlist(gitea_server, monkeypatch):
|
|
248
|
+
conn = db.connect(gitea_server.app.state.settings.db_path)
|
|
249
|
+
db.upsert_user(conn, 1001, "admin", "Admin", "") # users exist → allowlist applies
|
|
250
|
+
conn.close()
|
|
251
|
+
_patch_fetch(monkeypatch, token="good-token")
|
|
252
|
+
resp = gitea_server.get("/api/v1/whoami",
|
|
253
|
+
headers={"Authorization": "Bearer good-token"})
|
|
254
|
+
assert resp.status_code == 403
|
|
255
|
+
assert "not on the allowlist" in resp.json()["error"]
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def test_api_non_cs_token_rejected_without_gitea(tmp_path):
|
|
259
|
+
settings = ServerSettings(home=str(tmp_path), external_url="http://testserver",
|
|
260
|
+
github_client_id="gh-id")
|
|
261
|
+
app = create_app(settings)
|
|
262
|
+
with TestClient(app) as client:
|
|
263
|
+
resp = client.get("/api/v1/whoami",
|
|
264
|
+
headers={"Authorization": "Bearer gitea-style-token"})
|
|
265
|
+
assert resp.status_code == 401
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
"""GitHub OAuth flow and token/API-key generation helpers."""
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
import secrets
|
|
5
|
-
import string
|
|
6
|
-
|
|
7
|
-
import httpx
|
|
8
|
-
|
|
9
|
-
GITHUB_AUTHORIZE_URL = "https://github.com/login/oauth/authorize"
|
|
10
|
-
GITHUB_TOKEN_URL = "https://github.com/login/oauth/access_token"
|
|
11
|
-
GITHUB_USER_URL = "https://api.github.com/user"
|
|
12
|
-
|
|
13
|
-
_KEY_ALPHABET = string.ascii_letters + string.digits
|
|
14
|
-
KEY_PREFIX = "cs_"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def generate_api_key() -> str:
|
|
18
|
-
"""New API key: ``cs_`` + 48 random base62 chars. Plaintext is shown to
|
|
19
|
-
the user once; only its sha256 is stored server-side."""
|
|
20
|
-
return KEY_PREFIX + "".join(secrets.choice(_KEY_ALPHABET) for _ in range(48))
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def generate_session_token() -> str:
|
|
24
|
-
return secrets.token_urlsafe(32)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def generate_oauth_state() -> str:
|
|
28
|
-
return secrets.token_urlsafe(24)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def build_authorize_url(client_id: str, redirect_uri: str, state: str) -> str:
|
|
32
|
-
return (
|
|
33
|
-
f"{GITHUB_AUTHORIZE_URL}?client_id={client_id}"
|
|
34
|
-
f"&redirect_uri={redirect_uri}&state={state}&scope=read:user"
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
async def exchange_code(client_id: str, client_secret: str, code: str,
|
|
39
|
-
redirect_uri: str) -> str:
|
|
40
|
-
"""Exchange an OAuth code for a GitHub access token."""
|
|
41
|
-
async with httpx.AsyncClient(timeout=20) as client:
|
|
42
|
-
resp = await client.post(
|
|
43
|
-
GITHUB_TOKEN_URL,
|
|
44
|
-
data={
|
|
45
|
-
"client_id": client_id,
|
|
46
|
-
"client_secret": client_secret,
|
|
47
|
-
"code": code,
|
|
48
|
-
"redirect_uri": redirect_uri,
|
|
49
|
-
},
|
|
50
|
-
headers={"Accept": "application/json"},
|
|
51
|
-
)
|
|
52
|
-
data = resp.json()
|
|
53
|
-
token = data.get("access_token")
|
|
54
|
-
if not token:
|
|
55
|
-
raise ValueError(f"GitHub token exchange failed: {data.get('error_description') or data}")
|
|
56
|
-
return token
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
async def fetch_github_user(access_token: str) -> dict:
|
|
60
|
-
"""Return the GitHub profile: {id, login, name, avatar_url}."""
|
|
61
|
-
async with httpx.AsyncClient(timeout=20) as client:
|
|
62
|
-
resp = await client.get(
|
|
63
|
-
GITHUB_USER_URL,
|
|
64
|
-
headers={
|
|
65
|
-
"Authorization": f"Bearer {access_token}",
|
|
66
|
-
"Accept": "application/vnd.github+json",
|
|
67
|
-
},
|
|
68
|
-
)
|
|
69
|
-
if resp.status_code != 200:
|
|
70
|
-
raise ValueError(f"GitHub user fetch failed: HTTP {resp.status_code}")
|
|
71
|
-
data = resp.json()
|
|
72
|
-
return {
|
|
73
|
-
"id": int(data["id"]),
|
|
74
|
-
"login": str(data["login"]),
|
|
75
|
-
"name": str(data.get("name") or ""),
|
|
76
|
-
"avatar_url": str(data.get("avatar_url") or ""),
|
|
77
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|