OSDN Git Service

rename files & fix X.EventDispatcher.
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 10_XOAuth2.js
index 3564a13..a3967ff 100644 (file)
@@ -29,6 +29,7 @@ oauth2 = X.OAuth2({
        'tokenEndpoint'     : 'https://accounts.google.com/o/oauth2/token',
        'redirectURI'       : X.URL.cleanup( document.location.href ), // 専用の軽量ページを用意してもよいが、現在のアドレスでも可能
        'scopes'            : [ 'https://www.googleapis.com/auth/blogger' ],
+       'refreshMargin'     : 300000,
        'authorizeWindowWidth'  : 500,
        'authorizeWindowHeight' : 500
 }).listen( [ X.Event.NEED_AUTH, X.Event.CANCELED, X.Event.SUCCESS, X.Event.ERROR, X.Event.PROGRESS ], updateOAuth2State );
@@ -58,7 +59,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
                                obj.onAuthError   = X_NET_OAUTH2_onXHR401Error;
                                obj.updateRequest = X_NET_OAUTH2_updateRequest;                         
                                
-                               if( _getAccessToken( this ) && ( expires_at = _getAccessTokenExpiry( this ) ) ){
+                               if( X_OAuth2_getAccessToken( this ) && ( expires_at = X_OAuth2_getAccessTokenExpiry( this ) ) ){
                                        if( expires_at < X_Timer_now() + ( obj[ 'refreshMargin' ] || 300000 ) ){ // 寿命が5分を切った
                                                this[ 'refreshToken' ]();
                                        } else {
@@ -174,7 +175,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
                                                'client_id'     : pair[ 'clientID' ],
                                                'client_secret' : pair[ 'clientSecret' ],
                                                'grant_type'    : 'refresh_token',
-                                               'refresh_token' : _getRefreshToken( this )
+                                               'refresh_token' : X_OAuth2_getRefreshToken( this )
                                        }),
                                        'dataType' : 'json',
                                        'headers'  : {
@@ -203,9 +204,9 @@ function X_NET_OAUTH2_handleEvent( e ){
                        
                case X_EVENT_SUCCESS :
                        pair.refreshTimerID && X_Timer_remove( pair.refreshTimerID );
-                       if( _getRefreshToken( this ) ){
+                       if( X_OAuth2_getRefreshToken( this ) ){
                                // 自動リフレッシュ
-                               pair.refreshTimerID = X_Timer_once( _getAccessTokenExpiry( this ) - X_Timer_now() - pair[ 'refreshMargin' ], this, this[ 'refreshToken' ] );
+                               pair.refreshTimerID = X_Timer_once( X_OAuth2_getAccessTokenExpiry( this ) - X_Timer_now() - pair[ 'refreshMargin' ], this, this[ 'refreshToken' ] );
                        };
        };
 };
@@ -269,7 +270,7 @@ function X_Net_OAuth2_responceHandler( e ){
        switch( e.type ){
                case X_EVENT_SUCCESS :
                        if( isRefresh && data.error ){
-                               _removeRefreshToken( this );
+                               X_OAuth2_removeRefreshToken( this );
                                pair.oauth2State = 0;
                                this[ 'asyncDispatch' ]( { type : X_EVENT_ERROR, message : 'Refresh access token error.' } );
                                this[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
@@ -282,14 +283,14 @@ function X_Net_OAuth2_responceHandler( e ){
                                return;
                        };
                        
-                       _setAccessToken( this, data[ 'access_token' ] || '' );
-                       _setRefreshToken( this, data[ 'refresh_token' ] || '' );
+                       X_OAuth2_setAccessToken( this, data[ 'access_token' ] || '' );
+                       X_OAuth2_setRefreshToken( this, data[ 'refresh_token' ] || '' );
                        
                        if( data[ 'expires_in' ] ){
-                               _setAccessTokenExpiry( this, X_Timer_now() + data[ 'expires_in' ] * 1000 );
+                               X_OAuth2_setAccessTokenExpiry( this, X_Timer_now() + data[ 'expires_in' ] * 1000 );
                        } else
-                       if( _getAccessTokenExpiry( this ) ){
-                               _removeAccessTokenExpiry( this );
+                       if( X_OAuth2_getAccessTokenExpiry( this ) ){
+                               X_OAuth2_removeAccessTokenExpiry( this );
                        };
                        
                        pair.oauth2State = 4;
@@ -301,15 +302,15 @@ function X_Net_OAuth2_responceHandler( e ){
                                // other error, not auth
                                pair.oauth2State = 0;
                                this[ 'asyncDispatch' ]( { type : X_EVENT_ERROR, message : 'Refresh access token error.' } );
-                               _removeRefreshToken( this );
+                               X_OAuth2_removeRefreshToken( this );
                                this[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
                        } else
-                       if( _getAuthMechanism( this ) === 'param' ){
+                       if( X_OAuth2_getAuthMechanism( this ) === 'param' ){
                                pair.oauth2State = 0;
                                this[ 'asyncDispatch' ]( { type : X_EVENT_ERROR, message : 'network-error' } );
                        } else {
                                pair.oauth2State = 0;
-                               _setAuthMechanism( this, 'param' );
+                               X_OAuth2_setAuthMechanism( this, 'param' );
                                this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Refresh access token failed. retry header -> param. ' } );
                                // retry
                                X_Net_OAuth2_authorizationCode( this, pair );
@@ -323,7 +324,7 @@ function X_NET_OAUTH2_onXHR401Error( oauth2, e ){
                headers = e[ 'headers' ],
                xhr, bearerParams, headersExposed = false;
        
-       if( _getAuthMechanism( oauth2 ) !== 'param' ){
+       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
                bearerParams   = headersExposed && ( headers[ 'WWW-Authenticate' ] || headers[ 'www-authenticate' ] );
@@ -335,21 +336,21 @@ function X_NET_OAUTH2_onXHR401Error( oauth2, e ){
                pair.oauth2State = 0;
                oauth2[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
        } else
-       if ((( bearerParams && bearerParams.indexOf( 'invalid_token' ) !== -1 ) || !headersExposed) && _getRefreshToken( oauth2 ) ) {
-               _removeAccessToken( oauth2 ); // It doesn't work any more.
+       if ((( bearerParams && bearerParams.indexOf( 'invalid_token' ) !== -1 ) || !headersExposed) && X_OAuth2_getRefreshToken( oauth2 ) ) {
+               X_OAuth2_removeAccessToken( oauth2 ); // It doesn't work any more.
                pair.oauth2State = 3;
                oauth2[ 'refreshToken' ]();
        } else {
-       //if (!headersExposed && !_getRefreshToken( oauth2 )) {
-               _removeAccessToken( oauth2 ); // It doesn't work any more.
+       //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 );
        };
 };
 
 function X_NET_OAUTH2_updateRequest( oauth2, request ){
-       var token     = _getAccessToken( oauth2 ),
-               mechanism = _getAuthMechanism( oauth2 ),
+       var token     = X_OAuth2_getAccessToken( oauth2 ),
+               mechanism = X_OAuth2_getAuthMechanism( oauth2 ),
                url       = request[ 'url' ],
                headers;
 
@@ -363,25 +364,25 @@ function X_NET_OAUTH2_updateRequest( oauth2, request ){
        };
 };
 
-function _getAccessToken( that ){ return updateLocalStorage( '', that, 'accessToken' ); }
-function _getRefreshToken( that){ return updateLocalStorage( '', that, 'refreshToken' ); }
-function _getAccessTokenExpiry( that ){ return parseInt( updateLocalStorage( '', that, 'tokenExpiry' ) ) || 0; }
-function _getAuthMechanism( that ){
+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_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' : updateLocalStorage( '', that, 'AuthMechanism' );
+               return X_Net_XHR_createXDR ? 'param' : X_OAuth2_updateLocalStorage( '', that, 'AuthMechanism' );
        }
-function _setAccessToken( that, value ){ updateLocalStorage( '+', that, 'accessToken' , value); }
-function _setRefreshToken( that, value ){ updateLocalStorage( '+', that, 'refreshToken', value); }
-function _setAccessTokenExpiry( that, value ){ updateLocalStorage( '+', that, 'tokenExpiry', value); }
-function _setAuthMechanism( that, value ){ updateLocalStorage( '+', that, 'AuthMechanism', value); }
+function X_OAuth2_setAccessToken( that, value ){ X_OAuth2_updateLocalStorage( '+', that, 'accessToken' , value); }
+function X_OAuth2_setRefreshToken( that, value ){ X_OAuth2_updateLocalStorage( '+', that, 'refreshToken', value); }
+function X_OAuth2_setAccessTokenExpiry( that, value ){ X_OAuth2_updateLocalStorage( '+', that, 'tokenExpiry', value); }
+function X_OAuth2_setAuthMechanism( that, value ){ X_OAuth2_updateLocalStorage( '+', that, 'AuthMechanism', value); }
 
-function _removeAccessToken( that ){ updateLocalStorage( '-', that, 'accessToken' ); }
-function _removeRefreshToken( that ){ updateLocalStorage( '-', that, 'refreshToken' ); }
-function _removeAccessTokenExpiry( that ){ updateLocalStorage( '-', that, 'tokenExpiry' ); }
-function _removeAuthMechanism( that ){ updateLocalStorage( '-', that, 'AuthMechanism' ); }
+function X_OAuth2_removeAccessToken( that ){ X_OAuth2_updateLocalStorage( '-', that, 'accessToken' ); }
+function X_OAuth2_removeRefreshToken( that ){ X_OAuth2_updateLocalStorage( '-', that, 'refreshToken' ); }
+function X_OAuth2_removeAccessTokenExpiry( that ){ X_OAuth2_updateLocalStorage( '-', that, 'tokenExpiry' ); }
+function X_OAuth2_removeAuthMechanism( that ){ X_OAuth2_updateLocalStorage( '-', that, 'AuthMechanism' ); }
        
-function updateLocalStorage( cmd, that, name, value ){
+function X_OAuth2_updateLocalStorage( cmd, that, name, value ){
        var action = cmd === '+' ? 'setItem' : cmd === '-' ? 'removeItem' : 'getItem',
                pair;