def store_session_cookie(self, cookie_header): ''' Given the contents of a Set-Cookie header scan the header and extract each cookie contained within until the session cookie is located. Examine the session cookie if the domain and path are specified, if not update the cookie with those values from the request URL. Then write the session cookie into the key store for the principal. If the cookie header is None or the session cookie is not present in the header no action is taken. Context Dependencies: The per thread context is expected to contain: principal The current pricipal the HTTP request was issued for. request_url The URL of the HTTP request. ''' if cookie_header is None: return principal = getattr(context, 'principal', None) request_url = getattr(context, 'request_url', None) root_logger.debug("received Set-Cookie '%s'", cookie_header) # Search for the session cookie try: session_cookie = Cookie.get_named_cookie_from_string(cookie_header, COOKIE_NAME, request_url) except Exception, e: root_logger.error("unable to parse cookie header '%s': %s", cookie_header, e) return
def get_session_cookie_from_persistent_storage(self, principal): ''' Retrieves the session cookie for the given principal from the persistent secure storage. Returns None if not found or unable to retrieve the session cookie for any reason, otherwise returns a Cookie object containing the session cookie. ''' # Get the session data, it should contain a cookie string # (possibly with more than one cookie). try: cookie_string = read_persistent_client_session_data(principal) if cookie_string is None: return cookie_string = cookie_string.decode('utf-8') except Exception as e: logger.debug('Error reading client session data: %s', e) return None # Search for the session cookie within the cookie string try: session_cookie = Cookie.get_named_cookie_from_string( cookie_string, COOKIE_NAME, timestamp=datetime.datetime.utcnow()) except Exception as e: logger.debug( 'Error retrieving cookie from the persistent storage: %s', e) return None return session_cookie
def get_session_id_from_http_cookie(self, cookie_header): ''' Parse an HTTP cookie header and search for our session id. Return the session id if found, return None if not found. :parameters: cookie_header An HTTP cookie header. May be None, if None return None. :returns: Session id as string or None if not found. ''' if cookie_header is None: return None session_id = None try: session_cookie = Cookie.get_named_cookie_from_string(cookie_header, self.session_cookie_name) except Exception as e: session_cookie = None if session_cookie: session_id = session_cookie.value if session_id is None: self.debug('no session cookie found') else: self.debug('found session cookie_id = %s', session_id) return session_id
def get_session_id_from_http_cookie(self, cookie_header): ''' Parse an HTTP cookie header and search for our session id. Return the session id if found, return None if not found. :parameters: cookie_header An HTTP cookie header. May be None, if None return None. :returns: Session id as string or None if not found. ''' if cookie_header is None: return None session_id = None try: session_cookie = Cookie.get_named_cookie_from_string( cookie_header, self.session_cookie_name) except Exception as e: session_cookie = None if session_cookie: session_id = session_cookie.value if session_id is None: self.debug('no session cookie found') else: self.debug('found session cookie_id = %s', session_id) return session_id
def get_session_cookie_from_persistent_storage(self, principal): ''' Retrieves the session cookie for the given principal from the persistent secure storage. Returns None if not found or unable to retrieve the session cookie for any reason, otherwise returns a Cookie object containing the session cookie. ''' # Get the session data, it should contain a cookie string # (possibly with more than one cookie). try: cookie_string = read_persistent_client_session_data(principal) if cookie_string is None: return None cookie_string = cookie_string.decode('utf-8') except Exception as e: logger.debug('Error reading client session data: %s', e) return None # Search for the session cookie within the cookie string try: session_cookie = Cookie.get_named_cookie_from_string( cookie_string, COOKIE_NAME, timestamp=datetime.datetime.utcnow()) except Exception as e: logger.debug( 'Error retrieving cookie from the persistent storage: %s', e) return None return session_cookie
def store_session_cookie(self, cookie_header): ''' Given the contents of a Set-Cookie header scan the header and extract each cookie contained within until the session cookie is located. Examine the session cookie if the domain and path are specified, if not update the cookie with those values from the request URL. Then write the session cookie into the key store for the principal. If the cookie header is None or the session cookie is not present in the header no action is taken. Context Dependencies: The per thread context is expected to contain: principal The current pricipal the HTTP request was issued for. request_url The URL of the HTTP request. ''' if cookie_header is None: return principal = getattr(context, 'principal', None) request_url = getattr(context, 'request_url', None) logger.debug("received Set-Cookie (%s)'%s'", type(cookie_header), cookie_header) if not isinstance(cookie_header, list): cookie_header = [cookie_header] # Search for the session cookie session_cookie = None try: for cookie in cookie_header: session_cookie = (Cookie.get_named_cookie_from_string( cookie, COOKIE_NAME, request_url, timestamp=datetime.datetime.utcnow())) if session_cookie is not None: break except Exception as e: logger.error("unable to parse cookie header '%s': %s", cookie_header, e) return if session_cookie is None: return cookie_string = self._slice_session_cookie(session_cookie) logger.debug("storing cookie '%s' for principal %s", cookie_string, principal) try: update_persistent_client_session_data(principal, cookie_string) except Exception as e: # Not fatal, we just can't use the session cookie we were sent. pass
def store_session_cookie(self, cookie_header): ''' Given the contents of a Set-Cookie header scan the header and extract each cookie contained within until the session cookie is located. Examine the session cookie if the domain and path are specified, if not update the cookie with those values from the request URL. Then write the session cookie into the key store for the principal. If the cookie header is None or the session cookie is not present in the header no action is taken. Context Dependencies: The per thread context is expected to contain: principal The current pricipal the HTTP request was issued for. request_url The URL of the HTTP request. ''' if cookie_header is None: return principal = getattr(context, 'principal', None) request_url = getattr(context, 'request_url', None) logger.debug("received Set-Cookie (%s)'%s'", type(cookie_header), cookie_header) if not isinstance(cookie_header, list): cookie_header = [cookie_header] # Search for the session cookie session_cookie = None try: for cookie in cookie_header: session_cookie = ( Cookie.get_named_cookie_from_string( cookie, COOKIE_NAME, request_url, timestamp=datetime.datetime.utcnow()) ) if session_cookie is not None: break except Exception as e: logger.error("unable to parse cookie header '%s': %s", cookie_header, e) return if session_cookie is None: return cookie_string = self._slice_session_cookie(session_cookie) logger.debug("storing cookie '%s' for principal %s", cookie_string, principal) try: update_persistent_client_session_data(principal, cookie_string) except Exception as e: # Not fatal, we just can't use the session cookie we were sent. pass
def get_session_cookie_from_persistent_storage(self, principal): """ Retrieves the session cookie for the given principal from the persistent secure storage. Returns None if not found or unable to retrieve the session cookie for any reason, otherwise returns a Cookie object containing the session cookie. """ # Get the session data, it should contain a cookie string # (possibly with more than one cookie). try: cookie_string = read_persistent_client_session_data(principal) except Exception: return None # Search for the session cookie within the cookie string try: session_cookie = Cookie.get_named_cookie_from_string(cookie_string, COOKIE_NAME) except Exception: return None return session_cookie
def get_session_cookie_from_persistent_storage(self, principal): ''' Retrieves the session cookie for the given principal from the persistent secure storage. Returns None if not found or unable to retrieve the session cookie for any reason, otherwise returns a Cookie object containing the session cookie. ''' # Get the session data, it should contain a cookie string # (possibly with more than one cookie). try: cookie_string = read_persistent_client_session_data(principal) except Exception: return None # Search for the session cookie within the cookie string try: session_cookie = Cookie.get_named_cookie_from_string( cookie_string, COOKIE_NAME) except Exception: return None return session_cookie
def store_session_cookie(self, cookie_header): ''' Given the contents of a Set-Cookie header scan the header and extract each cookie contained within until the session cookie is located. Examine the session cookie if the domain and path are specified, if not update the cookie with those values from the request URL. Then write the session cookie into the key store for the principal. If the cookie header is None or the session cookie is not present in the header no action is taken. Context Dependencies: The per thread context is expected to contain: principal The current pricipal the HTTP request was issued for. request_url The URL of the HTTP request. ''' if cookie_header is None: return principal = getattr(context, 'principal', None) request_url = getattr(context, 'request_url', None) root_logger.debug("received Set-Cookie '%s'", cookie_header) # Search for the session cookie try: session_cookie = Cookie.get_named_cookie_from_string( cookie_header, COOKIE_NAME, request_url) except Exception, e: root_logger.error("unable to parse cookie header '%s': %s", cookie_header, e) return
Retrieves the session cookie for the given principal from the persistent secure storage. Returns None if not found or unable to retrieve the session cookie for any reason, otherwise returns a Cookie object containing the session cookie. ''' # Get the session data, it should contain a cookie string # (possibly with more than one cookie). try: cookie_string = read_persistent_client_session_data(principal) except Exception, e: return None # Search for the session cookie within the cookie string try: session_cookie = Cookie.get_named_cookie_from_string(cookie_string, COOKIE_NAME) except Exception, e: return None return session_cookie def apply_session_cookie(self, url): ''' Attempt to load a session cookie for the current principal from the persistent secure storage. If the cookie is successfully loaded adjust the input url's to point to the session path and insert the session cookie into the per thread context for later insertion into the HTTP request. If the cookie is not successfully loaded then the original url is returned and the per thread context is not modified.
def test_parse(self): # Empty string s = '' cookies = Cookie.parse(s) assert len(cookies) == 0 # Invalid single token s = 'color' with pytest.raises(ValueError): cookies = Cookie.parse(s) # Invalid single token that's keyword s = 'HttpOnly' with pytest.raises(ValueError): cookies = Cookie.parse(s) # Invalid key/value pair whose key is a keyword s = 'domain=example.com' with pytest.raises(ValueError): cookies = Cookie.parse(s) # 1 cookie with empty value s = 'color=' cookies = Cookie.parse(s) assert len(cookies) == 1 cookie = cookies[0] assert cookie.key == 'color' assert cookie.value == '' assert cookie.domain is None assert cookie.path is None assert cookie.max_age is None assert cookie.expires is None assert cookie.secure is None assert cookie.httponly is None assert str(cookie) == "color=" assert cookie.http_cookie() == "color=;" # 1 cookie with name/value s = 'color=blue' cookies = Cookie.parse(s) assert len(cookies) == 1 cookie = cookies[0] assert cookie.key == 'color' assert cookie.value == 'blue' assert cookie.domain is None assert cookie.path is None assert cookie.max_age is None assert cookie.expires is None assert cookie.secure is None assert cookie.httponly is None assert str(cookie) == "color=blue" assert cookie.http_cookie() == "color=blue;" # 1 cookie with whose value is quoted # Use "get by name" utility to extract specific cookie s = 'color="blue"' cookie = Cookie.get_named_cookie_from_string(s, 'color') assert cookie is not None, Cookie assert cookie.key == 'color' assert cookie.value == 'blue' assert cookie.domain is None assert cookie.path is None assert cookie.max_age is None assert cookie.expires is None assert cookie.secure is None assert cookie.httponly is None assert str(cookie) == "color=blue" assert cookie.http_cookie() == "color=blue;" # 1 cookie with name/value and domain, path attributes. # Change up the whitespace a bit. s = 'color =blue; domain= example.com ; path = /toplevel ' cookies = Cookie.parse(s) assert len(cookies) == 1 cookie = cookies[0] assert cookie.key == 'color' assert cookie.value == 'blue' assert cookie.domain == 'example.com' assert cookie.path == '/toplevel' assert cookie.max_age is None assert cookie.expires is None assert cookie.secure is None assert cookie.httponly is None assert str(cookie) == "color=blue; Domain=example.com; Path=/toplevel" assert cookie.http_cookie() == "color=blue;" # 2 cookies, various attributes s = 'color=blue; Max-Age=3600; temperature=hot; HttpOnly' cookies = Cookie.parse(s) assert len(cookies) == 2 cookie = cookies[0] assert cookie.key == 'color' assert cookie.value == 'blue' assert cookie.domain is None assert cookie.path is None assert cookie.max_age == 3600 assert cookie.expires is None assert cookie.secure is None assert cookie.httponly is None assert str(cookie) == "color=blue; Max-Age=3600" assert cookie.http_cookie() == "color=blue;" cookie = cookies[1] assert cookie.key == 'temperature' assert cookie.value == 'hot' assert cookie.domain is None assert cookie.path is None assert cookie.max_age is None assert cookie.expires is None assert cookie.secure is None assert cookie.httponly is True assert str(cookie) == "temperature=hot; HttpOnly" assert cookie.http_cookie() == "temperature=hot;"
Retrieves the session cookie for the given principal from the persistent secure storage. Returns None if not found or unable to retrieve the session cookie for any reason, otherwise returns a Cookie object containing the session cookie. ''' # Get the session data, it should contain a cookie string # (possibly with more than one cookie). try: cookie_string = read_persistent_client_session_data(principal) except Exception, e: return None # Search for the session cookie within the cookie string try: session_cookie = Cookie.get_named_cookie_from_string( cookie_string, COOKIE_NAME) except Exception, e: return None return session_cookie def apply_session_cookie(self, url): ''' Attempt to load a session cookie for the current principal from the persistent secure storage. If the cookie is successfully loaded adjust the input url's to point to the session path and insert the session cookie into the per thread context for later insertion into the HTTP request. If the cookie is not successfully loaded then the original url is returned and the per thread context is not modified.
def test_parse(self): # Empty string s = '' cookies = Cookie.parse(s) self.assertEqual(len(cookies), 0) # Invalid single token s = 'color' with self.assertRaises(ValueError): cookies = Cookie.parse(s) # Invalid single token that's keyword s = 'HttpOnly' with self.assertRaises(ValueError): cookies = Cookie.parse(s) # Invalid key/value pair whose key is a keyword s = 'domain=example.com' with self.assertRaises(ValueError): cookies = Cookie.parse(s) # 1 cookie with empty value s = 'color=' cookies = Cookie.parse(s) self.assertEqual(len(cookies), 1) cookie = cookies[0] self.assertEqual(cookie.key, 'color') self.assertEqual(cookie.value, '') self.assertEqual(cookie.domain, None) self.assertEqual(cookie.path, None) self.assertEqual(cookie.max_age, None) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, None) self.assertEqual(str(cookie), "color=") self.assertEqual(cookie.http_cookie(), "color=;") # 1 cookie with name/value s = 'color=blue' cookies = Cookie.parse(s) self.assertEqual(len(cookies), 1) cookie = cookies[0] self.assertEqual(cookie.key, 'color') self.assertEqual(cookie.value, 'blue') self.assertEqual(cookie.domain, None) self.assertEqual(cookie.path, None) self.assertEqual(cookie.max_age, None) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, None) self.assertEqual(str(cookie), "color=blue") self.assertEqual(cookie.http_cookie(), "color=blue;") # 1 cookie with whose value is quoted # Use "get by name" utility to extract specific cookie s = 'color="blue"' cookie = Cookie.get_named_cookie_from_string(s, 'color') self.assertIsNotNone(cookie) self.assertIsNotNone(cookie, Cookie) self.assertEqual(cookie.key, 'color') self.assertEqual(cookie.value, 'blue') self.assertEqual(cookie.domain, None) self.assertEqual(cookie.path, None) self.assertEqual(cookie.max_age, None) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, None) self.assertEqual(str(cookie), "color=blue") self.assertEqual(cookie.http_cookie(), "color=blue;") # 1 cookie with name/value and domain, path attributes. # Change up the whitespace a bit. s = 'color =blue; domain= example.com ; path = /toplevel ' cookies = Cookie.parse(s) self.assertEqual(len(cookies), 1) cookie = cookies[0] self.assertEqual(cookie.key, 'color') self.assertEqual(cookie.value, 'blue') self.assertEqual(cookie.domain, 'example.com') self.assertEqual(cookie.path, '/toplevel') self.assertEqual(cookie.max_age, None) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, None) self.assertEqual(str(cookie), "color=blue; Domain=example.com; Path=/toplevel") self.assertEqual(cookie.http_cookie(), "color=blue;") # 2 cookies, various attributes s = 'color=blue; Max-Age=3600; temperature=hot; HttpOnly' cookies = Cookie.parse(s) self.assertEqual(len(cookies), 2) cookie = cookies[0] self.assertEqual(cookie.key, 'color') self.assertEqual(cookie.value, 'blue') self.assertEqual(cookie.domain, None) self.assertEqual(cookie.path, None) self.assertEqual(cookie.max_age, 3600) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, None) self.assertEqual(str(cookie), "color=blue; Max-Age=3600") self.assertEqual(cookie.http_cookie(), "color=blue;") cookie = cookies[1] self.assertEqual(cookie.key, 'temperature') self.assertEqual(cookie.value, 'hot') self.assertEqual(cookie.domain, None) self.assertEqual(cookie.path, None) self.assertEqual(cookie.max_age, None) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, True) self.assertEqual(str(cookie), "temperature=hot; HttpOnly") self.assertEqual(cookie.http_cookie(), "temperature=hot;")
def test_parse(self): # Empty string s = '' cookies = Cookie.parse(s) self.assertEqual(len(cookies), 0) # Invalid single token s = 'color' with self.assertRaises(ValueError): cookies = Cookie.parse(s) # Invalid single token that's keyword s = 'HttpOnly' with self.assertRaises(ValueError): cookies = Cookie.parse(s) # Invalid key/value pair whose key is a keyword s = 'domain=example.com' with self.assertRaises(ValueError): cookies = Cookie.parse(s) # 1 cookie with empty value s = 'color=' cookies = Cookie.parse(s) self.assertEqual(len(cookies), 1) cookie = cookies[0] self.assertEqual(cookie.key, 'color') self.assertEqual(cookie.value, '') self.assertEqual(cookie.domain, None) self.assertEqual(cookie.path, None) self.assertEqual(cookie.max_age, None) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, None) self.assertEqual(str(cookie), "color=") self.assertEqual(cookie.http_cookie(), "color=;") # 1 cookie with name/value s = 'color=blue' cookies = Cookie.parse(s) self.assertEqual(len(cookies), 1) cookie = cookies[0] self.assertEqual(cookie.key, 'color') self.assertEqual(cookie.value, 'blue') self.assertEqual(cookie.domain, None) self.assertEqual(cookie.path, None) self.assertEqual(cookie.max_age, None) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, None) self.assertEqual(str(cookie), "color=blue") self.assertEqual(cookie.http_cookie(), "color=blue;") # 1 cookie with whose value is quoted # Use "get by name" utility to extract specific cookie s = 'color="blue"' cookie = Cookie.get_named_cookie_from_string(s, 'color') self.assertIsNotNone(cookie) self.assertIsNotNone(cookie, Cookie) self.assertEqual(cookie.key, 'color') self.assertEqual(cookie.value, 'blue') self.assertEqual(cookie.domain, None) self.assertEqual(cookie.path, None) self.assertEqual(cookie.max_age, None) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, None) self.assertEqual(str(cookie), "color=blue") self.assertEqual(cookie.http_cookie(), "color=blue;") # 1 cookie with name/value and domain, path attributes. # Change up the whitespace a bit. s = 'color =blue; domain= example.com ; path = /toplevel ' cookies = Cookie.parse(s) self.assertEqual(len(cookies), 1) cookie = cookies[0] self.assertEqual(cookie.key, 'color') self.assertEqual(cookie.value, 'blue') self.assertEqual(cookie.domain, 'example.com') self.assertEqual(cookie.path, '/toplevel') self.assertEqual(cookie.max_age, None) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, None) self.assertEqual(str(cookie), "color=blue; Domain=example.com; Path=/toplevel") self.assertEqual(cookie.http_cookie(), "color=blue;") # 2 cookies, various attributes s = 'color=blue; Max-Age=3600; temperature=hot; HttpOnly' cookies = Cookie.parse(s) self.assertEqual(len(cookies), 2) cookie = cookies[0] self.assertEqual(cookie.key, 'color') self.assertEqual(cookie.value, 'blue') self.assertEqual(cookie.domain, None) self.assertEqual(cookie.path, None) self.assertEqual(cookie.max_age, 3600) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, None) self.assertEqual(str(cookie), "color=blue; Max-Age=3600") self.assertEqual(cookie.http_cookie(), "color=blue;") cookie = cookies[1] self.assertEqual(cookie.key, 'temperature') self.assertEqual(cookie.value, 'hot') self.assertEqual(cookie.domain, None) self.assertEqual(cookie.path, None) self.assertEqual(cookie.max_age, None) self.assertEqual(cookie.expires, None) self.assertEqual(cookie.secure, None) self.assertEqual(cookie.httponly, True) self.assertEqual(str(cookie), "temperature=hot; HttpOnly") self.assertEqual(cookie.http_cookie(), "temperature=hot;")
class xmlclient(Connectible): """ Forwarding backend plugin for XML-RPC client. Also see the `ipaserver.rpcserver.xmlserver` plugin. """ def __init__(self): super(xmlclient, self).__init__() self.__errors = dict((e.errno, e) for e in public_errors) def get_url_list(self, xmlrpc_uri): """ Create a list of urls consisting of the available IPA servers. """ # the configured URL defines what we use for the discovered servers (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(xmlrpc_uri) servers = [] name = '_ldap._tcp.%s.' % self.env.domain rs = dnsclient.query(name, dnsclient.DNS_C_IN, dnsclient.DNS_T_SRV) for r in rs: if r.dns_type == dnsclient.DNS_T_SRV: rsrv = r.rdata.server.rstrip('.') servers.append('https://%s%s' % (ipautil.format_netloc(rsrv), path)) servers = list(set(servers)) # the list/set conversion won't preserve order so stick in the # local config file version here. cfg_server = xmlrpc_uri if cfg_server in servers: # make sure the configured master server is there just once and # it is the first one servers.remove(cfg_server) servers.insert(0, cfg_server) else: servers.insert(0, cfg_server) return servers def get_session_cookie_from_persistent_storage(self, principal): ''' Retrieves the session cookie for the given principal from the persistent secure storage. Returns None if not found or unable to retrieve the session cookie for any reason, otherwise returns a Cookie object containing the session cookie. ''' # Get the session data, it should contain a cookie string # (possibly with more than one cookie). try: cookie_string = read_persistent_client_session_data(principal) except Exception, e: return None # Search for the session cookie within the cookie string try: session_cookie = Cookie.get_named_cookie_from_string( cookie_string, COOKIE_NAME) except Exception, e: return None