diff options
| author | Quentin (OpenERP) <qdp-launchpad@openerp.com> | 2012-11-28 12:40:02 +0100 |
|---|---|---|
| committer | Quentin (OpenERP) <qdp-launchpad@openerp.com> | 2012-11-28 12:40:02 +0100 |
| commit | e037bc48e9bae5d4bb6e0ddf736c3260bebb5f20 (patch) | |
| tree | 1c11b4752558e76a76c1947943c33c3384005287 /addons/auth_oauth | |
| parent | 21baf8dfac5fd73c32748f15167a3c863e477a13 (diff) | |
| parent | cb32b7e76fbf59049654cbff7ad87c3bc5a881cb (diff) | |
[MERGE] trunk
bzr revid: qdp-launchpad@openerp.com-20121128114002-yk9zk24rk79v4pkd
Diffstat (limited to 'addons/auth_oauth')
| -rw-r--r-- | addons/auth_oauth/auth_oauth_data.xml | 17 | ||||
| -rw-r--r-- | addons/auth_oauth/res_users.py | 59 | ||||
| -rw-r--r-- | addons/auth_oauth/static/src/css/auth_oauth.css | 1 | ||||
| -rw-r--r-- | addons/auth_oauth/static/src/js/auth_oauth.js | 34 |
4 files changed, 56 insertions, 55 deletions
diff --git a/addons/auth_oauth/auth_oauth_data.xml b/addons/auth_oauth/auth_oauth_data.xml index c07359a062c..983549f9378 100644 --- a/addons/auth_oauth/auth_oauth_data.xml +++ b/addons/auth_oauth/auth_oauth_data.xml @@ -1,15 +1,15 @@ <?xml version="1.0"?> <openerp> <data noupdate="1"> - <record id="provider_openerp" model="auth.oauth.provider"> - <field name="name">OpenERP Accounts</field> + <field name="name">OpenERP.com Accounts</field> <field name="auth_endpoint">https://accounts.openerp.com/oauth2/auth</field> <field name="scope">userinfo</field> <field name="validation_endpoint">https://accounts.openerp.com/oauth2/tokeninfo</field> <field name="data_endpoint"></field> <field name="css_class">zocial openerp</field> - <field name="body">Sign in with OpenERP account</field> + <field name="body">Log in with OpenERP.com</field> + <field name="enabled" eval="True"/> </record> <record id="provider_facebook" model="auth.oauth.provider"> <field name="name">Facebook Graph</field> @@ -18,7 +18,7 @@ <field name="validation_endpoint">https://graph.facebook.com/me/permissions</field> <field name="data_endpoint"></field> <field name="css_class">zocial facebook</field> - <field name="body">Sign in with facebook</field> + <field name="body">Log in with facebook</field> </record> <record id="provider_google" model="auth.oauth.provider"> <field name="name">Google OAuth2</field> @@ -27,9 +27,10 @@ <field name="validation_endpoint">https://www.googleapis.com/oauth2/v1/tokeninfo</field> <field name="data_endpoint">https://www.googleapis.com/oauth2/v1/userinfo</field> <field name="css_class">zocial google</field> - <field name="body">Sign in with google</field> + <field name="body">Log in with google</field> </record> - <!-- <record id="provider_twitter" model="auth.oauth.provider"> + <!-- + <record id="provider_twitter" model="auth.oauth.provider"> <field name="name">Twitter OAuth</field> <field name="auth_endpoint">https://api.twitter.com/oauth/request_token</field> <field name="scope"></field> @@ -37,7 +38,7 @@ <field name="data_endpoint"></field> <field name="css_class">zocial twitter</field> <field name="body">Sign in with twitter</field> - </record> --> - + </record> + --> </data> </openerp> diff --git a/addons/auth_oauth/res_users.py b/addons/auth_oauth/res_users.py index 7e7b3753a4e..b1c7e214f12 100644 --- a/addons/auth_oauth/res_users.py +++ b/addons/auth_oauth/res_users.py @@ -45,6 +45,25 @@ class res_users(osv.Model): validation.update(data) return validation + def _auth_oauth_signin(self, cr, uid, provider, validation, params, context=None): + """ retrieve and sign in the user corresponding to provider and validated access token + :param provider: oauth provider id (int) + :param validation: result of validation of access token (dict) + :param params: oauth parameters (dict) + :return: user login (str) + :raise: openerp.exceptions.AccessDenied if signin failed + + This method can be overridden to add alternative signin methods. + """ + oauth_uid = validation['user_id'] + user_ids = self.search(cr, uid, [("oauth_uid", "=", oauth_uid), ('oauth_provider_id', '=', provider)]) + if not user_ids: + raise openerp.exceptions.AccessDenied() + assert len(user_ids) == 1 + user = self.browse(cr, uid, user_ids[0], context=context) + user.write({'oauth_access_token': params['access_token']}) + return user.login + def auth_oauth(self, cr, uid, provider, params, context=None): # Advice by Google (to avoid Confused Deputy Problem) # if validation.audience != OUR_CLIENT_ID: @@ -53,39 +72,15 @@ class res_users(osv.Model): # continue with the process access_token = params.get('access_token') validation = self._auth_oauth_validate(cr, uid, provider, access_token) - # required - oauth_uid = validation['user_id'] - if not oauth_uid: + # required check + if not validation.get('user_id'): raise openerp.exceptions.AccessDenied() - 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 - 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'): - raise openerp.exceptions.AccessDenied() - - new_user = { - 'name': name, - 'login': login, - 'user_email': email, - 'oauth_provider_id': provider, - 'oauth_uid': oauth_uid, - 'oauth_access_token': access_token, - 'active': True, - } - # TODO pass signup token to allow attach new user to right partner - self._signup_create_user(cr, uid, new_user) - - credentials = (cr.dbname, login, access_token) - return credentials + # retrieve and sign in user + login = self._auth_oauth_signin(cr, uid, provider, validation, params, context=context) + if not login: + raise openerp.exceptions.AccessDenied() + # return user credentials + return (cr.dbname, login, access_token) def check_credentials(self, cr, uid, password): try: diff --git a/addons/auth_oauth/static/src/css/auth_oauth.css b/addons/auth_oauth/static/src/css/auth_oauth.css index ed6130e3123..0489229042a 100644 --- a/addons/auth_oauth/static/src/css/auth_oauth.css +++ b/addons/auth_oauth/static/src/css/auth_oauth.css @@ -9,7 +9,6 @@ } .openerp a.zocial.openerp { - float: right; border: 1px solid #222222; color: white; margin: 0; diff --git a/addons/auth_oauth/static/src/js/auth_oauth.js b/addons/auth_oauth/static/src/js/auth_oauth.js index 2aa66877d42..c24dab3d55a 100644 --- a/addons/auth_oauth/static/src/js/auth_oauth.js +++ b/addons/auth_oauth/static/src/js/auth_oauth.js @@ -35,27 +35,33 @@ openerp.auth_oauth = function(instance) { on_oauth_sign_in: function(ev) { ev.preventDefault(); var index = $(ev.target).data('index'); - var p = this.oauth_providers[index]; - var ret = _.str.sprintf('%s//%s/auth_oauth/signin', location.protocol, location.host); + var provider = this.oauth_providers[index]; + return this.do_oauth_sign_in(provider); + }, + do_oauth_sign_in: function(provider) { + var return_url = _.str.sprintf('%s//%s/auth_oauth/signin', location.protocol, location.host); if (instance.session.debug) { - ret += '?debug'; + return_url += '?debug'; } - var dbname = self.$("form [name=db]").val(); - var state_object = { - d: dbname, - p: p.id - }; - var state = JSON.stringify(state_object); + var state = this._oauth_state(provider); var params = { response_type: 'token', - client_id: p.client_id, - redirect_uri: ret, - scope: p.scope, - state: state, + client_id: provider.client_id, + redirect_uri: return_url, + scope: provider.scope, + state: JSON.stringify(state), }; - var url = p.auth_endpoint + '?' + $.param(params); + var url = provider.auth_endpoint + '?' + $.param(params); window.location = url; }, + _oauth_state: function(provider) { + // return the state object sent back with the redirected uri + var dbname = this.$("form [name=db]").val(); + return { + d: dbname, + p: provider.id, + }; + }, }); }; |
