Ejemplo n.º 1
0
    def load(self):
        namespace = "session"
        if self.request.headers.authorization is None:
            if not self.cookie_id in self.request.headers.cookies:
                self.request.headers.cookies.add(Cookie(self.cookie_id, str(uuid.uuid4())))

            key = self.request.headers.cookies[self.cookie_id].value

        elif self.request.headers.authorization:
            key = self.request.headers.authorization.username

        self.data = RedisModel(namespace, key)
        self.key = key

        if self.request.headers.authorization:
            try:
                self.login(self.request.headers.authorization.username, self.request.headers.authorization.password)
            except:
                self.logout()

        if not "alerts" in self.data:
            self.data["alerts"] = []

        if not "groups" in self.data:
            self.data["groups"] = []
Ejemplo n.º 2
0
 def load(self):
     cookies = []
     raw_cookies = self.client.get(self.key)
     if raw_cookies:
         for l in [l.strip() for l in raw_cookies.split('\n') if l.strip() != '']:
             cookies.append(Cookie.from_str(l)) 
     return cookies
Ejemplo n.º 3
0
 def __getCookie(self, cookie):
     if isinstance(cookie, Cookie):
         return cookie
     elif isinstance(cookie, basestring):
         return Cookie.fromString(cookie)
     else:
         raise TypeError, "cookie must be instance of Cookie or string"
Ejemplo n.º 4
0
 def set_cookie(self, http_context):
     """
     Adds headers to :class:`aj.http.HttpContext` that set
     the session cookie
     """
     cookie = Cookie('session', self.key, path='/',
                     httponly=True).render_response()
     http_context.add_header('Set-Cookie', cookie)
Ejemplo n.º 5
0
 def GetSessionTokens(self):
     cookies = []
     try: # parsing may sometimes fail
         for cookie in self.Cookies_list:
             cookies.append(Cookie.from_string(cookie).to_dict())
     except:
         pass
     return json.dumps(cookies)
Ejemplo n.º 6
0
 def GetSessionTokens(self):
     cookies = []
     try:  # parsing may sometimes fail
         for cookie in self.Cookies_list:
             cookies.append(Cookie.from_string(cookie).to_dict())
     except:
         pass
     return json.dumps(cookies)
Ejemplo n.º 7
0
 def load(self):
     cookies = []
     raw_cookies = cache.get(self.key)
     if raw_cookies:
         for l in [
                 l.strip() for l in raw_cookies.split('\n')
                 if l.strip() != ''
         ]:
             cookies.append(Cookie.from_str(l))
     return cookies
Ejemplo n.º 8
0
    def setCookies(self, headers, encode=False):
        def next_token_is_not_semi(s):
            for i in xrange(len(s)):
                if s[i] == ';':
                    return False
                if s[i] == '=':
                    return True
            return True

        def parse_one_cookie(set_cookie_str):
            state = 0
            for i in xrange(len(set_cookies_str)):
                if state == 0 and set_cookies_str[i] == '=':
                    state = 1
                elif state == 1 and set_cookies_str[i] == ';':
                    state = 0
                if state == 0 and set_cookies_str[i] == ',':
                    return set_cookies_str[:i], set_cookies_str[i + 1:].strip()
                if state == 1 and set_cookies_str[
                        i] == ',' and next_token_is_not_semi(
                            set_cookies_str[i + 1:]):
                    return set_cookies_str[:i], set_cookies_str[i + 1:].strip()
                else:
                    continue
            return set_cookie_str, ""

        for header in headers:
            if header[0].lower() == 'set-cookie':
                #logger.debug(str(header))
                #set_cookies = header[1].split('; Path=/; ')
                set_cookies_str = header[1]
                #print set_cookies_str
                while len(set_cookies_str) > 0:
                    (one_cookie_str,
                     set_cookies_str) = parse_one_cookie(set_cookies_str)
                    #print one_cookie_str
                    #print set_cookies_str
                    cookie = Cookie.from_string(one_cookie_str)
                    #print "cookie.name=%s,cookie.value=%s"%(cookie.name, cookie.value)
                    #self.cookies[cookie.name] = cookie.value
                    if encode:
                        self.cookies[cookie.name] = urllib.quote(
                            cookie.value.encode('utf8'))
                    else:
                        self.cookies[cookie.name] = cookie.value.encode('utf8')
                #for set_cookie in set_cookies:
                #    cookie = Cookie.from_string(set_cookie)
                #    self.cookies[cookie.name] = cookie.value
        ks = self.cookies.keys()
        cookie_list = []
        for k in ks:
            cookie_list.append(k + '=' + self.cookies[k])
        self.headers['Cookie'] = string.join(cookie_list, '; ')
        logger.debug(str(self.headers))
Ejemplo n.º 9
0
    def get_session_tokens(self):
        """Get a JSON blob of all captured cookies

        :return:
        :rtype:
        """
        cookies = []
        try:  # parsing may sometimes fail
            for cookie in self.cookies_list:
                cookies.append(Cookie.from_string(cookie).to_dict())
        except InvalidCookieError:
            logging.debug("Cannot not parse the cookies")
        return cookies
Ejemplo n.º 10
0
def get_token(client, login="******", password="******"):
    data = {
        "login": login,
        "password": password,
        "id_application": current_app.config["ID_APPLICATION_GEONATURE"],
    }
    response = client.post(url_for("auth.login"),
                           data=json.dumps(data),
                           headers=headers)
    try:
        token = Cookie.from_string(response.headers["Set-Cookie"])
        return token.value
    except Exception:
        raise Exception("Invalid login {}, {}".format(login, password))
Ejemplo n.º 11
0
def get_token(client, login="******", password="******"):
    data = {
        "login": login,
        "password": password,
        "id_application": current_app.config["ID_APPLICATION_GEONATURE"],
    }
    response = client.post(
        url_for("auth.login"), data=json.dumps(data), headers=headers
    )
    try:
        token = Cookie.from_string(response.headers["Set-Cookie"])
        return token.value
    except Exception:
        raise Exception("Invalid login {}, {}".format(login, password))
Ejemplo n.º 12
0
def get_token(client, login="******", password="******"):  # noqa: S107
    data = {
        'login': login,
        'password': password,
        'id_application': APP_ID,
        'with_cruved': True
    }
    response = client.post(url_for('auth.login'),
                           data=json.dumps(data),
                           headers=headers)
    try:
        token = Cookie.from_string(response.headers['Set-Cookie'])
        return token.value
    except Exception:
        raise Exception('Invalid login {}, {}'.format(login, password))
 def parse(self, cookie):
     return Cookie.from_dict(cookie, ignore_bad_attributes=True)