Not logged in. · Lost password · Register
Forum: Customising UNB Modifications and plug-ins Plug-in gallery RSS
UNB authentication backend for Django
tiax #1
Member since Sep 2005 · 10 posts · Location: Augsburg
Group memberships: Members
Show profile · Link to this post
Subject: UNB authentication backend for Django
Not a plugin for UNB but for Django, a python web framework. With this backend, Forum users can be authenticated at django driven sites, e.g CMS, galleries, etc. I hope those kinds of Plugins are allowed here.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.contrib.auth.models import User, check_password
from django.db import connection
from random import choice
from string import ascii_letters
from md5 import md5

def make_unb_pw(klartextkennwort, salt=None):
    '''erstellt aus dem angegebenen Klartextkennwort einen zum UNB passenden hash'''
    if not salt:
        salt = choice(ascii_letters) + choice(ascii_letters)
    passwort_md5 = md5(klartextkennwort).hexdigest()
    pwhash = md5(salt+passwort_md5).hexdigest()
    return pwhash[0:16] + salt + pwhash[16:]

def check_unb_pw(klartextkennwort, pwhash):
    '''überprüft ob sich aus dem Klartextkennwort der pwhash erzeugen lässt'''
    return make_unb_pw(klartextkennwort, pwhash[16:18]) == pwhash


class UNBauthbackend:
    '''authentifiziert User gegen die Datenbank des Unclassified Newsboards
    http://newsboard.unclassified.de

    Übernommen werden nur die Usernamen und Mailadressen. Registrierung
    und Änderungen im Profil werden nach wie vor über das Forum gemacht.
    Das Passwort wird nach wie vor aus der Forendatenbank geholt'''
    def authenticate(self, username=None, password=None):
        cursor = connection.cursor()
        (passworthash, email) = cursor.execute('SELECT Password, EMail from unb1_Users where Name = %s', [username]).fetchone()
        if check_unb_pw(password, passworthash):
            try:
                user = User.objects.get(username=username)
            except User.DoesNotExist:
                user = User(username=username, password='aus der unb-db', email=email)
                user.is_staff=False
                user.is_superuser=False
                user.save()
            return user
        else:
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
This post was edited on 2006-10-24, 15:08 by tiax.
apollo13 #2
Member since May 2006 · 4 posts
Group memberships: Members
Show profile · Link to this post
lol, I was about to write this on my own.
You saved me 40 Lines of code, thx ;)

Django rocks
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
This board is powered by the Unclassified NewsBoard software, 20110527-dev, © 2003-2011 by Yves Goergen
Page created in 163.1 ms (78 ms) · 42 database queries in 133.2 ms
Current time: 2012-02-08, 09:22:24 (UTC +01:00)