<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def ensure_token(self, auth, name, username=None):
 """ Ensures the existence of a token with the specified name for the specified user. Creates a new token if none exists. If no user is specified, uses user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. Must be a username-password authentication, due to a restriction of the Gogs API :param str name: name of new token :param str username: username of owner of new token :return: token representation :rtype: Token :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """

        if username is None:
            username = self.authenticated_user(auth).username
        tokens = [token for token in self.get_tokens(auth, username) if token.name == name]
        if len(tokens) > 0:
            return tokens[0]
        return self.create_token(auth, name, username)