diff options
| author | Fabien Meghazi <fme@openerp.com> | 2014-01-27 19:16:06 +0100 |
|---|---|---|
| committer | Fabien Meghazi <fme@openerp.com> | 2014-01-27 19:16:06 +0100 |
| commit | 289074e0c835eb4d5bba2cf5b6da1706c22abc6f (patch) | |
| tree | df59e66edf75e4dece1a28342bfb324ddc8680b2 /addons/auth_oauth/controllers/main.py | |
| parent | c83ad0d1b6c1d683dedbcb477fa15329529f4af0 (diff) | |
[IMP] auth_oauth converted to server side views
bzr revid: fme@openerp.com-20140127181606-236dvru2tcvg42qo
Diffstat (limited to 'addons/auth_oauth/controllers/main.py')
| -rw-r--r-- | addons/auth_oauth/controllers/main.py | 102 |
1 files changed, 61 insertions, 41 deletions
diff --git a/addons/auth_oauth/controllers/main.py b/addons/auth_oauth/controllers/main.py index af8b36d2556..dfcb21e91b2 100644 --- a/addons/auth_oauth/controllers/main.py +++ b/addons/auth_oauth/controllers/main.py @@ -1,4 +1,3 @@ -import functools import logging import simplejson @@ -8,51 +7,72 @@ from werkzeug.exceptions import BadRequest import openerp from openerp import SUPERUSER_ID from openerp import http -from openerp.http import request +from openerp.http import request, LazyResponse from openerp.addons.web.controllers.main import db_monodb, set_cookie_and_redirect, login_and_redirect from openerp.modules.registry import RegistryManager +from openerp.tools.translate import _ _logger = logging.getLogger(__name__) #---------------------------------------------------------- -# helpers -#---------------------------------------------------------- -def fragment_to_query_string(func): - @functools.wraps(func) - def wrapper(self, *a, **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, *a, **kw) - return wrapper - - -#---------------------------------------------------------- # Controller #---------------------------------------------------------- -class OAuthController(http.Controller): - - @http.route('/auth_oauth/list_providers', type='json', auth='none') - def list_providers(self, dbname): +class OAuthLogin(openerp.addons.web.controllers.main.Home): + def list_providers(self): try: - registry = RegistryManager.get(dbname) - with registry.cursor() as cr: - providers = registry.get('auth.oauth.provider') - l = providers.read(cr, SUPERUSER_ID, providers.search(cr, SUPERUSER_ID, [('enabled', '=', True)])) + provider_obj = request.registry.get('auth.oauth.provider') + providers = provider_obj.search_read(request.cr, SUPERUSER_ID, [('enabled', '=', True)]) except Exception: - l = [] - return l + providers = [] + for provider in providers: + return_url = request.httprequest.url_root + 'auth_oauth/signin' + state = dict( + d=request.session.db, + p=provider['id'] + ) + params = dict( + debug=request.debug, + response_type='token', + client_id=provider['client_id'], + redirect_uri=return_url, + scope=provider['scope'], + state=simplejson.dumps(state), + ) + provider['auth_link'] = provider['auth_endpoint'] + '?' + werkzeug.url_encode(params) + + return providers + + @http.route() + def web_login(self, *args, **kw): + # TODO: ensure_db() + request.disable_db = False + + response = super(OAuthLogin, self).web_login(*args, **kw) + if isinstance(response, LazyResponse): + error = request.params.get('oauth_error') + if error == '1': + error = _("Sign up is not allowed on this database.") + elif error == '2': + error = _("Access Denied") + elif error == '3': + error = _("You do not have access to this database or your invitation has expired. Please ask for an invitation and be sure to follow the link in your invitation email.") + else: + error = None + response.params['values'].update( + providers=self.list_providers(), + error=error, + ) + + # TODO: code in old js controller that should be converted in auth_oauth_signup + # if (this.oauth_providers.length === 1 && params.type === 'signup') { + # this.do_oauth_sign_in(this.oauth_providers[0]); + # } + + return response + +class OAuthController(http.Controller): @http.route('/auth_oauth/signin', type='http', auth='none') - @fragment_to_query_string def signin(self, **kw): state = simplejson.loads(kw['state']) dbname = state['d'] @@ -66,27 +86,27 @@ class OAuthController(http.Controller): cr.commit() action = state.get('a') menu = state.get('m') - url = '/' + url = '/web' if action: - url = '/#action=%s' % action + url = '/web#action=%s' % action elif menu: - url = '/#menu_id=%s' % menu + url = '/web#menu_id=%s' % menu return login_and_redirect(*credentials, redirect_url=url) except AttributeError: # auth_signup is not installed _logger.error("auth_signup not installed on database %s: oauth sign up cancelled." % (dbname,)) - url = "/#action=login&oauth_error=1" + url = "/web/login?oauth_error=1" except openerp.exceptions.AccessDenied: # oauth credentials not valid, user could be on a temporary session _logger.info('OAuth2: access denied, redirect to main page in case a valid session exists, without setting cookies') - url = "/#action=login&oauth_error=3" + url = "/web/login?oauth_error=3" redirect = werkzeug.utils.redirect(url, 303) redirect.autocorrect_location_header = False return redirect except Exception, e: # signup error _logger.exception("OAuth2: %s" % str(e)) - url = "/#action=login&oauth_error=2" + url = "/web/login?oauth_error=2" return set_cookie_and_redirect(url) @@ -105,7 +125,7 @@ class OAuthController(http.Controller): try: model, provider_id = IMD.get_object_reference(cr, SUPERUSER_ID, 'auth_oauth', 'provider_openerp') except ValueError: - return set_cookie_and_redirect('/?db=%s' % dbname) + return set_cookie_and_redirect('/web?db=%s' % dbname) assert model == 'auth.oauth.provider' state = { |
