sanic-security 1.16.12__py3-none-any.whl → 1.17.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sanic_security/authentication.py +379 -363
- sanic_security/authorization.py +240 -240
- sanic_security/configuration.py +125 -125
- sanic_security/exceptions.py +164 -164
- sanic_security/models.py +721 -701
- sanic_security/oauth.py +242 -241
- sanic_security/test/server.py +368 -368
- sanic_security/test/tests.py +547 -547
- sanic_security/utils.py +121 -121
- sanic_security/verification.py +253 -248
- {sanic_security-1.16.12.dist-info → sanic_security-1.17.0.dist-info}/METADATA +672 -672
- sanic_security-1.17.0.dist-info/RECORD +17 -0
- {sanic_security-1.16.12.dist-info → sanic_security-1.17.0.dist-info}/licenses/LICENSE +21 -21
- sanic_security-1.16.12.dist-info/RECORD +0 -17
- {sanic_security-1.16.12.dist-info → sanic_security-1.17.0.dist-info}/WHEEL +0 -0
- {sanic_security-1.16.12.dist-info → sanic_security-1.17.0.dist-info}/top_level.txt +0 -0
sanic_security/verification.py
CHANGED
@@ -1,248 +1,253 @@
|
|
1
|
-
import functools
|
2
|
-
from contextlib import suppress
|
3
|
-
|
4
|
-
from sanic.log import logger
|
5
|
-
from sanic.request import Request
|
6
|
-
|
7
|
-
from sanic_security.exceptions import (
|
8
|
-
JWTDecodeError,
|
9
|
-
NotFoundError,
|
10
|
-
MaxedOutChallengeError,
|
11
|
-
DeactivatedError,
|
12
|
-
)
|
13
|
-
from sanic_security.models import (
|
14
|
-
Account,
|
15
|
-
TwoStepSession,
|
16
|
-
CaptchaSession,
|
17
|
-
)
|
18
|
-
from sanic_security.utils import get_ip
|
19
|
-
|
20
|
-
"""
|
21
|
-
Copyright (c) 2020-present Nicholas Aidan Stewart
|
22
|
-
|
23
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
24
|
-
of this software and associated documentation files (the "Software"), to deal
|
25
|
-
in the Software without restriction, including without limitation the rights
|
26
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
27
|
-
copies of the Software, and to permit persons to whom the Software is
|
28
|
-
furnished to do so, subject to the following conditions:
|
29
|
-
|
30
|
-
The above copyright notice and this permission notice shall be included in all
|
31
|
-
copies or substantial portions of the Software.
|
32
|
-
|
33
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
34
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
35
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
36
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
37
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
38
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
39
|
-
SOFTWARE.
|
40
|
-
"""
|
41
|
-
|
42
|
-
|
43
|
-
async def request_two_step_verification(
|
44
|
-
request: Request, account: Account = None
|
45
|
-
) -> TwoStepSession:
|
46
|
-
"""
|
47
|
-
Creates a two-step session and deactivates the client's current two-step session if found.
|
48
|
-
|
49
|
-
Args:
|
50
|
-
request (Request): Sanic request parameter. Request body should contain form-data with the following argument(s): email.
|
51
|
-
account (Account): The account being associated with the new verification session. If None, an account is retrieved via the email in the request form-data or an existing two-step session.
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
request
|
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
|
-
two_step_session.
|
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
|
-
|
1
|
+
import functools
|
2
|
+
from contextlib import suppress
|
3
|
+
|
4
|
+
from sanic.log import logger
|
5
|
+
from sanic.request import Request
|
6
|
+
|
7
|
+
from sanic_security.exceptions import (
|
8
|
+
JWTDecodeError,
|
9
|
+
NotFoundError,
|
10
|
+
MaxedOutChallengeError,
|
11
|
+
DeactivatedError,
|
12
|
+
)
|
13
|
+
from sanic_security.models import (
|
14
|
+
Account,
|
15
|
+
TwoStepSession,
|
16
|
+
CaptchaSession,
|
17
|
+
)
|
18
|
+
from sanic_security.utils import get_ip
|
19
|
+
|
20
|
+
"""
|
21
|
+
Copyright (c) 2020-present Nicholas Aidan Stewart
|
22
|
+
|
23
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
24
|
+
of this software and associated documentation files (the "Software"), to deal
|
25
|
+
in the Software without restriction, including without limitation the rights
|
26
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
27
|
+
copies of the Software, and to permit persons to whom the Software is
|
28
|
+
furnished to do so, subject to the following conditions:
|
29
|
+
|
30
|
+
The above copyright notice and this permission notice shall be included in all
|
31
|
+
copies or substantial portions of the Software.
|
32
|
+
|
33
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
34
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
35
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
36
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
37
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
38
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
39
|
+
SOFTWARE.
|
40
|
+
"""
|
41
|
+
|
42
|
+
|
43
|
+
async def request_two_step_verification(
|
44
|
+
request: Request, account: Account = None, tag: str = "2sv"
|
45
|
+
) -> TwoStepSession:
|
46
|
+
"""
|
47
|
+
Creates a two-step session and deactivates the client's current two-step session if found.
|
48
|
+
|
49
|
+
Args:
|
50
|
+
request (Request): Sanic request parameter. Request body should contain form-data with the following argument(s): email.
|
51
|
+
account (Account): The account being associated with the new verification session. If None, an account is retrieved via the email in the request form-data or an existing two-step session.
|
52
|
+
tag (str): Label used to distinguish verification for specific purposes.
|
53
|
+
|
54
|
+
Raises:
|
55
|
+
NotFoundError
|
56
|
+
|
57
|
+
Returns:
|
58
|
+
two_step_session
|
59
|
+
"""
|
60
|
+
with suppress(NotFoundError, JWTDecodeError):
|
61
|
+
two_step_session = await TwoStepSession.decode(request)
|
62
|
+
if two_step_session.active:
|
63
|
+
await two_step_session.deactivate()
|
64
|
+
if not account:
|
65
|
+
account = two_step_session.bearer
|
66
|
+
if request.form.get("email") or not account:
|
67
|
+
account = await Account.get_via_email(request.form.get("email"))
|
68
|
+
two_step_session = await TwoStepSession.new(request, account, tag=tag)
|
69
|
+
request.ctx.session = two_step_session
|
70
|
+
return two_step_session
|
71
|
+
|
72
|
+
|
73
|
+
async def two_step_verification(request: Request, tag: str = "2sv") -> TwoStepSession:
|
74
|
+
"""
|
75
|
+
Validates a two-step verification attempt.
|
76
|
+
|
77
|
+
Args:
|
78
|
+
request (Request): Sanic request parameter. Request body should contain form-data with the following argument(s): code.
|
79
|
+
tag (str): Label used to distinguish verification for specific purposes.
|
80
|
+
|
81
|
+
Raises:
|
82
|
+
NotFoundError
|
83
|
+
JWTDecodeError
|
84
|
+
DeletedError
|
85
|
+
ExpiredError
|
86
|
+
DeactivatedError
|
87
|
+
UnverifiedError
|
88
|
+
DisabledError
|
89
|
+
ChallengeError
|
90
|
+
MaxedOutChallengeError
|
91
|
+
|
92
|
+
Returns:
|
93
|
+
two_step_session
|
94
|
+
"""
|
95
|
+
two_step_session = await TwoStepSession.decode(request, tag=tag)
|
96
|
+
two_step_session.validate()
|
97
|
+
two_step_session.bearer.validate()
|
98
|
+
try:
|
99
|
+
await two_step_session.check_code(request.form.get("code"))
|
100
|
+
except MaxedOutChallengeError as e:
|
101
|
+
logger.warning(
|
102
|
+
f"Client {get_ip(request)} has exceeded maximum two-step session {two_step_session.id} challenge attempts."
|
103
|
+
)
|
104
|
+
raise e
|
105
|
+
logger.info(
|
106
|
+
f"Client {get_ip(request)} has completed two-step session {two_step_session.id} challenge."
|
107
|
+
)
|
108
|
+
return two_step_session
|
109
|
+
|
110
|
+
|
111
|
+
def requires_two_step_verification(func=None, *, tag="2sv"):
|
112
|
+
"""
|
113
|
+
Validates a two-step verification attempt.
|
114
|
+
|
115
|
+
Args:
|
116
|
+
tag (str): Label used to distinguish verification for specific purposes.
|
117
|
+
|
118
|
+
Example:
|
119
|
+
This method is not called directly and instead used as a decorator:
|
120
|
+
|
121
|
+
@app.post("api/verification/attempt")
|
122
|
+
@requires_two_step_verification
|
123
|
+
async def on_verified(request):
|
124
|
+
response = json("Two-step verification attempt successful!", two_step_session.json())
|
125
|
+
return response
|
126
|
+
|
127
|
+
Raises:
|
128
|
+
NotFoundError
|
129
|
+
JWTDecodeError
|
130
|
+
DeletedError
|
131
|
+
ExpiredError
|
132
|
+
DeactivatedError
|
133
|
+
UnverifiedError
|
134
|
+
DisabledError
|
135
|
+
ChallengeError
|
136
|
+
MaxedOutChallengeError
|
137
|
+
"""
|
138
|
+
|
139
|
+
def decorator(func):
|
140
|
+
@functools.wraps(func)
|
141
|
+
async def wrapper(request, *args, **kwargs):
|
142
|
+
await two_step_verification(request, tag)
|
143
|
+
return await func(request, *args, **kwargs)
|
144
|
+
|
145
|
+
return wrapper
|
146
|
+
|
147
|
+
return decorator if func is None else decorator(func)
|
148
|
+
|
149
|
+
|
150
|
+
async def verify_account(request: Request) -> TwoStepSession:
|
151
|
+
"""
|
152
|
+
Verifies the client's account via two-step session code.
|
153
|
+
|
154
|
+
Args:
|
155
|
+
request (Request): Sanic request parameter. Request body should contain form-data with the following argument(s): code.
|
156
|
+
|
157
|
+
Raises:
|
158
|
+
NotFoundError
|
159
|
+
JWTDecodeError
|
160
|
+
DeletedError
|
161
|
+
ExpiredError
|
162
|
+
DeactivatedError
|
163
|
+
ChallengeError
|
164
|
+
MaxedOutChallengeError
|
165
|
+
|
166
|
+
Returns:
|
167
|
+
two_step_session
|
168
|
+
"""
|
169
|
+
two_step_session = await TwoStepSession.decode(request, tag="2fa")
|
170
|
+
if two_step_session.bearer.verified:
|
171
|
+
raise DeactivatedError("Account already verified.", 403)
|
172
|
+
two_step_session.validate()
|
173
|
+
try:
|
174
|
+
await two_step_session.check_code(request.form.get("code"))
|
175
|
+
except MaxedOutChallengeError as e:
|
176
|
+
logger.warning(
|
177
|
+
f"Client {get_ip(request)} has exceeded maximum two-step session {two_step_session.id} challenge attempts "
|
178
|
+
"during account verification."
|
179
|
+
)
|
180
|
+
raise e
|
181
|
+
two_step_session.bearer.verified = True
|
182
|
+
await two_step_session.bearer.save(update_fields=["verified"])
|
183
|
+
logger.info(
|
184
|
+
f"Client {get_ip(request)} has verified account {two_step_session.bearer.id}."
|
185
|
+
)
|
186
|
+
return two_step_session
|
187
|
+
|
188
|
+
|
189
|
+
async def captcha(request: Request) -> CaptchaSession:
|
190
|
+
"""
|
191
|
+
Validates a captcha challenge attempt.
|
192
|
+
|
193
|
+
Args:
|
194
|
+
request (Request): Sanic request parameter. Request body should contain form-data with the following argument(s): captcha.
|
195
|
+
|
196
|
+
Raises:
|
197
|
+
DeletedError
|
198
|
+
ExpiredError
|
199
|
+
DeactivatedError
|
200
|
+
JWTDecodeError
|
201
|
+
NotFoundError
|
202
|
+
ChallengeError
|
203
|
+
MaxedOutChallengeError
|
204
|
+
|
205
|
+
Returns:
|
206
|
+
captcha_session
|
207
|
+
"""
|
208
|
+
captcha_session = await CaptchaSession.decode(request)
|
209
|
+
captcha_session.validate()
|
210
|
+
try:
|
211
|
+
await captcha_session.check_code(request.form.get("captcha"))
|
212
|
+
except MaxedOutChallengeError as e:
|
213
|
+
logger.warning(
|
214
|
+
f"Client {get_ip(request)} has exceeded maximum captcha session {captcha_session.id} challenge attempts."
|
215
|
+
)
|
216
|
+
raise e
|
217
|
+
logger.info(
|
218
|
+
f"Client {get_ip(request)} has completed captcha session {captcha_session.id} challenge."
|
219
|
+
)
|
220
|
+
return captcha_session
|
221
|
+
|
222
|
+
|
223
|
+
def requires_captcha(arg=None):
|
224
|
+
"""
|
225
|
+
Validates a captcha challenge attempt.
|
226
|
+
|
227
|
+
Example:
|
228
|
+
This method is not called directly and instead used as a decorator:
|
229
|
+
|
230
|
+
@app.post("api/captcha/attempt")
|
231
|
+
@requires_captcha
|
232
|
+
async def on_captcha_attempt(request):
|
233
|
+
return json("Captcha attempt successful!", captcha_session.json())
|
234
|
+
|
235
|
+
Raises:
|
236
|
+
DeletedError
|
237
|
+
ExpiredError
|
238
|
+
DeactivatedError
|
239
|
+
JWTDecodeError
|
240
|
+
NotFoundError
|
241
|
+
ChallengeError
|
242
|
+
MaxedOutChallengeError
|
243
|
+
"""
|
244
|
+
|
245
|
+
def decorator(func):
|
246
|
+
@functools.wraps(func)
|
247
|
+
async def wrapper(request, *args, **kwargs):
|
248
|
+
await captcha(request)
|
249
|
+
return await func(request, *args, **kwargs)
|
250
|
+
|
251
|
+
return wrapper
|
252
|
+
|
253
|
+
return decorator(arg) if callable(arg) else decorator
|