Login

OracleAuthBackend

Author:
nosrednakram
Posted:
August 10, 2009
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

This code uses oracle as an authentication back end. It creates a new connection to the db and attempts to login. If successful it will then create an upper case User account with _ORACLE appended to the username.

My urls.py call:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
   (r'^accounts/login/$', 'django.contrib.auth.views.login',
    {'template_name': 'login.html'}),
)

My setting.py specific settings:

AUTHENTICATION_BACKENDS = (
     'oracleauth.views.OracleAuthBackend',
)
LOGIN_URL = '/accounts/login/'
ORACLE_CONNECT = 'database-host:1521/database'
DEBUG=True
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from django.contrib.auth.models import User
from django.conf import settings
import cx_Oracle

# set in config or here depending on your taste
try:
    ORACLE_CONNECT = settings.ORACLE_CONNECT
except:
    ORACLE_CONNECT = None

# when using runserver I turn debugging on, you can
# set to false of remove.
try:
    DEBUG = settings.DEBUG
except:
    DEBUG = False

class OracleAuthBackend:
    """
    This class is used to authenticate against an Oracle database and
    adds a user if authentication is successful.  It uppercases the
    username since oracle usernames are not currently case sensitive.
    Additionally I append _ORACLE to the username to help make it
    easier to identify what auth source was used and to avoid conflicts
    with ldap authenticated sessions.
    """
    def authenticate(self, username=None, password=None):
        if DEBUG:
            print "Attempting to log in as %s" % (username)
        if ORACLE_CONNECT == None:
            constr = '%s/%s' % (username, password)
        else:
            constr = '%s/%s@%s' % (username, password, ORACLE_CONNECT)
        try:
            auth_con = cx_Oracle.connect(constr.encode('ascii','ignore'))
            auth_con.close()
        except Exception, e:
            if DEBUG:
                print e
            return None
        oracle_user = username.upper() + '_ORACLE'
        try:
            if DEBUG:
                print 'Looking up: %s' % oracle_user
            user = User.objects.get(username=oracle_user)
        except:
            if DEBUG:
                print 'Adding user: %s' % oracle_user
            user = User(username=oracle_user)
            user.set_unusable_password()
            try:
                user.save()
            except:
                if DEBUG:
                    print "ERROR: adding %s" % oracle_user
                return None
        return user

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.