def new(self, authentication_token=None, loid_id=None, loid_created_ms=None, session_id=None): """Return a new EdgeRequestContext object made from scratch. Services at the edge that communicate directly with clients should use this to pass on the information they get to downstream services. They can then use this information to check authentication, run experiments, etc. To use this, create and attach the context early in your request flow: .. code-block:: python auth_cookie = request.cookies["authentication"] token = request.authentication_service.authenticate_cookie(cookie) loid = parse_loid(request.cookies["loid"]) session = parse_session(request.cookies["session"]) edge_context = self.edgecontext_factory.new( authentication_token=token, loid_id=loid.id, loid_created_ms=loid.created, session_id=session.id, ) edge_context.attach_context(request) :param authentication_token: (Optional) A raw authentication token as returned by the authentication service. :param str loid_id: (Optional) ID for the current LoID in fullname format. :param int loid_created_ms: (Optional) Epoch milliseconds when the current LoID cookie was created. :param str session_id: (Optional) ID for the current session cookie. """ # Importing the Thrift models inline so that building them is not a # hard, import-time dependency for tasks like building the docs. from baseplate.thrift.ttypes import Loid as TLoid from baseplate.thrift.ttypes import Request as TRequest from baseplate.thrift.ttypes import Session as TSession if loid_id is not None and not loid_id.startswith("t2_"): raise ValueError( "loid_id <%s> is not in a valid format, it should be in the " "fullname format with the '0' padding removed: 't2_loid_id'" % loid_id ) t_request = TRequest( loid=TLoid(id=loid_id, created_ms=loid_created_ms), session=TSession(id=session_id), authentication_token=authentication_token, ) header = TSerialization.serialize(t_request, EdgeRequestContext._HEADER_PROTOCOL_FACTORY) context = EdgeRequestContext(self.authn_token_validator, header) # Set the _t_request property so we can skip the deserialization step # since we already have the thrift object. context._t_request = t_request return context
def _t_request(self) -> TRequest: # pylint: disable=method-hidden _t_request = TRequest() _t_request.loid = TLoid() _t_request.session = TSession() if self._header: try: TSerialization.deserialize(_t_request, self._header, self._HEADER_PROTOCOL_FACTORY) except Exception: logger.debug("Invalid Edge-Request header. %s", self._header) return _t_request
def _t_request(self): # pylint: disable=method-hidden # Importing the Thrift models inline so that building them is not a # hard, import-time dependency for tasks like building the docs. from baseplate.thrift.ttypes import Loid as TLoid from baseplate.thrift.ttypes import Request as TRequest from baseplate.thrift.ttypes import Session as TSession _t_request = TRequest() _t_request.loid = TLoid() _t_request.session = TSession() if self._header: try: TSerialization.deserialize(_t_request, self._header, self._HEADER_PROTOCOL_FACTORY) except Exception: logger.debug("Invalid Edge-Request header. %s", self._header) return _t_request
def new( self, authentication_token: Optional[bytes] = None, loid_id: Optional[str] = None, loid_created_ms: Optional[int] = None, session_id: Optional[str] = None, device_id: Optional[str] = None, origin_service_name: Optional[str] = None, ) -> "EdgeRequestContext": """Return a new EdgeRequestContext object made from scratch. Services at the edge that communicate directly with clients should use this to pass on the information they get to downstream services. They can then use this information to check authentication, run experiments, etc. To use this, create and attach the context early in your request flow: .. code-block:: python auth_cookie = request.cookies["authentication"] token = request.authentication_service.authenticate_cookie(cookie) loid = parse_loid(request.cookies["loid"]) session = parse_session(request.cookies["session"]) device_id = request.headers["x-device-id"] edge_context = self.edgecontext_factory.new( authentication_token=token, loid_id=loid.id, loid_created_ms=loid.created, session_id=session.id, device_id=device_id, ) edge_context.attach_context(request) :param authentication_token: A raw authentication token as returned by the authentication service. :param loid_id: ID for the current LoID in fullname format. :param loid_created_ms: Epoch milliseconds when the current LoID cookie was created. :param session_id: ID for the current session cookie. :param device_id: ID for the device where the request originated from. :param origin_service_name: Name for the "origin" service handling the request from the client. """ if loid_id is not None and not loid_id.startswith("t2_"): raise ValueError( "loid_id <%s> is not in a valid format, it should be in the " "fullname format with the '0' padding removed: 't2_loid_id'" % loid_id) t_request = TRequest( loid=TLoid(id=loid_id, created_ms=loid_created_ms), session=TSession(id=session_id), authentication_token=authentication_token, device=TDevice(id=device_id), origin_service=TOriginService(name=origin_service_name), ) header = TSerialization.serialize( t_request, EdgeRequestContext._HEADER_PROTOCOL_FACTORY) context = EdgeRequestContext(self.authn_token_validator, header) # Set the _t_request property so we can skip the deserialization step # since we already have the thrift object. context._t_request = t_request return context