Last active
August 29, 2015 14:14
-
-
Save r14c/d64c89442a0bd4a16cc7 to your computer and use it in GitHub Desktop.
Case-insensitive ModelBackend for Django
This file contains hidden or 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
| # -*- coding: utf-8 -*- | |
| """ | |
| Case-Insensitive ModelBackend | |
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| This is free and unencumbered software released into the public domain. | |
| Anyone is free to copy, modify, publish, use, compile, sell, or | |
| distribute this software, either in source code form or as a compiled | |
| binary, for any purpose, commercial or non-commercial, and by any | |
| means. | |
| In jurisdictions that recognize copyright laws, the author or authors | |
| of this software dedicate any and all copyright interest in the | |
| software to the public domain. We make this dedication for the benefit | |
| of the public at large and to the detriment of our heirs and | |
| successors. We intend this dedication to be an overt act of | |
| relinquishment in perpetuity of all present and future rights to this | |
| software under copyright law. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
| IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
| OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
| ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
| OTHER DEALINGS IN THE SOFTWARE. | |
| For more information, please refer to <http://unlicense.org> | |
| """ | |
| from django.contrib.auth.backends import ModelBackend as ModelBackend_ | |
| from django.contrib.auth import get_user_model | |
| User = get_user_model() | |
| class ModelBackend(ModelBackend_): | |
| """ | |
| A bunch of idiots working on `django.contrib.auth` decided it would be a good idea to make | |
| usernames **case-sensitive**. This works around that. | |
| There is an open issue `on the topic`_, I added the following comment (copied here just in case | |
| it gets removed or the issue deleted): | |
| Whose idea was it to make usernames case-sensitive in the first place? I suppose nothing can | |
| be done at this point, but y'all probably should have thought about it a bit more before | |
| committing yourselves to it for so long. *It was a stupid decision.* | |
| Think about a user logging into a Django-powered site, do you think she gives a shit about | |
| which letters she capitalized when she filled out the registration form? I doubt it. Why | |
| even make her remember? Capitalization isn't that important! | |
| .. _on the topic: https://code.djangoproject.com/ticket/2273 | |
| """ | |
| def authenticate(self, **kwargs): | |
| try: | |
| key = '{0}__iexact'.format(User.USERNAME_FIELD) | |
| user = User.objects.get(**{key: kwargs.get(User.USERNAME_FIELD, kwargs.get('username'))}) | |
| if user.check_password(kwargs['password']): | |
| return user | |
| else: | |
| return None | |
| except User.DoesNotExist: | |
| return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment