Django UnicodeDecodeError at /admin/login/

240 views
Skip to first unread message

Supermario

unread,
Jan 27, 2015, 12:53:56 PM1/27/15
to django...@googlegroups.com
I try to implement this hasher in order to be able to authenticate user who are imported from drupal 7. 

So I put the hasher in the settings.py as follows:


PASSWORD_HASHERS = (
   
   
'myproj.drupal_hasher.DrupalPasswordHasher', #drupal hasher
     
   
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
   
   
'django.contrib.auth.hashers.BCryptPasswordHasher',
   
'django.contrib.auth.hashers.SHA1PasswordHasher',
   
'django.contrib.auth.hashers.MD5PasswordHasher',
   
'django.contrib.auth.hashers.CryptPasswordHasher',
     
)



However it does not authenticate imported users from drupal and for users who are created in Django I get:

    'ascii' codec can't decode byte 0xcf in position 0: ordinal not in range(128)




Here is the trackback:


   
Environment:
   
   
   
Request Method: POST
   
Request URL: http://127.0.0.1:8000/auth/
   
   
Django Version: 1.7.3
   
Python Version: 2.7.3
   
Installed Applications:
   
('django.contrib.admin',
     
'django.contrib.auth',
     
'django.contrib.contenttypes',
     
'django.contrib.sessions',
     
'django.contrib.messages',
     
'django.contrib.staticfiles',
     
'article',
     
'photo',
     
'debug_toolbar',
     
'django_markdown',
     
'haystack')
   
Installed Middleware:
   
(u'debug_toolbar.middleware.DebugToolbarMiddleware',
     
'django.contrib.sessions.middleware.SessionMiddleware',
     
'django.middleware.common.CommonMiddleware',
     
'django.middleware.csrf.CsrfViewMiddleware',
     
'django.contrib.auth.middleware.AuthenticationMiddleware',
     
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
     
'django.contrib.messages.middleware.MessageMiddleware',
     
'django.middleware.clickjacking.XFrameOptionsMiddleware')
   
   
   
Traceback:
   
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
     
111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
   
File "/home/mario/myproj/myauth/views.py" in auth_view
     
16.     user = auth.authenticate(username=username, password=password)
   
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in authenticate
     
60.             user = backend.authenticate(**credentials)
   
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/backends.py" in authenticate
     
17.             if user.check_password(password):
   
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in check_password
     
237.         return check_password(raw_password, self.password, setter)
   
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/hashers.py" in check_password
     
63.         setter(password)
   
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in setter
     
235.             self.set_password(raw_password)
   
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in set_password
     
227.         self.password = make_password(raw_password)
   
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/hashers.py" in make_password
     
85.     return hasher.encode(password, salt)
   
File "/home/mario/myproj/myproj/drupal_hasher.py" in encode
     
81.         encoded_hash = self._apply_hash(password, self._digests[digest], settings)
   
File "/home/mario/myproj/myproj/drupal_hasher.py" in _apply_hash
     
68.             password_hash = digest(password_hash + password).digest()
   
   
Exception Type: UnicodeDecodeError at /auth/
   
Exception Value: 'ascii' codec can't decode byte 0xcf in position 0: ordinal not in range(128)


All source and destination database tables are converted to utf8_unicode_ci.

I am stock on this problem for days so really appreciate your help. 



James Schneider

unread,
Jan 27, 2015, 3:04:31 PM1/27/15
to django...@googlegroups.com

Did you read the comments on that snippet? It appears there may need to be some extra encoding/decoding needed (example additional code is listed there), which would make sense given the decoding errors you are seeing.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5dd15420-01d5-4b1f-adf0-af3ee585ba73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Human Bsd

unread,
Jan 27, 2015, 9:24:51 PM1/27/15
to django...@googlegroups.com
Hi James. your hint  was actually very inspiring.

After concatenating  'drupal$' to the imported password, I was still unable to authenticate. Then I realized that the passwords are in unicode but the hasher considers them to be ascii, so I added :

password = password.encode('ascii','ignore')

at the begining of the both 'encode' and 'verify' methods 

This solved the problem and saved my escape rout out of drupal :)

Thank you very much for your hint.  

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/SsTGFmnlNgM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages