'Get the User for a given cookie, if there is one'

def _user_for_cookie(self, cookie_name, cookie_value=None):

    cookie_id = self.get_secure_cookie(cookie_name, cookie_value, max_age_days=self.cookie_max_age_days)
    def clear():
        self.clear_cookie(cookie_name, path=self.hub.base_url)
    if (cookie_id is None):
        if self.get_cookie(cookie_name):
            self.log.warning('Invalid    or    expired    cookie    token')
            clear()
        return
    cookie_id = cookie_id.decode('utf8', 'replace')
    u = self.db.query(orm.User).filter((orm.User.cookie_id == cookie_id)).first()
    user = self._user_from_orm(u)
    if (user is None):
        self.log.warning('Invalid    cookie    token')
        clear()
    return user
