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/utils.py
CHANGED
@@ -1,121 +1,121 @@
|
|
1
|
-
import datetime
|
2
|
-
import random
|
3
|
-
from string import ascii_uppercase, digits
|
4
|
-
|
5
|
-
from argon2 import PasswordHasher
|
6
|
-
from captcha.audio import AudioCaptcha
|
7
|
-
from captcha.image import ImageCaptcha
|
8
|
-
from sanic.request import Request
|
9
|
-
from sanic.response import json as sanic_json, HTTPResponse
|
10
|
-
from sanic.utils import str_to_bool as sanic_str_to_bool
|
11
|
-
|
12
|
-
from sanic_security.configuration import config
|
13
|
-
|
14
|
-
"""
|
15
|
-
Copyright (c) 2020-Present Nicholas Aidan Stewart
|
16
|
-
|
17
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
18
|
-
of this software and associated documentation files (the "Software"), to deal
|
19
|
-
in the Software without restriction, including without limitation the rights
|
20
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
21
|
-
copies of the Software, and to permit persons to whom the Software is
|
22
|
-
furnished to do so, subject to the following conditions:
|
23
|
-
|
24
|
-
The above copyright notice and this permission notice shall be included in all
|
25
|
-
copies or substantial portions of the Software.
|
26
|
-
|
27
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
28
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
29
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
30
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
31
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
32
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
33
|
-
SOFTWARE.
|
34
|
-
"""
|
35
|
-
|
36
|
-
image_generator: ImageCaptcha = ImageCaptcha(
|
37
|
-
190, 90, fonts=config.CAPTCHA_FONT.replace(" ", "").split(",")
|
38
|
-
)
|
39
|
-
audio_generator: AudioCaptcha = AudioCaptcha(voicedir=config.CAPTCHA_VOICE)
|
40
|
-
password_hasher: PasswordHasher = PasswordHasher()
|
41
|
-
|
42
|
-
|
43
|
-
def get_ip(request: Request) -> str:
|
44
|
-
"""
|
45
|
-
Retrieves ip address from client request.
|
46
|
-
|
47
|
-
Args:
|
48
|
-
request (Request): Sanic request parameter.
|
49
|
-
|
50
|
-
Returns:
|
51
|
-
ip
|
52
|
-
"""
|
53
|
-
return request.remote_addr or request.ip
|
54
|
-
|
55
|
-
|
56
|
-
def get_code(digits_only: bool = False) -> str:
|
57
|
-
"""
|
58
|
-
Generates random code to be used for verification.
|
59
|
-
|
60
|
-
Args:
|
61
|
-
digits_only: Determines if code should only contain digits.
|
62
|
-
|
63
|
-
Returns:
|
64
|
-
code
|
65
|
-
"""
|
66
|
-
return "".join(
|
67
|
-
random.choice(("" if digits_only else ascii_uppercase) + digits)
|
68
|
-
for _ in range(6)
|
69
|
-
)
|
70
|
-
|
71
|
-
|
72
|
-
def is_expired(date):
|
73
|
-
"""
|
74
|
-
Checks if current date has surpassed the date passed into the function.
|
75
|
-
|
76
|
-
Args:
|
77
|
-
date: The date being checked for expiration.
|
78
|
-
|
79
|
-
Returns:
|
80
|
-
is_expired
|
81
|
-
"""
|
82
|
-
return date and datetime.datetime.now(datetime.timezone.utc) >= date
|
83
|
-
|
84
|
-
|
85
|
-
def get_expiration_date(seconds: int) -> datetime.datetime:
|
86
|
-
"""
|
87
|
-
Retrieves the date after which something (such as a session) is no longer valid.
|
88
|
-
|
89
|
-
Args:
|
90
|
-
seconds: Seconds added to current time.
|
91
|
-
|
92
|
-
Returns:
|
93
|
-
expiration_date
|
94
|
-
"""
|
95
|
-
return (
|
96
|
-
datetime.datetime.now(datetime.UTC) + datetime.timedelta(seconds=seconds)
|
97
|
-
if seconds > 0
|
98
|
-
else None
|
99
|
-
)
|
100
|
-
|
101
|
-
|
102
|
-
def str_to_bool(val: str) -> bool:
|
103
|
-
"""Returns false if val is None instead of raising ValueError (Sanic's implementation)."""
|
104
|
-
return sanic_str_to_bool(val) if val else False
|
105
|
-
|
106
|
-
|
107
|
-
def json(message: str, data, status_code: int = 200) -> HTTPResponse:
|
108
|
-
"""
|
109
|
-
A preformatted Sanic json response.
|
110
|
-
|
111
|
-
Args:
|
112
|
-
message (str): Message describing data or relaying human-readable information.
|
113
|
-
data (Any): Raw information to be used by client.
|
114
|
-
status_code (int): HTTP response code.
|
115
|
-
|
116
|
-
Returns:
|
117
|
-
json
|
118
|
-
"""
|
119
|
-
return sanic_json(
|
120
|
-
{"message": message, "code": status_code, "data": data}, status=status_code
|
121
|
-
)
|
1
|
+
import datetime
|
2
|
+
import random
|
3
|
+
from string import ascii_uppercase, digits
|
4
|
+
|
5
|
+
from argon2 import PasswordHasher
|
6
|
+
from captcha.audio import AudioCaptcha
|
7
|
+
from captcha.image import ImageCaptcha
|
8
|
+
from sanic.request import Request
|
9
|
+
from sanic.response import json as sanic_json, HTTPResponse
|
10
|
+
from sanic.utils import str_to_bool as sanic_str_to_bool
|
11
|
+
|
12
|
+
from sanic_security.configuration import config
|
13
|
+
|
14
|
+
"""
|
15
|
+
Copyright (c) 2020-Present Nicholas Aidan Stewart
|
16
|
+
|
17
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
18
|
+
of this software and associated documentation files (the "Software"), to deal
|
19
|
+
in the Software without restriction, including without limitation the rights
|
20
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
21
|
+
copies of the Software, and to permit persons to whom the Software is
|
22
|
+
furnished to do so, subject to the following conditions:
|
23
|
+
|
24
|
+
The above copyright notice and this permission notice shall be included in all
|
25
|
+
copies or substantial portions of the Software.
|
26
|
+
|
27
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
28
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
29
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
30
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
31
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
32
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
33
|
+
SOFTWARE.
|
34
|
+
"""
|
35
|
+
|
36
|
+
image_generator: ImageCaptcha = ImageCaptcha(
|
37
|
+
190, 90, fonts=config.CAPTCHA_FONT.replace(" ", "").split(",")
|
38
|
+
)
|
39
|
+
audio_generator: AudioCaptcha = AudioCaptcha(voicedir=config.CAPTCHA_VOICE)
|
40
|
+
password_hasher: PasswordHasher = PasswordHasher()
|
41
|
+
|
42
|
+
|
43
|
+
def get_ip(request: Request) -> str:
|
44
|
+
"""
|
45
|
+
Retrieves ip address from client request.
|
46
|
+
|
47
|
+
Args:
|
48
|
+
request (Request): Sanic request parameter.
|
49
|
+
|
50
|
+
Returns:
|
51
|
+
ip
|
52
|
+
"""
|
53
|
+
return request.remote_addr or request.ip
|
54
|
+
|
55
|
+
|
56
|
+
def get_code(digits_only: bool = False) -> str:
|
57
|
+
"""
|
58
|
+
Generates random code to be used for verification.
|
59
|
+
|
60
|
+
Args:
|
61
|
+
digits_only: Determines if code should only contain digits.
|
62
|
+
|
63
|
+
Returns:
|
64
|
+
code
|
65
|
+
"""
|
66
|
+
return "".join(
|
67
|
+
random.choice(("" if digits_only else ascii_uppercase) + digits)
|
68
|
+
for _ in range(6)
|
69
|
+
)
|
70
|
+
|
71
|
+
|
72
|
+
def is_expired(date):
|
73
|
+
"""
|
74
|
+
Checks if current date has surpassed the date passed into the function.
|
75
|
+
|
76
|
+
Args:
|
77
|
+
date: The date being checked for expiration.
|
78
|
+
|
79
|
+
Returns:
|
80
|
+
is_expired
|
81
|
+
"""
|
82
|
+
return date and datetime.datetime.now(datetime.timezone.utc) >= date
|
83
|
+
|
84
|
+
|
85
|
+
def get_expiration_date(seconds: int) -> datetime.datetime:
|
86
|
+
"""
|
87
|
+
Retrieves the date after which something (such as a session) is no longer valid.
|
88
|
+
|
89
|
+
Args:
|
90
|
+
seconds: Seconds added to current time.
|
91
|
+
|
92
|
+
Returns:
|
93
|
+
expiration_date
|
94
|
+
"""
|
95
|
+
return (
|
96
|
+
datetime.datetime.now(datetime.UTC) + datetime.timedelta(seconds=seconds)
|
97
|
+
if seconds > 0
|
98
|
+
else None
|
99
|
+
)
|
100
|
+
|
101
|
+
|
102
|
+
def str_to_bool(val: str) -> bool:
|
103
|
+
"""Returns false if val is None instead of raising ValueError (Sanic's implementation)."""
|
104
|
+
return sanic_str_to_bool(val) if val else False
|
105
|
+
|
106
|
+
|
107
|
+
def json(message: str, data, status_code: int = 200) -> HTTPResponse:
|
108
|
+
"""
|
109
|
+
A preformatted Sanic json response.
|
110
|
+
|
111
|
+
Args:
|
112
|
+
message (str): Message describing data or relaying human-readable information.
|
113
|
+
data (Any): Raw information to be used by client.
|
114
|
+
status_code (int): HTTP response code.
|
115
|
+
|
116
|
+
Returns:
|
117
|
+
json
|
118
|
+
"""
|
119
|
+
return sanic_json(
|
120
|
+
{"message": message, "code": status_code, "data": data}, status=status_code
|
121
|
+
)
|