-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix messy setting (Operation Clean Config Project) (#477)
* Fix settings pt 1 * Fix messy settings pt 2 * fix messy setting pt 3 * Fix messy setting pt 4 * fix tests.utils.override_api_settings and tests.test_api.APIBasicTests.test_blacklisting * fix tests.test_api.APIBasicTests.test_misconfigured_token_model * fix tests.test_api.APIBasicTests.test_registration * fix tests.test_api.APIBasicTests.test_registration_allowed_with_custom_no_password_serializer * fix tests.test_social.TestSocialConnectAuth.test_social_connect * fix tests.test_api.APIBasicTests.test_rotate_token_refresh_view * delete utils.import_callable * remove useless code in utils * added new test tests.test_utils.TestFormatLazy.test_it_should_work * fix tests.utils.override_api_settings * added `'JWT_AUTH_HTTPONLY': False,` to demo project settings * fix documentation * remove `reload_api_settings`
- Loading branch information
Showing
23 changed files
with
595 additions
and
353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,84 @@ | ||
from django.conf import settings | ||
from django.utils.translation import gettext_lazy as _ | ||
from rest_framework.settings import APISettings as _APISettings | ||
|
||
from dj_rest_auth.serializers import JWTSerializer as DefaultJWTSerializer | ||
from dj_rest_auth.serializers import ( | ||
JWTSerializerWithExpiration as DefaultJWTSerializerWithExpiration, | ||
) | ||
from dj_rest_auth.serializers import LoginSerializer as DefaultLoginSerializer | ||
from dj_rest_auth.serializers import ( | ||
PasswordChangeSerializer as DefaultPasswordChangeSerializer, | ||
) | ||
from dj_rest_auth.serializers import ( | ||
PasswordResetConfirmSerializer as DefaultPasswordResetConfirmSerializer, | ||
) | ||
from dj_rest_auth.serializers import ( | ||
PasswordResetSerializer as DefaultPasswordResetSerializer, | ||
) | ||
from dj_rest_auth.serializers import TokenSerializer as DefaultTokenSerializer | ||
from dj_rest_auth.serializers import ( | ||
UserDetailsSerializer as DefaultUserDetailsSerializer, | ||
) | ||
|
||
from .utils import default_create_token, import_callable | ||
USER_SETTINGS = getattr(settings, "REST_AUTH", None) | ||
|
||
DEFAULTS = { | ||
'LOGIN_SERIALIZER': 'dj_rest_auth.serializers.LoginSerializer', | ||
'TOKEN_SERIALIZER': 'dj_rest_auth.serializers.TokenSerializer', | ||
'JWT_SERIALIZER': 'dj_rest_auth.serializers.JWTSerializer', | ||
'JWT_SERIALIZER_WITH_EXPIRATION': 'dj_rest_auth.serializers.JWTSerializerWithExpiration', | ||
'JWT_TOKEN_CLAIMS_SERIALIZER': 'rest_framework_simplejwt.serializers.TokenObtainPairSerializer', | ||
'USER_DETAILS_SERIALIZER': 'dj_rest_auth.serializers.UserDetailsSerializer', | ||
'PASSWORD_RESET_SERIALIZER': 'dj_rest_auth.serializers.PasswordResetSerializer', | ||
'PASSWORD_RESET_CONFIRM_SERIALIZER': 'dj_rest_auth.serializers.PasswordResetConfirmSerializer', | ||
'PASSWORD_CHANGE_SERIALIZER': 'dj_rest_auth.serializers.PasswordChangeSerializer', | ||
|
||
create_token = import_callable(getattr(settings, 'REST_AUTH_TOKEN_CREATOR', default_create_token)) | ||
'REGISTER_SERIALIZER': 'dj_rest_auth.registration.serializers.RegisterSerializer', | ||
|
||
serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {}) | ||
'REGISTER_PERMISSION_CLASSES': ('rest_framework.permissions.AllowAny',), | ||
|
||
TokenSerializer = import_callable(serializers.get('TOKEN_SERIALIZER', DefaultTokenSerializer)) | ||
'TOKEN_MODEL': 'rest_framework.authtoken.models.Token', | ||
'TOKEN_CREATOR': 'dj_rest_auth.utils.default_create_token', | ||
|
||
JWTSerializer = import_callable(serializers.get('JWT_SERIALIZER', DefaultJWTSerializer)) | ||
'PASSWORD_RESET_USE_SITES_DOMAIN': False, | ||
'OLD_PASSWORD_FIELD_ENABLED': False, | ||
'LOGOUT_ON_PASSWORD_CHANGE': False, | ||
'SESSION_LOGIN': True, | ||
'USE_JWT': False, | ||
|
||
JWTSerializerWithExpiration = import_callable(serializers.get('JWT_SERIALIZER_WITH_EXPIRATION', DefaultJWTSerializerWithExpiration)) | ||
'JWT_AUTH_COOKIE': None, | ||
'JWT_AUTH_REFRESH_COOKIE': None, | ||
'JWT_AUTH_REFRESH_COOKIE_PATH': '/', | ||
'JWT_AUTH_SECURE': False, | ||
'JWT_AUTH_HTTPONLY': True, | ||
'JWT_AUTH_SAMESITE': 'Lax', | ||
'JWT_AUTH_RETURN_EXPIRATION': False, | ||
'JWT_AUTH_COOKIE_USE_CSRF': False, | ||
'JWT_AUTH_COOKIE_ENFORCE_CSRF_ON_UNAUTHENTICATED': False, | ||
} | ||
|
||
UserDetailsSerializer = import_callable(serializers.get('USER_DETAILS_SERIALIZER', DefaultUserDetailsSerializer)) | ||
# List of settings that may be in string import notation. | ||
IMPORT_STRINGS = ( | ||
'TOKEN_CREATOR', | ||
'TOKEN_MODEL', | ||
'TOKEN_SERIALIZER', | ||
'JWT_SERIALIZER', | ||
'JWT_SERIALIZER_WITH_EXPIRATION', | ||
'JWT_TOKEN_CLAIMS_SERIALIZER', | ||
'USER_DETAILS_SERIALIZER', | ||
'LOGIN_SERIALIZER', | ||
'PASSWORD_RESET_SERIALIZER', | ||
'PASSWORD_RESET_CONFIRM_SERIALIZER', | ||
'PASSWORD_CHANGE_SERIALIZER', | ||
'REGISTER_SERIALIZER', | ||
'REGISTER_PERMISSION_CLASSES', | ||
) | ||
|
||
LoginSerializer = import_callable(serializers.get('LOGIN_SERIALIZER', DefaultLoginSerializer)) | ||
# List of settings that have been removed | ||
REMOVED_SETTINGS = ( ) | ||
|
||
PasswordResetSerializer = import_callable( | ||
serializers.get( | ||
'PASSWORD_RESET_SERIALIZER', DefaultPasswordResetSerializer, | ||
), | ||
) | ||
|
||
PasswordResetConfirmSerializer = import_callable( | ||
serializers.get( | ||
'PASSWORD_RESET_CONFIRM_SERIALIZER', DefaultPasswordResetConfirmSerializer, | ||
), | ||
) | ||
class APISettings(_APISettings): # pragma: no cover | ||
def __check_user_settings(self, user_settings): | ||
from .utils import format_lazy | ||
SETTINGS_DOC = 'https://dj-rest-auth.readthedocs.io/en/latest/configuration.html' | ||
|
||
for setting in REMOVED_SETTINGS: | ||
if setting in user_settings: | ||
raise RuntimeError( | ||
format_lazy( | ||
_( | ||
"The '{}' setting has been removed. Please refer to '{}' for available settings." | ||
), | ||
setting, | ||
SETTINGS_DOC, | ||
) | ||
) | ||
|
||
return user_settings | ||
|
||
PasswordChangeSerializer = import_callable( | ||
serializers.get('PASSWORD_CHANGE_SERIALIZER', DefaultPasswordChangeSerializer), | ||
) | ||
|
||
JWT_AUTH_COOKIE = getattr(settings, 'JWT_AUTH_COOKIE', None) | ||
JWT_AUTH_REFRESH_COOKIE = getattr(settings, 'JWT_AUTH_REFRESH_COOKIE', None) | ||
api_settings = APISettings(USER_SETTINGS, DEFAULTS, IMPORT_STRINGS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.utils.module_loading import import_string | ||
from rest_framework.authtoken.models import Token as DefaultTokenModel | ||
|
||
from .app_settings import api_settings | ||
|
||
def get_token_model(): | ||
default_model = 'rest_framework.authtoken.models.Token' | ||
import_path = getattr(settings, 'REST_AUTH_TOKEN_MODEL', default_model) | ||
session_login = getattr(settings, 'REST_SESSION_LOGIN', True) | ||
use_jwt = getattr(settings, 'REST_USE_JWT', False) | ||
|
||
if not any((session_login, import_path, use_jwt)): | ||
token_model = api_settings.TOKEN_MODEL | ||
session_login = api_settings.SESSION_LOGIN | ||
use_jwt = api_settings.USE_JWT | ||
|
||
if not any((session_login, token_model, use_jwt)): | ||
raise ImproperlyConfigured( | ||
'No authentication is configured for rest auth. You must enable one or ' | ||
'more of `REST_AUTH_TOKEN_MODEL`, `REST_USE_JWT` or `REST_SESSION_LOGIN`' | ||
) | ||
'more of `TOKEN_MODEL`, `USE_JWT` or `SESSION_LOGIN`' | ||
) | ||
if ( | ||
import_path == default_model | ||
token_model == DefaultTokenModel | ||
and 'rest_framework.authtoken' not in settings.INSTALLED_APPS | ||
): | ||
raise ImproperlyConfigured( | ||
'You must include `rest_framework.authtoken` in INSTALLED_APPS ' | ||
'or set REST_AUTH_TOKEN_MODEL to None' | ||
'or set TOKEN_MODEL to None' | ||
) | ||
return import_string(import_path) if import_path else None | ||
return token_model | ||
|
||
TokenModel = get_token_model() |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.