oauth wip

bzr revid: al@openerp.com-20120812232612-9cib8w333no9hg3d
This commit is contained in:
Antony Lesuisse 2012-08-13 01:26:12 +02:00
parent 9e43be663b
commit 7ff74cc4b1
5 changed files with 97 additions and 53 deletions

View File

@ -1,20 +1,2 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2010-2011 OpenERP s.a. (<http://openerp.com>).
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
import controllers
import res_users

View File

@ -0,0 +1,3 @@
import main
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,39 @@
import logging
import werkzeug.urls
import openerp.modules.registry
import openerp.addons.web.controllers.main
import openerp.addons.web.common.http as openerpweb
_logger = logging.getLogger(__name__)
class OAuthController(openerpweb.Controller):
_cp_path = '/auth_oauth'
@openerpweb.httprequest
def signin(self, req, **kw):
dbname = kw.get("state")
registry = openerp.modules.registry.RegistryManager.get(dbname)
cr = registry.db.cursor()
try:
try:
u = registry.get('res.users')
r = u.auth_oauth(cr, 1, kw)
cr.commit()
return openerp.addons.web.controllers.main.login_and_redirect(req, *r)
# or
req.authenticate(*r)
url = "/"
except AttributeError:
# auth_signup is not installed
url = "/#action=auth_signup&error=1"
except Exception,e:
# signup error
url = "/#action=auth_signup&error=2"
finally:
cr.close()
return ""
return werkzeug.utils.redirect(url)
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,40 @@
import urllib2
import openerp
from openerp.osv import osv, fields
class res_users(osv.Model):
_inherit = 'res.users'
def auth_oauth(self, cr, uid, params, context=None):
print params
url = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + params.get('access_token')
f = urllib2.urlopen(url)
validation = f.read()
print validation
r = (cr.dbname, login, password)
try:
# check for existing user
if not self.auth_signup_check(cr, uid, login, password):
print "NEW USER"
# new user
new_user = {
'name': name,
'login': login,
'user_email': login,
'password': password,
'active': True,
}
self.auth_signup_create(cr,uid, new_user)
return r
else:
print "Existing same"
# already existing with same password
return r
except openerp.exceptions.AccessDenied:
print "Existing different"
# already existing with diffrent password
raise
#

View File

@ -1,5 +1,4 @@
openerp.auth_oauth = function(instance) {
var QWeb = instance.web.qweb;
instance.web.Login = instance.web.Login.extend({
@ -8,56 +7,37 @@ openerp.auth_oauth = function(instance) {
var self = this;
self.$element.on('click', '.oe_login_oauth a', this.on_google_oauth);
},
on_google_oauth: function(ev) {
var url = this._oauth_url();
window.location = url;
},
_oauth_url: function() {
oauth_url: function(state) {
var endpoint = 'https://accounts.google.com/o/oauth2/auth';
var params = {
var params = {
response_type: 'token',
client_id: '108010644258-duuhmp6pu7li4tsmnqg7j9rvdeklg0ki.apps.googleusercontent.com',
redirect_uri: 'https://localhost/',
scope: 'https://www.googleapis.com/auth/userinfo.email',
state: 'TEST',
state: state,
};
var url = endpoint + '?' + $.param(params);
return url;
},
// do_warn: function(title, msg) {
// },
// reset_error_message: function() {
// }
on_google_oauth: function(ev) {
var dbname = self.$("form [name=db]").val();
var url = this.oauth_url(dbname);
window.location = url;
},
});
instance.web.WebClient = instance.web.WebClient.extend({
start: function() {
this._super.apply(this, arguments);
// console.log($.deparam(window.location.hash));
var params = $.deparam(window.location.hash);
if (params.hasOwnProperty('access_token')) {
console.log(params);
// Do login using Google User credentials
var url = {
};
// fix params for python marshmalling
params.state = params["#state"]
delete params["#state"]
var url = "/auth_oauth/signin" + '?' + $.param(params);
window.location = url;
}
},
bind_hashchange: function() {
var state = $.bbq.getState(true);
if (state.hasOwnProperty("access_token")) {
state.action = "login";
$.bbq.setState(state);
}
this._super();
},
// on_hashchange: function(event) {
// console.log(event);
// this._super.apply(this, arguments);
// },
});
};
// https://accounts.google.com/o/oauth2/auth?state=%2Fprofile&redirect_uri=http%3A%2F%2Foauth2-login-demo.appspot.com%2Fcode&response_type=code&client_id=812741506391.apps.googleusercontent.com&approval_prompt=force&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile