OSDN Git Service

Version 0.6.176, add X.Script.
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 10_XOAuth2.js
index a3967ff..6d28665 100644 (file)
@@ -1,7 +1,6 @@
 
 //{+oauth2"OAuth2 サービスの定義"(OAuth2外部サービスを定義し、認可プロセス・xhrの署名を自動化します)[+xhr]
-var X_NET_OAUTH2_detection      = new Function( 'w', 'try{return w.location.search}catch(e){}' ),
-       X_NET_OAUTH2_authorizationWindow,
+var X_NET_OAUTH2_authorizationWindow,
        X_NET_OAUTH2_authorizationTimerID;
 
 /**
@@ -16,6 +15,7 @@ var X_NET_OAUTH2_detection      = new Function( 'w', 'try{return w.location.sear
  * 
  * original :
  *  oauth2.js , <opendata@oucs.ox.ac.uk>
+ *  https://github.com/ox-it/javascript-oauth2/blob/master/oauth2/oauth2.js
  * 
  * @alias X.OAuth2
  * @class OAuth2 サービスを定義し接続状況をモニタする。適宜にトークンのアップデートなどを行う
@@ -27,9 +27,10 @@ oauth2 = X.OAuth2({
        'clientSecret'      : 'xxxxxxxx',
        'authorizeEndpoint' : 'https://accounts.google.com/o/oauth2/auth',
        'tokenEndpoint'     : 'https://accounts.google.com/o/oauth2/token',
-       'redirectURI'       : X.URL.cleanup( document.location.href ), // 専用の軽量ページを用意してもよいが、現在のアドレスでも可能
+       'redirectURI'       : X.URL.cleanup( document.location.href ), // 専用の軽量ページを用意してもよいが、現在のアドレスでも可能, gif は?
        'scopes'            : [ 'https://www.googleapis.com/auth/blogger' ],
        'refreshMargin'     : 300000,
+       // canuse
        'authorizeWindowWidth'  : 500,
        'authorizeWindowHeight' : 500
 }).listen( [ X.Event.NEED_AUTH, X.Event.CANCELED, X.Event.SUCCESS, X.Event.ERROR, X.Event.PROGRESS ], updateOAuth2State );
@@ -52,7 +53,8 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
                        'Constructor' : function( obj ){
                                var expires_at;
                                
-                               obj = X_Object_clone( obj );
+                               obj = X_Object_copy( obj );
+                               obj[ 'refreshMargin' ] = obj[ 'refreshMargin' ] || 300000;
                                
                                X_Pair_create( this, obj );
                                
@@ -60,7 +62,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
                                obj.updateRequest = X_NET_OAUTH2_updateRequest;                         
                                
                                if( X_OAuth2_getAccessToken( this ) && ( expires_at = X_OAuth2_getAccessTokenExpiry( this ) ) ){
-                                       if( expires_at < X_Timer_now() + ( obj[ 'refreshMargin' ] || 300000 ) ){ // 寿命が5分を切った
+                                       if( expires_at < X_Timer_now() + obj[ 'refreshMargin' ] ){ // 寿命が5分を切った
                                                this[ 'refreshToken' ]();
                                        } else {
                                                obj.oauth2State = 4;
@@ -112,7 +114,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
                                                        'response_type' : 'code',
                                                        'client_id'     : pair[ 'clientID' ],
                                                        'redirect_uri'  : pair[ 'redirectURI' ],
-                                                       'scope'         : ( pair[ 'scopes' ] || []).join(' ')
+                                                       'scope'         : ( pair[ 'scopes' ] || [] ).join(' ')
                                                }
                                        ),
                                        'oauthauthorize',
@@ -145,8 +147,14 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
                                };
                                
                                // http://kojikoji75.hatenablog.com/entry/2013/12/15/223839
-                               X_NET_OAUTH2_authorizationWindow && X_NET_OAUTH2_authorizationWindow.open( 'about:blank', '_self' ).close();
-                               X_NET_OAUTH2_authorizationWindow  = null;
+                               if( X_NET_OAUTH2_authorizationWindow ){
+                                       if( 9 < X_UA[ 'IEHost' ] ){
+                                               X_NET_OAUTH2_authorizationWindow.close();
+                                       } else {
+                                               X_NET_OAUTH2_authorizationWindow.open( 'about:blank', '_self' ).close();
+                                       };
+                                       X_NET_OAUTH2_authorizationWindow  = null;                                       
+                               };
                                
                                X_NET_OAUTH2_authorizationTimerID && X_Timer_remove( X_NET_OAUTH2_authorizationTimerID );
                                X_NET_OAUTH2_authorizationTimerID = 0;
@@ -158,7 +166,14 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
                         * アクセストークンのリフレッシュ。
                         */
                        'refreshToken' : function(){
-                               var pair = X_Pair_get( this );
+                               var pair = X_Pair_get( this ),
+                                       refreshToken = X_OAuth2_getRefreshToken( this );
+                               
+                               if( !refreshToken ){
+                                       pair.oauth2State = 0;
+                                       this[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
+                                       return;
+                               };
                                
                                if( pair.net ) return;
                                
@@ -175,14 +190,14 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
                                                'client_id'     : pair[ 'clientID' ],
                                                'client_secret' : pair[ 'clientSecret' ],
                                                'grant_type'    : 'refresh_token',
-                                               'refresh_token' : X_OAuth2_getRefreshToken( this )
+                                               'refresh_token' : refreshToken
                                        }),
                                        'dataType' : 'json',
                                        'headers'  : {
                                                                        'Accept'       : 'application/json',
                                                                        'Content-Type' : 'application/x-www-form-urlencoded'
                                                                },
-                                       'test'     : 'gadget'
+                                       'test'     : 'gadget' // canuse
                                } ).listenOnce( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], this, X_Net_OAuth2_responceHandler );
                                
                                this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Start to refresh token.' } );
@@ -220,11 +235,15 @@ function X_Net_OAuth2_detectAuthPopup(){
 
                this[ 'asyncDispatch' ]( X_EVENT_CANCELED );
        } else
-       if( search = X_NET_OAUTH2_detection( X_NET_OAUTH2_authorizationWindow ) ){
+       if( search = X_Script_try( X_Object_find, [ X_NET_OAUTH2_authorizationWindow, 'location>search' ] ) ){
                pair      = X_Pair_get( this );
-               pair.code = X_URL_ParamToObj( search.slice( 1 ) )[ 'code' ];
+               pair.code = X_URL_paramToObj( search.slice( 1 ) )[ 'code' ];
 
-               X_NET_OAUTH2_authorizationWindow.open( 'about:blank', '_self' ).close();
+               if( 9 < X_UA[ 'IEHost' ] ){
+                       X_NET_OAUTH2_authorizationWindow.close();
+               } else {
+                       X_NET_OAUTH2_authorizationWindow.open( 'about:blank', '_self' ).close();
+               };
                closed = true;
 
                X_Net_OAuth2_authorizationCode( this, pair );
@@ -237,7 +256,7 @@ function X_Net_OAuth2_detectAuthPopup(){
                X_NET_OAUTH2_authorizationWindow  = null;
                X_NET_OAUTH2_authorizationTimerID = 0;
                
-               return X_Callback_UN_LISTEN;    
+               return X_CALLBACK_UN_LISTEN;
        };
 };
 
@@ -284,7 +303,7 @@ function X_Net_OAuth2_responceHandler( e ){
                        };
                        
                        X_OAuth2_setAccessToken( this, data[ 'access_token' ] || '' );
-                       X_OAuth2_setRefreshToken( this, data[ 'refresh_token' ] || '' );
+                       ( !isRefresh || data[ 'refresh_token' ] ) && X_OAuth2_setRefreshToken( this, data[ 'refresh_token' ] || '' );
                        
                        if( data[ 'expires_in' ] ){
                                X_OAuth2_setAccessTokenExpiry( this, X_Timer_now() + data[ 'expires_in' ] * 1000 );
@@ -294,6 +313,12 @@ function X_Net_OAuth2_responceHandler( e ){
                        };
                        
                        pair.oauth2State = 4;
+                       
+                       if( pair.lazyRequests && pair.lazyRequests.length ){
+                               //X_NET_QUEUE_LIST.push.apply( X_NET_QUEUE_LIST, pair.lazyRequests );
+                               //pair.lazyRequests.length = 0;                         
+                       };
+
                        this[ 'asyncDispatch' ]( { type : X_EVENT_SUCCESS, message : isRefresh ? 'Refresh access token success.' : 'Get new access token success.' } );
                        break;
                        
@@ -322,17 +347,16 @@ function X_Net_OAuth2_responceHandler( e ){
 function X_NET_OAUTH2_onXHR401Error( oauth2, e ){
        var pair = this,
                headers = e[ 'headers' ],
-               xhr, bearerParams, headersExposed = false;
+               bearerParams, headersExposed = false;
        
        if( X_OAuth2_getAuthMechanism( oauth2 ) !== 'param' ){
-               xhr            = X_NET_currentWrapper[ '_rawObject' ];
-               headersExposed = !X_Net_XHR_createXDR || !!headers; // this is a hack for Firefox and IE
+               headersExposed = !X_NET_currentWrapper.isXDR || !!headers; // this is a hack for Firefox and IE
                bearerParams   = headersExposed && ( headers[ 'WWW-Authenticate' ] || headers[ 'www-authenticate' ] );
                X_Type_isArray( bearerParams ) && ( bearerParams = bearerParams.join( '\n' ) );
        };
        
        // http://d.hatena.ne.jp/ritou/20110402/1301679908
-       if ( bearerParams && bearerParams.indexOf( ' error=' ) === -1 ) {
+       if ( bearerParams && bearerParams.indexOf( ' error=' ) === -1 ) { // bearerParams.error == undefined
                pair.oauth2State = 0;
                oauth2[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
        } else
@@ -341,7 +365,6 @@ function X_NET_OAUTH2_onXHR401Error( oauth2, e ){
                pair.oauth2State = 3;
                oauth2[ 'refreshToken' ]();
        } else {
-       //if (!headersExposed && !X_OAuth2_getRefreshToken( oauth2 )) {
                X_OAuth2_removeAccessToken( oauth2 ); // It doesn't work any more.
                pair.oauth2State = 0;
                oauth2[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
@@ -365,12 +388,12 @@ function X_NET_OAUTH2_updateRequest( oauth2, request ){
 };
 
 function X_OAuth2_getAccessToken( that ){ return X_OAuth2_updateLocalStorage( '', that, 'accessToken' ); }
-function X_OAuth2_getRefreshToken( that){ return X_OAuth2_updateLocalStorage( '', that, 'refreshToken' ); }
+function X_OAuth2_getRefreshToken( that ){ return X_OAuth2_updateLocalStorage( '', that, 'refreshToken' ); }
 function X_OAuth2_getAccessTokenExpiry( that ){ return parseInt( X_OAuth2_updateLocalStorage( '', that, 'tokenExpiry' ) ) || 0; }
 function X_OAuth2_getAuthMechanism( that ){
                // TODO use gadget | flash ...
                // IE's XDomainRequest doesn't support sending headers, so don't try.
-               return X_Net_XHR_createXDR ? 'param' : X_OAuth2_updateLocalStorage( '', that, 'AuthMechanism' );
+               return ( X_NET_currentWrapper === X_XHR ) && X_XHR_createXDR ? 'param' : X_OAuth2_updateLocalStorage( '', that, 'AuthMechanism' );
        }
 function X_OAuth2_setAccessToken( that, value ){ X_OAuth2_updateLocalStorage( '+', that, 'accessToken' , value); }
 function X_OAuth2_setRefreshToken( that, value ){ X_OAuth2_updateLocalStorage( '+', that, 'refreshToken', value); }