[FIX]Resolved traceback on moving to the previous record from serial numbers in Trace...
[odoo/odoo.git] / addons / auth_oauth / res_users.py
1 import logging
2
3 import urllib
4 import urlparse
5 import urllib2
6 import simplejson
7
8 import openerp
9 from openerp.osv import osv, fields
10 from openerp import SUPERUSER_ID
11
12 _logger = logging.getLogger(__name__)
13
14 class res_users(osv.Model):
15     _inherit = 'res.users'
16
17     _columns = {
18         'oauth_provider_id': fields.many2one('auth.oauth.provider', 'OAuth Provider'),
19         'oauth_uid': fields.char('OAuth User ID', help="Oauth Provider user_id"),
20         'oauth_access_token': fields.char('OAuth Access Token', readonly=True),
21     }
22
23     _sql_constraints = [
24         ('uniq_users_oauth_provider_oauth_uid', 'unique(oauth_provider_id, oauth_uid)', 'OAuth UID must be unique per provider'),
25     ]
26
27     def _auth_oauth_rpc(self, cr, uid, endpoint, access_token, context=None):
28         params = urllib.urlencode({'access_token': access_token})
29         if urlparse.urlparse(endpoint)[4]:
30             url = endpoint + '&' + params
31         else:
32             url = endpoint + '?' + params
33         f = urllib2.urlopen(url)
34         response = f.read()
35         return simplejson.loads(response)
36
37     def _auth_oauth_validate(self, cr, uid, provider, access_token, context=None):
38         """ return the validation data corresponding to the access token """
39         p = self.pool.get('auth.oauth.provider').browse(cr, uid, provider, context=context)
40         validation = self._auth_oauth_rpc(cr, uid, p.validation_endpoint, access_token)
41         if validation.get("error"):
42             raise Exception(validation['error'])
43         if p.data_endpoint:
44             data = self._auth_oauth_rpc(cr, uid, p.data_endpoint, access_token)
45             validation.update(data)
46         return validation
47
48     def _auth_oauth_signin(self, cr, uid, provider, validation, params, context=None):
49         """ retrieve and sign in the user corresponding to provider and validated access token
50             :param provider: oauth provider id (int)
51             :param validation: result of validation of access token (dict)
52             :param params: oauth parameters (dict)
53             :return: user login (str)
54             :raise: openerp.exceptions.AccessDenied if signin failed
55
56             This method can be overridden to add alternative signin methods.
57         """
58         oauth_uid = validation['user_id']
59         user_ids = self.search(cr, uid, [("oauth_uid", "=", oauth_uid), ('oauth_provider_id', '=', provider)])
60         if not user_ids:
61             raise openerp.exceptions.AccessDenied()
62         assert len(user_ids) == 1
63         user = self.browse(cr, uid, user_ids[0], context=context)
64         user.write({'oauth_access_token': params['access_token']})
65         return user.login
66
67     def auth_oauth(self, cr, uid, provider, params, context=None):
68         # Advice by Google (to avoid Confused Deputy Problem)
69         # if validation.audience != OUR_CLIENT_ID:
70         #   abort()
71         # else:
72         #   continue with the process
73         access_token = params.get('access_token')
74         validation = self._auth_oauth_validate(cr, uid, provider, access_token)
75         # required check
76         if not validation.get('user_id'):
77             raise openerp.exceptions.AccessDenied()
78         # retrieve and sign in user
79         login = self._auth_oauth_signin(cr, uid, provider, validation, params, context=context)
80         if not login:
81             raise openerp.exceptions.AccessDenied()
82         # return user credentials
83         return (cr.dbname, login, access_token)
84
85     def check_credentials(self, cr, uid, password):
86         try:
87             return super(res_users, self).check_credentials(cr, uid, password)
88         except openerp.exceptions.AccessDenied:
89             res = self.search(cr, SUPERUSER_ID, [('id', '=', uid), ('oauth_access_token', '=', password)])
90             if not res:
91                 raise
92
93 #