def build_from_signed_dict(cls, dict_data, v1_dict_format=False): if not v1_dict_format: entity_kind = ContextEntityKind(dict_data.get("entity_kind", "anonymous")) return SignedAuthContext(entity_kind, dict_data, v1_dict_format) # Legacy handling. # TODO: Remove this all once the new code is fully deployed. kind_string = dict_data.get("kind", "anonymous") if kind_string == "oauth": kind_string = "oauthtoken" kind = ContextEntityKind(kind_string) return SignedAuthContext(kind, dict_data, v1_dict_format)
def _get_validated(self): """ Returns a ValidatedAuthContext for this signed context, resolving all the necessary references. """ if not self.v1_dict_format: if self.kind == ContextEntityKind.anonymous: return ValidatedAuthContext() serialized_entity_reference = self.signed_data["entity_reference"] handler = CONTEXT_ENTITY_HANDLERS[self.kind]() entity_reference = handler.deserialize_entity_reference(serialized_entity_reference) if entity_reference is None: logger.debug( "Could not deserialize entity reference `%s` under kind `%s`", serialized_entity_reference, self.kind, ) return ValidatedAuthContext() return ValidatedAuthContext(**{self.kind.value: entity_reference}) # Legacy handling. # TODO: Remove this all once the new code is fully deployed. kind_string = self.signed_data.get("kind", "anonymous") if kind_string == "oauth": kind_string = "oauthtoken" kind = ContextEntityKind(kind_string) if kind == ContextEntityKind.anonymous: return ValidatedAuthContext() if kind == ContextEntityKind.user or kind == ContextEntityKind.robot: user = model.user.get_user(self.signed_data.get("user", "")) if not user: return None return ( ValidatedAuthContext(robot=user) if user.robot else ValidatedAuthContext(user=user) ) if kind == ContextEntityKind.token: token = model.token.load_token_data(self.signed_data.get("token")) if not token: return None return ValidatedAuthContext(token=token) if kind == ContextEntityKind.oauthtoken: user = model.user.get_user(self.signed_data.get("user", "")) if not user: return None token_uuid = self.signed_data.get("oauth", "") oauthtoken = model.oauth.lookup_access_token_for_user(user, token_uuid) if not oauthtoken: return None return ValidatedAuthContext(oauthtoken=oauthtoken) raise Exception( "Unknown auth context kind `%s` when deserializing %s" % (kind, self.signed_data) )