Commit d08544a6 authored by Philipp Hörist's avatar Philipp Hörist

SecurityLabels: Add more stuff

- Add request_catalog()
- Refactor parsing data
parent 37ce80bc
Pipeline #6670 passed with stages
in 45 seconds
......@@ -15,10 +15,16 @@
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
from typing import Dict
from dataclasses import dataclass
from nbxmpp.protocol import Iq
from nbxmpp.simplexml import Node
from nbxmpp.namespaces import Namespace
from nbxmpp.structs import StanzaHandler
from nbxmpp.structs import SecurityLabel
from nbxmpp.structs import DisplayMarking
from nbxmpp.errors import StanzaError
from nbxmpp.task import iq_request_task
from nbxmpp.modules.base import BaseModule
......@@ -39,18 +45,116 @@ class SecurityLabels(BaseModule):
if security is None:
return
displaymarking = security.getTag('displaymarking')
if displaymarking is None:
try:
security_label = SecurityLabel.from_node(security)
except ValueError as error:
self._log.warning(error)
return
label = displaymarking.getData()
if not label:
self._log.warning('No label found')
self._log.warning(stanza)
return
properties.security_label = security_label
@iq_request_task
def request_catalog(self, jid):
_task = yield
response = yield _make_catalog_request(self._client.domain, jid)
if response.isError():
raise StanzaError(response)
catalog_node = response.getTag('catalog',
namespace=Namespace.SECLABEL_CATALOG)
to = catalog_node.getAttr('to')
items = catalog_node.getTags('item')
labels = {}
default = None
for item in items:
label = item.getAttr('selector')
if label is None:
continue
security = item.getTag('securitylabel',
namespace=Namespace.SECLABEL)
if security is None:
continue
try:
security_label = SecurityLabel.from_node(security)
except ValueError:
continue
labels[label] = security_label
if item.getAttr('default') == 'true':
default = label
yield Catalog(labels=labels, default=default)
def _make_catalog_request(domain, jid):
iq = Iq(typ='get', to=domain)
iq.addChild(name='catalog',
namespace=Namespace.SECLABEL_CATALOG,
attrs={'to': jid})
return iq
@dataclass
class Displaymarking:
name: str
fgcolor: str
bgcolor: str
def to_node(self):
displaymarking = Node(tag='displaymarking')
if self.fgcolor and self.fgcolor != '#000':
displaymarking.setAttr('fgcolor', self.fgcolor)
if self.bgcolor and self.bgcolor != '#FFF':
displaymarking.setAttr('bgcolor', self.bgcolor)
if self.name:
displaymarking.setData(self.name)
return displaymarking
@classmethod
def from_node(cls, node):
return cls(name=node.getData(),
fgcolor=node.getAttr('fgcolor') or '#000',
bgcolor=node.getAttr('bgcolor') or '#FFF')
@dataclass
class SecurityLabel:
displaymarking: Displaymarking
label: Node
def to_node(self):
security = Node(tag='securitylabel',
attrs={'xmlns': Namespace.SECLABEL})
if self.displaymarking is not None:
security.addChild(node=self.displaymarking.to_node())
security.addChild(node=self.label)
return security
@classmethod
def from_node(cls, security):
displaymarking = security.getTag('displaymarking')
if displaymarking is not None:
displaymarking = Displaymarking.from_node(displaymarking)
label = security.getTag('label')
if label is None:
raise ValueError('label node missing')
return cls(displaymarking=displaymarking, label=label)
fgcolor = displaymarking.getAttr('fgcolor')
bgcolor = displaymarking.getAttr('bgcolor')
@dataclass
class Catalog:
labels: Dict[str, SecurityLabel]
default: str
properties.security_label = SecurityLabel(
DisplayMarking(label, fgcolor, bgcolor))
def get_label_names(self):
return list(self.labels.keys())
......@@ -115,9 +115,6 @@ OOBData = namedtuple('OOBData', 'url desc')
CorrectionData = namedtuple('CorrectionData', 'id')
DisplayMarking = namedtuple('DisplayMarking', 'label fgcolor bgcolor')
SecurityLabel = namedtuple('SecurityLabel', 'displaymarking')
RegisterData = namedtuple('RegisterData', 'instructions form fields_form oob_url bob_data')
HTTPUploadData = namedtuple('HTTPUploadData', 'put_uri get_uri headers')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment