comparison 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
comparison
equal deleted inserted replaced
1591:f1d09a4d38dc 1592:291a7026cb2b
978 #  no, we have to create it 978 #  no, we have to create it
979 979
980 state = yield defer.ensureDeferred(self._logged(profile, request)) 980 state = yield defer.ensureDeferred(self._logged(profile, request))
981 defer.returnValue(state) 981 defer.returnValue(state)
982 982
983 def register_new_account(self, request, login, password, email): 983 async def check_registration_id(self, request: server.Request) -> None:
984 """Check if a valid registration ID is found in request data
985
986 @param request: request used for account registration
987
988 @raise exceptions.NotFound: not valid registration ID has been found
989 """
990 try:
991 r_data = request.data
992 registration_id = r_data["registration_id"]
993 except (AttributeError, KeyError):
994 log.warning("Can't find registration ID")
995 raise exceptions.NotFound
996 if registration_id is None:
997 raise exceptions.NotFound
998 try:
999 registration_link_data = data_format.deserialise(
1000 await self.bridge_call(
1001 "registration_link_get",
1002 registration_id
1003 )
1004 )
1005 except BridgeException as e:
1006 if e.classname == "NotFound":
1007 log.warning(f"Invalid registration ID: {registration_id!r}")
1008 else:
1009 log.exception(f"Can't get registration ID data: {e}")
1010 raise exceptions.NotFound
1011 else:
1012 log.info(
1013 f"Using registration ID {registration_id!r} ("
1014 f"{registration_link_data['recipient']})."
1015 )
1016
1017
1018 async def register_new_account(
1019 self,
1020 request: server.Request,
1021 login: str,
1022 password: str,
1023 email: str
1024 ) -> str:
984 """Create a new account, or return error 1025 """Create a new account, or return error
985 @param request(server.Request): request linked to the session 1026
986 @param login(unicode): new account requested login 1027 @param request: request linked to the session
987 @param email(unicode): new account email 1028 @param login: new account requested login
988 @param password(unicode): new account password 1029 @param email: new account email
989 @return(unicode): a constant indicating the state: 1030 @param password: new account password
1031 @return: a constant indicating the state:
990 - C.BAD_REQUEST: something is wrong in the request (bad arguments) 1032 - C.BAD_REQUEST: something is wrong in the request (bad arguments)
991 - C.INVALID_INPUT: one of the data is not valid 1033 - C.INVALID_INPUT: one of the data is not valid
992 - C.REGISTRATION_SUCCEED: new account has been successfully registered 1034 - C.REGISTRATION_SUCCEED: new account has been successfully registered
993 - C.ALREADY_EXISTS: the given profile already exists 1035 - C.ALREADY_EXISTS: the given profile already exists
994 - C.INTERNAL_ERROR or any unmanaged fault string 1036 - C.INTERNAL_ERROR or any unmanaged fault string
995 @raise PermissionError: registration is now allowed in server configuration 1037 @raise PermissionError: registration is now allowed in server configuration
996 """ 1038 """
997 if not self.options["allow_registration"]: 1039 if not self.options["allow_registration"]:
998 log.warning( 1040 try:
999 _("Registration received while it is not allowed, hack attempt?") 1041 await self.check_registration_id(request)
1000 ) 1042 except exceptions.NotFound:
1001 raise failure.Failure( 1043 log.warning(
1002 exceptions.PermissionError("Registration is not allowed on this server") 1044 _("Registration received while it is not allowed, hack attempt?")
1003 ) 1045 )
1046 raise failure.Failure(
1047 exceptions.PermissionError("Registration is not allowed on this server")
1048 )
1004 1049
1005 if ( 1050 if (
1006 not re.match(C.REG_LOGIN_RE, login) 1051 not re.match(C.REG_LOGIN_RE, login)
1007 or not re.match(C.REG_EMAIL_RE, email, re.IGNORECASE) 1052 or not re.match(C.REG_EMAIL_RE, email, re.IGNORECASE)
1008 or len(password) < C.PASSWORD_MIN_LENGTH 1053 or len(password) < C.PASSWORD_MIN_LENGTH
1009 ): 1054 ):
1010 return C.INVALID_INPUT 1055 return C.INVALID_INPUT
1011 1056
1012 def registered(result): 1057 try:
1013 return C.REGISTRATION_SUCCEED 1058 await self.bridge_call("libervia_account_register", email, password, login)
1014 1059 except BridgeException as e:
1015 def registering_error(failure_): 1060 status = e.classname
1016 # FIXME: better error handling for bridge error is needed
1017 status = failure_.value.fullname.split('.')[-1]
1018 if status == "ConflictError": 1061 if status == "ConflictError":
1019 return C.ALREADY_EXISTS 1062 return C.ALREADY_EXISTS
1020 elif status == "InvalidCertificate": 1063 elif status == "InvalidCertificate":
1021 return C.INVALID_CERTIFICATE 1064 return C.INVALID_CERTIFICATE
1022 elif status == "InternalError": 1065 elif status == "InternalError":
1023 return C.INTERNAL_ERROR 1066 return C.INTERNAL_ERROR
1024 else: 1067 else:
1025 log.error( 1068 log.error(
1026 _("Unknown registering error status: {status}\n{traceback}").format( 1069 _("Unknown registering error status: {status}\n{e}").format(
1027 status=status, traceback=failure_.value.message 1070 status=status, e=e
1028 ) 1071 )
1029 ) 1072 )
1030 return status 1073 return status
1031 1074 else:
1032 d = self.bridge_call("libervia_account_register", email, password, login) 1075 return C.REGISTRATION_SUCCEED
1033 d.addCallback(registered)
1034 d.addErrback(registering_error)
1035 return d
1036 1076
1037 def addCleanup(self, callback, *args, **kwargs): 1077 def addCleanup(self, callback, *args, **kwargs):
1038 """Add cleaning method to call when service is stopped 1078 """Add cleaning method to call when service is stopped
1039 1079
1040 cleaning method will be called in reverse order of they insertion 1080 cleaning method will be called in reverse order of they insertion