diff libervia/web/server/server.py @ 1592:291a7026cb2b

server: handle new registration link feature, following backend implementation
author Goffi <goffi@goffi.org>
date Sun, 10 Dec 2023 18:33:00 +0100
parents 9e3f7bf55749
children 93abef9a3548
line wrap: on
line diff
--- a/libervia/web/server/server.py	Sun Dec 10 11:01:57 2023 +0100
+++ b/libervia/web/server/server.py	Sun Dec 10 18:33:00 2023 +0100
@@ -980,13 +980,55 @@
         state = yield defer.ensureDeferred(self._logged(profile, request))
         defer.returnValue(state)
 
-    def register_new_account(self, request, login, password, email):
+    async def check_registration_id(self, request: server.Request) -> None:
+        """Check if a valid registration ID is found in request data
+
+        @param request: request used for account registration
+
+        @raise exceptions.NotFound: not valid registration ID has been found
+        """
+        try:
+            r_data = request.data
+            registration_id = r_data["registration_id"]
+        except (AttributeError, KeyError):
+            log.warning("Can't find registration ID")
+            raise exceptions.NotFound
+        if registration_id is None:
+            raise exceptions.NotFound
+        try:
+            registration_link_data = data_format.deserialise(
+                await self.bridge_call(
+                    "registration_link_get",
+                    registration_id
+                )
+            )
+        except BridgeException as e:
+            if e.classname == "NotFound":
+                log.warning(f"Invalid registration ID: {registration_id!r}")
+            else:
+                log.exception(f"Can't get registration ID data: {e}")
+            raise exceptions.NotFound
+        else:
+            log.info(
+                f"Using registration ID {registration_id!r} ("
+                f"{registration_link_data['recipient']})."
+            )
+
+
+    async def register_new_account(
+        self,
+        request: server.Request,
+        login: str,
+        password: str,
+        email: str
+    ) -> str:
         """Create a new account, or return error
-        @param request(server.Request): request linked to the session
-        @param login(unicode): new account requested login
-        @param email(unicode): new account email
-        @param password(unicode): new account password
-        @return(unicode): a constant indicating the state:
+
+        @param request: request linked to the session
+        @param login: new account requested login
+        @param email: new account email
+        @param password: new account password
+        @return: a constant indicating the state:
             - C.BAD_REQUEST: something is wrong in the request (bad arguments)
             - C.INVALID_INPUT: one of the data is not valid
             - C.REGISTRATION_SUCCEED: new account has been successfully registered
@@ -995,12 +1037,15 @@
         @raise PermissionError: registration is now allowed in server configuration
         """
         if not self.options["allow_registration"]:
-            log.warning(
-                _("Registration received while it is not allowed, hack attempt?")
-            )
-            raise failure.Failure(
-                exceptions.PermissionError("Registration is not allowed on this server")
-            )
+            try:
+                await self.check_registration_id(request)
+            except exceptions.NotFound:
+                log.warning(
+                    _("Registration received while it is not allowed, hack attempt?")
+                )
+                raise failure.Failure(
+                    exceptions.PermissionError("Registration is not allowed on this server")
+                )
 
         if (
             not re.match(C.REG_LOGIN_RE, login)
@@ -1009,12 +1054,10 @@
         ):
             return C.INVALID_INPUT
 
-        def registered(result):
-            return C.REGISTRATION_SUCCEED
-
-        def registering_error(failure_):
-            # FIXME: better error handling for bridge error is needed
-            status = failure_.value.fullname.split('.')[-1]
+        try:
+            await self.bridge_call("libervia_account_register", email, password, login)
+        except BridgeException as e:
+            status = e.classname
             if status == "ConflictError":
                 return C.ALREADY_EXISTS
             elif status == "InvalidCertificate":
@@ -1023,16 +1066,13 @@
                 return C.INTERNAL_ERROR
             else:
                 log.error(
-                    _("Unknown registering error status: {status}\n{traceback}").format(
-                        status=status, traceback=failure_.value.message
+                    _("Unknown registering error status: {status}\n{e}").format(
+                        status=status, e=e
                     )
                 )
                 return status
-
-        d = self.bridge_call("libervia_account_register", email, password, login)
-        d.addCallback(registered)
-        d.addErrback(registering_error)
-        return d
+        else:
+            return C.REGISTRATION_SUCCEED
 
     def addCleanup(self, callback, *args, **kwargs):
         """Add cleaning method to call when service is stopped