aboutsummaryrefslogtreecommitdiff
path: root/gn3/auth/authorisation/users
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-09-07 15:49:00 +0300
committerFrederick Muriuki Muriithi2023-10-10 11:12:40 +0300
commit0a8279891190e49867d3a1d72db0f7c7cd275646 (patch)
tree9acceecfcf2667abeaac743e4c7f5139fd5e0afd /gn3/auth/authorisation/users
parente4af0bbac585b46a5d6303d752cea18ca527d676 (diff)
downloadgenenetwork3-0a8279891190e49867d3a1d72db0f7c7cd275646.tar.gz
Remove authentication from GN3
Authentication should be handled by the auth server (gn-auth) and thus, this commit removes code handling user authentication from the GN3 system.
Diffstat (limited to 'gn3/auth/authorisation/users')
-rw-r--r--gn3/auth/authorisation/users/__init__.py12
-rw-r--r--gn3/auth/authorisation/users/admin/__init__.py2
-rw-r--r--gn3/auth/authorisation/users/admin/ui.py27
-rw-r--r--gn3/auth/authorisation/users/admin/views.py230
-rw-r--r--gn3/auth/authorisation/users/base.py128
-rw-r--r--gn3/auth/authorisation/users/collections/views.py4
-rw-r--r--gn3/auth/authorisation/users/masquerade/__init__.py1
-rw-r--r--gn3/auth/authorisation/users/masquerade/models.py67
-rw-r--r--gn3/auth/authorisation/users/masquerade/views.py48
-rw-r--r--gn3/auth/authorisation/users/models.py2
-rw-r--r--gn3/auth/authorisation/users/views.py9
11 files changed, 146 insertions, 384 deletions
diff --git a/gn3/auth/authorisation/users/__init__.py b/gn3/auth/authorisation/users/__init__.py
index e69de29..5f0c89c 100644
--- a/gn3/auth/authorisation/users/__init__.py
+++ b/gn3/auth/authorisation/users/__init__.py
@@ -0,0 +1,12 @@
+"""Initialise the users' package."""
+from .base import (
+ User,
+ users,
+ save_user,
+ user_by_id,
+ # valid_login,
+ user_by_email,
+ hash_password, # only used in tests... maybe make gn-auth a GN3 dependency
+ same_password,
+ set_user_password
+)
diff --git a/gn3/auth/authorisation/users/admin/__init__.py b/gn3/auth/authorisation/users/admin/__init__.py
deleted file mode 100644
index 8aa0743..0000000
--- a/gn3/auth/authorisation/users/admin/__init__.py
+++ /dev/null
@@ -1,2 +0,0 @@
-"""The admin module"""
-from .views import admin
diff --git a/gn3/auth/authorisation/users/admin/ui.py b/gn3/auth/authorisation/users/admin/ui.py
deleted file mode 100644
index 242c7a6..0000000
--- a/gn3/auth/authorisation/users/admin/ui.py
+++ /dev/null
@@ -1,27 +0,0 @@
-"""UI utilities for the auth system."""
-from functools import wraps
-from flask import flash, url_for, redirect
-
-from gn3.auth.authentication.users import User
-from gn3.auth.db_utils import with_db_connection
-from gn3.auth.authorisation.roles.models import user_roles
-
-from gn3.session import logged_in, session_user, clear_session_info
-
-def is_admin(func):
- """Verify user is a system admin."""
- @wraps(func)
- @logged_in
- def __admin__(*args, **kwargs):
- admin_roles = [
- role for role in with_db_connection(
- lambda conn: user_roles(
- conn, User(**session_user())))
- if role.role_name == "system-administrator"]
- if len(admin_roles) > 0:
- return func(*args, **kwargs)
- flash("Expected a system administrator.", "alert-danger")
- flash("You have been logged out of the system.", "alert-info")
- clear_session_info()
- return redirect(url_for("oauth2.admin.login"))
- return __admin__
diff --git a/gn3/auth/authorisation/users/admin/views.py b/gn3/auth/authorisation/users/admin/views.py
deleted file mode 100644
index c9f1887..0000000
--- a/gn3/auth/authorisation/users/admin/views.py
+++ /dev/null
@@ -1,230 +0,0 @@
-"""UI for admin stuff"""
-import uuid
-import json
-import random
-import string
-from functools import partial
-from datetime import datetime, timezone, timedelta
-
-from email_validator import validate_email, EmailNotValidError
-from flask import (
- flash,
- request,
- url_for,
- redirect,
- Blueprint,
- current_app,
- render_template)
-
-
-from gn3 import session
-from gn3.auth import db
-from gn3.auth.db_utils import with_db_connection
-
-from gn3.auth.authentication.oauth2.models.oauth2client import (
- save_client,
- OAuth2Client,
- oauth2_clients,
- client as oauth2_client,
- delete_client as _delete_client)
-from gn3.auth.authentication.users import (
- User,
- user_by_id,
- valid_login,
- user_by_email,
- hash_password)
-
-from .ui import is_admin
-
-admin = Blueprint("admin", __name__)
-
-@admin.before_request
-def update_expires():
- """Update session expiration."""
- if session.session_info() and not session.update_expiry():
- flash("Session has expired. Logging out...", "alert-warning")
- session.clear_session_info()
- return redirect(url_for("oauth2.admin.login"))
- return None
-
-@admin.route("/dashboard", methods=["GET"])
-@is_admin
-def dashboard():
- """Admin dashboard."""
- return render_template("admin/dashboard.html")
-
-@admin.route("/login", methods=["GET", "POST"])
-def login():
- """Log in to GN3 directly without OAuth2 client."""
- if request.method == "GET":
- return render_template(
- "admin/login.html",
- next_uri=request.args.get("next", "oauth2.admin.dashboard"))
-
- form = request.form
- next_uri = form.get("next_uri", "oauth2.admin.dashboard")
- error_message = "Invalid email or password provided."
- login_page = redirect(url_for("oauth2.admin.login", next=next_uri))
- try:
- email = validate_email(form.get("email", "").strip(),
- check_deliverability=False)
- password = form.get("password")
- with db.connection(current_app.config["AUTH_DB"]) as conn:
- user = user_by_email(conn, email["email"])
- if valid_login(conn, user, password):
- session.update_session_info(
- user=user._asdict(),
- expires=(
- datetime.now(tz=timezone.utc) + timedelta(minutes=10)))
- return redirect(url_for(next_uri))
- flash(error_message, "alert-danger")
- return login_page
- except EmailNotValidError as _enve:
- flash(error_message, "alert-danger")
- return login_page
-
-@admin.route("/logout", methods=["GET"])
-def logout():
- """Log out the admin."""
- if not session.session_info():
- flash("Not logged in.", "alert-info")
- return redirect(url_for("oauth2.admin.login"))
- session.clear_session_info()
- flash("Logged out", "alert-success")
- return redirect(url_for("oauth2.admin.login"))
-
-def random_string(length: int = 64) -> str:
- """Generate a random string."""
- return "".join(
- random.choice(string.ascii_letters + string.digits + string.punctuation)
- for _idx in range(0, length))
-
-def __response_types__(grant_types: tuple[str, ...]) -> tuple[str, ...]:
- """Compute response types from grant types."""
- resps = {
- "password": ("token",),
- "authorization_code": ("token", "code"),
- "refresh_token": ("token",)
- }
- return tuple(set(
- resp_typ for types_list
- in (types for grant, types in resps.items() if grant in grant_types)
- for resp_typ in types_list))
-
-@admin.route("/register-client", methods=["GET", "POST"])
-@is_admin
-def register_client():
- """Register an OAuth2 client."""
- def __list_users__(conn):
- with db.cursor(conn) as cursor:
- cursor.execute("SELECT * FROM users")
- return tuple(
- User(uuid.UUID(row["user_id"]), row["email"], row["name"])
- for row in cursor.fetchall())
- if request.method == "GET":
- return render_template(
- "admin/register-client.html",
- scope=current_app.config["OAUTH2_SCOPE"],
- users=with_db_connection(__list_users__),
- current_user=session.session_user())
-
- form = request.form
- raw_client_secret = random_string()
- default_redirect_uri = form["redirect_uri"]
- grant_types = form.getlist("grants[]")
- client = OAuth2Client(
- client_id = uuid.uuid4(),
- client_secret = hash_password(raw_client_secret),
- client_id_issued_at = datetime.now(tz=timezone.utc),
- client_secret_expires_at = datetime.fromtimestamp(0),
- client_metadata = {
- "client_name": "GN2 Dev Server",
- "token_endpoint_auth_method": [
- "client_secret_post", "client_secret_basic"],
- "client_type": "confidential",
- "grant_types": ["password", "authorization_code", "refresh_token"],
- "default_redirect_uri": default_redirect_uri,
- "redirect_uris": [default_redirect_uri] + form.get("other_redirect_uri", "").split(),
- "response_type": __response_types__(tuple(grant_types)),
- "scope": form.getlist("scope[]")
- },
- user = with_db_connection(partial(
- user_by_id, user_id=uuid.UUID(form["user"])))
- )
- client = with_db_connection(partial(save_client, the_client=client))
- return render_template(
- "admin/registered-client.html",
- client=client,
- client_secret = raw_client_secret)
-
-def __parse_client__(sqlite3_row) -> dict:
- """Parse the client details into python datatypes."""
- return {
- **dict(sqlite3_row),
- "client_metadata": json.loads(sqlite3_row["client_metadata"])
- }
-
-@admin.route("/list-client", methods=["GET"])
-@is_admin
-def list_clients():
- """List all registered OAuth2 clients."""
- return render_template(
- "admin/list-oauth2-clients.html",
- clients=with_db_connection(oauth2_clients))
-
-@admin.route("/view-client/<uuid:client_id>", methods=["GET"])
-@is_admin
-def view_client(client_id: uuid.UUID):
- """View details of OAuth2 client with given `client_id`."""
- return render_template(
- "admin/view-oauth2-client.html",
- client=with_db_connection(partial(oauth2_client, client_id=client_id)),
- scope=current_app.config["OAUTH2_SCOPE"])
-
-@admin.route("/edit-client", methods=["POST"])
-@is_admin
-def edit_client():
- """Edit the details of the given client."""
- form = request.form
- the_client = with_db_connection(partial(
- oauth2_client, client_id=uuid.UUID(form["client_id"])))
- if the_client.is_nothing():
- flash("No such client.", "alert-danger")
- return redirect(url_for("oauth2.admin.list_clients"))
- the_client = the_client.value
- client_metadata = {
- **the_client.client_metadata,
- "default_redirect_uri": form["default_redirect_uri"],
- "redirect_uris": list(set(
- [form["default_redirect_uri"]] +
- form["other_redirect_uris"].split("\r\n"))),
- "grants": form.getlist("grants[]"),
- "scope": form.getlist("scope[]")
- }
- with_db_connection(partial(save_client, the_client=OAuth2Client(
- the_client.client_id,
- the_client.client_secret,
- the_client.client_id_issued_at,
- the_client.client_secret_expires_at,
- client_metadata,
- the_client.user)))
- flash("Client updated.", "alert-success")
- return redirect(url_for("oauth2.admin.view_client",
- client_id=the_client.client_id))
-
-@admin.route("/delete-client", methods=["POST"])
-@is_admin
-def delete_client():
- """Delete the details of the client."""
- form = request.form
- the_client = with_db_connection(partial(
- oauth2_client, client_id=uuid.UUID(form["client_id"])))
- if the_client.is_nothing():
- flash("No such client.", "alert-danger")
- return redirect(url_for("oauth2.admin.list_clients"))
- the_client = the_client.value
- with_db_connection(partial(_delete_client, client=the_client))
- flash((f"Client '{the_client.client_metadata.client_name}' was deleted "
- "successfully."),
- "alert-success")
- return redirect(url_for("oauth2.admin.list_clients"))
diff --git a/gn3/auth/authorisation/users/base.py b/gn3/auth/authorisation/users/base.py
new file mode 100644
index 0000000..0e72ed2
--- /dev/null
+++ b/gn3/auth/authorisation/users/base.py
@@ -0,0 +1,128 @@
+"""User-specific code and data structures."""
+from uuid import UUID, uuid4
+from typing import Any, Tuple, NamedTuple
+
+from argon2 import PasswordHasher
+from argon2.exceptions import VerifyMismatchError
+
+from gn3.auth import db
+from gn3.auth.authorisation.errors import NotFoundError
+
+class User(NamedTuple):
+ """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",
+ name="Dummy user to use as placeholder")
+
+def user_by_email(conn: db.DbConnection, email: str) -> User:
+ """Retrieve user from database by their email address"""
+ with db.cursor(conn) as cursor:
+ cursor.execute("SELECT * FROM users WHERE email=?", (email,))
+ row = cursor.fetchone()
+
+ if row:
+ return User(UUID(row["user_id"]), row["email"], row["name"])
+
+ raise NotFoundError(f"Could not find user with email {email}")
+
+def user_by_id(conn: db.DbConnection, user_id: UUID) -> User:
+ """Retrieve user from database by their user id"""
+ with db.cursor(conn) as cursor:
+ cursor.execute("SELECT * FROM users WHERE user_id=?", (str(user_id),))
+ row = cursor.fetchone()
+
+ if row:
+ return User(UUID(row["user_id"]), row["email"], row["name"])
+
+ raise NotFoundError(f"Could not find user with ID {user_id}")
+
+def same_password(password: str, hashed: str) -> bool:
+ """Check that `raw_password` is hashed to `hash`"""
+ try:
+ return hasher().verify(hashed, password)
+ except VerifyMismatchError as _vme:
+ return False
+
+def valid_login(conn: db.DbConnection, user: User, password: str) -> bool:
+ """Check the validity of the provided credentials for login."""
+ with db.cursor(conn) as cursor:
+ cursor.execute(
+ ("SELECT * FROM users LEFT JOIN user_credentials "
+ "ON users.user_id=user_credentials.user_id "
+ "WHERE users.user_id=?"),
+ (str(user.user_id),))
+ row = cursor.fetchone()
+
+ if row is None:
+ return False
+
+ return same_password(password, row["password"])
+
+def save_user(cursor: db.DbCursor, email: str, name: str) -> User:
+ """
+ Create and persist a user.
+
+ The user creation could be done during a transaction, therefore the function
+ takes a cursor object rather than a connection.
+
+ The newly created and persisted user is then returned.
+ """
+ user_id = uuid4()
+ cursor.execute("INSERT INTO users VALUES (?, ?, ?)",
+ (str(user_id), email, name))
+ return User(user_id, email, name)
+
+def hasher():
+ """Retrieve PasswordHasher object"""
+ # TODO: Maybe tune the parameters here...
+ # Tuneable Parameters:
+ # - time_cost (default: 2)
+ # - memory_cost (default: 102400)
+ # - parallelism (default: 8)
+ # - hash_len (default: 16)
+ # - salt_len (default: 16)
+ # - encoding (default: 'utf-8')
+ # - type (default: <Type.ID: 2>)
+ return PasswordHasher()
+
+def hash_password(password):
+ """Hash the password."""
+ return hasher().hash(password)
+
+def set_user_password(
+ cursor: db.DbCursor, user: User, password: str) -> Tuple[User, bytes]:
+ """Set the given user's password in the database."""
+ hashed_password = hash_password(password)
+ cursor.execute(
+ ("INSERT INTO user_credentials VALUES (:user_id, :hash) "
+ "ON CONFLICT (user_id) DO UPDATE SET password=:hash"),
+ {"user_id": str(user.user_id), "hash": hashed_password})
+ return user, hashed_password
+
+def users(conn: db.DbConnection,
+ ids: tuple[UUID, ...] = tuple()) -> tuple[User, ...]:
+ """
+ Fetch all users with the given `ids`. If `ids` is empty, return ALL users.
+ """
+ params = ", ".join(["?"] * len(ids))
+ with db.cursor(conn) as cursor:
+ query = "SELECT * FROM users" + (
+ f" WHERE user_id IN ({params})"
+ if len(ids) > 0 else "")
+ print(query)
+ cursor.execute(query, tuple(str(the_id) for the_id in ids))
+ return tuple(User(UUID(row["user_id"]), row["email"], row["name"])
+ for row in cursor.fetchall())
+ return tuple()
diff --git a/gn3/auth/authorisation/users/collections/views.py b/gn3/auth/authorisation/users/collections/views.py
index 1fa25a3..775e8bc 100644
--- a/gn3/auth/authorisation/users/collections/views.py
+++ b/gn3/auth/authorisation/users/collections/views.py
@@ -9,8 +9,8 @@ from gn3.auth.db_utils import with_db_connection
from gn3.auth.authorisation.checks import require_json
from gn3.auth.authorisation.errors import NotFoundError
-from gn3.auth.authentication.users import User, user_by_id
-from gn3.auth.authentication.oauth2.resource_server import require_oauth
+from gn3.auth.authorisation.users import User, user_by_id
+from gn3.auth.authorisation.oauth2.resource_server import require_oauth
from .models import (
add_traits,
diff --git a/gn3/auth/authorisation/users/masquerade/__init__.py b/gn3/auth/authorisation/users/masquerade/__init__.py
deleted file mode 100644
index 69d64f0..0000000
--- a/gn3/auth/authorisation/users/masquerade/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-"""Package to deal with masquerading."""
diff --git a/gn3/auth/authorisation/users/masquerade/models.py b/gn3/auth/authorisation/users/masquerade/models.py
deleted file mode 100644
index 9f24b6b..0000000
--- a/gn3/auth/authorisation/users/masquerade/models.py
+++ /dev/null
@@ -1,67 +0,0 @@
-"""Functions for handling masquerade."""
-from uuid import uuid4
-from functools import wraps
-from datetime import datetime
-
-from flask import current_app as app
-
-from gn3.auth import db
-
-from gn3.auth.authorisation.errors import ForbiddenAccess
-from gn3.auth.authorisation.roles.models import user_roles
-
-from gn3.auth.authentication.users import User
-from gn3.auth.authentication.oauth2.models.oauth2token import (
- OAuth2Token, save_token)
-
-__FIVE_HOURS__ = (60 * 60 * 5)
-
-def can_masquerade(func):
- """Security decorator."""
- @wraps(func)
- def __checker__(*args, **kwargs):
- if len(args) == 3:
- conn, token, _masq_user = args
- elif len(args) == 2:
- conn, token = args
- elif len(args) == 1:
- conn = args[0]
- token = kwargs["original_token"]
- else:
- conn = kwargs["conn"]
- token = kwargs["original_token"]
-
- masq_privs = [priv for role in user_roles(conn, token.user)
- for priv in role.privileges
- if priv.privilege_id == "system:user:masquerade"]
- if len(masq_privs) == 0:
- raise ForbiddenAccess(
- "You do not have the ability to masquerade as another user.")
- return func(*args, **kwargs)
- return __checker__
-
-@can_masquerade
-def masquerade_as(
- conn: db.DbConnection,
- original_token: OAuth2Token,
- masqueradee: User) -> OAuth2Token:
- """Get a token that enables `masquerader` to act as `masqueradee`."""
- token_details = app.config["OAUTH2_SERVER"].generate_token(
- client=original_token.client,
- grant_type="authorization_code",
- user=masqueradee,
- expires_in=__FIVE_HOURS__,
- include_refresh_token=True)
- new_token = OAuth2Token(
- token_id=uuid4(),
- client=original_token.client,
- token_type=token_details["token_type"],
- access_token=token_details["access_token"],
- refresh_token=token_details.get("refresh_token"),
- scope=original_token.scope,
- revoked=False,
- issued_at=datetime.now(),
- expires_in=token_details["expires_in"],
- user=masqueradee)
- save_token(conn, new_token)
- return new_token
diff --git a/gn3/auth/authorisation/users/masquerade/views.py b/gn3/auth/authorisation/users/masquerade/views.py
deleted file mode 100644
index 43286a1..0000000
--- a/gn3/auth/authorisation/users/masquerade/views.py
+++ /dev/null
@@ -1,48 +0,0 @@
-"""Endpoints for user masquerade"""
-from uuid import UUID
-from functools import partial
-
-from flask import request, jsonify, Response, Blueprint
-
-from gn3.auth.db_utils import with_db_connection
-from gn3.auth.authorisation.errors import InvalidData
-from gn3.auth.authorisation.checks import require_json
-
-from gn3.auth.authentication.users import user_by_id
-from gn3.auth.authentication.oauth2.resource_server import require_oauth
-
-from .models import masquerade_as
-
-masq = Blueprint("masquerade", __name__)
-
-@masq.route("/", methods=["POST"])
-@require_oauth("profile user masquerade")
-@require_json
-def masquerade() -> Response:
- """Masquerade as a particular user."""
- with require_oauth.acquire("profile user masquerade") as token:
- masqueradee_id = UUID(request.json["masquerade_as"])#type: ignore[index]
- if masqueradee_id == token.user.user_id:
- raise InvalidData("You are not allowed to masquerade as yourself.")
-
- masq_user = with_db_connection(partial(
- user_by_id, user_id=masqueradee_id))
- def __masq__(conn):
- new_token = masquerade_as(conn, original_token=token, masqueradee=masq_user)
- return new_token
- def __dump_token__(tok):
- return {
- key: value for key, value in (tok._asdict().items())
- if key in ("access_token", "refresh_token", "expires_in",
- "token_type")
- }
- return jsonify({
- "original": {
- "user": token.user._asdict(),
- "token": __dump_token__(token)
- },
- "masquerade_as": {
- "user": masq_user._asdict(),
- "token": __dump_token__(with_db_connection(__masq__))
- }
- })
diff --git a/gn3/auth/authorisation/users/models.py b/gn3/auth/authorisation/users/models.py
index 89c1d22..0157154 100644
--- a/gn3/auth/authorisation/users/models.py
+++ b/gn3/auth/authorisation/users/models.py
@@ -7,7 +7,7 @@ from gn3.auth.authorisation.roles.models import Role
from gn3.auth.authorisation.checks import authorised_p
from gn3.auth.authorisation.privileges import Privilege
-from gn3.auth.authentication.users import User
+from .base import User
@authorised_p(
("system:user:list",),
diff --git a/gn3/auth/authorisation/users/views.py b/gn3/auth/authorisation/users/views.py
index 826e222..f75b51e 100644
--- a/gn3/auth/authorisation/users/views.py
+++ b/gn3/auth/authorisation/users/views.py
@@ -10,9 +10,11 @@ from flask import request, jsonify, Response, Blueprint, current_app
from gn3.auth import db
from gn3.auth.dictify import dictify
from gn3.auth.db_utils import with_db_connection
+from gn3.auth.authorisation.oauth2.resource_server import require_oauth
+from gn3.auth.authorisation.users import User, save_user, set_user_password
+from gn3.auth.authorisation.oauth2.oauth2token import token_by_access_token
from .models import list_users
-from .masquerade.views import masq
from .collections.views import collections
from ..groups.models import user_group as _user_group
@@ -21,12 +23,7 @@ from ..roles.models import assign_default_roles, user_roles as _user_roles
from ..errors import (
NotFoundError, UsernameError, PasswordError, UserRegistrationError)
-from ...authentication.oauth2.resource_server import require_oauth
-from ...authentication.users import User, save_user, set_user_password
-from ...authentication.oauth2.models.oauth2token import token_by_access_token
-
users = Blueprint("users", __name__)
-users.register_blueprint(masq, url_prefix="/masquerade")
users.register_blueprint(collections, url_prefix="/collections")
@users.route("/", methods=["GET"])