synapse/api/auth.py
def _parse_and_validate_macaroon(self, token, rights="access"):
        """Takes a macaroon and tries to parse and validate it. This is cached
        if and only if rights == access and there isn't an expiry.

        On invalid macaroon raises _InvalidMacaroonException

        Returns:
            (user_id, is_guest)
        """
        if rights == "access":
            cached = self.token_cache.get(token, None)
            if cached:
                return cached

        try:
            macaroon = pymacaroons.Macaroon.deserialize(token)
        except Exception:  # deserialize can throw more-or-less anything
            # doesn't look like a macaroon: treat it as an opaque token which
            # must be in the database.
            # TODO: it would be nice to get rid of this, but apparently some
            # people use access tokens which aren't macaroons
            raise _InvalidMacaroonException()

        try:
            user_id = self.get_user_id_from_macaroon(macaroon)

            has_expiry = False
            guest = False
            for caveat in macaroon.caveats:
                if caveat.caveat_id.startswith("time "):
                    has_expiry = True
                elif caveat.caveat_id == "guest = true":
                    guest = True

            self.validate_macaroon(
                macaroon, rights, self.hs.config.expire_access_token,
                user_id=user_id,
            )
        except (pymacaroons.exceptions.MacaroonException, TypeError, ValueError):
            raise AuthError(
                self.TOKEN_NOT_FOUND_HTTP_STATUS, "Invalid macaroon passed.",
                errcode=Codes.UNKNOWN_TOKEN
            )

        if not has_expiry and rights == "access":
            self.token_cache[token] = (user_id, guest)

        return user_id, guest
Takes a macaroon and tries to parse and validate it. This is cached
        if and only if rights == access and there isn't an expiry.

        On invalid macaroon raises _InvalidMacaroonException

        Returns:
            (user_id, is_guest)
        
Takes a macaroon and tries to parse and validate it. This is cached
if and only if rights == access and there isn't an expiry.

On invalid macaroon raises _InvalidMacaroonException
Takes a macaroon and tries to parse and validate it . This is cached if and only if rights == access and there isn ' t an expiry . On invalid macaroon raises _InvalidMacaroonException
def _parse_and_validate_macaroon(self, token, rights="access"):
        if rights == "access":
            cached = self.token_cache.get(token, None)
            if cached:
                return cached
        try:
            macaroon = pymacaroons.Macaroon.deserialize(token)
        except Exception:  
            raise _InvalidMacaroonException()
        try:
            user_id = self.get_user_id_from_macaroon(macaroon)
            has_expiry = False
            guest = False
            for caveat in macaroon.caveats:
                if caveat.caveat_id.startswith("time "):
                    has_expiry = True
                elif caveat.caveat_id == "guest = true":
                    guest = True
            self.validate_macaroon(
                macaroon, rights, self.hs.config.expire_access_token,
                user_id=user_id,
            )
        except (pymacaroons.exceptions.MacaroonException, TypeError, ValueError):
            raise AuthError(
                self.TOKEN_NOT_FOUND_HTTP_STATUS, "Invalid macaroon passed.",
                errcode=Codes.UNKNOWN_TOKEN
            )
        if not has_expiry and rights == "access":
            self.token_cache[token] = (user_id, guest)
        return user_id, guest
def _parse_and_validate_macaroon ( self , token , rights = "access" ) : if rights == "access" : cached = self . token_cache . get ( token , None ) if cached : return cached try : macaroon = pymacaroons . Macaroon . deserialize ( token ) except Exception : raise _InvalidMacaroonException ( ) try : user_id = self . get_user_id_from_macaroon ( macaroon ) has_expiry = False guest = False for caveat in macaroon . caveats : if caveat . caveat_id . startswith ( "time " ) : has_expiry = True elif caveat . caveat_id == "guest = true" : guest = True self . validate_macaroon ( macaroon , rights , self . hs . config . expire_access_token , user_id = user_id , ) except ( pymacaroons . exceptions . MacaroonException , TypeError , ValueError ) : raise AuthError ( self . TOKEN_NOT_FOUND_HTTP_STATUS , "Invalid macaroon passed." , errcode = Codes . UNKNOWN_TOKEN ) if not has_expiry and rights == "access" : self . token_cache [ token ] = ( user_id , guest ) return user_id , guest
Takes a macaroon and tries to parse and validate it.
Takes a macaroon and tries to parse and validate it .
"""Takes a macaroon and tries to parse and validate it. This is cached
        if and only if rights == access and there isn't an expiry.

        On invalid macaroon raises _InvalidMacaroonException

        Returns:
            (user_id, is_guest)
        """ # deserialize can throw more-or-less anything # doesn't look like a macaroon: treat it as an opaque token which # must be in the database. # TODO: it would be nice to get rid of this, but apparently some # people use access tokens which aren't macaroons
None
self
token
rights