aboutsummaryrefslogtreecommitdiffstats
path: root/paste
diff options
context:
space:
mode:
Diffstat (limited to 'paste')
-rw-r--r--paste/__init__.py25
-rw-r--r--paste/__main__.py29
-rw-r--r--paste/store.py6
3 files changed, 29 insertions, 31 deletions
diff --git a/paste/__init__.py b/paste/__init__.py
index 567aa8e..155e927 100644
--- a/paste/__init__.py
+++ b/paste/__init__.py
@@ -8,7 +8,8 @@ from functools import wraps
from typing import Optional
from wsgiref.util import application_uri, request_uri
-from . import store
+from . import db
+from .store import Store
from .types import (
App,
Closable,
@@ -134,10 +135,10 @@ def options(app: App, environ: Env, start_response: StartResponse) -> Response:
@middleware
-def open_store(app: App, environ: Env, start_response: StartResponse) -> Response:
+def open_database(app: App, environ: Env, start_response: StartResponse) -> Response:
db_path = environ.get("PASTE_DB", DB_PATH)
- with store.open(db_path) as stor:
- environ["paste.store"] = stor
+ with db.connect(db_path) as conn:
+ environ["paste.db_conn"] = conn
return app(environ, start_response)
@@ -154,7 +155,7 @@ def authenticate(app: App, environ: Env, start_response: StartResponse) -> Respo
value = b64decode(value.encode(), validate=True)
except (binascii.Error, UnicodeEncodeError):
return False
- return environ["paste.store"].check_token(value)
+ return Store(environ["paste.db_conn"]).check_token(value)
if environ["REQUEST_METHOD"] in {"GET", "HEAD"} or check_auth():
return app(environ, start_response)
@@ -169,13 +170,13 @@ def authenticate(app: App, environ: Env, start_response: StartResponse) -> Respo
@validate_method
@options
@if_none_match
-@open_store
+@open_database
@authenticate
def application(environ: Env, start_response: StartResponse) -> Response:
- stor = environ["paste.store"]
+ store = Store(environ["paste.db_conn"])
name = environ["PATH_INFO"]
if environ["REQUEST_METHOD"] == "GET":
- row = stor.get(name)
+ row = store.get(name)
if not row:
return simple_response(start_response, "404 Not Found")
content_type, content_hash, content = row
@@ -189,7 +190,7 @@ def application(environ: Env, start_response: StartResponse) -> Response:
)
return [content]
elif environ["REQUEST_METHOD"] == "HEAD":
- row = stor.head(name)
+ row = store.head(name)
if not row:
return simple_response(start_response, "404 Not Found")
content_type, content_hash, content_length = row
@@ -206,7 +207,7 @@ def application(environ: Env, start_response: StartResponse) -> Response:
content_type = environ.get("CONTENT_TYPE", "text/plain")
content_length = int(environ["CONTENT_LENGTH"])
content = environ["wsgi.input"].read(content_length)
- created, content_hash = stor.put(name, content, content_type)
+ created, content_hash = store.put(name, content, content_type)
start_response(
"201 Created" if created else "204 No Content",
[
@@ -219,7 +220,7 @@ def application(environ: Env, start_response: StartResponse) -> Response:
content_type = environ.get("CONTENT_TYPE", "text/plain")
content_length = int(environ["CONTENT_LENGTH"])
content = environ["wsgi.input"].read(content_length)
- path, content_hash = stor.post(name, content, content_type)
+ path, content_hash = store.post(name, content, content_type)
uri = application_uri(environ)
path = urllib.parse.quote(path)
if uri[-1] == "/" and path[:1] == "/":
@@ -234,7 +235,7 @@ def application(environ: Env, start_response: StartResponse) -> Response:
)
return []
elif environ["REQUEST_METHOD"] == "DELETE":
- if stor.delete(name):
+ if store.delete(name):
start_response("204 No Content", [])
return []
return simple_response(start_response, "404 Not Found")
diff --git a/paste/__main__.py b/paste/__main__.py
index 27ff72b..6e684c4 100644
--- a/paste/__main__.py
+++ b/paste/__main__.py
@@ -1,11 +1,12 @@
from base64 import b64decode, b64encode
-from contextlib import AbstractContextManager
+from collections.abc import Iterator
+from contextlib import contextmanager
from datetime import datetime, timezone
from os import getenv
from sys import argv, stderr
from wsgiref.simple_server import make_server
-from . import DB_PATH, application, store
+from . import DB_PATH, application, db, store
PROGRAM_NAME = "paste"
@@ -42,8 +43,10 @@ Environment:
db_path = getenv("PASTE_DB", DB_PATH)
-def open_db() -> AbstractContextManager[store.Store]:
- return store.open(db_path)
+@contextmanager
+def open_auth() -> Iterator[store.Auth]:
+ with db.connect(db_path) as conn:
+ yield store.Auth(conn)
def main():
@@ -54,11 +57,11 @@ def main():
httpd.serve_forever()
elif len(argv) == 2:
if argv[1] == "new-token":
- with open_db() as db:
- print(b64encode(db.generate_token()).decode())
+ with open_auth() as auth:
+ print(b64encode(auth.generate_token()).decode())
elif argv[1] == "list-tokens":
- with open_db() as db:
- for token_hash, created_at in db.get_tokens():
+ with open_auth() as auth:
+ for token_hash, created_at in auth.get_tokens():
created_at = datetime.fromtimestamp(created_at, timezone.utc)
print(f"{token_hash.hex()}\t{created_at.ctime()}")
elif argv[1] == "-h" or argv[1] == "--help":
@@ -70,12 +73,12 @@ def main():
elif len(argv) == 3:
if argv[1] == "delete-token":
token = argv[2]
- with open_db() as db:
+ with open_auth() as auth:
try:
try:
- db.delete_token(b64decode(token))
+ auth.delete_token(b64decode(token))
except ValueError:
- db.delete_token_hash(bytes.fromhex(token))
+ auth.delete_token_hash(bytes.fromhex(token))
except ValueError:
print("Malformed token", file=stderr)
exit(1)
@@ -83,9 +86,9 @@ def main():
print("Token not found", file=stderr)
exit(1)
elif argv[1] == "verify-token":
- with open_db() as db:
+ with open_auth() as auth:
try:
- if not db.check_token(b64decode(argv[2])):
+ if not auth.check_token(b64decode(argv[2])):
print("Token not found", file=stderr)
exit(1)
print("Found")
diff --git a/paste/store.py b/paste/store.py
index e7022f6..f69c03d 100644
--- a/paste/store.py
+++ b/paste/store.py
@@ -120,9 +120,3 @@ class Store:
"SELECT COUNT(*) FROM token WHERE hash = SHA256(?)", (token,)
).fetchone()
return count > 0
-
-
-@contextmanager
-def open(uri: str) -> Iterator[Store]:
- with db.connect(uri) as conn:
- yield Store(conn)