2015-05-17 21:24:41 +00:00
|
|
|
# ⁻*- coding: utf-8 -*-
|
2015-05-27 19:56:39 +00:00
|
|
|
"""Some authentication classes for the CAS"""
|
2015-05-17 21:24:41 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
try:
|
|
|
|
import MySQLdb
|
|
|
|
import MySQLdb.cursors
|
|
|
|
import crypt
|
|
|
|
except ImportError:
|
|
|
|
MySQLdb = None
|
2015-05-27 19:56:39 +00:00
|
|
|
|
2015-05-17 21:24:41 +00:00
|
|
|
class DummyAuthUser(object):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""A Dummy authentication class"""
|
2015-05-17 21:24:41 +00:00
|
|
|
def __init__(self, username):
|
|
|
|
self.username = username
|
|
|
|
|
|
|
|
def test_password(self, password):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""test `password` agains the user"""
|
2015-05-17 21:24:41 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def attributs(self):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""return a dict of user attributes"""
|
2015-05-17 21:24:41 +00:00
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
class TestAuthUser(DummyAuthUser):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""A test authentication class with one user test having
|
|
|
|
alose test as password and some attributes"""
|
2015-05-17 21:24:41 +00:00
|
|
|
def __init__(self, username):
|
2015-05-27 19:56:39 +00:00
|
|
|
super(TestAuthUser, self).__init__(username)
|
2015-05-17 21:24:41 +00:00
|
|
|
|
|
|
|
def test_password(self, password):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""test `password` agains the user"""
|
2015-05-17 21:24:41 +00:00
|
|
|
return self.username == "test" and password == "test"
|
|
|
|
|
|
|
|
def attributs(self):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""return a dict of user attributes"""
|
2015-05-17 21:24:41 +00:00
|
|
|
return {'nom':'Nymous', 'prenom':'Ano', 'email':'anonymous@example.net'}
|
|
|
|
|
|
|
|
|
|
|
|
class MysqlAuthUser(DummyAuthUser):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""A mysql auth class: authentication user agains a mysql database"""
|
2015-05-17 21:24:41 +00:00
|
|
|
user = None
|
|
|
|
def __init__(self, username):
|
|
|
|
mysql_config = {
|
2015-05-27 19:56:39 +00:00
|
|
|
"user": settings.CAS_SQL_USERNAME,
|
|
|
|
"passwd": settings.CAS_SQL_PASSWORD,
|
|
|
|
"db": settings.CAS_SQL_DBNAME,
|
|
|
|
"host": settings.CAS_SQL_HOST,
|
|
|
|
"charset":settings.CAS_SQL_DBCHARSET,
|
|
|
|
"cursorclass":MySQLdb.cursors.DictCursor
|
2015-05-17 21:24:41 +00:00
|
|
|
}
|
|
|
|
if not MySQLdb:
|
|
|
|
raise RuntimeError("Please install MySQLdb before using the MysqlAuthUser backend")
|
|
|
|
conn = MySQLdb.connect(**mysql_config)
|
|
|
|
curs = conn.cursor()
|
|
|
|
if curs.execute(settings.CAS_SQL_USER_QUERY, (username,)) == 1:
|
|
|
|
self.user = curs.fetchone()
|
|
|
|
super(MysqlAuthUser, self).__init__(username)
|
|
|
|
|
|
|
|
def test_password(self, password):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""test `password` agains the user"""
|
2015-05-17 21:24:41 +00:00
|
|
|
if not self.user:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if settings.CAS_SQL_PASSWORD_CHECK == "plain":
|
|
|
|
return password == self.user["password"]
|
|
|
|
elif settings.CAS_SQL_PASSWORD_CHECK == "crypt":
|
|
|
|
if self.user["password"].startswith('$'):
|
|
|
|
salt = '$'.join(self.user["password"].split('$', 3)[:-1])
|
|
|
|
return crypt.crypt(password, salt) == self.user["password"]
|
|
|
|
else:
|
|
|
|
return crypt.crypt(password, self.user["password"][:2]) == self.user["password"]
|
|
|
|
|
|
|
|
def attributs(self):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""return a dict of user attributes"""
|
2015-05-17 21:24:41 +00:00
|
|
|
if not self.user:
|
|
|
|
return {}
|
|
|
|
else:
|
|
|
|
return self.user
|
|
|
|
|
|
|
|
class DjangoAuthUser(DummyAuthUser):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""A django auth class: authenticate user agains django internal users"""
|
2015-05-17 21:24:41 +00:00
|
|
|
user = None
|
|
|
|
def __init__(self, username):
|
|
|
|
try:
|
|
|
|
self.user = User.objects.get(username=username)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
pass
|
|
|
|
super(DjangoAuthUser, self).__init__(username)
|
|
|
|
|
|
|
|
|
|
|
|
def test_password(self, password):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""test `password` agains the user"""
|
2015-05-17 21:24:41 +00:00
|
|
|
if not self.user:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return self.user.check_password(password)
|
|
|
|
|
|
|
|
def attributs(self):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""return a dict of user attributes"""
|
2015-05-17 21:24:41 +00:00
|
|
|
if not self.user:
|
|
|
|
return {}
|
|
|
|
else:
|
|
|
|
attr = {}
|
|
|
|
for field in self.user._meta.fields:
|
2015-05-27 19:56:39 +00:00
|
|
|
attr[field.attname] = getattr(self.user, field.attname)
|
2015-05-17 21:24:41 +00:00
|
|
|
return attr
|