OracleAuthBackend

 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. Login with email or username by zeeg 4 years, 10 months ago
  2. Case Insensitive Authentication Backend by ericflo 4 years, 3 months ago
  3. HTTPS redirections middleware with updated URL template tag by xlq 8 months ago
  4. Require login by url by zbyte64 4 years, 10 months ago
  5. Use django-social-auth & Google Accounts for admin login by pmdarrow 6 months, 2 weeks ago

Comments

(Forgotten your password?)