source: subversion/sites/other/tilesAtHome_tahngo/user/auth.py @ 8953

Revision 8953, 2.8 KB checked in by spaetz, 6 years ago (diff)

make getting display user name from OSM more bullet proof

Line 
1#custom authentication system that checks OSM users and creates them as needed.
2from django.conf import settings
3from django.core.validators import email_re
4from django.contrib.auth.models import User, check_password
5from tah.user.models import TahUser
6import urllib2
7import re
8
9class OSMBackend:
10    def get_user(self, user_id):
11        try:
12            return User.objects.get(pk=user_id)
13        except User.DoesNotExist:
14            return None
15
16    def authenticate(self, username=None, password=None):
17        #If username is an email address, then try to pull it up
18        if email_re.search(username):
19            try:
20                user = User.objects.get(email__iexact=username)
21            except User.DoesNotExist:
22                #user not found locally. check OSM
23                usernick = self.checkOSMpasswd(username, password)
24                if usernick == None:
25                   #wrong OSM password too
26                   return None
27                user = self.insertOSMuser(usernick, username, password)
28                t = TahUser(user = user)
29                t.save()
30        else:
31            #We have a non-email address username we should try username
32            try:
33                user = User.objects.get(username=username)
34            except User.DoesNotExist:
35                return None
36
37        if user.check_password(password) and user.is_active:
38            return user
39    #-------------------------------------------------------------
40
41    def checkOSMpasswd(self,username=None, password=None):
42        password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
43        top_level_url = "http://www.openstreetmap.org/api/0.5"
44        password_mgr.add_password(None, top_level_url, username, password)
45        handler = urllib2.HTTPBasicAuthHandler(password_mgr)
46
47        opener = urllib2.build_opener(handler)
48        try:
49            data = opener.open('http://www.openstreetmap.org/api/0.5/user/details').read() 
50        except urllib2.HTTPError, e:
51            # if e.code == 404: could be used for more specific action
52            # The OSM auth was unsuccessful. Bail out without auth.
53            return None 
54
55        #reply line looks like this
56        #<user display_name="spaetz" account_created="2007-03-19T19:00:56+00:00">
57
58        p = re.compile('<user display_name="(\w+)"')
59        m = p.search(data)
60
61        if (m):
62            return m.group(1)
63        else:
64            # Authentication was OK, but we did not find a valid username.
65            # weird!!! construct username from email
66            p = re.compile('\W+')
67            return p.sub('_', username)
68
69
70    def insertOSMuser(self, usernick, email, password):
71        ###TODO: if user already exists, just update passwd
72        user = User.objects.create_user(usernick, email, password)
73        user.save()
74        return user
Note: See TracBrowser for help on using the repository browser.