From 5e3a747290f6e322e19de984e9d8328871e05d7d Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Wed, 26 Sep 2012 13:31:02 +0200 Subject: [PATCH] [REN] Renamed the module to auth_oauth2_provider bzr revid: fme@openerp.com-20120926113102-pdhpz9zh6zirrapw --- .../__init__.py | 2 +- .../__openerp__.py | 13 ++-- .../auth_oauth2_provider.py | 64 +++++++++++++++++ .../controllers/__init__.py | 0 .../controllers/main.py | 0 .../res_users.py | 0 .../static/src/css/auth_oauth2_provider.css} | 0 .../static/src/css/auth_oauth2_provider.sass} | 0 .../static/src/css/oauth_provider.css | 14 ++++ .../static/src/css/oauth_provider.sass | 17 +++++ .../static/src/js/auth_oauth2_provider.js} | 0 .../static/src/js/oauth_provider.js | 70 +++++++++++++++++++ .../static/src/xml/oauth_provider.xml | 0 13 files changed, 172 insertions(+), 8 deletions(-) rename addons/{auth_oauth_provider => auth_oauth2_provider}/__init__.py (97%) rename addons/{auth_oauth_provider => auth_oauth2_provider}/__openerp__.py (80%) create mode 100644 addons/auth_oauth2_provider/auth_oauth2_provider.py rename addons/{auth_oauth_provider => auth_oauth2_provider}/controllers/__init__.py (100%) rename addons/{auth_oauth_provider => auth_oauth2_provider}/controllers/main.py (100%) rename addons/{auth_oauth_provider => auth_oauth2_provider}/res_users.py (100%) rename addons/{auth_oauth_provider/static/src/css/oauth_provider.css => auth_oauth2_provider/static/src/css/auth_oauth2_provider.css} (100%) rename addons/{auth_oauth_provider/static/src/css/oauth_provider.sass => auth_oauth2_provider/static/src/css/auth_oauth2_provider.sass} (100%) create mode 100644 addons/auth_oauth2_provider/static/src/css/oauth_provider.css create mode 100644 addons/auth_oauth2_provider/static/src/css/oauth_provider.sass rename addons/{auth_oauth_provider/static/src/js/oauth_provider.js => auth_oauth2_provider/static/src/js/auth_oauth2_provider.js} (100%) create mode 100644 addons/auth_oauth2_provider/static/src/js/oauth_provider.js rename addons/{auth_oauth_provider => auth_oauth2_provider}/static/src/xml/oauth_provider.xml (100%) diff --git a/addons/auth_oauth_provider/__init__.py b/addons/auth_oauth2_provider/__init__.py similarity index 97% rename from addons/auth_oauth_provider/__init__.py rename to addons/auth_oauth2_provider/__init__.py index a6f4a72100b..4c5cc7b36e1 100644 --- a/addons/auth_oauth_provider/__init__.py +++ b/addons/auth_oauth2_provider/__init__.py @@ -20,7 +20,7 @@ ############################################################################## import controllers -import res_users +import auth_oauth2_provider # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/auth_oauth_provider/__openerp__.py b/addons/auth_oauth2_provider/__openerp__.py similarity index 80% rename from addons/auth_oauth_provider/__openerp__.py rename to addons/auth_oauth2_provider/__openerp__.py index d8b3d4e3c41..b792f3b6f10 100644 --- a/addons/auth_oauth_provider/__openerp__.py +++ b/addons/auth_oauth2_provider/__openerp__.py @@ -21,21 +21,20 @@ { - 'name': 'OAuth Provider', + 'name': 'OAuth2 Provider', 'version': '0.1', 'category': 'Tools', 'description': """ -Allow an openerp server to provide partial OAuth service. -========================================================= +Allow an openerp server to provide OAuth2 authentication service. +================================================================= """, 'author': 'OpenERP s.a.', 'maintainer': 'OpenERP s.a.', 'website': 'http://www.openerp.com', 'depends': ['base', 'web'], - #'data': ['res_users.xml'], - 'js': ['static/src/js/oauth_provider.js'], - 'css': ['static/src/css/oauth_provider.css'], - 'qweb': ['static/src/xml/oauth_provider.xml'], + 'js': ['static/src/js/auth_oauth2_provider.js'], + 'css': ['static/src/css/auth_oauth2_provider.css'], + 'qweb': ['static/src/xml/auth_oauth2_provider.xml'], 'installable': True, 'auto_install': False, } diff --git a/addons/auth_oauth2_provider/auth_oauth2_provider.py b/addons/auth_oauth2_provider/auth_oauth2_provider.py new file mode 100644 index 00000000000..1792d038b27 --- /dev/null +++ b/addons/auth_oauth2_provider/auth_oauth2_provider.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2010-2012 OpenERP s.a. (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from openerp.osv import osv, fields +import uuid + +class res_users(osv.osv): + """Oauth2 Provider Class""" + _inherit = 'res.users' + + _columns = { + # TODO: partial implementation supporting only one client_id for the moment. + 'last_oauth_token': fields.char('Last OAuth Token', size=32, readonly=True, invisible=True), + 'last_oauth_token_scope': fields.char('Last OAuth Token Scope', readonly=True, invisible=True), + } + + def auth_oauth_provider_get_token(self, cr, uid, client_id="", scope="", context=None): + token = str(uuid.uuid4()).replace('-', '') + self.write(cr, uid, [uid], { + "last_oauth_token": token, + "last_oauth_token_scope": scope, + }, context=context) + return token + + def auth_oauth_provider_tokeninfo(self, cr, uid, access_token, context=None): + user_id = self.search(cr, uid, [('last_oauth_token', '=', access_token)], context=context) + if len(user_id) != 1: + return { + "error": "invalid_token" + } + user = self.browse(cr, uid, user_id[0], context=context) + if access_token == user.last_oauth_token: + r = { + "user_id": user.id, + "scope": user.last_oauth_token_scope, + #"audience": "8819981768.apps.googleusercontent.com", + #"expires_in": 436 + } + if user.partner_id.email: # TODO: should deliver only according to scopes + r['email'] = user.partner_id.email + return r + else: + return { + "error": "invalid_token" + } + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/auth_oauth_provider/controllers/__init__.py b/addons/auth_oauth2_provider/controllers/__init__.py similarity index 100% rename from addons/auth_oauth_provider/controllers/__init__.py rename to addons/auth_oauth2_provider/controllers/__init__.py diff --git a/addons/auth_oauth_provider/controllers/main.py b/addons/auth_oauth2_provider/controllers/main.py similarity index 100% rename from addons/auth_oauth_provider/controllers/main.py rename to addons/auth_oauth2_provider/controllers/main.py diff --git a/addons/auth_oauth_provider/res_users.py b/addons/auth_oauth2_provider/res_users.py similarity index 100% rename from addons/auth_oauth_provider/res_users.py rename to addons/auth_oauth2_provider/res_users.py diff --git a/addons/auth_oauth_provider/static/src/css/oauth_provider.css b/addons/auth_oauth2_provider/static/src/css/auth_oauth2_provider.css similarity index 100% rename from addons/auth_oauth_provider/static/src/css/oauth_provider.css rename to addons/auth_oauth2_provider/static/src/css/auth_oauth2_provider.css diff --git a/addons/auth_oauth_provider/static/src/css/oauth_provider.sass b/addons/auth_oauth2_provider/static/src/css/auth_oauth2_provider.sass similarity index 100% rename from addons/auth_oauth_provider/static/src/css/oauth_provider.sass rename to addons/auth_oauth2_provider/static/src/css/auth_oauth2_provider.sass diff --git a/addons/auth_oauth2_provider/static/src/css/oauth_provider.css b/addons/auth_oauth2_provider/static/src/css/oauth_provider.css new file mode 100644 index 00000000000..778179b63b4 --- /dev/null +++ b/addons/auth_oauth2_provider/static/src/css/oauth_provider.css @@ -0,0 +1,14 @@ +@charset "utf-8"; +.openerp .oe_oauth_provider { + padding: 0 10px; +} +.openerp .oe_oauth_provider .oe_oauth_provider_error span.oe_i { + font-size: 60px; + color: #990000; + float: left; +} +.openerp .oe_oauth_provider .oe_oauth_provider_error ul { + list-style: none; + float: left; + font-weight: bold; +} diff --git a/addons/auth_oauth2_provider/static/src/css/oauth_provider.sass b/addons/auth_oauth2_provider/static/src/css/oauth_provider.sass new file mode 100644 index 00000000000..7921f4cc055 --- /dev/null +++ b/addons/auth_oauth2_provider/static/src/css/oauth_provider.sass @@ -0,0 +1,17 @@ +@charset "utf-8" + + +.openerp .oe_oauth_provider + padding: 0 10px + .oe_oauth_provider_error + span.oe_i + font-size: 60px + color: #900 + float: left + ul + list-style: none + float: left + font-weight: bold + +// au BufWritePost,FileWritePost *.sass :!sass --style expanded --line-numbers > "%:p:r.css" +// vim:tabstop=4:shiftwidth=4:softtabstop=4:fdm=marker: diff --git a/addons/auth_oauth_provider/static/src/js/oauth_provider.js b/addons/auth_oauth2_provider/static/src/js/auth_oauth2_provider.js similarity index 100% rename from addons/auth_oauth_provider/static/src/js/oauth_provider.js rename to addons/auth_oauth2_provider/static/src/js/auth_oauth2_provider.js diff --git a/addons/auth_oauth2_provider/static/src/js/oauth_provider.js b/addons/auth_oauth2_provider/static/src/js/oauth_provider.js new file mode 100644 index 00000000000..84a89415ed3 --- /dev/null +++ b/addons/auth_oauth2_provider/static/src/js/oauth_provider.js @@ -0,0 +1,70 @@ +openerp.auth_oauth_provider = function(instance) { + +var QWeb = instance.web.qweb; +var _t = instance.web._t; + +instance.web.client_actions.add('oauth2_auth', 'instance.auth_oauth_provider.ProviderAction'); +instance.auth_oauth_provider.ProviderAction = instance.web.Widget.extend({ + template: "auth_oauth_provider", + start: function (parent) { + var self = this; + this._super.apply(this, arguments); + var params = $.deparam($.param.querystring()); + if (params.response_type !== 'token') { + this.error(_t("Unsupported 'response_type' parameter")); + } + if (!params.redirect_uri) { + this.error(_t("No 'redirect_uri' parameter given")); + } + // params.client_id + // TODO: check if client_id application is authorized to use the service, and get it's name + // that should be displayed in the approval confirmation dialog. + + if (!this._error) { + // params.approval_prompt TODO --> add 'none' (default) + // TODO: get client_id and scope + this.$('.oe_oauth_provider_approval').show().on('click', '.oe_oauth_provider_allow', function() { + instance.session.rpc('/oauth2/get_token', { + client_id: params.client_id || '', + scope: params.scope || '', + }).then(function(result) { + self.redirect(result); + }).fail(function() { + self.error(_t("An error occured while contacting the OpenERP server.")); + }); + }).on('click', '.oe_oauth_provider_deny', function() { + self.redirect({ + error: 'access_denied' + }); + }); + } + }, + redirect: function(result) { + var params = $.deparam($.param.querystring()); + var a = document.createElement('a'); + a.href = params.redirect_uri; + var new_params = {}; + if (!result.error) { + new_params.access_token = result.access_token; + new_params.token_type = 'Bearer'; + if (result.expires_in) { + new_params.expires_in = result.expires_in; + } + } else { + new_params.error = result.error; + } + if (params.state) { + new_params.state = params.state; + } + var redirect = params.redirect_uri + (a.hash ? '&' : '#') + $.param(new_params); + window.location = redirect; + }, + error: function(msg) { + this._error = true; + var $msg = $('
  • ').addClass('oe_oauth_provider_error_text').text(msg); + $msg.appendTo(this.$('.oe_oauth_provider_error').show().find('ul')); + return false; + }, +}); + +}; diff --git a/addons/auth_oauth_provider/static/src/xml/oauth_provider.xml b/addons/auth_oauth2_provider/static/src/xml/oauth_provider.xml similarity index 100% rename from addons/auth_oauth_provider/static/src/xml/oauth_provider.xml rename to addons/auth_oauth2_provider/static/src/xml/oauth_provider.xml