X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=0.6.x%2Fjs%2F06_net%2F10_XOAuth2.js;h=5f23c6ced529fad16f6574e1d0cef31203a1e2cb;hb=9e607206c5510435fc1daeac93a19427ce33cc6f;hp=063cb4ab3266d35b64b85542acd8ef3c6a305fb4;hpb=a720658bd6302a1bd5efae47333b6df793370f3d;p=pettanr%2FclientJs.git
diff --git a/0.6.x/js/06_net/10_XOAuth2.js b/0.6.x/js/06_net/10_XOAuth2.js
index 063cb4a..5f23c6c 100644
--- a/0.6.x/js/06_net/10_XOAuth2.js
+++ b/0.6.x/js/06_net/10_XOAuth2.js
@@ -1,12 +1,12 @@
-//{+oauth2"OAuth2 ãµã¼ãã¹ã®å®ç¾©"(OAuth2å¤é¨ãµã¼ãã¹ãå®ç¾©ããèªå¯ããã»ã¹ã»xhrã®ç½²åãèªååãã¾ã)[+xhr]
-var X_NET_OAUTH2_authorizationWindow,
- X_NET_OAUTH2_authorizationTimerID;
+//{+oauth2"OAuth2 ãµã¼ãã¹ã®å®ç¾©"(OAuth2å¤é¨ãµã¼ãã¹ãå®ç¾©ããèªå¯ããã»ã¹ã»xhrã®ç½²åãèªååãã¾ã)[+xhr,+window]
+var X_OAUTH2_authWindow,
+ X_OAUTH2_authTimerID;
/**
* ã¤ãã³ã
*
- * - X.Event.NEED_AUTH
- window ããpopup ãã¦èªå¯ãè¡ãå¿
è¦ããããã¤ã³ã¿ã¼ã¤ãã³ãå
㧠oauth2.requestAuth() ãå¼ã¶ã
+ *
- X.Event.NEED_AUTH
- window ããpopup ãã¦èªå¯ãè¡ãå¿
è¦ããããã¤ã³ã¿ã¼ã¤ãã³ãå
㧠oauth2.requestAuth() ãå¼ã¶ããã®ã¤ãã³ãããã£ã³ã»ã«ããã¨
*
- X.Event.CANCELED
- èªå¯ window ãéããããã([x]çã§ã¦ã¤ã³ãã¦ãéãããããoauth2.cancelAuth() ãå¼ã°ãã)
*
- X.Event.SUCCESS
- èªå¯ window ã§ã¦ã¼ã¶ã¼ãèªå¯ããç¶ãã¦ã³ã¼ãã®èªå¯ãæ¸ãã ã
*
- X.Event.ERROR
- ã³ã¼ãã®èªå¯ã®ã¨ã©ã¼ããªãã¬ãã·ã¥ãã¼ã¯ã³ã®ï½´ï¾ï½°ããããã¯ã¼ã¯ã¨ã©ã¼
@@ -73,7 +73,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
};
// TODO canUse gadgetProxy
- this[ 'listen' ]( [ X_EVENT_KILL_INSTANCE, X_EVENT_SUCCESS, X_EVENT_ERROR, X_EVENT_NEED_AUTH ], X_NET_OAUTH2_handleEvent );
+ this[ 'listen' ]( [ X_EVENT_KILL_INSTANCE, X_EVENT_SUCCESS, X_EVENT_ERROR, X_EVENT_NEED_AUTH ], X_OAUTH2_handleEvent );
},
/**
@@ -104,7 +104,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
*/
'requestAuth' : function(){
var e = X_EventDispatcher_CURRENT_EVENTS[ X_EventDispatcher_CURRENT_EVENTS.length - 1 ],
- w, h;
+ w, h, pair;
// TODO pointer event å
ãï¼ãã§ãã¯
if( !e || !e[ 'pointerType' ] ){
@@ -113,7 +113,7 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
};
// äºã¤ä»¥ä¸ã® popup ãä½ããªã
- if( X_NET_OAUTH2_authorizationWindow ) return;
+ if( X_OAUTH2_authWindow ) return;
pair = X_Pair_get( this );
@@ -121,25 +121,25 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
w = pair[ 'authorizeWindowWidth' ] || 500;
h = pair[ 'authorizeWindowHeight' ] || 500;
+
+ X_OAUTH2_authWindow = X_Window( {
+ 'url' : X_URL_create( pair[ 'authorizeEndpoint' ],
+ {
+ 'response_type' : 'code',
+ 'client_id' : pair[ 'clientID' ],
+ 'redirect_uri' : pair[ 'redirectURI' ],
+ 'scope' : ( pair[ 'scopes' ] || [] ).join( ' ' )
+ }
+ ),
+ 'name' : 'oauthauthorize',
+ 'params' : 'width=' + w
+ + ',height=' + h
+ + ',left=' + ( screen.width - w ) / 2
+ + ',top=' + ( screen.height - h ) / 2
+ + ',menubar=no,toolbar=no'
+ } )[ 'listen' ]( X_EVENT_UNLOAD, this, X_OAuth2_detectAuthPopup );
- // TODO X.Util.Window
- X_NET_OAUTH2_authorizationWindow = window.open(
- X_URL_create( pair[ 'authorizeEndpoint' ],
- {
- 'response_type' : 'code',
- 'client_id' : pair[ 'clientID' ],
- 'redirect_uri' : pair[ 'redirectURI' ],
- 'scope' : ( pair[ 'scopes' ] || [] ).join( ' ' )
- }
- ),
- 'oauthauthorize',
- 'width=' + w
- + ',height=' + h
- + ',left=' + ( screen.width - w ) / 2
- + ',top=' + ( screen.height - h ) / 2
- + ',menubar=no,toolbar=no');
-
- X_NET_OAUTH2_authorizationTimerID = X_Timer_add( 333, 0, this, X_Net_OAuth2_detectAuthPopup );
+ X_OAUTH2_authTimerID = X_Timer_add( 333, 0, this, X_OAuth2_detectAuthPopup );
pair.oauth2State = 1;
@@ -162,17 +162,13 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
};
// http://kojikoji75.hatenablog.com/entry/2013/12/15/223839
- 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;
+ if( X_OAUTH2_authWindow ){
+ X_OAUTH2_authWindow[ 'kill' ]();
+ X_OAUTH2_authWindow = null;
};
- X_NET_OAUTH2_authorizationTimerID && X_Timer_remove( X_NET_OAUTH2_authorizationTimerID );
- X_NET_OAUTH2_authorizationTimerID = 0;
+ X_OAUTH2_authTimerID && X_Timer_remove( X_OAUTH2_authTimerID );
+ X_OAUTH2_authTimerID = 0;
this[ 'asyncDispatch' ]( X_EVENT_CANCELED );
},
@@ -213,14 +209,14 @@ X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
'Content-Type' : 'application/x-www-form-urlencoded'
},
'test' : 'gadget' // canuse
- } ).listenOnce( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], this, X_Net_OAuth2_responceHandler );
+ } ).listenOnce( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], this, X_OAuth2_responceHandler );
this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Start to refresh token.' } );
}
}
);
-function X_NET_OAUTH2_handleEvent( e ){
+function X_OAUTH2_handleEvent( e ){
var pair = X_Pair_get( this );
switch( e.type ){
@@ -241,41 +237,35 @@ function X_NET_OAUTH2_handleEvent( e ){
};
};
-function X_Net_OAuth2_detectAuthPopup(){
- var closed, search, pair = X_Pair_get( this );
-
- if( X_NET_OAUTH2_authorizationWindow.closed ){
- pair.oauth2State = 0;
- closed = true;
+function X_OAuth2_detectAuthPopup( e ){
+ var pair = X_Pair_get( this ),
+ status, search;
+ if( X_OAUTH2_authWindow[ 'closed' ]() ){
+ status = 0;
this[ 'asyncDispatch' ]( X_EVENT_CANCELED );
} else
- if( search = X_Script_try( X_Object_find, [ X_NET_OAUTH2_authorizationWindow, 'location>search' ] ) ){
- pair = X_Pair_get( this );
+ if( search = X_OAUTH2_authWindow[ 'find' ]( 'location>search' ) ){
+ pair = X_Pair_get( this );
pair.code = X_URL_paramToObj( search.slice( 1 ) )[ 'code' ];
-
- 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 );
-
- pair.oauth2State = 2;
+ status = 2;
+ X_OAuth2_authorizationCode( this, pair );
this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Get code success, then authorization code.' } );
};
- if( closed ){
- X_NET_OAUTH2_authorizationWindow = null;
- X_NET_OAUTH2_authorizationTimerID = 0;
+ if( 0 <= status ){
+ pair = pair || X_Pair_get( this );
+ pair.oauth2State = status;
+
+ X_OAUTH2_authWindow[ 'kill' ]();
+ X_OAUTH2_authWindow = null;
+ X_OAUTH2_authTimerID = X_Timer_remove( X_OAUTH2_authTimerID );
return X_CALLBACK_UN_LISTEN;
};
};
-function X_Net_OAuth2_authorizationCode( oauth2, pair ){
+function X_OAuth2_authorizationCode( oauth2, pair ){
pair.net = X.Net( {
'xhr' : pair[ 'tokenEndpoint' ],
'postdata' : X_URL_objToParam({
@@ -291,10 +281,10 @@ function X_Net_OAuth2_authorizationCode( oauth2, pair ){
'Content-Type' : 'application/x-www-form-urlencoded'
},
'test' : 'gadget'
- } ).listenOnce( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], oauth2, X_Net_OAuth2_responceHandler );
+ } ).listenOnce( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], oauth2, X_OAuth2_responceHandler );
};
-function X_Net_OAuth2_responceHandler( e ){
+function X_OAuth2_responceHandler( e ){
var data = e.response,
pair = X_Pair_get( this ),
isRefresh = pair.oauth2State === 3;
@@ -353,7 +343,7 @@ function X_Net_OAuth2_responceHandler( e ){
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 );
+ X_OAuth2_authorizationCode( this, pair );
};
break;
};
@@ -424,7 +414,7 @@ function X_OAuth2_updateLocalStorage( cmd, that, name, value ){
var action = cmd === '+' ? 'setItem' : cmd === '-' ? 'removeItem' : 'getItem',
pair;
- if( window.localStorage ){
+ if( window.localStorage ){ // TODO http://qiita.com/narikei/items/f55fb9cb398beac52ea9
return window.localStorage[ action ]( X_Pair_get( that )[ 'clientID' ] + name, value );
};