I want to use CAS, Central Authentication Service as only login method in OSQA. I installed django_cas that allows authentication in Django with CAS. I configured everything as stated in the django_cas page and I changed the login URLs this way:

url(r'^%s%s$' % (_('account/'), _('signin/')), django_cas.views.login, name='auth_signin'),
url(r'^%s%s$' % (_('account/'), _('signout/')), django_cas.views.logout, name='user_signout'),

This seems to work at first, since it redirect me to the CAS server where I log myself in and then it redirects to the page I were before login, but it doesn't work because I am not authenticated (I still see the login link at the top right).

Where should I look?

@Gweakliem adds: I've been through the same exercise. I'm stuck right now figuring out what this error is. What I see in log/django.osqa.log is as follows:

/opt/OSQA/forum/middleware/extended_user.py TIME: 2011-07-20 00:07:17,068 MSG: extended_user.py:process_request:23 Unable to convert auth_user 32 to forum_user: 
Traceback (most recent call last):
  File "/opt/OSQA/forum/middleware/extended_user.py", line 12, in process_request
    request.user = request.user.user
  File "/opt/ActivePython-2.7/lib/python2.7/site-packages/django/db/models/fields/related.py", line 239, in __get__
    rel_obj = self.related.model._base_manager.using(db).get(**params)
  File "/opt/OSQA/forum/models/base.py", line 81, in get
    obj = self._base_clone().get(*args, **kwargs)
  File "/opt/ActivePython-2.7/lib/python2.7/site-packages/django/db/models/query.py", line 349, in get
    % self.model._meta.object_name)
DoesNotExist: User matching query does not exist.

asked 11 Mar '11, 16:33

sbassi's gravatar image

sbassi
15113
accept rate: 0%

edited 20 Jul '11, 10:20

Andrew_S's gravatar image

Andrew_S ♦
5.6k45674

I've been through the same exercise. I'm stuck right now figuring out what this error is. What I see in log/django.osqa.log is as follows:

/opt/OSQA/forum/middleware/extended_user.py TIME: 2011-07-20 00:07:17,068 MSG: extended_user.py:process_request:23 Unable to convert auth_user 32 to forum_user: 
Traceback (most recent call last):
  File "/opt/OSQA/forum/middleware/extended_user.py", line 12, in process_request
    request.user = request.user.user
  File "/opt/ActivePython-2.7/lib/python2.7/site-packages/django/db/models/fields/related.py", line 239, in __get__
    rel_obj = self.related.model._base_manager.using(db).get(**params)
  File "/opt/OSQA/forum/models/base.py", line 81, in get
    obj = self._base_clone().get(*args, **kwargs)
  File "/opt/ActivePython-2.7/lib/python2.7/site-packages/django/db/models/query.py", line 349, in get
    % self.model._meta.object_name)
DoesNotExist: User matching query does not exist.
(20 Jul '11, 01:01) gweakliem

user data is not updated to forum_user table of osqa causes the link to be still login even after successful login through cas

link

answered 05 May '11, 08:42

sudha's gravatar image

sudha
3215
accept rate: 0%

After much trial and error, I have a working CAS-authenticated instance of OSQA.

First of all, I had to modify the source for django_cas. What I needed was a method to verify the CAS ticket without creating a user - the default will try to create a User object and that doesn't do things the right way with respect to OSQA:

site-packages/django_cas-2.0.3-py2.7.egg/django_cas/backends.py (this is just a snippet - the key point is that I refactored out a verify method from authenticate)

def verify(self, ticket, service):
    return _verify(ticket, service)

def authenticate(self, ticket, service):
    """Verifies CAS ticket and gets or creates User object"""

username = self.verify(ticket, service)
    if not username:
        return None

Next, I created a PopulatedCASBackend as suggested in the Google Code installation docs, however, the exception handler is somewhat different. I cribbed this code from the LDAP handler examples that I've found on here. The UserJoinsAction seems to be a key bit.

forum/PopulatedCASBackend.py

    from django_cas.backends import CASBackend
    from forum.authentication.base import  AuthenticationConsumer, InvalidAuthentication, ConsumerTemplateContext
    from forum.models import User
    from forum.actions import UserJoinsAction
    import logging

class PopulatedCASBackend(CASBackend): """CAS authentication backend with user data populated from AD"""

def authenticate(self, ticket, service):
    """Authenticates CAS ticket and retrieves user data"""

    username = super(PopulatedCASBackend, self).verify(
        ticket, service)

    if not username:
    return None

# Connect to AD, modify user object, etc.
logger = logging.getLogger(__name__)
try: # If user is in datatbase carry on
        _user = User.objects.get(email=username)
    except User.DoesNotExist:# not in the database add user to database
        _user =  User(username=username, email=username)
        _user.email_isvalid = True
        _user.set_unusable_password()
        _user.save()
        UserJoinsAction(user=_user).save()

    return _user

Settings.py has a few new settings:

 MIDDLEWARE_CLASSES = [

... other stuff

'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django_cas.middleware.CASMiddleware', 'forum.middleware.extended_user.ExtendedUser', ] AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend', 'forum.PopulatedCASBackend.PopulatedCASBackend', ]

Also note that you have to modify urls.py as mentioned in the original question.

There may be an easier way to do this, but this is what I found that works. The bit about modifying django_cas is a bit hacky but it seems like the path of least resistance, maybe they'll accept a patch to refactor the verify method, or maybe they'll be open to creating a callback for doing extended user creation.

link

answered 01 Aug '11, 13:53

gweakliem's gravatar image

gweakliem
12
accept rate: 0%

edited 01 Aug '11, 13:55

gweakliem,

Could you share the code you had to change in django-cas?

(18 Aug '11, 07:55) ultronion

It's the first code block, following the line "site-packages/django_cas-2.0.3-py2.7.egg/django_cas/backends.py (this is just a snippet - the key point is that I refactored out a verify method from authenticate)"

The point is that I needed access to the verify() method without going through authenticate(ticket, service) in the case of a new user it doesn't set up the new user correctly, and there's no way to tell when a new user's been created so that you can fix it (that I know of, maybe someone can correct me). Refactoring out the verification allows you to simply authenticate and then handle the new user code as shown in the 2nd code block (PopulatedCASBackend.py)

(18 Aug '11, 11:18) gweakliem
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×75
×32
×1

Asked: 11 Mar '11, 16:33

Seen: 852 times

Last updated: 18 Aug '11, 11:21

powered by OSQA