9 from openerp.osv import osv, fields
10 from openerp import SUPERUSER_ID
12 _logger = logging.getLogger(__name__)
14 class res_users(osv.Model):
15 _inherit = 'res.users'
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),
24 ('uniq_users_oauth_provider_oauth_uid', 'unique(oauth_provider_id, oauth_uid)', 'OAuth UID must be unique per provider'),
27 def _auth_oauth_rpc(self, cr, uid, endpoint, access_token, context=None):
28 params = werkzeug.url_encode({'access_token': access_token})
29 if urlparse.urlparse(endpoint)[4]:
30 url = endpoint + '&' + params
32 url = endpoint + '?' + params
33 f = urllib2.urlopen(url)
35 return simplejson.loads(response)
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'])
44 data = self._auth_oauth_rpc(cr, uid, p.data_endpoint, access_token)
45 validation.update(data)
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
56 This method can be overridden to add alternative signin methods.
58 oauth_uid = validation['user_id']
59 user_ids = self.search(cr, uid, [("oauth_uid", "=", oauth_uid), ('oauth_provider_id', '=', provider)])
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']})
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:
72 # continue with the process
73 access_token = params.get('access_token')
74 validation = self._auth_oauth_validate(cr, uid, provider, access_token)
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)
81 raise openerp.exceptions.AccessDenied()
82 # return user credentials
83 return (cr.dbname, login, access_token)
85 def check_credentials(self, cr, uid, password):
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)])