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