def get_cookie(self, key): cookie_string = self.req_env.get('HTTP_COOKIE', None) if cookie_string is None: return cookie = SimpleCookie() cookie.load(cookie_string) return cookie.get(key, None).value
def test_cookie_parse(self): string = 'some_string' class RequestHeader(ComplexModel): some_field = String class SomeService(ServiceBase): __in_header__ = RequestHeader @rpc(String) def some_call(ctx, s): assert ctx.in_header.some_field == string def start_response(code, headers): assert code == HTTP_200 c = SimpleCookie() c['some_field'] = string ''.join(wsgiref_validator(WsgiApplication(Application([SomeService], 'tns', in_protocol=HttpRpc(parse_cookie=True), out_protocol=HttpRpc())))({ 'SCRIPT_NAME': '', 'QUERY_STRING': '', 'PATH_INFO': '/some_call', 'REQUEST_METHOD': 'GET', 'SERVER_NAME': 'localhost', 'SERVER_PORT': "9999", 'HTTP_COOKIE': str(c), 'wsgi.url_scheme': 'http', 'wsgi.version': (1,0), 'wsgi.input': StringIO(), 'wsgi.errors': StringIO(), 'wsgi.multithread': False, 'wsgi.multiprocess': False, 'wsgi.run_once': True, }, start_response))
def _on_method_call(ctx): if ctx.descriptor.name == "authenticate": # No checking of session cookie for call to authenticate return cookie = SimpleCookie() http_cookie = ctx.transport.req_env.get("HTTP_COOKIE") if http_cookie: cookie.load(http_cookie) if "session-id" not in cookie: raise UnauthenticatedError() session_cookie = cookie["session-id"].value session_id = tuple(base64.urlsafe_b64decode(session_cookie).split("\0", 1)) if not session_id in session_db: raise AuthenticationError(session_id[0]) ctx.udc = session_id[0] # user name
def decompose_incoming_envelope(self, ctx, message): assert message == SimpleDictDocument.REQUEST ctx.transport.itself.decompose_incoming_envelope(self, ctx, message) if self.parse_cookie: cookies = ctx.in_header_doc.get('cookie', None) if cookies is None: cookies = ctx.in_header_doc.get('Cookie', None) if cookies is not None: for cookie_string in cookies: cookie = SimpleCookie() cookie.load(cookie_string) for k,v in cookie.items(): l = ctx.in_header_doc.get(k, []) l.append(v.coded_value) ctx.in_header_doc[k] = l logger.debug('\theader : %r' % (ctx.in_header_doc)) logger.debug('\tbody : %r' % (ctx.in_body_doc))
def decompose_incoming_envelope(self, ctx, message): assert message == SimpleDictDocument.REQUEST ctx.transport.itself.decompose_incoming_envelope(self, ctx, message) if self.parse_cookie: cookies = ctx.in_header_doc.get('cookie', None) if cookies is None: cookies = ctx.in_header_doc.get('Cookie', None) if cookies is not None: for cookie_string in cookies: cookie = SimpleCookie() cookie.load(cookie_string) for k, v in cookie.items(): l = ctx.in_header_doc.get(k, []) l.append(v.coded_value) ctx.in_header_doc[k] = l logger.debug('\theader : %r' % (ctx.in_header_doc)) logger.debug('\tbody : %r' % (ctx.in_body_doc))
def authenticate(ctx, user_name, password): password_hash = user_db.get(user_name, None) if password_hash is None: raise AuthenticationError(user_name) if bcrypt.hashpw(password, password_hash) != password_hash: raise AuthenticationError(user_name) session_id = (user_name, '%x' % random.randint(1 << 128, (1 << 132) - 1)) session_db.add(session_id) cookie = SimpleCookie() cookie["session-id"] = base64.urlsafe_b64encode( str(session_id[0]) + "\0" + str(session_id[1])) cookie["session-id"]["max-age"] = 3600 header_name, header_value = cookie.output().split(":", 1) ctx.transport.resp_headers[header_name] = header_value.strip() from pprint import pprint pprint(ctx.transport.resp_headers)