2012-08-10 11:16:16 +00:00
openerp . auth _oauth = function ( instance ) {
2013-08-09 14:55:06 +00:00
var _t = instance . web . _t ,
_lt = instance . web . _lt ;
2012-08-10 11:16:16 +00:00
var QWeb = instance . web . qweb ;
2014-01-14 09:08:19 +00:00
/ * T O D O : m a k e t h i s a s e r v e r s i d e c o n t r o l l e r
2012-09-27 12:34:29 +00:00
instance . web . Login . include ( {
2012-08-12 15:07:39 +00:00
start : function ( parent , params ) {
2012-09-27 12:34:29 +00:00
var self = this ;
2012-08-15 18:09:13 +00:00
var d = this . _super . apply ( this , arguments ) ;
2013-03-28 08:47:28 +00:00
this . $el . hide ( ) ;
2012-08-24 18:27:43 +00:00
this . $el . on ( 'click' , 'a.zocial' , this . on _oauth _sign _in ) ;
2012-08-15 18:09:13 +00:00
this . oauth _providers = [ ] ;
if ( this . params . oauth _error === 1 ) {
2013-08-09 14:55:06 +00:00
this . do _warn ( _t ( "Sign up error" ) , _t ( "Sign up is not allowed on this database." ) , true ) ;
2012-08-15 18:09:13 +00:00
} else if ( this . params . oauth _error === 2 ) {
2013-08-09 14:55:06 +00:00
this . do _warn ( _t ( "Authentication error" ) , _t ( "Access Denied" ) , true ) ;
} else if ( this . params . oauth _error === 3 ) {
this . do _warn ( _t ( "Authentication error" ) , _t ( "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." ) , true ) ;
2012-08-15 18:09:13 +00:00
}
2012-10-31 10:31:48 +00:00
return d . done ( this . do _oauth _load ) . fail ( function ( ) {
2012-09-27 12:34:29 +00:00
self . do _oauth _load ( [ ] ) ;
} ) ;
} ,
on _db _loaded : function ( result ) {
this . _super . apply ( this , arguments ) ;
this . $ ( "form [name=db]" ) . change ( this . do _oauth _load ) ;
2012-08-15 18:09:13 +00:00
} ,
do _oauth _load : function ( ) {
var db = this . $ ( "form [name=db]" ) . val ( ) ;
if ( db ) {
2012-10-31 10:31:48 +00:00
this . rpc ( "/auth_oauth/list_providers" , { dbname : db } ) . done ( this . on _oauth _loaded ) ;
2013-03-28 08:47:28 +00:00
} else {
this . $el . show ( ) ;
2012-08-15 18:09:13 +00:00
}
} ,
on _oauth _loaded : function ( result ) {
this . oauth _providers = result ;
2012-12-18 15:32:55 +00:00
var params = $ . deparam ( $ . param . querystring ( ) ) ;
if ( this . oauth _providers . length === 1 && params . type === 'signup' ) {
this . do _oauth _sign _in ( this . oauth _providers [ 0 ] ) ;
} else {
2013-03-28 08:47:28 +00:00
this . $el . show ( ) ;
2012-12-18 15:32:55 +00:00
this . $ ( '.oe_oauth_provider_login_button' ) . remove ( ) ;
var buttons = QWeb . render ( "auth_oauth.Login.button" , { "widget" : this } ) ;
this . $ ( ".oe_login_pane form ul" ) . after ( buttons ) ;
}
2012-08-10 11:16:16 +00:00
} ,
2012-08-15 18:09:13 +00:00
on _oauth _sign _in : function ( ev ) {
2012-11-28 09:36:17 +00:00
ev . preventDefault ( ) ;
2012-08-15 18:09:13 +00:00
var index = $ ( ev . target ) . data ( 'index' ) ;
2012-11-26 08:53:34 +00:00
var provider = this . oauth _providers [ index ] ;
2012-11-28 10:53:42 +00:00
return this . do _oauth _sign _in ( provider ) ;
} ,
do _oauth _sign _in : function ( provider ) {
2012-11-26 08:53:34 +00:00
var return _url = _ . str . sprintf ( '%s//%s/auth_oauth/signin' , location . protocol , location . host ) ;
2012-11-16 16:24:21 +00:00
if ( instance . session . debug ) {
2012-11-26 08:53:34 +00:00
return _url += '?debug' ;
2012-11-16 16:24:21 +00:00
}
2012-11-27 15:35:32 +00:00
var state = this . _oauth _state ( provider ) ;
2012-08-12 23:26:12 +00:00
var params = {
2012-08-12 15:07:39 +00:00
response _type : 'token' ,
2012-11-26 08:53:34 +00:00
client _id : provider . client _id ,
redirect _uri : return _url ,
scope : provider . scope ,
state : JSON . stringify ( state ) ,
2012-08-12 15:07:39 +00:00
} ;
2012-11-26 08:53:34 +00:00
var url = provider . auth _endpoint + '?' + $ . param ( params ) ;
2013-03-28 08:47:28 +00:00
instance . web . redirect ( url ) ;
2012-08-12 23:26:12 +00:00
} ,
2012-11-26 08:53:34 +00:00
_oauth _state : function ( provider ) {
// return the state object sent back with the redirected uri
2012-11-27 15:30:40 +00:00
var dbname = this . $ ( "form [name=db]" ) . val ( ) ;
2012-11-26 08:53:34 +00:00
return {
d : dbname ,
p : provider . id ,
} ;
} ,
2012-08-10 11:16:16 +00:00
} ) ;
2014-01-14 09:08:19 +00:00
* /
2012-08-12 15:07:39 +00:00
} ;