2012-08-14 12:46:36 +00:00
|
|
|
import logging
|
|
|
|
|
2012-08-15 18:09:13 +00:00
|
|
|
import urllib
|
2012-09-26 09:43:36 +00:00
|
|
|
import urlparse
|
2012-08-12 23:26:12 +00:00
|
|
|
import urllib2
|
2012-08-13 12:33:33 +00:00
|
|
|
import simplejson
|
2012-08-12 23:26:12 +00:00
|
|
|
|
|
|
|
import openerp
|
|
|
|
from openerp.osv import osv, fields
|
2012-08-31 13:51:36 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2012-08-12 23:26:12 +00:00
|
|
|
|
2012-08-14 12:46:36 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2012-08-12 23:26:12 +00:00
|
|
|
class res_users(osv.Model):
|
|
|
|
_inherit = 'res.users'
|
|
|
|
|
2012-08-14 06:34:13 +00:00
|
|
|
_columns = {
|
2012-10-10 14:01:04 +00:00
|
|
|
'oauth_provider_id': fields.many2one('auth.oauth.provider', 'OAuth Provider'),
|
2012-08-15 18:09:13 +00:00
|
|
|
'oauth_uid': fields.char('OAuth User ID', help="Oauth Provider user_id"),
|
2012-10-10 14:01:04 +00:00
|
|
|
'oauth_access_token': fields.char('OAuth Access Token', readonly=True),
|
2012-08-14 06:34:13 +00:00
|
|
|
}
|
|
|
|
|
2012-10-10 14:01:04 +00:00
|
|
|
_sql_constraints = [
|
2012-10-11 11:40:58 +00:00
|
|
|
('uniq_users_oauth_provider_oauth_uid', 'unique(oauth_provider_id, oauth_uid)', 'OAuth UID must be unique per provider'),
|
2012-10-10 14:01:04 +00:00
|
|
|
]
|
|
|
|
|
2012-11-16 09:26:12 +00:00
|
|
|
def _auth_oauth_rpc(self, cr, uid, endpoint, access_token, context=None):
|
2012-10-10 14:01:04 +00:00
|
|
|
params = urllib.urlencode({'access_token': access_token})
|
2012-09-26 09:43:36 +00:00
|
|
|
if urlparse.urlparse(endpoint)[4]:
|
|
|
|
url = endpoint + '&' + params
|
|
|
|
else:
|
|
|
|
url = endpoint + '?' + params
|
2012-08-14 06:34:13 +00:00
|
|
|
f = urllib2.urlopen(url)
|
2012-08-14 11:55:55 +00:00
|
|
|
response = f.read()
|
|
|
|
return simplejson.loads(response)
|
2012-08-14 06:34:13 +00:00
|
|
|
|
2012-11-16 09:26:12 +00:00
|
|
|
def _auth_oauth_validate(self, cr, uid, provider, access_token, context=None):
|
|
|
|
""" return the validation data corresponding to the access token """
|
|
|
|
p = self.pool.get('auth.oauth.provider').browse(cr, uid, provider, context=context)
|
|
|
|
validation = self._auth_oauth_rpc(cr, uid, p.validation_endpoint, access_token)
|
|
|
|
if validation.get("error"):
|
|
|
|
raise Exception(validation['error'])
|
|
|
|
if p.data_endpoint:
|
|
|
|
data = self._auth_oauth_rpc(cr, uid, p.data_endpoint, access_token)
|
|
|
|
validation.update(data)
|
|
|
|
return validation
|
|
|
|
|
2012-08-15 22:47:08 +00:00
|
|
|
def auth_oauth(self, cr, uid, provider, params, context=None):
|
2012-08-13 12:33:33 +00:00
|
|
|
# Advice by Google (to avoid Confused Deputy Problem)
|
|
|
|
# if validation.audience != OUR_CLIENT_ID:
|
|
|
|
# abort()
|
|
|
|
# else:
|
|
|
|
# continue with the process
|
2012-08-14 11:55:55 +00:00
|
|
|
access_token = params.get('access_token')
|
2012-11-16 09:26:12 +00:00
|
|
|
validation = self._auth_oauth_validate(cr, uid, provider, access_token)
|
2012-09-26 09:43:36 +00:00
|
|
|
# required
|
2012-08-14 06:34:13 +00:00
|
|
|
oauth_uid = validation['user_id']
|
2012-09-26 09:43:36 +00:00
|
|
|
if not oauth_uid:
|
2012-10-10 14:01:04 +00:00
|
|
|
raise openerp.exceptions.AccessDenied()
|
2012-11-16 09:26:12 +00:00
|
|
|
email = validation.get('email', 'provider_%d_user_%d' % (provider, oauth_uid))
|
2012-11-16 10:58:20 +00:00
|
|
|
login = email
|
2012-09-26 09:43:36 +00:00
|
|
|
# optional
|
|
|
|
name = validation.get('name', email)
|
2012-10-10 14:01:04 +00:00
|
|
|
res = self.search(cr, uid, [("oauth_uid", "=", oauth_uid), ('oauth_provider_id', '=', provider)])
|
2012-08-14 11:55:55 +00:00
|
|
|
if res:
|
2012-10-10 14:01:04 +00:00
|
|
|
assert len(res) == 1
|
2012-11-16 10:58:20 +00:00
|
|
|
user = self.browse(cr, uid, res[0], context=context)
|
|
|
|
login = user.login
|
|
|
|
user.write({'oauth_access_token': access_token})
|
2012-08-14 11:55:55 +00:00
|
|
|
else:
|
2012-10-10 14:01:04 +00:00
|
|
|
# New user if signup module available
|
|
|
|
if not hasattr(self, '_signup_create_user'):
|
|
|
|
raise openerp.exceptions.AccessDenied()
|
|
|
|
|
2012-08-14 11:55:55 +00:00
|
|
|
new_user = {
|
|
|
|
'name': name,
|
2012-11-16 10:58:20 +00:00
|
|
|
'login': login,
|
2012-09-26 09:43:36 +00:00
|
|
|
'user_email': email,
|
2012-11-16 10:58:20 +00:00
|
|
|
'oauth_provider_id': provider,
|
2012-08-14 11:55:55 +00:00
|
|
|
'oauth_uid': oauth_uid,
|
|
|
|
'oauth_access_token': access_token,
|
|
|
|
'active': True,
|
|
|
|
}
|
2012-10-10 14:01:04 +00:00
|
|
|
# TODO pass signup token to allow attach new user to right partner
|
2012-10-10 12:55:45 +00:00
|
|
|
self._signup_create_user(cr, uid, new_user)
|
2012-10-10 14:01:04 +00:00
|
|
|
|
2012-11-16 10:58:20 +00:00
|
|
|
credentials = (cr.dbname, login, access_token)
|
2012-08-15 18:09:13 +00:00
|
|
|
return credentials
|
2012-08-13 12:33:33 +00:00
|
|
|
|
2012-08-15 21:10:04 +00:00
|
|
|
def check_credentials(self, cr, uid, password):
|
2012-08-14 06:34:13 +00:00
|
|
|
try:
|
2012-08-15 21:10:04 +00:00
|
|
|
return super(res_users, self).check_credentials(cr, uid, password)
|
|
|
|
except openerp.exceptions.AccessDenied:
|
2012-10-10 14:01:04 +00:00
|
|
|
res = self.search(cr, SUPERUSER_ID, [('id', '=', uid), ('oauth_access_token', '=', password)])
|
2012-08-15 21:10:04 +00:00
|
|
|
if not res:
|
2012-08-14 06:34:13 +00:00
|
|
|
raise
|
2012-08-13 12:33:33 +00:00
|
|
|
|
2012-08-12 23:26:12 +00:00
|
|
|
#
|