summaryrefslogtreecommitdiff
path: root/plugins/b64.py
blob: 82300a0f081c5c393efe6a3a73fafd3e15e40b19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2019 Maxime “pep” Buquet <pep@bouah.net>
#
# Distributed under terms of the GPL-3.0+ license.

"""
Usage
-----

Base64 encryption plugin.

This plugin also respects security guidelines listed in XEP-0419.

.. glossary::
    /b64
        **Usage:** ``/b64``

        This command enables encryption of outgoing messages for the current
        tab.
"""

from base64 import b64decode, b64encode
from typing import List, Optional
from slixmpp import Message, JID

from poezio.plugin_e2ee import E2EEPlugin
from poezio.tabs import (
    ChatTab,
    MucTab,
    PrivateTab,
    DynamicConversationTab,
    StaticConversationTab,
)


class Plugin(E2EEPlugin):
    """Base64 Plugin"""

    encryption_name = 'base64'
    encryption_short_name = 'b64'
    eme_ns = 'urn:xmpps:base64:0'

    # This encryption mechanism is using <body/> as a container
    replace_body_with_eme = False

    # In what tab is it ok to use this plugin. Here we want all of them
    supported_tab_types = (
        MucTab,
        PrivateTab,
        DynamicConversationTab,
        StaticConversationTab,
    )

    async def decrypt(self, message: Message, jid: Optional[JID], _tab: Optional[ChatTab]) -> None:
        """
            Decrypt base64
        """
        body = message['body']
        message['body'] = b64decode(body.encode()).decode()

    async def encrypt(self, message: Message, _jid: Optional[List[JID]], _tab: ChatTab) -> None:
        """
            Encrypt to base64
        """
        # TODO: Stop using <body/> for this. Put the encoded payload in another element.
        body = message['body']
        message['body'] = b64encode(body.encode()).decode()