aboutsummaryrefslogtreecommitdiff
path: root/app/models/token.py
blob: 42357a0a0bea534c3a59425335cd4303427af4bb (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# Copyright (C) 2014 Linaro Ltd.
#
# This program 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 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""The API token model to store token in the DB."""

import bson
import datetime
import netaddr
import netaddr.core
import types
import uuid

import models
import models.base as modb

PROPERTIES_SIZE = 16


# pylint: disable=too-many-instance-attributes
# pylint: disable=invalid-name
# pylint: disable=too-many-public-methods
class Token(modb.BaseDocument):
    """This is an API token as stored in the DB.

    A token can be:
     - basic: All permissions have to be set (GET, POST, DELETE)
     - superuser: All methods are permitted, but cannot be used to create nor
        delete tokens.
     - admin: All operations permitted, can query, create and delete tokens.

    A token can be restricted to be used based on an IP address.

    Each token object has a properity called `properties`, a list of integers
    (0, 1) that describe the token. When the object is initialized, the list
    is all set to 0, and it has a default length of 16. Not all slots are used
    and will be left for future expansion.

    The property lists (index - property description) is as follows:
    - 0: if the token is an admin token
    - 1: if the token is a superuser token
    - 2: if the token can perform GET
    - 3: if the token can perfrom POST and PUT
    - 4: if the token can perform DELETE
    - 5: if the token is IP restricted
    - 6: if the token can create new tokens
    - 7: if the token is a boot lab token
    - 8: if the token can upload (POST/PUT) files
    """

    def __init__(self):
        self._created_on = None
        self._id = None
        self._name = None
        self._version = None

        self._expires_on = None
        self._ip_address = None
        self._properties = [0 for _ in range(0, PROPERTIES_SIZE)]
        self._token = None
        self.email = None
        self.expired = False
        self.username = None

    @property
    def collection(self):
        return models.TOKEN_COLLECTION

    @property
    def name(self):
        """The name of the object."""
        if not self._name:
            self._name = self.email
        return self._name

    @name.setter
    def name(self, value):
        """Set the name of the object."""
        self._name = value

    @property
    def id(self):
        """The ID of this object as returned by mongodb."""
        return self._id

    @id.setter
    def id(self, value):
        """Set the ID of this object with the ObjectID from mongodb.

        :param value: The ID of this object.
        :type value: str
        """
        self._id = value

    @property
    def token(self):
        """The real token value. A UUID4 string."""
        if self._token is None:
            self._token = str(uuid.uuid4())

        return self._token

    @token.setter
    def token(self, value):
        """Set the value of the token."""
        self._token = value

    @property
    def properties(self):
        """The properties array."""
        return self._properties

    @properties.setter
    def properties(self, value):
        """Set the properties array.

        :param value: The array.
        :type value: list
        """
        if not isinstance(value, types.ListType):
            raise TypeError(
                "Properties field must be a list, got: %s", type(value)
            )
        if len(value) != 16:
            raise ValueError(
                "Properties list size must be %s", PROPERTIES_SIZE
            )
        self._properties = value

    @property
    def created_on(self):
        """When this object was created.

        A datetime object in UTC timezone.
        """
        if not self._created_on:
            self._created_on = datetime.datetime.now(tz=bson.tz_util.utc)
        return self._created_on

    @created_on.setter
    def created_on(self, value):
        """Set the creation date of the object."""
        self._created_on = value

    @property
    def expires_on(self):
        """When this token is supposed to expire."""
        return self._expires_on

    @expires_on.setter
    def expires_on(self, value):
        """Set the expiry date of the token.

        A datetime object with the following format: %Y-%M-%d
        """
        self._expires_on = check_expires_date(value)

    @property
    def ip_address(self):
        """The list of IP addresses associated with this token."""
        return self._ip_address

    @ip_address.setter
    def ip_address(self, value):
        """Set the IP address for this token.

        :param value: The IP address or a list of.
        :type value: str or list
        """
        if value is not None:
            if not isinstance(value, types.ListType):
                value = [value]
            value = check_ip_address(value)

        self._ip_address = value

    @property
    def is_admin(self):
        """If the token is an admin one."""
        return self._properties[0]

    @is_admin.setter
    def is_admin(self, value):
        """Make this token an admin one.

        This will update also other fields, turning it into a real admin token.
        An admin token can perform GET, POST and DELETE and can create new
        tokens.
        """
        value = check_attribute_value(value)

        self._properties[0] = value
        # Admin tokens can GET, POST and DELETE, are superuser and can create
        # new tokens.
        self._properties[1] = value
        self._properties[2] = value
        self._properties[3] = value
        self._properties[4] = value
        self._properties[6] = value
        self._properties[8] = value

    @property
    def is_superuser(self):
        """If the token is a super user one."""
        return self._properties[1]

    @is_superuser.setter
    def is_superuser(self, value):
        """Make this token a superuser one.

        This will update also other fields. A superuser token cannot create
        new tokens.
        """
        value = check_attribute_value(value)

        # Force admin to zero, and also if it can create new tokens, regardless
        # of what is passed. A super user cannot create new tokens.
        self._properties[0] = 0
        self._properties[6] = 0

        self._properties[1] = value
        self._properties[2] = value
        self._properties[3] = value
        self._properties[4] = value
        self._properties[8] = value

    @property
    def is_get_token(self):
        """If the token can perform GET requests."""
        return self._properties[2]

    @is_get_token.setter
    def is_get_token(self, value):
        """Set whether the token can perform GET requests."""
        value = check_attribute_value(value)
        self._properties[2] = value

    @property
    def is_post_token(self):
        """If the token can perform POST requests."""
        return self._properties[3]

    @is_post_token.setter
    def is_post_token(self, value):
        """Sets whether the token can perform POST requests."""
        value = check_attribute_value(value)
        self._properties[3] = value

    @property
    def is_delete_token(self):
        """If the token can perform DELETE requests."""
        return self._properties[4]

    @is_delete_token.setter
    def is_delete_token(self, value):
        """Set whether the token can perform DELETE requests."""
        value = check_attribute_value(value)
        self._properties[4] = value

    @property
    def is_ip_restricted(self):
        """If the token is IP restricted."""
        return self._properties[5]

    @is_ip_restricted.setter
    def is_ip_restricted(self, value):
        """Set whether the token is IP restricted."""
        value = check_attribute_value(value)
        self._properties[5] = value

    @property
    def can_create_token(self):
        """If with this token it is possible to create new tokens."""
        return self._properties[6]

    @can_create_token.setter
    def can_create_token(self, value):
        """Sets whether this token can create new tokens."""
        value = check_attribute_value(value)
        self._properties[6] = value

    @property
    def is_lab_token(self):
        """If the token is used as a lab one."""
        return self._properties[7]

    @is_lab_token.setter
    def is_lab_token(self, value):
        """Set whether the token is used as a lab one."""
        value = check_attribute_value(value)
        self._properties[7] = value

    @property
    def is_upload_token(self):
        """If the token can be used to upload files."""
        return self._properties[8]

    @is_upload_token.setter
    def is_upload_token(self, value):
        """Set whether the token can upload files."""
        value = check_attribute_value(value)
        self._properties[8] = value

    def is_valid_ip(self, address):
        """Check if an IP address is valid for a token.

        :param address: The IP address to verify.
        :return True or False.
        """
        return_value = False

        if not self.is_ip_restricted:
            return_value = True
        else:
            try:
                address = convert_ip_address(address)
                if address in netaddr.IPSet(self.ip_address):
                    return_value = True
            except netaddr.core.AddrFormatError:
                # If we get an error converting the IP address, consider it
                # not valid and force False.
                return_value = False

        return return_value

    @property
    def version(self):
        """The schema version of this object."""
        return self._version

    @version.setter
    def version(self, value):
        """Set the schema version of this object.

        :param value: The schema string.
        :type param: str
        """
        self._version = value

    def to_dict(self):
        """Return a dictionary view of the object.

        :return The object as a dictionary.
        """
        doc_dict = {
            models.CREATED_KEY: self.created_on,
            models.EMAIL_KEY: self.email,
            models.EXPIRED_KEY: self.expired,
            models.EXPIRES_KEY: self.expires_on,
            models.NAME_KEY: self.name,
            models.VERSION_KEY: self.version,
        }
        if self.ip_address is not None:
            doc_dict[models.IP_ADDRESS_KEY] = \
                [str(x) for x in self.ip_address if x]
        else:
            doc_dict[models.IP_ADDRESS_KEY] = None
        doc_dict[models.PROPERTIES_KEY] = self.properties
        doc_dict[models.TOKEN_KEY] = self.token
        doc_dict[models.USERNAME_KEY] = self.username
        if self.id:
            doc_dict[models.ID_KEY] = self.id

        return doc_dict

    # pylint: disable=maybe-no-member
    @staticmethod
    def from_json(json_obj):
        """Build a Token object from a JSON string.

        :param json_obj: The JSON object to start from.
        :return An instance of `Token`.
        """
        token_doc = None
        if json_obj:
            token_doc = Token()
            json_get = json_obj.get
            token_doc.id = json_get(models.ID_KEY)
            token_doc.name = json_get(models.NAME_KEY)
            token_doc.email = json_get(models.EMAIL_KEY)
            token_doc.username = json_get(models.USERNAME_KEY, None)
            token_doc.token = json_get(models.TOKEN_KEY, None)
            token_doc.created_on = json_get(models.CREATED_KEY, None)
            token_doc.expired = json_get(models.EXPIRED_KEY, False)
            token_doc.expires_on = json_get(models.EXPIRES_KEY, None)
            token_doc.properties = json_get(
                models.PROPERTIES_KEY, [0 for _ in range(0, PROPERTIES_SIZE)])
            token_doc.ip_address = json_get(models.IP_ADDRESS_KEY, None)
            token_doc.version = json_get(models.VERSION_KEY, "1.0")
        return token_doc


def check_attribute_value(value):
    """Make sure the value passed for the properties list is valid.

    A properties value must be an integer or a boolean, either 0 or 1.
    Negative number will be converted into their absolute values.

    :param value: The value to check.
    :return The value converted into an int.
    :raise TypeError if the value is not IntType or BooleanType; ValueError
        if it is not 0 or 1.
    """
    if not isinstance(value, (types.IntType, types.BooleanType)):
        raise TypeError("Wrong value passed, must be int or bool")

    value = abs(int(value))
    if 0 != value != 1:
        raise ValueError("Value must be 0 or 1")

    return value


def check_expires_date(value):
    """Check and convert the expiry date.

    Expiry date must follow this format: %Y-%m-%d.

    :param value: The date string.
    :return The converted date, or None if the passed value is None.
    :raise ValueError if the date string cannot be parsed accordingly to
        the predefined format.
    """
    try:
        if value:
            value = datetime.datetime.strptime(value, "%Y-%m-%d")
    except ValueError, ex:
        raise ex
    else:
        return value


def convert_ip_address(address):
    """Convert a string into an IPAddress or IPNetwork.

    :return An `IPAddress` or `IPNetwork` object.
    """
    if "/" in address:
        address = netaddr.IPNetwork(address).ipv6(ipv4_compatible=True)
    else:
        address = netaddr.IPAddress(address).ipv6(ipv4_compatible=True)
    return address


def check_ip_address(addrlist):
    """Perform sanity check and conversion on the IP address list.

    :return The address list converted with `IPaddress` and/or `IPNetwork`
        objects.
    """
    if not isinstance(addrlist, types.ListType):
        raise TypeError("Value must be a list of addresses")

    for idx, address in enumerate(addrlist):
        try:
            addrlist[idx] = convert_ip_address(address)
        except netaddr.core.AddrFormatError:
            raise ValueError(
                "Address %s is not a valid IP address or network", address
            )
    return addrlist