async def _verify_json_req(request: web.Request, verify_func) -> Tuple[str, dict]: """ { 'code': 0, 'type': 'raffle', 'verification': {'signature': f'Hello World. This is {name} at {time}.', 'name': name, 'time': int}, 'data': {...} } """ try: json_data = await request.json() except json.JSONDecodeError: raise json_req_exceptions.ReqFormatError() except: raise json_req_exceptions.OtherError() if isinstance( json_data, dict) and 'verification' in json_data and 'data' in json_data: verification = json_data['verification'] if isinstance( verification, dict ) and 'signature' in verification and 'time' in verification: try: time = int(verification['time']) name = str(verification.get('name', 'super_admin')) verify_func(name=name, time=time, signature=verification['signature']) data = json_data['data'] if isinstance(data, dict): return name, data except RsaHandlerVerificationError: raise json_req_exceptions.VerificationError() except RsaHandlerTimeError: raise json_req_exceptions.TimeError() except: raise json_req_exceptions.DataError() raise json_req_exceptions.DataError()
async def create_key_handler(self, request): try: _, data = await self._verify_json_req(request, self._super_admin_pubkey) max_users = data.get('max_users', 3) available_days = data.get('available_days', 30) if not isinstance(max_users, int): raise json_req_exceptions.DataError() return web.json_response({ 'code': 0, 'type': '', 'data': { 'encrypted_key': self._create_key(max_users, available_days) } }) except json_req_exceptions.JsonReqError as e: return web.json_response(e.RSP_SUGGESTED)
async def create_key_handler(self, request: web.Request) -> web.StreamResponse: try: _, data = await self._verify_json_req( request, self._rsa_handler.verify_super_admin) max_users = data.get('max_users', 1) available_days = data.get('available_days', 30) if not isinstance(max_users, int) or not isinstance( available_days, int): raise json_req_exceptions.DataError() except json_req_exceptions.JsonReqError as e: return web.json_response(e.RSP_SUGGESTED) orig_key = self._key_handler.create_key(max_users, available_days) encrypted_key = self._rsa_handler.encrypt_super_admin(orig_key) return web.json_response({ 'code': 0, 'type': '', 'data': { 'encrypted_key': encrypted_key } })