@defer.inlineCallbacks
    def get_user_from_macaroon(self, macaroon_str):
        try:
            macaroon = pymacaroons.Macaroon.deserialize(macaroon_str)
            self.validate_macaroon(macaroon, "access", False)

            user_prefix = "user_id = "
            user = None
            guest = False
            for caveat in macaroon.caveats:
                if caveat.caveat_id.startswith(user_prefix):
                    user = UserID.from_string(caveat.caveat_id[len(user_prefix):])
                elif caveat.caveat_id == "guest = true":
                    guest = True

            if user is None:
                raise AuthError(
                    self.TOKEN_NOT_FOUND_HTTP_STATUS, "No user caveat in macaroon",
                    errcode=Codes.UNKNOWN_TOKEN
                )

            if guest:
                ret = {
                    "user": user,
                    "is_guest": True,
                    "token_id": None,
                }
            else:
                # This codepath exists so that we can actually return a
                # token ID, because we use token IDs in place of device
                # identifiers throughout the codebase.
                # TODO(daniel): Remove this fallback when device IDs are
                # properly implemented.
                ret = yield self._look_up_user_by_access_token(macaroon_str)
                if ret["user"] != user:
                    logger.error(
                        "Macaroon user (%s) != DB user (%s)",
                        user,
                        ret["user"]
                    )
                    raise AuthError(
                        self.TOKEN_NOT_FOUND_HTTP_STATUS,
                        "User mismatch in macaroon",
                        errcode=Codes.UNKNOWN_TOKEN
                    )
            defer.returnValue(ret)
        except (pymacaroons.exceptions.MacaroonException, TypeError, __HOLE__):
            raise AuthError(
                self.TOKEN_NOT_FOUND_HTTP_STATUS, "Invalid macaroon passed.",
                errcode=Codes.UNKNOWN_TOKEN
            )
dataset/ETHPy150Open matrix-org/synapse/synapse/api/auth.py/Auth.get_user_from_macaroon