OSDN Git Service

Version 0.6.168, fix X.UI.Repeater.
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 10_XOAuth2.js
index c4f92b2..91f7f67 100644 (file)
@@ -28,7 +28,7 @@ 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
@@ -54,7 +54,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 );
                                
@@ -62,7 +63,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;
@@ -114,7 +115,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',
@@ -147,8 +148,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;
@@ -160,7 +167,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;
                                
@@ -177,7 +191,7 @@ 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'  : {
@@ -226,7 +240,11 @@ function X_Net_OAuth2_detectAuthPopup(){
                pair      = X_Pair_get( this );
                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 );
@@ -239,7 +257,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;
        };
 };
 
@@ -286,7 +304,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 );
@@ -298,8 +316,8 @@ 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;                           
+                               //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.' } );