sanic-security 1.16.12__py3-none-any.whl → 1.17.1__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.1.dist-info}/METADATA +672 -672
- sanic_security-1.17.1.dist-info/RECORD +17 -0
- {sanic_security-1.16.12.dist-info → sanic_security-1.17.1.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.1.dist-info}/WHEEL +0 -0
- {sanic_security-1.16.12.dist-info → sanic_security-1.17.1.dist-info}/top_level.txt +0 -0
sanic_security/configuration.py
CHANGED
@@ -1,125 +1,125 @@
|
|
1
|
-
from os import environ
|
2
|
-
from types import SimpleNamespace
|
3
|
-
|
4
|
-
from sanic.utils import str_to_bool
|
5
|
-
|
6
|
-
"""
|
7
|
-
Copyright (c) 2020-present Nicholas Aidan Stewart
|
8
|
-
|
9
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
10
|
-
of this software and associated documentation files (the "Software"), to deal
|
11
|
-
in the Software without restriction, including without limitation the rights
|
12
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
13
|
-
copies of the Software, and to permit persons to whom the Software is
|
14
|
-
furnished to do so, subject to the following conditions:
|
15
|
-
|
16
|
-
The above copyright notice and this permission notice shall be included in all
|
17
|
-
copies or substantial portions of the Software.
|
18
|
-
|
19
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
21
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
22
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
23
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
24
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
25
|
-
SOFTWARE.
|
26
|
-
"""
|
27
|
-
|
28
|
-
DEFAULT_CONFIG = {
|
29
|
-
"SECRET": "This is a big secret. Shhhhh",
|
30
|
-
"PUBLIC_SECRET": None,
|
31
|
-
"OAUTH_CLIENT": None,
|
32
|
-
"OAUTH_SECRET": None,
|
33
|
-
"OAUTH_REDIRECT": None,
|
34
|
-
"SESSION_SAMESITE": "Strict",
|
35
|
-
"SESSION_SECURE": True,
|
36
|
-
"SESSION_HTTPONLY": True,
|
37
|
-
"SESSION_DOMAIN": None,
|
38
|
-
"SESSION_PREFIX": "tkn",
|
39
|
-
"SESSION_ENCODING_ALGORITHM": "HS256",
|
40
|
-
"MAX_CHALLENGE_ATTEMPTS": 3,
|
41
|
-
"CAPTCHA_SESSION_EXPIRATION": 180,
|
42
|
-
"CAPTCHA_FONT": "captcha-font.ttf",
|
43
|
-
"CAPTCHA_VOICE": "captcha-voice/",
|
44
|
-
"TWO_STEP_SESSION_EXPIRATION": 300,
|
45
|
-
"AUTHENTICATION_SESSION_EXPIRATION": 86400,
|
46
|
-
"AUTHENTICATION_REFRESH_EXPIRATION": 604800,
|
47
|
-
"ALLOW_LOGIN_WITH_USERNAME": False,
|
48
|
-
"INITIAL_ADMIN_EMAIL": "admin@example.com",
|
49
|
-
"INITIAL_ADMIN_PASSWORD": "admin123",
|
50
|
-
"TEST_DATABASE_URL": "sqlite
|
51
|
-
}
|
52
|
-
|
53
|
-
|
54
|
-
class Config(SimpleNamespace):
|
55
|
-
"""
|
56
|
-
Sanic Security configuration.
|
57
|
-
|
58
|
-
Attributes:
|
59
|
-
SECRET (str): The secret used by the hashing algorithm for generating and signing JWTs. This should be a string unique to your application. Keep it safe.
|
60
|
-
PUBLIC_SECRET (str): The secret used for verifying and decoding JWTs and can be publicly shared. This should be a string unique to your application.
|
61
|
-
OAUTH_CLIENT (str): The client ID provided by the OAuth provider, this is used to identify the application making the OAuth request.
|
62
|
-
OAUTH_SECRET (str): The client secret provided by the OAuth provider, this is used in conjunction with the client ID to authenticate the application.
|
63
|
-
OAUTH_REDIRECT (str): The redirect URI registered with the OAuth provider, This is the URI where the user will be redirected after a successful authentication.
|
64
|
-
SESSION_SAMESITE (str): The SameSite attribute of session cookies.
|
65
|
-
SESSION_SECURE (bool): The Secure attribute of session cookies.
|
66
|
-
SESSION_HTTPONLY (bool): The HttpOnly attribute of session cookies. HIGHLY recommended that you do not turn this off, unless you know what you are doing.
|
67
|
-
SESSION_DOMAIN (bool): The Domain attribute of session cookies.
|
68
|
-
SESSION_ENCODING_ALGORITHM (str): The algorithm used to encode sessions to a JWT.
|
69
|
-
SESSION_PREFIX (str): Prefix attached to the beginning of session cookies.
|
70
|
-
MAX_CHALLENGE_ATTEMPTS (str): The maximum amount of session challenge attempts allowed.
|
71
|
-
CAPTCHA_SESSION_EXPIRATION (int): The amount of seconds till captcha session expiration on creation. Setting to 0 will disable expiration.
|
72
|
-
CAPTCHA_FONT (str): The file path to the font being used for captcha generation.
|
73
|
-
CAPTCHA_VOICE (str): The directory of the voice library being used for audio captcha generation.
|
74
|
-
TWO_STEP_SESSION_EXPIRATION (int): The amount of seconds till two-step session expiration on creation. Setting to 0 will disable expiration.
|
75
|
-
AUTHENTICATION_SESSION_EXPIRATION (int): The amount of seconds till authentication session expiration on creation. Setting to 0 will disable expiration.
|
76
|
-
AUTHENTICATION_REFRESH_EXPIRATION (int): The amount of seconds till authentication session refresh expiration. Setting to 0 will disable refresh mechanism.
|
77
|
-
ALLOW_LOGIN_WITH_USERNAME (bool): Allows login via username and email.
|
78
|
-
INITIAL_ADMIN_EMAIL (str): Email used when creating the initial admin account.
|
79
|
-
INITIAL_ADMIN_PASSWORD (str): Password used when creating the initial admin account.
|
80
|
-
TEST_DATABASE_URL (str): Database URL for connecting to the database Sanic Security will use for testing
|
81
|
-
"""
|
82
|
-
|
83
|
-
SECRET: str
|
84
|
-
PUBLIC_SECRET: str
|
85
|
-
OAUTH_CLIENT: str
|
86
|
-
OAUTH_SECRET: str
|
87
|
-
OAUTH_REDIRECT: str
|
88
|
-
SESSION_SAMESITE: str
|
89
|
-
SESSION_SECURE: bool
|
90
|
-
SESSION_HTTPONLY: bool
|
91
|
-
SESSION_DOMAIN: str
|
92
|
-
SESSION_ENCODING_ALGORITHM: str
|
93
|
-
SESSION_PREFIX: str
|
94
|
-
MAX_CHALLENGE_ATTEMPTS: int
|
95
|
-
CAPTCHA_SESSION_EXPIRATION: int
|
96
|
-
CAPTCHA_FONT: str
|
97
|
-
CAPTCHA_VOICE: str
|
98
|
-
TWO_STEP_SESSION_EXPIRATION: int
|
99
|
-
AUTHENTICATION_SESSION_EXPIRATION: int
|
100
|
-
AUTHENTICATION_REFRESH_EXPIRATION: int
|
101
|
-
ALLOW_LOGIN_WITH_USERNAME: bool
|
102
|
-
INITIAL_ADMIN_EMAIL: str
|
103
|
-
INITIAL_ADMIN_PASSWORD: str
|
104
|
-
TEST_DATABASE_URL: str
|
105
|
-
|
106
|
-
def __init__(self, default_config: dict = None):
|
107
|
-
super().__init__(**(default_config or DEFAULT_CONFIG))
|
108
|
-
self.load_environment_variables()
|
109
|
-
|
110
|
-
def load_environment_variables(self, env_prefix: str = "SANIC_SECURITY_"):
|
111
|
-
for key, value in environ.items():
|
112
|
-
if not key.startswith(env_prefix):
|
113
|
-
continue
|
114
|
-
|
115
|
-
_, config_key = key.split(env_prefix, 1)
|
116
|
-
|
117
|
-
for converter in (int, float, str_to_bool, str):
|
118
|
-
try:
|
119
|
-
setattr(self, config_key, converter(value))
|
120
|
-
break
|
121
|
-
except ValueError:
|
122
|
-
pass
|
123
|
-
|
124
|
-
|
125
|
-
config = Config(DEFAULT_CONFIG)
|
1
|
+
from os import environ
|
2
|
+
from types import SimpleNamespace
|
3
|
+
|
4
|
+
from sanic.utils import str_to_bool
|
5
|
+
|
6
|
+
"""
|
7
|
+
Copyright (c) 2020-present Nicholas Aidan Stewart
|
8
|
+
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
11
|
+
in the Software without restriction, including without limitation the rights
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
14
|
+
furnished to do so, subject to the following conditions:
|
15
|
+
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
17
|
+
copies or substantial portions of the Software.
|
18
|
+
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
25
|
+
SOFTWARE.
|
26
|
+
"""
|
27
|
+
|
28
|
+
DEFAULT_CONFIG = {
|
29
|
+
"SECRET": "This is a big secret. Shhhhh",
|
30
|
+
"PUBLIC_SECRET": None,
|
31
|
+
"OAUTH_CLIENT": None,
|
32
|
+
"OAUTH_SECRET": None,
|
33
|
+
"OAUTH_REDIRECT": None,
|
34
|
+
"SESSION_SAMESITE": "Strict",
|
35
|
+
"SESSION_SECURE": True,
|
36
|
+
"SESSION_HTTPONLY": True,
|
37
|
+
"SESSION_DOMAIN": None,
|
38
|
+
"SESSION_PREFIX": "tkn",
|
39
|
+
"SESSION_ENCODING_ALGORITHM": "HS256",
|
40
|
+
"MAX_CHALLENGE_ATTEMPTS": 3,
|
41
|
+
"CAPTCHA_SESSION_EXPIRATION": 180,
|
42
|
+
"CAPTCHA_FONT": "captcha-font.ttf",
|
43
|
+
"CAPTCHA_VOICE": "captcha-voice/",
|
44
|
+
"TWO_STEP_SESSION_EXPIRATION": 300,
|
45
|
+
"AUTHENTICATION_SESSION_EXPIRATION": 86400,
|
46
|
+
"AUTHENTICATION_REFRESH_EXPIRATION": 604800,
|
47
|
+
"ALLOW_LOGIN_WITH_USERNAME": False,
|
48
|
+
"INITIAL_ADMIN_EMAIL": "admin@example.com",
|
49
|
+
"INITIAL_ADMIN_PASSWORD": "admin123",
|
50
|
+
"TEST_DATABASE_URL": "sqlite://db.sqlite3",
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
class Config(SimpleNamespace):
|
55
|
+
"""
|
56
|
+
Sanic Security configuration.
|
57
|
+
|
58
|
+
Attributes:
|
59
|
+
SECRET (str): The secret used by the hashing algorithm for generating and signing JWTs. This should be a string unique to your application. Keep it safe.
|
60
|
+
PUBLIC_SECRET (str): The secret used for verifying and decoding JWTs and can be publicly shared. This should be a string unique to your application.
|
61
|
+
OAUTH_CLIENT (str): The client ID provided by the OAuth provider, this is used to identify the application making the OAuth request.
|
62
|
+
OAUTH_SECRET (str): The client secret provided by the OAuth provider, this is used in conjunction with the client ID to authenticate the application.
|
63
|
+
OAUTH_REDIRECT (str): The redirect URI registered with the OAuth provider, This is the URI where the user will be redirected after a successful authentication.
|
64
|
+
SESSION_SAMESITE (str): The SameSite attribute of session cookies.
|
65
|
+
SESSION_SECURE (bool): The Secure attribute of session cookies.
|
66
|
+
SESSION_HTTPONLY (bool): The HttpOnly attribute of session cookies. HIGHLY recommended that you do not turn this off, unless you know what you are doing.
|
67
|
+
SESSION_DOMAIN (bool): The Domain attribute of session cookies.
|
68
|
+
SESSION_ENCODING_ALGORITHM (str): The algorithm used to encode sessions to a JWT.
|
69
|
+
SESSION_PREFIX (str): Prefix attached to the beginning of session cookies.
|
70
|
+
MAX_CHALLENGE_ATTEMPTS (str): The maximum amount of session challenge attempts allowed.
|
71
|
+
CAPTCHA_SESSION_EXPIRATION (int): The amount of seconds till captcha session expiration on creation. Setting to 0 will disable expiration.
|
72
|
+
CAPTCHA_FONT (str): The file path to the font being used for captcha generation.
|
73
|
+
CAPTCHA_VOICE (str): The directory of the voice library being used for audio captcha generation.
|
74
|
+
TWO_STEP_SESSION_EXPIRATION (int): The amount of seconds till two-step session expiration on creation. Setting to 0 will disable expiration.
|
75
|
+
AUTHENTICATION_SESSION_EXPIRATION (int): The amount of seconds till authentication session expiration on creation. Setting to 0 will disable expiration.
|
76
|
+
AUTHENTICATION_REFRESH_EXPIRATION (int): The amount of seconds till authentication session refresh expiration. Setting to 0 will disable refresh mechanism.
|
77
|
+
ALLOW_LOGIN_WITH_USERNAME (bool): Allows login via username and email.
|
78
|
+
INITIAL_ADMIN_EMAIL (str): Email used when creating the initial admin account.
|
79
|
+
INITIAL_ADMIN_PASSWORD (str): Password used when creating the initial admin account.
|
80
|
+
TEST_DATABASE_URL (str): Database URL for connecting to the database Sanic Security will use for testing
|
81
|
+
"""
|
82
|
+
|
83
|
+
SECRET: str
|
84
|
+
PUBLIC_SECRET: str
|
85
|
+
OAUTH_CLIENT: str
|
86
|
+
OAUTH_SECRET: str
|
87
|
+
OAUTH_REDIRECT: str
|
88
|
+
SESSION_SAMESITE: str
|
89
|
+
SESSION_SECURE: bool
|
90
|
+
SESSION_HTTPONLY: bool
|
91
|
+
SESSION_DOMAIN: str
|
92
|
+
SESSION_ENCODING_ALGORITHM: str
|
93
|
+
SESSION_PREFIX: str
|
94
|
+
MAX_CHALLENGE_ATTEMPTS: int
|
95
|
+
CAPTCHA_SESSION_EXPIRATION: int
|
96
|
+
CAPTCHA_FONT: str
|
97
|
+
CAPTCHA_VOICE: str
|
98
|
+
TWO_STEP_SESSION_EXPIRATION: int
|
99
|
+
AUTHENTICATION_SESSION_EXPIRATION: int
|
100
|
+
AUTHENTICATION_REFRESH_EXPIRATION: int
|
101
|
+
ALLOW_LOGIN_WITH_USERNAME: bool
|
102
|
+
INITIAL_ADMIN_EMAIL: str
|
103
|
+
INITIAL_ADMIN_PASSWORD: str
|
104
|
+
TEST_DATABASE_URL: str
|
105
|
+
|
106
|
+
def __init__(self, default_config: dict = None):
|
107
|
+
super().__init__(**(default_config or DEFAULT_CONFIG))
|
108
|
+
self.load_environment_variables()
|
109
|
+
|
110
|
+
def load_environment_variables(self, env_prefix: str = "SANIC_SECURITY_"):
|
111
|
+
for key, value in environ.items():
|
112
|
+
if not key.startswith(env_prefix):
|
113
|
+
continue
|
114
|
+
|
115
|
+
_, config_key = key.split(env_prefix, 1)
|
116
|
+
|
117
|
+
for converter in (int, float, str_to_bool, str):
|
118
|
+
try:
|
119
|
+
setattr(self, config_key, converter(value))
|
120
|
+
break
|
121
|
+
except ValueError:
|
122
|
+
pass
|
123
|
+
|
124
|
+
|
125
|
+
config = Config(DEFAULT_CONFIG)
|
sanic_security/exceptions.py
CHANGED
@@ -1,164 +1,164 @@
|
|
1
|
-
from sanic.exceptions import SanicException
|
2
|
-
|
3
|
-
from sanic_security.utils import json
|
4
|
-
|
5
|
-
"""
|
6
|
-
Copyright (c) 2020-present Nicholas Aidan Stewart
|
7
|
-
|
8
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
-
of this software and associated documentation files (the "Software"), to deal
|
10
|
-
in the Software without restriction, including without limitation the rights
|
11
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
-
copies of the Software, and to permit persons to whom the Software is
|
13
|
-
furnished to do so, subject to the following conditions:
|
14
|
-
|
15
|
-
The above copyright notice and this permission notice shall be included in all
|
16
|
-
copies or substantial portions of the Software.
|
17
|
-
|
18
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
-
SOFTWARE.
|
25
|
-
"""
|
26
|
-
|
27
|
-
|
28
|
-
class SecurityError(SanicException):
|
29
|
-
"""
|
30
|
-
Sanic Security related error.
|
31
|
-
|
32
|
-
Attributes:
|
33
|
-
json (HTTPResponse): Security error json response.
|
34
|
-
|
35
|
-
Args:
|
36
|
-
message (str): Human readable error message.
|
37
|
-
code (int): HTTP error code.
|
38
|
-
"""
|
39
|
-
|
40
|
-
def __init__(self, message: str, code: int):
|
41
|
-
self.json = json(message, self.__class__.__name__, code)
|
42
|
-
super().__init__(message, code)
|
43
|
-
|
44
|
-
|
45
|
-
class NotFoundError(SecurityError):
|
46
|
-
"""Raised when a resource cannot be found on the database."""
|
47
|
-
|
48
|
-
def __init__(self, message):
|
49
|
-
super().__init__(message, 404)
|
50
|
-
|
51
|
-
|
52
|
-
class DeletedError(SecurityError):
|
53
|
-
"""Raised when attempting to access a resource marked as deleted."""
|
54
|
-
|
55
|
-
def __init__(self, message):
|
56
|
-
super().__init__(message, 404)
|
57
|
-
|
58
|
-
|
59
|
-
class CredentialsError(SecurityError):
|
60
|
-
"""Raised when credentials are invalid."""
|
61
|
-
|
62
|
-
def __init__(self, message, code=400):
|
63
|
-
super().__init__(message, code)
|
64
|
-
|
65
|
-
|
66
|
-
class OAuthError(SecurityError):
|
67
|
-
"""Raised when an error occurs during OAuth flow."""
|
68
|
-
|
69
|
-
def __init__(self, message, code=401):
|
70
|
-
super().__init__(message, code)
|
71
|
-
|
72
|
-
|
73
|
-
class AccountError(SecurityError):
|
74
|
-
"""Base account error that all other account errors derive from."""
|
75
|
-
|
76
|
-
def __init__(self, message, code):
|
77
|
-
super().__init__(message, code)
|
78
|
-
|
79
|
-
|
80
|
-
class DisabledError(AccountError):
|
81
|
-
"""Raised when account is disabled."""
|
82
|
-
|
83
|
-
def __init__(self, message: str = "Account is disabled.", code: int = 401):
|
84
|
-
super().__init__(message, code)
|
85
|
-
|
86
|
-
|
87
|
-
class UnverifiedError(AccountError):
|
88
|
-
"""Raised when account is unverified."""
|
89
|
-
|
90
|
-
def __init__(self):
|
91
|
-
super().__init__("Account requires verification.", 401)
|
92
|
-
|
93
|
-
|
94
|
-
class SessionError(SecurityError):
|
95
|
-
"""Base session error that all other session errors derive from."""
|
96
|
-
|
97
|
-
def __init__(self, message, code=401):
|
98
|
-
super().__init__(message, code)
|
99
|
-
|
100
|
-
|
101
|
-
class JWTDecodeError(SessionError):
|
102
|
-
"""Raised when client JWT is invalid."""
|
103
|
-
|
104
|
-
def __init__(
|
105
|
-
self, message="Session token invalid, not provided, or expired.", code=401
|
106
|
-
):
|
107
|
-
super().__init__(message, code)
|
108
|
-
|
109
|
-
|
110
|
-
class DeactivatedError(SessionError):
|
111
|
-
"""Raised when session is deactivated."""
|
112
|
-
|
113
|
-
def __init__(
|
114
|
-
self,
|
115
|
-
message: str = "Session has been deactivated.",
|
116
|
-
code: int = 401,
|
117
|
-
):
|
118
|
-
super().__init__(message, code)
|
119
|
-
|
120
|
-
|
121
|
-
class ExpiredError(SessionError):
|
122
|
-
"""Raised when session has expired."""
|
123
|
-
|
124
|
-
def __init__(self, message="Session has expired."):
|
125
|
-
super().__init__(message)
|
126
|
-
|
127
|
-
|
128
|
-
class SecondFactorRequiredError(SessionError):
|
129
|
-
"""Raised when authentication session two-factor requirement isn't met."""
|
130
|
-
|
131
|
-
def __init__(self):
|
132
|
-
super().__init__("Session requires second factor for authentication.")
|
133
|
-
|
134
|
-
|
135
|
-
class ChallengeError(SessionError):
|
136
|
-
"""Raised when a session challenge attempt is invalid."""
|
137
|
-
|
138
|
-
def __init__(self, message):
|
139
|
-
super().__init__(message)
|
140
|
-
|
141
|
-
|
142
|
-
class MaxedOutChallengeError(ChallengeError):
|
143
|
-
"""Raised when a session's challenge attempt limit is reached."""
|
144
|
-
|
145
|
-
def __init__(self):
|
146
|
-
super().__init__("The maximum amount of attempts has been reached.")
|
147
|
-
|
148
|
-
|
149
|
-
class AuthorizationError(SecurityError):
|
150
|
-
"""Raised when an account has insufficient permissions or roles for an action."""
|
151
|
-
|
152
|
-
def __init__(self, message):
|
153
|
-
super().__init__(message, 403)
|
154
|
-
|
155
|
-
|
156
|
-
class AnonymousError(AuthorizationError):
|
157
|
-
"""Raised when attempting to authorize an anonymous user."""
|
158
|
-
|
159
|
-
def __init__(self):
|
160
|
-
super().__init__("Session is anonymous.")
|
161
|
-
|
162
|
-
|
163
|
-
class AuditWarning(Warning):
|
164
|
-
"""Raised when configuration may be dangerous."""
|
1
|
+
from sanic.exceptions import SanicException
|
2
|
+
|
3
|
+
from sanic_security.utils import json
|
4
|
+
|
5
|
+
"""
|
6
|
+
Copyright (c) 2020-present Nicholas Aidan Stewart
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
10
|
+
in the Software without restriction, including without limitation the rights
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
13
|
+
furnished to do so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
16
|
+
copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
+
SOFTWARE.
|
25
|
+
"""
|
26
|
+
|
27
|
+
|
28
|
+
class SecurityError(SanicException):
|
29
|
+
"""
|
30
|
+
Sanic Security related error.
|
31
|
+
|
32
|
+
Attributes:
|
33
|
+
json (HTTPResponse): Security error json response.
|
34
|
+
|
35
|
+
Args:
|
36
|
+
message (str): Human readable error message.
|
37
|
+
code (int): HTTP error code.
|
38
|
+
"""
|
39
|
+
|
40
|
+
def __init__(self, message: str, code: int):
|
41
|
+
self.json = json(message, self.__class__.__name__, code)
|
42
|
+
super().__init__(message, code)
|
43
|
+
|
44
|
+
|
45
|
+
class NotFoundError(SecurityError):
|
46
|
+
"""Raised when a resource cannot be found on the database."""
|
47
|
+
|
48
|
+
def __init__(self, message):
|
49
|
+
super().__init__(message, 404)
|
50
|
+
|
51
|
+
|
52
|
+
class DeletedError(SecurityError):
|
53
|
+
"""Raised when attempting to access a resource marked as deleted."""
|
54
|
+
|
55
|
+
def __init__(self, message):
|
56
|
+
super().__init__(message, 404)
|
57
|
+
|
58
|
+
|
59
|
+
class CredentialsError(SecurityError):
|
60
|
+
"""Raised when credentials are invalid."""
|
61
|
+
|
62
|
+
def __init__(self, message, code=400):
|
63
|
+
super().__init__(message, code)
|
64
|
+
|
65
|
+
|
66
|
+
class OAuthError(SecurityError):
|
67
|
+
"""Raised when an error occurs during OAuth flow."""
|
68
|
+
|
69
|
+
def __init__(self, message, code=401):
|
70
|
+
super().__init__(message, code)
|
71
|
+
|
72
|
+
|
73
|
+
class AccountError(SecurityError):
|
74
|
+
"""Base account error that all other account errors derive from."""
|
75
|
+
|
76
|
+
def __init__(self, message, code):
|
77
|
+
super().__init__(message, code)
|
78
|
+
|
79
|
+
|
80
|
+
class DisabledError(AccountError):
|
81
|
+
"""Raised when account is disabled."""
|
82
|
+
|
83
|
+
def __init__(self, message: str = "Account is disabled.", code: int = 401):
|
84
|
+
super().__init__(message, code)
|
85
|
+
|
86
|
+
|
87
|
+
class UnverifiedError(AccountError):
|
88
|
+
"""Raised when account is unverified."""
|
89
|
+
|
90
|
+
def __init__(self):
|
91
|
+
super().__init__("Account requires verification.", 401)
|
92
|
+
|
93
|
+
|
94
|
+
class SessionError(SecurityError):
|
95
|
+
"""Base session error that all other session errors derive from."""
|
96
|
+
|
97
|
+
def __init__(self, message, code=401):
|
98
|
+
super().__init__(message, code)
|
99
|
+
|
100
|
+
|
101
|
+
class JWTDecodeError(SessionError):
|
102
|
+
"""Raised when client JWT is invalid."""
|
103
|
+
|
104
|
+
def __init__(
|
105
|
+
self, message="Session token invalid, not provided, or expired.", code=401
|
106
|
+
):
|
107
|
+
super().__init__(message, code)
|
108
|
+
|
109
|
+
|
110
|
+
class DeactivatedError(SessionError):
|
111
|
+
"""Raised when session is deactivated."""
|
112
|
+
|
113
|
+
def __init__(
|
114
|
+
self,
|
115
|
+
message: str = "Session has been deactivated.",
|
116
|
+
code: int = 401,
|
117
|
+
):
|
118
|
+
super().__init__(message, code)
|
119
|
+
|
120
|
+
|
121
|
+
class ExpiredError(SessionError):
|
122
|
+
"""Raised when session has expired."""
|
123
|
+
|
124
|
+
def __init__(self, message="Session has expired."):
|
125
|
+
super().__init__(message)
|
126
|
+
|
127
|
+
|
128
|
+
class SecondFactorRequiredError(SessionError):
|
129
|
+
"""Raised when authentication session two-factor requirement isn't met."""
|
130
|
+
|
131
|
+
def __init__(self):
|
132
|
+
super().__init__("Session requires second factor for authentication.")
|
133
|
+
|
134
|
+
|
135
|
+
class ChallengeError(SessionError):
|
136
|
+
"""Raised when a session challenge attempt is invalid."""
|
137
|
+
|
138
|
+
def __init__(self, message):
|
139
|
+
super().__init__(message)
|
140
|
+
|
141
|
+
|
142
|
+
class MaxedOutChallengeError(ChallengeError):
|
143
|
+
"""Raised when a session's challenge attempt limit is reached."""
|
144
|
+
|
145
|
+
def __init__(self):
|
146
|
+
super().__init__("The maximum amount of attempts has been reached.")
|
147
|
+
|
148
|
+
|
149
|
+
class AuthorizationError(SecurityError):
|
150
|
+
"""Raised when an account has insufficient permissions or roles for an action."""
|
151
|
+
|
152
|
+
def __init__(self, message):
|
153
|
+
super().__init__(message, 403)
|
154
|
+
|
155
|
+
|
156
|
+
class AnonymousError(AuthorizationError):
|
157
|
+
"""Raised when attempting to authorize an anonymous user."""
|
158
|
+
|
159
|
+
def __init__(self):
|
160
|
+
super().__init__("Session is anonymous.")
|
161
|
+
|
162
|
+
|
163
|
+
class AuditWarning(Warning):
|
164
|
+
"""Raised when configuration may be dangerous."""
|