summaryrefslogtreecommitdiffstats
path: root/roles/modernpaste/files/paste.py
blob: 3da551d8e672023ab314d1d5d9b6c42b5cf6f3ef (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import flask
from flask_login import current_user
from modern_paste import app

import config
from uri.main import *
from uri.paste import *
from util.exception import *
from api.decorators import require_form_args
from api.decorators import require_login_api
from api.decorators import optional_login_api
import constants.api
import database.attachment
import database.paste
import database.user
import util.cryptography

import datetime

@app.route(PasteSubmitURI.path, methods=['POST'])
@require_form_args(['contents'])
@optional_login_api
def submit_paste():
    """
    Endpoint for submitting a new paste.
    """
    if config.REQUIRE_LOGIN_TO_PASTE and not current_user.is_authenticated:
        return (
            flask.jsonify(constants.api.UNAUTHENTICATED_PASTES_DISABLED_FAILURE),
            constants.api.UNAUTHENTICATED_PASTES_DISABLED_FAILURE_CODE,
        )

    data = flask.request.get_json()

    if not config.ENABLE_PASTE_ATTACHMENTS and len(data.get('attachments', [])) > 0:
        return (
            flask.jsonify(constants.api.PASTE_ATTACHMENTS_DISABLED_FAILURE),
            constants.api.PASTE_ATTACHMENTS_DISABLED_FAILURE_CODE,
        )

    is_attachment_too_large = [
        # The data is encoded as a string: each character takes 1 B
        # The base64-encoded string is at 4/3x larger in size than the raw file
        len(attachment.get('data', '')) * 3 / 4.0 > config.MAX_ATTACHMENT_SIZE * 1000 * 1000
        for attachment in data.get('attachments', [])
    ]
    if any(is_attachment_too_large) and config.MAX_ATTACHMENT_SIZE > 0:
        return (
            flask.jsonify(constants.api.PASTE_ATTACHMENT_TOO_LARGE_FAILURE),
            constants.api.PASTE_ATTACHMENT_TOO_LARGE_FAILURE_CODE,
        )

    try:
        new_paste = database.paste.create_new_paste(
            contents=data.get('contents'),
            user_id=current_user.user_id if current_user.is_authenticated else None,
            #expiry_time=data.get('expiry_time'),
            expiry_time=(datetime.datetime.now() + datetime.timedelta(weeks=1)).strftime('%s'),
            title=data.get('title'),
            language=data.get('language'),
            password=data.get('password'),
            # The paste is considered an API post if any of the following conditions are met:
            # (1) The referrer is null.
            # (2) The Home or PastePostInterface URIs are *not* contained within the referrer string (if a paste was
            # posted via the web interface, this is where the user should be coming from, unless the client performed
            # some black magic and spoofed the referrer string or something equally sketchy).
            is_api_post=not flask.request.referrer or not any(
                [uri in flask.request.referrer for uri in [HomeURI.full_uri(), PastePostInterfaceURI.full_uri()]]
            ),
        )
        new_attachments = [
            database.attachment.create_new_attachment(
                paste_id=new_paste.paste_id,
                file_name=attachment.get('name'),
                file_size=attachment.get('size'),
                mime_type=attachment.get('mime_type'),
                file_data=attachment.get('data'),
            )
            for attachment in data.get('attachments', [])
        ]
        resp_data = new_paste.as_dict().copy()
        resp_data['attachments'] = [
            {
                'name': attachment.file_name,
                'size': attachment.file_size,
                'mime_type': attachment.mime_type,
            }
            for attachment in new_attachments
        ]
        return flask.jsonify(resp_data), constants.api.SUCCESS_CODE
    except:
        return flask.jsonify(constants.api.UNDEFINED_FAILURE), constants.api.UNDEFINED_FAILURE_CODE


@app.route(PasteDeactivateURI.path, methods=['POST'])
@require_form_args(['paste_id'])
@optional_login_api
def deactivate_paste():
    """
    Endpoint for deactivating an existing paste.
    The user can deactivate a paste with this endpoint in two ways:
    (1) Supply a deactivation token in the request, or
    (2) Be currently logged in, and own the paste.
    """
    data = flask.request.get_json()
    try:
        paste = database.paste.get_paste_by_id(util.cryptography.get_decid(data['paste_id']), active_only=True)
        if (current_user.is_authenticated and current_user.user_id == paste.user_id) or data.get('deactivation_token') == paste.deactivation_token:
            database.paste.deactivate_paste(paste.paste_id)
            return flask.jsonify({
                constants.api.RESULT: constants.api.RESULT_SUCCESS,
                constants.api.MESSAGE: None,
                'paste_id': util.cryptography.get_id_repr(paste.paste_id),
            }), constants.api.SUCCESS_CODE
        fail_msg = 'User does not own requested paste' if current_user.is_authenticated else 'Deactivation token is invalid'
        return flask.jsonify({
            constants.api.RESULT: constants.api.RESULT_FAULURE,
            constants.api.MESSAGE: fail_msg,
            constants.api.FAILURE: 'auth_failure',
            'paste_id': util.cryptography.get_id_repr(paste.paste_id),
        }), constants.api.AUTH_FAILURE_CODE
    except (PasteDoesNotExistException, InvalidIDException):
        return flask.jsonify(constants.api.NONEXISTENT_PASTE_FAILURE), constants.api.NONEXISTENT_PASTE_FAILURE_CODE
    except:
        return flask.jsonify(constants.api.UNDEFINED_FAILURE), constants.api.UNDEFINED_FAILURE_CODE


@app.route(PasteSetPasswordURI.path, methods=['POST'])
@require_form_args(['paste_id', 'password'], allow_blank_values=True)
@require_login_api
def set_paste_password():
    """
    Modify a paste's password, unset it, or set a new one.
    """
    data = flask.request.get_json()
    try:
        paste = database.paste.get_paste_by_id(util.cryptography.get_decid(data['paste_id']), active_only=True)
        if paste.user_id != current_user.user_id:
            return flask.jsonify({
                constants.api.RESULT: constants.api.RESULT_FAULURE,
                constants.api.MESSAGE: 'User does not own the specified paste',
                constants.api.FAILURE: 'auth_failure',
                'paste_id': util.cryptography.get_id_repr(paste.paste_id),
            }), constants.api.AUTH_FAILURE_CODE
        database.paste.set_paste_password(paste.paste_id, data['password'])
        return flask.jsonify({
            constants.api.RESULT: constants.api.RESULT_SUCCESS,
            constants.api.MESSAGE: None,
            'paste_id': util.cryptography.get_id_repr(paste.paste_id),
        }), constants.api.SUCCESS_CODE
    except (PasteDoesNotExistException, InvalidIDException):
        return flask.jsonify(constants.api.NONEXISTENT_PASTE_FAILURE), constants.api.NONEXISTENT_PASTE_FAILURE_CODE
    except:
        return flask.jsonify(constants.api.UNDEFINED_FAILURE), constants.api.UNDEFINED_FAILURE_CODE


@app.route(PasteDetailsURI.path, methods=['POST'])
@require_form_args(['paste_id'])
def paste_details():
    """
    Retrieve details for a particular paste ID.
    """
    data = flask.request.get_json()
    try:
        paste = database.paste.get_paste_by_id(util.cryptography.get_decid(data['paste_id']), active_only=True)
        attachments = database.attachment.get_attachments_for_paste(util.cryptography.get_decid(data['paste_id']), active_only=True)
        paste_details_dict = paste.as_dict()
        paste_details_dict['poster_username'] = 'Anonymous'
        paste_details_dict['attachments'] = [
            attachment.as_dict()
            for attachment in attachments
        ]
        if paste.user_id:
            poster = database.user.get_user_by_id(paste.user_id)
            paste_details_dict['poster_username'] = poster.username
        if not paste.password_hash or (data.get('password') and paste.password_hash == util.cryptography.secure_hash(data.get('password'))):
            return flask.jsonify({
                constants.api.RESULT: constants.api.RESULT_SUCCESS,
                constants.api.MESSAGE: None,
                'details': paste_details_dict,
            }), constants.api.SUCCESS_CODE
        else:
            return flask.jsonify({
                constants.api.RESULT: constants.api.RESULT_FAULURE,
                constants.api.MESSAGE: 'Password-protected paste: either no password or wrong password supplied',
                constants.api.FAILURE: 'password_mismatch_failure',
                'details': {},
            }), constants.api.AUTH_FAILURE_CODE
    except (PasteDoesNotExistException, UserDoesNotExistException, InvalidIDException):
        return flask.jsonify(constants.api.NONEXISTENT_PASTE_FAILURE), constants.api.NONEXISTENT_PASTE_FAILURE_CODE
    except:
        return flask.jsonify(constants.api.UNDEFINED_FAILURE), constants.api.UNDEFINED_FAILURE_CODE


@app.route(PastesForUserURI.path, methods=['POST'])
@require_login_api
def pastes_for_user():
    """
    Get all pastes for the currently logged in user.
    """
    try:
        return flask.jsonify({
            constants.api.RESULT: constants.api.RESULT_SUCCESS,
            constants.api.MESSAGE: None,
            'pastes': [
                paste.as_dict()
                for paste in database.paste.get_all_pastes_for_user(current_user.user_id, active_only=True)
            ],
        }), constants.api.SUCCESS_CODE
    except:
        return flask.jsonify(constants.api.UNDEFINED_FAILURE), constants.api.UNDEFINED_FAILURE_CODE


@app.route(RecentPastesURI.path, methods=['POST'])
@require_form_args(['page_num', 'num_per_page'])
def recent_pastes():
    """
    Get details for the most recent pastes.
    """
    try:
        data = flask.request.get_json()
        return flask.jsonify({
            constants.api.RESULT: constants.api.RESULT_SUCCESS,
            constants.api.MESSAGE: None,
            'pastes': [
                paste.as_dict() for paste in database.paste.get_recent_pastes(data['page_num'], data['num_per_page'])
            ],
        }), constants.api.SUCCESS_CODE
    except:
        return flask.jsonify(constants.api.UNDEFINED_FAILURE), constants.api.UNDEFINED_FAILURE_CODE


@app.route(TopPastesURI.path, methods=['POST'])
@require_form_args(['page_num', 'num_per_page'])
def top_pastes():
    """
    Get details for the top pastes.
    """
    try:
        data = flask.request.get_json()
        return flask.jsonify({
            constants.api.RESULT: constants.api.RESULT_SUCCESS,
            constants.api.MESSAGE: None,
            'pastes': [
                paste.as_dict() for paste in database.paste.get_top_pastes(data['page_num'], data['num_per_page'])
            ],
        }), constants.api.SUCCESS_CODE
    except:
        return flask.jsonify(constants.api.UNDEFINED_FAILURE), constants.api.UNDEFINED_FAILURE_CODE