about summary refs log tree commit diff
diff options
context:
space:
mode:
authorClaude Sonnet 4.62026-08-26 18:24:49 +0000
committerFrederick Muriuki Muriithi2026-08-26 14:13:36 -0500
commit13119ccaa6de82076c384d7d83618e390f9b8e05 (patch)
tree18857480626f5098b6abb2c794ec3d1e9271f831
parent66da439e8ca5acd12a33d68f269cdcd454cd70b3 (diff)
downloadgn-auth-13119ccaa6de82076c384d7d83618e390f9b8e05.tar.gz
feat(users/admin): implement POST /auth/user/create endpoint
Parses email/name/password from the JSON body, calls create_verified_user,
and returns 201 with the new user's user_id, email, and name.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Reviewed-By: Frederick M. Muriithi <fredmanglis@gmail.com>

Added input validation.
-rw-r--r--gn_auth/auth/authorisation/users/views.py33
1 files changed, 32 insertions, 1 deletions
diff --git a/gn_auth/auth/authorisation/users/views.py b/gn_auth/auth/authorisation/users/views.py
index 544074a..cfc6720 100644
--- a/gn_auth/auth/authorisation/users/views.py
+++ b/gn_auth/auth/authorisation/users/views.py
@@ -61,6 +61,7 @@ from gn_auth.auth.authentication.oauth2.models.oauth2token import (
     token_by_access_token)
 
 from .models import list_users
+from .admin.models import create_verified_user
 from .masquerade.views import masq
 from .collections.views import collections
 
@@ -768,4 +769,34 @@ def create_new_user() -> Response:
                 for priv in role.privileges)):
             raise ForbiddenAccess(
                 "You need the `system:user:create-user` privilege.")
-    return make_response(jsonify({}), 501)
+        form = request_json()
+        errors = {}
+        try:
+            email = validate_email(
+                form.get("email", "").strip(), check_deliverability=False)
+        except EmailNotValidError as enve:
+            errors["email"] = (
+                f"E-Mail error: {'==>'.join(str(arg) for arg in enve.args)}")
+
+        try:
+            username = validate_username(form.get("name", "").strip())
+        except UsernameError as uerr:
+            errors["name"] = str(uerr.args[0])
+
+        try:
+            passwd = validate_password(
+                form.get("password", "").strip(),
+                form.get("password", "").strip())
+        except PasswordError as perr:
+            errors["password"] = str(perr.args[0])
+
+        if len(tuple(errors.keys())) > 0:
+            raise UserRegistrationError(tuple(errors.values()))
+
+        user = create_verified_user(conn, email.normalized, username, passwd)
+
+    return make_response(jsonify({
+        "user_id": str(user.user_id),
+        "email": user.email,
+        "name": user.name
+    }), 201)