references pour roundup
[cyril.git] / Apps / roundup / auf_roa.py
1 import hashlib, simplejson, urllib
2
3 from roundup.cgi.actions import LoginAction
4 from roundup.i18n import _
5
6 class AufRoaLoginAction(LoginAction):
7 auf_roa_url = 'https://references.auf.org/roa/utilisateur/'
8 auf_email_suffix = "@auf.org"
9
10 def verifyLocalPassword(self, password):
11 rc = False
12
13 stored = self.db.user.get(self.client.userid, 'password')
14 if password == stored:
15 rc = True
16
17 return rc
18
19 def local_login (self, password):
20 rc = True
21
22 try:
23 self.client.userid = self.db.user.lookup(self.client.user)
24 except KeyError:
25 self.client.error_message.append(_('Unknown user "%s"')%self.client.user)
26 rc = False
27 if not self.verifyLocalPassword(password):
28 self.client.error_message.append(_('Invalid password'))
29 rc = False
30
31 return rc
32
33
34 def roa_login (self, email, password):
35 rc = None
36
37 api_key = ""
38 url = "%s?filter_email=%s&filter_password=%s&format=json&api-key=%s" \
39 % (self.auf_roa_url, email, password, api_key)
40
41 try:
42 ud = urllib.urlopen (url)
43 list = simplejson.load (ud)
44 ud.close ()
45 rc = list[0]
46 except:
47 rc = None
48
49 return rc
50
51
52 def verifyLogin(self, username, password):
53 if username.endswith (self.auf_email_suffix):
54 username = username.replace (self.auf_email_suffix, "")
55
56 email = "%s%s" % (username, self.auf_email_suffix)
57 md5pass = hashlib.md5 (password).hexdigest ()
58
59
60 roa = False
61 if not self.local_login(password):
62 roa = self.roa_login(email, md5pass)
63 if not roa:
64 self.client.make_user_anonymous ()
65 return
66
67 try:
68 self.client.userid = self.db.user.lookup(self.client.user)
69 except:
70 if roa:
71 props = {'realname': "%s %s" % (roa['first_name'], roa['last_name']),
72 'username': email,
73 'address': email}
74 props['roles'] = self.db.config.NEW_WEB_USER_ROLES
75 self.journaltag = 'admin'
76 cl = self.db.user
77 self.userid = cl.create (**props)
78 self.db.commit ()
79 self.client.userid = self.db.user.lookup(self.client.user)
80 else:
81 self.client.make_user_anonymous()
82 self.client.error_message.append(_("No account created without ROA"))
83
84 def init(instance):
85 instance.registerAction('login', AufRoaLoginAction)
86