summaryrefslogtreecommitdiff
path: root/qvisqve/authz_attempt.py
blob: 21d9be532ef1e2aa21f0001076040180613dda1a (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
# Copyright (C) 2018  Lars Wirzenius
#
# 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/>.


import qvisqve


class AuthorizationAttempt:

    def __init__(self):
        self._attempt_id = None
        self._client_id = None
        self._subject_id = None
        self._state = None
        self._redirect_uri = None
        self._scope = None
        self._authorization_code = None

    def set_client_id(self, client_id):
        self._client_id = client_id

    def get_client_id(self):
        return self._client_id

    def set_subject_id(self, subject_id):
        self._subject_id = subject_id

    def get_subject_id(self):
        return self._subject_id

    def set_state(self, state):
        self._state = state

    def get_state(self):
        return self._state

    def set_redirect_uri(self, uri):
        self._redirect_uri = uri

    def get_redirect_uri(self):
        return self._redirect_uri

    def set_scope(self, scope):
        self._scope = scope

    def get_scope(self):
        return self._scope

    def set_attempt_id(self, attempt_id):
        required = [
            '_client_id',
            '_state',
            '_redirect_uri',
            '_scope',
        ]
        for attr in required:
            if getattr(self, attr, None) is None:
                raise AuthorizationAttemptError()

        self._attempt_id = attempt_id

    def get_attempt_id(self):
        return self._attempt_id

    def set_authorization_code(self, authorization_code):
        self._authorization_code = authorization_code

    def get_authorization_code(self):
        return self._authorization_code


class AuthorizationAttemptError(Exception):

    pass


class AuthorizationAttempts:

    def __init__(self):
        self._attempts = []

    def create_attempt(self, urlparams):
        gen = qvisqve.NonceGenerator()
        attempt_id = gen.create_nonce()

        aa = AuthorizationAttempt()

        aa.set_client_id(urlparams['client_id'])
        aa.set_state(urlparams['state'])
        aa.set_redirect_uri(urlparams['redirect_uri'])
        aa.set_scope(urlparams['scope'])

        aa.set_attempt_id(attempt_id)

        self._attempts.append(aa)
        return aa

    def delete_by_id(self, attempt_id):
        self._attempts = [
            aa for aa in self._attempts
            if aa.get_attempt_id() != attempt_id
        ]

    def find_by_id(self, attempt_id):
        for aa in self._attempts:
            if aa.get_attempt_id() == attempt_id:
                return aa
        return None

    def find_by_code(self, code):
        for aa in self._attempts:
            if aa.get_authorization_code() == code:
                return aa
        return None