summaryrefslogtreecommitdiff
path: root/addons/auth_oauth
diff options
context:
space:
mode:
authorRandhir Mayatra (OpenERP) <rma@tinyerp.com>2012-11-19 16:38:24 +0530
committerRandhir Mayatra (OpenERP) <rma@tinyerp.com>2012-11-19 16:38:24 +0530
commitd23c5c8dca985dd0fde280c9237dee85b7bb4d7a (patch)
treecf03bc2a74a1d9e93b5ef571fb78070c09c9857b /addons/auth_oauth
parentf4ab81afb8285a8157258232c5fb2a6745ae7f72 (diff)
parent66afff1b52b7ab6ab8c340e18960fb6367cfb714 (diff)
[MERGE]merge with trunk
bzr revid: rma@tinyerp.com-20121119110824-rj4y32rfbszqln7g
Diffstat (limited to 'addons/auth_oauth')
-rw-r--r--addons/auth_oauth/controllers/main.py81
-rw-r--r--addons/auth_oauth/res_users.py35
-rw-r--r--addons/auth_oauth/static/src/js/auth_oauth.js17
3 files changed, 92 insertions, 41 deletions
diff --git a/addons/auth_oauth/controllers/main.py b/addons/auth_oauth/controllers/main.py
index 26016f3f0a1..4c7c5377184 100644
--- a/addons/auth_oauth/controllers/main.py
+++ b/addons/auth_oauth/controllers/main.py
@@ -1,48 +1,101 @@
+import functools
import logging
import simplejson
-import werkzeug.urls
-import werkzeug.utils
+from werkzeug.exceptions import BadRequest
-import openerp
+from openerp import SUPERUSER_ID
+import openerp.addons.web.http as oeweb
+from openerp.addons.web.controllers.main import db_monodb, set_cookie_and_redirect, login_and_redirect
+from openerp.modules.registry import RegistryManager
_logger = logging.getLogger(__name__)
-class OAuthController(openerp.addons.web.http.Controller):
+#----------------------------------------------------------
+# helpers
+#----------------------------------------------------------
+def fragment_to_query_string(func):
+ @functools.wraps(func)
+ def wrapper(self, req, **kw):
+ if not kw:
+ return """<html><head><script>
+ var l = window.location;
+ var q = l.hash.substring(1);
+ var r = '/' + l.search;
+ if(q.length !== 0) {
+ var s = l.search ? (l.search === '?' ? '' : '&') : '?';
+ r = l.pathname + l.search + s + q;
+ }
+ window.location = r;
+ </script></head><body></body></html>"""
+ return func(self, req, **kw)
+ return wrapper
+
+
+#----------------------------------------------------------
+# Controller
+#----------------------------------------------------------
+class OAuthController(oeweb.Controller):
_cp_path = '/auth_oauth'
- @openerp.addons.web.http.jsonrequest
+ @oeweb.jsonrequest
def list_providers(self, req, dbname):
try:
- registry = openerp.modules.registry.RegistryManager.get(dbname)
+ registry = RegistryManager.get(dbname)
with registry.cursor() as cr:
providers = registry.get('auth.oauth.provider')
- l = providers.read(cr, openerp.SUPERUSER_ID, providers.search(cr, openerp.SUPERUSER_ID, [('enabled','=',True)]))
+ l = providers.read(cr, SUPERUSER_ID, providers.search(cr, SUPERUSER_ID, [('enabled', '=', True)]))
except Exception:
l = []
return l
- @openerp.addons.web.http.httprequest
+ @oeweb.httprequest
+ @fragment_to_query_string
def signin(self, req, **kw):
state = simplejson.loads(kw['state'])
dbname = state['d']
provider = state['p']
- registry = openerp.modules.registry.RegistryManager.get(dbname)
+ context = state.get('c', {})
+ registry = RegistryManager.get(dbname)
with registry.cursor() as cr:
try:
u = registry.get('res.users')
- credentials = u.auth_oauth(cr, openerp.SUPERUSER_ID, provider, kw)
+ credentials = u.auth_oauth(cr, SUPERUSER_ID, provider, kw, context=context)
cr.commit()
- return openerp.addons.web.controllers.main.login_and_redirect(req, *credentials)
+ return login_and_redirect(req, *credentials)
except AttributeError:
# auth_signup is not installed
- _logger.error("auth_signup not installed on database %s: oauth sign up cancelled."%dbname)
+ _logger.error("auth_signup not installed on database %s: oauth sign up cancelled." % (dbname,))
url = "/#action=login&oauth_error=1"
- except Exception,e:
+ except Exception, e:
# signup error
_logger.exception("OAuth2: %s" % str(e))
url = "/#action=login&oauth_error=2"
- return openerp.addons.web.controllers.main.set_cookie_and_redirect(req, url)
+ return set_cookie_and_redirect(req, url)
+
+ @oeweb.httprequest
+ def oea(self, req, **kw):
+ """login user via OpenERP Account provider"""
+ dbname = kw.pop('db', None)
+ if not dbname:
+ dbname = db_monodb(req)
+ if not dbname:
+ return BadRequest()
+
+ registry = RegistryManager.get(dbname)
+ with registry.cursor() as cr:
+ IMD = registry['ir.model.data']
+ model, provider_id = IMD.get_object_reference(cr, SUPERUSER_ID, 'auth_oauth', 'provider_openerp')
+ assert model == 'auth.oauth.provider'
+
+ state = {
+ 'd': dbname,
+ 'p': provider_id,
+ 'c': {'no_user_creation': True},
+ }
+
+ kw['state'] = simplejson.dumps(state)
+ return self.signin(req, **kw)
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:
diff --git a/addons/auth_oauth/res_users.py b/addons/auth_oauth/res_users.py
index 115e3136f00..7e7b3753a4e 100644
--- a/addons/auth_oauth/res_users.py
+++ b/addons/auth_oauth/res_users.py
@@ -24,7 +24,7 @@ class res_users(osv.Model):
('uniq_users_oauth_provider_oauth_uid', 'unique(oauth_provider_id, oauth_uid)', 'OAuth UID must be unique per provider'),
]
- def auth_oauth_rpc(self, cr, uid, endpoint, access_token, context=None):
+ def _auth_oauth_rpc(self, cr, uid, endpoint, access_token, context=None):
params = urllib.urlencode({'access_token': access_token})
if urlparse.urlparse(endpoint)[4]:
url = endpoint + '&' + params
@@ -34,6 +34,17 @@ class res_users(osv.Model):
response = f.read()
return simplejson.loads(response)
+ 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
+
def auth_oauth(self, cr, uid, provider, params, context=None):
# Advice by Google (to avoid Confused Deputy Problem)
# if validation.audience != OUR_CLIENT_ID:
@@ -41,25 +52,21 @@ class res_users(osv.Model):
# else:
# continue with the process
access_token = params.get('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)
+ validation = self._auth_oauth_validate(cr, uid, provider, access_token)
# required
oauth_uid = validation['user_id']
if not oauth_uid:
raise openerp.exceptions.AccessDenied()
- email = validation.get('email', 'provider_%d_user_%d' % (p.id, oauth_uid))
+ email = validation.get('email', 'provider_%d_user_%d' % (provider, oauth_uid))
+ login = email
# optional
name = validation.get('name', email)
res = self.search(cr, uid, [("oauth_uid", "=", oauth_uid), ('oauth_provider_id', '=', provider)])
if res:
assert len(res) == 1
- self.write(cr, uid, res[0], {'oauth_access_token': access_token})
+ user = self.browse(cr, uid, res[0], context=context)
+ login = user.login
+ user.write({'oauth_access_token': access_token})
else:
# New user if signup module available
if not hasattr(self, '_signup_create_user'):
@@ -67,9 +74,9 @@ class res_users(osv.Model):
new_user = {
'name': name,
- 'login': email,
+ 'login': login,
'user_email': email,
- 'oauth_provider_id': p.id,
+ 'oauth_provider_id': provider,
'oauth_uid': oauth_uid,
'oauth_access_token': access_token,
'active': True,
@@ -77,7 +84,7 @@ class res_users(osv.Model):
# TODO pass signup token to allow attach new user to right partner
self._signup_create_user(cr, uid, new_user)
- credentials = (cr.dbname, email, access_token)
+ credentials = (cr.dbname, login, access_token)
return credentials
def check_credentials(self, cr, uid, password):
diff --git a/addons/auth_oauth/static/src/js/auth_oauth.js b/addons/auth_oauth/static/src/js/auth_oauth.js
index 15683cfac2a..2aa66877d42 100644
--- a/addons/auth_oauth/static/src/js/auth_oauth.js
+++ b/addons/auth_oauth/static/src/js/auth_oauth.js
@@ -36,7 +36,10 @@ openerp.auth_oauth = function(instance) {
ev.preventDefault();
var index = $(ev.target).data('index');
var p = this.oauth_providers[index];
- var ret = location.protocol+"//"+location.host+"/";
+ var ret = _.str.sprintf('%s//%s/auth_oauth/signin', location.protocol, location.host);
+ if (instance.session.debug) {
+ ret += '?debug';
+ }
var dbname = self.$("form [name=db]").val();
var state_object = {
d: dbname,
@@ -55,16 +58,4 @@ openerp.auth_oauth = function(instance) {
},
});
- instance.web.WebClient = instance.web.WebClient.extend({
- start: function() {
- this._super.apply(this, arguments);
- var params = $.deparam(window.location.hash.substring(1));
- // alert(JSON.stringify(params));
- if (params.hasOwnProperty('access_token')) {
- var url = "/auth_oauth/signin" + '?' + $.param(params);
- window.location = url;
- }
- },
- });
-
};