aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMunyoki Kilyungi2024-03-11 19:34:18 +0300
committerMunyoki Kilyungi2024-03-13 10:13:27 +0300
commit27d40788e2e2c8fbeb8873e895d77a76bbd49a45 (patch)
tree06fdcd39670332c743e590c85f8a99211f834205
parentb6970d21dd0f8e132f8625c09f8aa595350b0218 (diff)
downloadgn-auth-27d40788e2e2c8fbeb8873e895d77a76bbd49a45.tar.gz
Define User using a frozen dataclass.
* gn_auth/auth/authentication/users.py: Import dataclass. Remove NamedTuple and Tuple import. (User): Use a frozen dataclass. (User.get_user_id): Delete. (User.dictify): Ditto. * gn_auth/auth/authorisation/data/views.py: Import dataclasses.dict. (authorisation): Replace user._asdict() with asdict(user). (metadata_resources): Ditto. * gn_auth/auth/authorisation/resources/groups/views.py: (group_members): Replace dictify with asdict. * gn_auth/auth/authorisation/resources/models.py: Import dataclasses.asdict. (assign_resource_user): Replace dictify(user) with asdict(user). (unassign_resource_user): Ditto. * gn_auth/auth/authorisation/resources/views.py: (resource_users): Replace dictify with asdict. * gn_auth/auth/authorisation/users/masquerade/views.py: Import dataclasses.asdict. (masquerade): Replace masq_user._asdict() with asdict(masq_user). * gn_auth/auth/authorisation/users/views.py: (list_all_users): Replace dictify with asdict. Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r--gn_auth/auth/authentication/users.py14
-rw-r--r--gn_auth/auth/authorisation/data/views.py5
-rw-r--r--gn_auth/auth/authorisation/resources/groups/views.py4
-rw-r--r--gn_auth/auth/authorisation/resources/models.py5
-rw-r--r--gn_auth/auth/authorisation/resources/views.py2
-rw-r--r--gn_auth/auth/authorisation/users/masquerade/views.py3
-rw-r--r--gn_auth/auth/authorisation/users/views.py2
7 files changed, 17 insertions, 18 deletions
diff --git a/gn_auth/auth/authentication/users.py b/gn_auth/auth/authentication/users.py
index 46cd838..2f8caa1 100644
--- a/gn_auth/auth/authentication/users.py
+++ b/gn_auth/auth/authentication/users.py
@@ -1,6 +1,7 @@
"""User-specific code and data structures."""
from uuid import UUID, uuid4
-from typing import Any, Tuple, NamedTuple
+from typing import Tuple
+from dataclasses import dataclass
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError
@@ -8,19 +9,14 @@ from argon2.exceptions import VerifyMismatchError
from gn_auth.auth.db import sqlite3 as db
from gn_auth.auth.authorisation.errors import NotFoundError
-class User(NamedTuple):
+
+@dataclass(frozen=True)
+class User:
"""Class representing a user."""
user_id: UUID
email: str
name: str
- def get_user_id(self):
- """Return the user's UUID. Mostly for use with Authlib."""
- return self.user_id
-
- def dictify(self) -> dict[str, Any]:
- """Return a dict representation of `User` objects."""
- return {"user_id": self.user_id, "email": self.email, "name": self.name}
DUMMY_USER = User(user_id=UUID("a391cf60-e8b7-4294-bd22-ddbbda4b3530"),
email="gn3@dummy.user",
diff --git a/gn_auth/auth/authorisation/data/views.py b/gn_auth/auth/authorisation/data/views.py
index e5c8fd6..9d59a70 100644
--- a/gn_auth/auth/authorisation/data/views.py
+++ b/gn_auth/auth/authorisation/data/views.py
@@ -2,6 +2,7 @@
import sys
import uuid
import json
+from dataclasses import asdict
from typing import Any
from functools import partial
@@ -152,7 +153,7 @@ def authorisation() -> Response:
return jsonify(tuple(
{
- "user": user._asdict(),
+ "user": asdict(user),
**{key:trait[key] for key in ("trait_fullname", "trait_name")},
"dataset_name": trait["db"]["dataset_name"],
"dataset_type": __translate__(trait["db"]["dataset_type"]),
@@ -377,7 +378,7 @@ def metadata_resources() -> Response:
}
return jsonify(
{
- "user": user._asdict(),
+ "user": asdict(user),
"resource_id": resource_map.get(
request.json.get("name") #type: ignore[union-attr]
),
diff --git a/gn_auth/auth/authorisation/resources/groups/views.py b/gn_auth/auth/authorisation/resources/groups/views.py
index 96cfb67..26534fc 100644
--- a/gn_auth/auth/authorisation/resources/groups/views.py
+++ b/gn_auth/auth/authorisation/resources/groups/views.py
@@ -59,7 +59,7 @@ def create_group():
new_group = _create_group(
conn, group_name, user, request.form.get("group_description"))
return jsonify({
- **dictify(new_group), "group_leader": dictify(user)
+ **dictify(new_group), "group_leader": asdict(user)
})
@groups.route("/members/<uuid:group_id>", methods=["GET"])
@@ -71,7 +71,7 @@ def group_members(group_id: uuid.UUID) -> Response:
## Check that user has appropriate privileges and remove the pylint disable above
with db.connection(db_uri) as conn:
return jsonify(tuple(
- dictify(user) for user in _group_users(conn, group_id)))
+ asdict(user) for user in _group_users(conn, group_id)))
@groups.route("/requests/join/<uuid:group_id>", methods=["POST"])
@require_oauth("profile group")
diff --git a/gn_auth/auth/authorisation/resources/models.py b/gn_auth/auth/authorisation/resources/models.py
index 3693ad1..7ebf5f7 100644
--- a/gn_auth/auth/authorisation/resources/models.py
+++ b/gn_auth/auth/authorisation/resources/models.py
@@ -1,4 +1,5 @@
"""Handle the management of resources."""
+from dataclasses import asdict
from uuid import UUID, uuid4
from functools import reduce, partial
from sqlite3 import Row
@@ -341,7 +342,7 @@ def assign_resource_user(
str(resource.resource_id)))
return {
"resource": dictify(resource),
- "user": dictify(user),
+ "user": asdict(user),
"role": dictify(role),
"description": (
f"The user '{user.name}'({user.email}) was assigned the "
@@ -365,7 +366,7 @@ def unassign_resource_user(
str(resource.resource_id)))
return {
"resource": dictify(resource),
- "user": dictify(user),
+ "user": asdict(user),
"role": dictify(role),
"description": (
f"The user '{user.name}'({user.email}) had the "
diff --git a/gn_auth/auth/authorisation/resources/views.py b/gn_auth/auth/authorisation/resources/views.py
index c5da257..8976dfa 100644
--- a/gn_auth/auth/authorisation/resources/views.py
+++ b/gn_auth/auth/authorisation/resources/views.py
@@ -193,9 +193,9 @@ def resource_users(resource_id: uuid.UUID):
"users.")
results = (
{
- "user": dictify(row["user"]),
"user_group": dictify(row["user_group"]),
"roles": tuple(dictify(role) for role in row["roles"])
+ "user": asdict(row["user"]),
} for row in (
user_row for user_id, user_row
in with_db_connection(__the_users__).items()))
diff --git a/gn_auth/auth/authorisation/users/masquerade/views.py b/gn_auth/auth/authorisation/users/masquerade/views.py
index 259cdfe..b0464ba 100644
--- a/gn_auth/auth/authorisation/users/masquerade/views.py
+++ b/gn_auth/auth/authorisation/users/masquerade/views.py
@@ -1,4 +1,5 @@
"""Endpoints for user masquerade"""
+from dataclasses import asdict
from uuid import UUID
from functools import partial
@@ -42,7 +43,7 @@ def masquerade() -> Response:
"token": __dump_token__(token)
},
"masquerade_as": {
- "user": masq_user._asdict(),
+ "user": asdict(masq_user),
"token": __dump_token__(with_db_connection(__masq__))
}
})
diff --git a/gn_auth/auth/authorisation/users/views.py b/gn_auth/auth/authorisation/users/views.py
index bbb220e..9e6c0c3 100644
--- a/gn_auth/auth/authorisation/users/views.py
+++ b/gn_auth/auth/authorisation/users/views.py
@@ -178,4 +178,4 @@ def list_all_users() -> Response:
"""List all the users."""
with require_oauth.acquire("profile group") as _the_token:
return jsonify(tuple(
- dictify(user) for user in with_db_connection(list_users)))
+ asdict(user) for user in with_db_connection(list_users)))