summaryrefslogtreecommitdiff
path: root/talerbank/app/schemas.py
blob: 6b529b301e9dbd48f78788bc6d174e85d963fb0a (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
##
# This file is part of TALER
# (C) 2014, 2015, 2016 Taler Systems SA
#
#  TALER is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation; either version 3, or
# (at your option) any later version.
#
#  TALER is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public
# License along with TALER; see the file COPYING.  If not, see
# <http://www.gnu.org/licenses/>
#
#  @author Marcello Stanisci
#  @brief definitions of JSON schemas for validating data

import json
from django.conf import settings
from django.core.exceptions import ValidationError
from django import forms
from django.core.validators import RegexValidator
from urllib.parse import urlparse

##
# Constant value for the biggest number the bank handles.
# This value is just equal to the biggest number that JavaScript
# can handle (because of the wallet).
# FIXME: also defined in views.py.  Need a common.py to contain
# such definitions ?
UINT64_MAX = (2**64) - 1

##
# Pattern for amounts, plain RegEx.
AMOUNT_REGEX = "^[A-Za-z0-9_-]+:([0-9]+)\.?([0-9]+)?$"


##
# Exception class to be raised when a expected URL parameter
# is not found.
class InvalidSession(ValueError):
    ##
    # Init method.
    #
    # @param self the object itself.
    # @param http_status_code the HTTP response code to return
    #        to the caller (client).
    def __init__(self, http_status_code):
        self.hint = "Landed on a broken session"
        self.http_status_code = http_status_code
        super().__init__()

class InternalServerError(Exception):
    def __init__(self, hint):
        self.hint = hint
        self.http_status_code = 500
        self.taler_error_code = 1011 # TALER_EC_INTERNAL_LOGIC_ERROR

##
# Exception class to be raised when a JSON
# object does not respect a specification.
class JSONFieldException(ValueError):

    ##
    # Init method.
    #
    # @param self the object itself.
    # @param error object containing the hint.
    # @param http_status_code the HTTP response code to return
    #        to the caller (client).
    def __init__(self, error, http_status_code):
        self.hint = json.dumps(error.as_json())
        self.http_status_code = http_status_code
        self.taler_error_code = 5106
        super().__init__()


##
# Exception class to be raised when at least one expected URL
# parameter is either not found or malformed.
class URLParamValidationError(ValueError):
    ##
    # Init method.
    #
    # @param self the object itself.
    # @param error object containing the hint.
    # @param http_status_code the HTTP response code to return
    #        to the caller (client).
    def __init__(self, error, http_status_code):
        self.hint = json.stringify(error.as_json())
        self.http_status_code = http_status_code
        self.taler_error_code = 5105
        super().__init__()


class AuthForm(forms.Form):
    type = forms.CharField(
        validators=[
            RegexValidator(
                "^basic$", message="Only 'basic' method provided for now"
            )
        ]
    )

    data = forms.Field(required=False)


class AuthField(forms.Field):
    ##
    # No need to touch the input.  Dict is good
    # and gets validated by the "validate()" method.
    def to_python(self, value):
        return value

    ##
    # Validate input.
    def validate(self, value):
        af = AuthForm(value)
        if not af.is_valid():
            raise ValidationError(json.dumps(af.errors.as_json()))


class BankValidator():
    def __init__(self, validator, data):
        self.validation_result = validator(data)
        if not self.validation_result.is_valid():
            raise JSONFieldException(self.validation_result.errors, 400)

    def get(self, name, default=None):
        ret = self.validation_result.cleaned_data.get(name) 
        if not ret:
            return default
        return ret


class RejectData(BankValidator):
    def __init__(self, data):
        super(RejectData, self).__init__(self.InnerValidator, data)

    class InnerValidator(forms.Form):
        auth = AuthField()
        # FIXME: adjust min/max values.
        row_id = forms.IntegerField()
        account_number = forms.IntegerField()


class AddIncomingData(BankValidator):
    def __init__(self, data):
        super(AddIncomingData, self).__init__(self.InnerValidator, data)

    class InnerValidator(forms.Form):
        auth = AuthField()
        amount = forms.CharField(
            validators=[
                RegexValidator(
                    AMOUNT_REGEX, message="Format CURRENCY:X[.Y] not respected"
                )
            ]
        )
        subject = forms.CharField()
        credit_account = forms.IntegerField(min_value=1)
        exchange_url = forms.URLField()


##
# Subset of /history and /history-range input.
class HistoryParamsBase(forms.Form):
    auth = forms.CharField(
        validators=[
            RegexValidator("^basic$", message="Only 'basic' is allowed")
        ]
    )

    cancelled = forms.CharField(
        required=False,
        empty_value="show",
        validators=[
            RegexValidator(
                "^(omit|show)$", message="Only 'omit' or 'show' are valid"
            )
        ]
    )

    ordering = forms.CharField(
        required=False,
        empty_value="descending",
        validators=[
            RegexValidator(
                "^(ascending|descending)$",
                message="Only 'ascending' or 'descending' are valid"
            )
        ]
    )

    direction = forms.CharField(
        validators=[
            RegexValidator(
                "^(debit|credit|both|cancel\+|cancel-)$",
                message="Only: debit/credit/both/cancel+/cancel-"
            )
        ]
    )

    # FIXME: adjust min/max values.
    account_number = forms.IntegerField(required=False)


class HistoryParams(BankValidator):
    def __init__(self, data):
        super(HistoryParams, self).__init__(self.InnerValidator, data)
 
    class InnerValidator(HistoryParamsBase):
        # FIXME: adjust min/max values.
        delta = forms.IntegerField()
        start = forms.IntegerField(required=False)


class HistoryRangeParams(BankValidator):

    def __init__(self, data):
        super(HistoryRangeParams, self).__init__(self.InnerValidator, data)

    class InnerValidator(HistoryParamsBase):
        # FIXME: adjust min/max values.
        end = forms.IntegerField()
        start = forms.IntegerField()


class PaytoField(forms.Field):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def to_python(self, value):
        return value

    def validate(self, value):

        # The request misses this, default exchange
        # will be used.  NOTE: experience showed that the
        # "required=False" argument given when init the object
        # does NOT prevent this function from being called!
        if not value:
            return
        wire_uri = urlparse(value)
        if "payto" != wire_uri.scheme:
            raise ValidationError("URL is not 'payto'")


class WithdrawHeadless(BankValidator):

    def __init__(self, data):
        super(WithdrawHeadless, self).__init__(self.InnerValidator, data)

    class InnerValidator(forms.Form):
        auth = AuthField()
        amount = forms.CharField(
            validators=[
                RegexValidator(
                    AMOUNT_REGEX, message="Format CURRENCY:X[.Y] not respected"
                )
            ]
        )
        reserve_pub = forms.CharField(required=True)
        exchange_wire_details = PaytoField(required=False)

class WithdrawHeadlessUri(forms.Form):
    amount = forms.CharField(
        validators=[
            RegexValidator(
                AMOUNT_REGEX, message="Format CURRENCY:X[.Y] not respected"
            )
        ]
    )


class SenderWireDetails(forms.Form):
    # FIXME: must be changed to 'payto' format.
    type = forms.CharField()
    bank_url = forms.URLField()
    account_number = forms.IntegerField(min_value=1)


class SenderWireDetailsField(forms.Field):
    def to_python(self, value):
        return value

    def validate(self, value):
        swd = SenderWireDetails(value)
        if not swd.is_valid():
            raise ValidationError(json.dumps(swd.errors.as_json()))