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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Appier Framework
# Copyright (c) 2008-2024 Hive Solutions Lda.
#
# This file is part of Hive Appier Framework.
#
# Hive Appier Framework is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Appier Framework 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
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Appier Framework. If not, see <http://www.apache.org/licenses/>.

__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """

__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

import os
import time
import uuid
import hmac
import zlib
import pickle
import shelve
import base64
import hashlib
import datetime

from . import config
from . import legacy
from . import redisdb
from . import exceptions

EXPIRE_TIME = datetime.timedelta(days=31)
""" The default expire time to be used in new sessions
in case no expire time is provided to the creation of
the session instance """


class Session(object):
    """
    Abstract session class to be used as a reference in
    the implementation of the proper classes.

    Some of the global functionality may be abstracted
    under this class and reused in the concrete classes.
    """

    def __init__(
        self, name="session", expire=EXPIRE_TIME, sid=None, address=None, growing=True
    ):
        object.__init__(self)
        self.sid = sid if sid else self._gen_sid()
        self.name = name
        self.address = address
        self.create = time.time()
        self.modify = self.create
        self.duration = self._to_seconds(expire)
        self.expire = self.create + self.duration
        self.growing = growing
        self.dirty = True
        self.transient = False
        self.data_t = dict()

    def __len__(self):
        return self.data_t.__len__()

    def __getitem__(self, key):
        return self.data_t.__getitem__(key)

    def __setitem__(self, key, value):
        self.mark(extend=self.growing)
        self.data_t.__setitem__(key, value)

    def __delitem__(self, key):
        self.mark(extend=self.growing)
        self.data_t.__delitem__(key)

    def __iter__(self):
        return self.data_t.__iter__()

    def __contains__(self, item):
        return self.data_t.__contains__(item)

    def __nonzero__(self):
        return True

    def __bool__(self):
        return True

    def __getstate__(self):
        return dict(
            sid=self.sid,
            name=self.name,
            address=self.address,
            create=self.create,
            modify=self.modify,
            duration=self.duration,
            expire=self.expire,
            growing=self.growing,
            dirty=self.dirty,
        )

    def __setstate__(self, state):
        for name in (
            "sid",
            "name",
            "address",
            "create",
            "modify",
            "duration",
            "expire",
            "growing",
            "dirty",
        ):
            value = state.get(name, None)
            setattr(self, name, value)
        self.data_t = dict()

    @classmethod
    def new(cls, *args, **kwargs):
        return cls(*args, **kwargs)

    @classmethod
    def get_s(cls, sid, request=None):
        return cls()

    @classmethod
    def expire(cls, sid):
        pass

    @classmethod
    def count(cls):
        return 0

    @classmethod
    def all(cls):
        return {}

    @classmethod
    def open(cls):
        cls.gc()

    @classmethod
    def close(cls):
        pass

    @classmethod
    def empty(cls):
        pass

    @classmethod
    def gc(cls):
        pass

    def keys(self):
        return ()

    def iterkeys(self):
        return self.keys()

    def values(self):
        return ()

    def itervalues(self):
        return self.values()

    def items(self):
        return ()

    def iteritems(self):
        return self.items()

    def pop(self, key, default=None):
        if not key in self:
            return default
        value = self[key]
        del self[key]
        return value

    def start(self):
        pass

    def flush(self, request=None):
        self.mark(dirty=False)

    def mark(self, dirty=True, extend=False):
        self.dirty = dirty
        if extend:
            self.extend()

    def extend(self, duration=None):
        duration = duration or self.duration
        if not duration:
            return
        self.modify = time.time()
        self.duration = duration
        self.expire = self.modify + duration
        self.mark()
        return self.expire

    def is_expired(self):
        has_expire = hasattr(self, "expire")
        if not has_expire:
            return False

        current = time.time()
        return current >= self.expire

    def is_dirty(self):
        if not hasattr(self, "dirty"):
            return True
        return self.dirty

    def is_loaded(self):
        return True

    def ensure(self, *args, **kwargs):
        return self

    def get(self, key, default=None):
        try:
            value = self.__getitem__(key)
        except KeyError:
            value = default
        return value

    def set(self, key, value):
        self.__setitem__(key, value)

    def delete(self, key, force=False):
        if not force and not key in self:
            return
        self.__delitem__(key)

    def get_t(self, key, default=None):
        try:
            value = self.data_t[key]
        except KeyError:
            value = default
        return value

    def set_t(self, key, value):
        self.data_t[key] = value

    def delete_t(self, key, force=False):
        if not force and not key in self.data_t:
            return
        del self.data_t[key]

    def timeout(self):
        current = time.time()
        return self.expire - current

    def set_transient(self, value=True):
        """
        Marks the current session as transient only meaning
        that all of the operations will be made in memory
        and not persisted through session engine.

        This behaviour is ideal for secret key based session
        that do not have long lived life-cycles.

        :type value: bool
        :param value: The new value to be set for transient.
        """

        self.transient = value

    def _gen_sid(self):
        token_s = str(uuid.uuid4())
        token_s = legacy.bytes(token_s)
        token = hashlib.sha256(token_s).hexdigest()
        return token

    def _to_seconds(self, delta):
        return (
            delta.microseconds + (delta.seconds + delta.days * 24 * 3600) * 10**6
        ) / 10**6


class MockSession(Session):
    """
    Temporary mock session to be used while no final
    session is yet defined in the request, this is a
    special case of a session and should not comply
    with the pre-defined standard operations.
    """

    def __init__(self, request, name="mock", *args, **kwargs):
        Session.__init__(self, name=name, *args, **kwargs)
        self.request = request
        self.setter = None

    def __setitem__(self, key, value):
        if self.transient:
            return Session.__setitem__(self, key, value)
        session = self.ensure(sid=self.sid, address=self.address)
        return session.__setitem__(key, value)

    def __setstate__(self, state):
        Session.__setstate__(self, state)
        self.request = None

    def is_loaded(self):
        return False

    def ensure(self, *args, **kwargs):
        if self.transient:
            return self
        self._ensure_names(kwargs)
        session_c = self.request.session_c
        session = session_c.new(*args, **kwargs)
        self.request.session = session
        self.request.set_cookie = "sid=%s" % session.sid
        return session

    def _ensure_names(self, kwargs):
        for name in ("sid", "address"):
            if name in kwargs:
                continue
            kwargs[name] = getattr(self, name)


class DataSession(Session):
    def __init__(self, *args, **kwargs):
        Session.__init__(self, *args, **kwargs)
        self.data = {}

    def __len__(self):
        return Session.__len__(self) + self.data.__len__()

    def __getitem__(self, key):
        try:
            return self.data.__getitem__(key)
        except KeyError:
            return Session.__getitem__(self, key)

    def __setitem__(self, key, value):
        self.mark(extend=self.growing)
        return self.data.__setitem__(key, value)

    def __delitem__(self, key):
        try:
            self.mark(extend=self.growing)
            return self.data.__delitem__(key)
        except KeyError:
            return Session.__delitem__(self, key)

    def __iter__(self):
        return self._merged.__iter__()

    def __contains__(self, item):
        return Session.__contains__(self, item) or self.data.__contains__(item)

    def __getstate__(self):
        state = Session.__getstate__(self)
        state.update(data=self.data)
        return state

    def __setstate__(self, state):
        Session.__setstate__(self, state)
        for name in ("data",):
            setattr(self, name, state[name])

    def keys(self):
        return self._merged.keys()

    def values(self):
        return self._merged.values()

    def items(self):
        return self._merged.items()

    def sorted(self):
        keys = self.keys()
        keys = list(keys)
        keys.sort()
        return keys

    @property
    def _merged(self):
        if not self.data_t:
            return self.data
        if not self.data:
            return self.data_t
        merged = dict()
        merged.update(self.data_t)
        merged.update(self.data)
        return merged


class MemorySession(DataSession):
    SESSIONS = {}
    """ Global static sessions map where all the
    (in-memory) session instances are going to be
    stored to be latter retrieved """

    def __init__(self, name="session", *args, **kwargs):
        DataSession.__init__(self, name=name, *args, **kwargs)
        self["sid"] = self.sid

    @classmethod
    def new(cls, *args, **kwargs):
        session = cls(*args, **kwargs)
        cls.SESSIONS[session.sid] = session.__getstate__()
        return session

    @classmethod
    def get_s(cls, sid, request=None):
        state = cls.SESSIONS.get(sid, None)
        if not state:
            return state
        session = cls.__new__(cls)
        session.__setstate__(state)
        is_expired = session.is_expired()
        if is_expired:
            cls.expire(sid)
        session = None if is_expired else session
        return session

    @classmethod
    def expire(cls, sid):
        del cls.SESSIONS[sid]

    @classmethod
    def count(cls):
        return len(cls.SESSIONS)

    @classmethod
    def all(cls):
        return cls.SESSIONS

    @classmethod
    def empty(cls):
        cls.SESSIONS.empty()


class FileSession(DataSession):
    """
    Shelve based file system session engine that store the
    session information in a single file.

    This session engine should be used carefully as race
    conditions in the file access may corrupt its contents,
    thus making it not suitable for multi-threaded or multi-
    process environments.
    """

    SHELVE = None
    """ Global shelve object reference should reflect the
    result of opening a file in shelve mode, this is a global
    object and only one instance should exist per process """

    def __init__(self, name="file", *args, **kwargs):
        DataSession.__init__(self, name=name, *args, **kwargs)
        self["sid"] = self.sid

    @classmethod
    def new(cls, *args, **kwargs):
        if cls.SHELVE == None:
            cls.open()
        session = cls(*args, **kwargs)
        cls.SHELVE[session.sid] = session
        return session

    @classmethod
    def get_s(cls, sid, request=None):
        if cls.SHELVE == None:
            cls.open()
        session = cls.SHELVE.get(sid, None)
        if not session:
            return session
        is_expired = session.is_expired()
        if is_expired:
            cls.expire(sid)
        session = None if is_expired else session
        return session

    @classmethod
    def expire(cls, sid):
        del cls.SHELVE[sid]

    @classmethod
    def count(cls):
        return len(cls.SHELVE)

    @classmethod
    def all(cls):
        return cls.SHELVE

    @classmethod
    def open(cls, file_path="session.shelve"):
        base_path = config.conf("APPIER_BASE_PATH", "")
        base_path = config.conf("SESSION_FILE_PATH", base_path)
        if base_path and not os.path.exists(base_path):
            os.makedirs(base_path)
        base_path = os.path.expanduser(base_path)
        base_path = os.path.abspath(base_path)
        base_path = os.path.normpath(base_path)
        file_path = os.path.join(base_path, file_path)
        cls.SHELVE = shelve.open(file_path, protocol=2, writeback=True)
        cls.gc()

    @classmethod
    def close(cls):
        if not cls.SHELVE:
            return
        cls.SHELVE.close()
        cls.SHELVE = None

    @classmethod
    def empty(cls):
        for sid in cls.SHELVE:
            del cls.SHELVE[sid]

    @classmethod
    def gc(cls):
        for sid in cls.SHELVE:
            session = cls.SHELVE.get(sid, None)
            is_expired = session.is_expired()
            if is_expired:
                cls.expire(sid)

    @classmethod
    def db_type(cls):
        shelve_cls = type(cls.SHELVE.dict)
        shelve_dbm = shelve_cls.__name__
        return shelve_dbm

    @classmethod
    def db_secure(cls):
        return cls.db_type() == "dbm"

    def flush(self, request=None, secure=None):
        if not self.is_dirty():
            return
        self.mark(dirty=False)
        self.sync(secure=secure)

    def sync(self, secure=None):
        cls = self.__class__
        if secure == None:
            secure = cls.db_secure()
        if secure:
            cls.SHELVE.close()
            cls.open()
        else:
            cls.SHELVE.sync()


class RedisSession(DataSession):
    REDIS = None
    """ Global shelve object reference should reflect the
    result of opening a file in shelve mode, this is a global
    object and only one instance should exist per process """

    SERIALIZER = pickle
    """ The serializer to be used for the values contained in
    the session (used on top of the class) """

    def __init__(self, name="redis", *args, **kwargs):
        DataSession.__init__(self, name=name, *args, **kwargs)
        self["sid"] = self.sid

    @classmethod
    def new(cls, *args, **kwargs):
        if cls.REDIS == None:
            cls.open()
        session = cls(*args, **kwargs)
        data = cls.SERIALIZER.dumps(session)
        timeout = session.timeout()
        timeout = int(timeout)
        cls.REDIS.setex(session.sid, value=data, time=timeout)
        return session

    @classmethod
    def get_s(cls, sid, request=None):
        if cls.REDIS == None:
            cls.open()
        data = cls.REDIS.get(sid)
        if not data:
            return data
        session = cls.SERIALIZER.loads(data)
        is_expired = session.is_expired()
        if is_expired:
            cls.expire(sid)
        session = None if is_expired else session
        return session

    @classmethod
    def expire(cls, sid):
        cls.REDIS.delete(sid)

    @classmethod
    def count(cls):
        if cls.REDIS == None:
            cls.open()
        return cls.REDIS.dbsize()

    @classmethod
    def all(cls):
        if cls.REDIS == None:
            cls.open()
        sessions = dict()
        sids = cls.REDIS.keys()
        for sid in sids:
            data = cls.REDIS.get(sid)
            try:
                session = cls.SERIALIZER.loads(data)
            except Exception:
                continue
            sessions[sid] = session
        return sessions

    @classmethod
    def open(cls):
        cls.REDIS = redisdb.get_connection()

    @classmethod
    def empty(cls):
        if cls.REDIS == None:
            cls.open()
        cls.REDIS.flushdb()

    def flush(self, request=None):
        if not self.is_dirty():
            return
        self.mark(dirty=False)
        cls = self.__class__
        data = cls.SERIALIZER.dumps(self)
        timeout = self.timeout()
        timeout = int(timeout)
        cls.REDIS.setex(self.sid, value=data, time=timeout)


class ClientSession(DataSession):
    """
    Client side based session, that used encryption to store the
    complete set of information for the session on the client side
    through the usage of cookies.

    This session class provides extreme scalability as every server
    instance is independent and does not require inter-communication.
    """

    SERIALIZER = pickle
    """ The serializer to be used for the values contained in
    the session (used on top of the class) """

    COOKIE_LIMIT = 4096
    """ The limit (in bytes) of a cookie to be properly handled
    by most of the modern web browsers """

    def __init__(self, name="client", *args, **kwargs):
        DataSession.__init__(self, name=name, *args, **kwargs)
        self["sid"] = self.sid

    @classmethod
    def get_s(cls, sid, request=None):
        data_b64 = request.cookies.get("session", None)
        if not data_b64:
            return None
        data = base64.b64decode(data_b64)
        data = zlib.decompress(data)
        data = cls._verify(data, request)
        session = cls.SERIALIZER.loads(data)
        is_expired = session.is_expired()
        if is_expired:
            cls.expire(sid)
        session = None if is_expired else session
        return session

    @classmethod
    def _sign(cls, data, request):
        secret = cls._secret(request)
        secret = legacy.bytes(secret)
        digest = hmac.new(secret, data).hexdigest()
        digest = legacy.bytes(digest)
        data = digest + b":" + data
        return data

    @classmethod
    def _verify(cls, data, request):
        secret = cls._secret(request)
        secret = legacy.bytes(secret)
        digest, data = data.split(b":", 1)
        expected = hmac.new(secret, data).hexdigest()
        expected = legacy.bytes(expected)
        valid = digest == expected
        if not valid:
            raise exceptions.SecurityError(message="Invalid signature for message")
        return data

    @classmethod
    def _secret(cls, request):
        owner = request.owner
        if not hasattr(owner, "secret"):
            return None
        return owner.secret

    def flush(self, request=None):
        cls = self.__class__
        if not self.is_dirty():
            return
        self.mark(dirty=False)
        cls = self.__class__
        data = cls.SERIALIZER.dumps(self)
        data = cls._sign(data, request)
        data = zlib.compress(data)
        data_b64 = base64.b64encode(data)
        data_b64 = legacy.str(data_b64)
        valid = len(data_b64) <= cls.COOKIE_LIMIT
        if not valid:
            raise exceptions.OperationalError(
                message="Session payload size over cookie limit"
            )
        request.set_cookie = "session=%s" % data_b64