예제 #1
0
파일: app.py 프로젝트: CsacsoBacsi/VS2019
    def __call__(
        self, req, resp, resource, params
    ):  # 'Before' wrapper must be callable. This function is called for the object
        if 'Admin' in self.roles:
            req.user_id = 5
        else:
            print('Only the Admin is authorized to execute this!')
            raise falcon.HTTPBadRequest('Bad request',
                                        'Only Admin can execute this!')
        if req.auth is not None:
            auth_exp = req.auth.split(
                ' '
            )  # Splits into auth type such as 'basic' and the encoded user:password
        else:
            auth_exp = (
                None,
                None,
            )

        if auth_exp[0] is not None and auth_exp[0].lower() == 'basic':
            auth = base64.b64decode(auth_exp[1]).decode('utf-8').split(
                ':')  # Decode user:password
            username = auth[0]
            password = auth[1]
            self.auth_basic(
                username, password
            )  # Check if both username and password match to any test user/password pairs
        else:
            print("Wrong authentication method. Should be 'basic'!")
            raise falcon.HTTPNotImplemented('Not implemented',
                                            'Wrong authentication method!')
예제 #2
0
    def __call__(self, req, resp, resource, params):
        print("Before Trigger - Class Authorise")
        auth_exp = req.auth.split(" ") if req.auth is not None else (None, None, )

        if auth_exp[0] is not None and auth_exp[0].lower() == 'basic':
            auth = base64.b64decode(auth_exp[1]).decode('utf-8').split(':')
            username = auth[0]
            password = auth[1]
            self._auth_basic(username, password)
        else:
            raise falcon.HTTPNotImplemented("Not Implemented", "You don't have the right auth method")
예제 #3
0
    def on_get(self, req, resp, inst_id):
        """
        Handling data retrieval, besides "live". Not implemented.
        """
        if not self._accept_inst(inst_id):
            resp.status = falcon.HTTP_400
            resp.body = (
                "Installation not in list for this InstallationManager"
                "instance.")
            return

        raise falcon.HTTPNotImplemented("Data retrieval not implemented",
                                        "Data retrieval not implemented")
예제 #4
0
    def on_post(self, req: Request, resp: Response, app_id, msg_type):
        """
        POST请求处理函数,
        URL path参数:app_id msg_type
        """
        if msg_type not in ["text", "card", "md", "image"]:
            raise falcon.HTTPInvalidParam("should be text/card/md", "msg_type")

        client = self._get_client(app_id)
        groups = req.get_param_as_list("groups", default=[])
        tags = req.get_param_as_list("tags", default=[])
        users = req.get_param_as_list(
            "users", default="@all" if not groups and not tags else [])

        if msg_type == "text":
            content = req.media["content"]
            resp.media = client.message.send_text(app_id,
                                                  users,
                                                  content,
                                                  party_ids=groups,
                                                  tag_ids=tags)

        elif msg_type == "markdown":
            content = req.media["content"]
            resp.media = client.message.send_markdown(app_id,
                                                      users,
                                                      content,
                                                      party_ids=groups,
                                                      tag_ids=tags)
        elif msg_type == "image":
            if req.content_type == falcon.MEDIA_JSON:
                resp_upload = req.media
            else:
                resp_upload = client.media.upload("image", req.bounded_stream)
            media_id = resp_upload["media_id"]
            resp_post = client.message.send(app_id,
                                            users,
                                            party_ids=groups,
                                            tag_ids=tags,
                                            msg={
                                                "msgtype": "image",
                                                "image": {
                                                    "media_id": media_id
                                                }
                                            })
            resp_post["media"] = resp_upload
            resp.media = resp_post
        else:
            raise falcon.HTTPNotImplemented(description="I'm lazy")
예제 #5
0
 def __collection__(self):
     raise falcon.HTTPNotImplemented(
         description='__collection__ property missing on Class: '
                     f'{self.__class__.__name__}'
     )