OSDN Git Service

ab34837d2331a0b340b2ca829db33d74103a9442
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 10_XOAuth2.js
1
2 var X_NET_OAUTH2_detection      = new Function( 'w', 'try{return w.location.search}catch(e){}' ),
3         X_NET_OAUTH2_authorizationWindow,
4         X_NET_OAUTH2_authorizationTimerID;
5
6 /**
7  * イベント
8  * <dl>
9  * <dt>X.Event.NEED_AUTH<dd>window を popup して認可を行う必要あり。ポインターイベント内で oauth2.requestAuth() を呼ぶ。
10  * <dt>X.Event.CANCELED<dd>認可 window が閉じられた。([x]等でウインドウが閉じられた、oauth2.cancelAuth() が呼ばれた)
11  * <dt>X.Event.SUCCESS<dd>認可 window でユーザーが認可し、続いてコードの認可が済んだ。
12  * <dt>X.Event.ERROR<dd>コードの認可のエラー、リフレッシュトークンのエラー、ネットワークエラー
13  * <dt>X.Event.PROGRESS<dd>コードを window から受け取った、リフレッシュトークンの開始、コードの認可を header -> params に切替
14  * </dl>
15  * 
16  * original :
17  *  oauth2.js , <opendata@oucs.ox.ac.uk>
18  * 
19  * @alias X.OAuth2
20  * @class OAuth2 サービスを定義し接続状況をモニタする。適宜にトークンのアップデートなどを行う
21  * @constructs OAuth2
22  * @extends {EventDispatcher}
23  * @example // OAuth2 サービスの定義
24 oauth2 = X.OAuth2({
25         'clientID'          : 'xxxxxxxx.apps.googleusercontent.com',
26         'clientSecret'      : 'xxxxxxxx',
27         'authorizeEndpoint' : 'https://accounts.google.com/o/oauth2/auth',
28         'tokenEndpoint'     : 'https://accounts.google.com/o/oauth2/token',
29         'redirectURI'       : X.URL.cleanup( document.location.href ), // 専用の軽量ページを用意してもよいが、現在のアドレスでも可能
30         'scopes'            : [ 'https://www.googleapis.com/auth/blogger' ],
31         'authorizeWindowWidth'  : 500,
32         'authorizeWindowHeight' : 500
33 }).listen( [ X.Event.NEED_AUTH, X.Event.CANCELED, X.Event.SUCCESS, X.Event.ERROR, X.Event.PROGRESS ], updateOAuth2State );
34
35 // XHR 時に oauth2 を渡す
36 X.Net( {
37         xhr      : 'https://www.googleapis.com/blogger/v3/users/self/blogs',
38         dataType : 'json',
39         auth     : oauth2,
40         test     : 'gadget' // http -> https:xProtocol なリクエストのため、google ガジェットを proxy に使用
41         } )
42         .listen( [ X.Event.SUCCESS, X.Event.ERROR, X.Event.PROGRESS ], updateOAuth2State );
43  */
44 X[ 'OAuth2' ] = X_EventDispatcher[ 'inherits' ](
45                 'X.OAuth2',
46                 X_Class.NONE,
47                 
48                 /** @lends OAuth2.prototype */
49                 {
50                         'Constructor' : function( obj ){
51                                 var expires_at;
52                                 
53                                 obj = X_Object_clone( obj );
54                                 
55                                 X_Pair_create( this, obj );
56                                 
57                                 obj.onAuthError   = X_NET_OAUTH2_onXHR401Error;
58                                 obj.updateRequest = X_NET_OAUTH2_updateRequest;                         
59                                 
60                                 if( _getAccessToken( this ) && ( expires_at = _getAccessTokenExpiry( this ) ) ){
61                                         if( expires_at < X_Timer_now() + ( obj[ 'refreshMargin' ] || 300000 ) ){ // 寿命が5分を切った
62                                                 this[ 'refreshToken' ]();
63                                         } else {
64                                                 obj.oauth2State = 4;
65                                                 this[ 'asyncDispatch' ]( X_EVENT_SUCCESS );                                             
66                                         };
67                                 } else {
68                                         this[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
69                                 };
70                                 
71                                 // TODO canUse gadgetProxy
72                                 this[ 'listen' ]( [ X_EVENT_KILL_INSTANCE, X_EVENT_SUCCESS, X_EVENT_ERROR, X_EVENT_NEED_AUTH ], X_NET_OAUTH2_handleEvent );
73                         },
74
75                         /**
76                          * OAuth2 の状態。
77                          * <dl>
78                          * <dt>0 : <dd>未接続
79                          * <dt>1 : <dd>認可用 window がポップアップ中
80                          * <dt>2 : <dd>コードを認可中
81                          * <dt>3 : <dd>トークンのリフレッシュ中
82                          * <dt>4 : <dd>接続
83                          * </dl>
84                          * @return {number}
85                          */
86                         'state' : function(){
87                                 return X_Pair_get( this ).oauth2State || 0;
88                         },
89                         
90                         /**
91                          * 認可用 window をポップアップする。ポップアップブロックが働かないように必ず pointer event 内で呼ぶこと。
92                          */
93                         'requestAuth' : function(){
94                                 var url, w, h;
95                                 // TODO pointer event 内か?チェック
96                                 // 二つ以上の popup を作らない
97                                 if( X_NET_OAUTH2_authorizationWindow ) return;
98                                 
99                                 pair = X_Pair_get( this );
100                                 
101                                 if( pair.net || pair.oauth2State ) return;
102                                 
103                                 url = pair[ 'authorizeEndpoint' ];
104                                 w   = pair[ 'authorizeWindowWidth' ]  || 500;
105                                 h   = pair[ 'authorizeWindowHeight' ] || 500;
106                                 
107                                 X_NET_OAUTH2_authorizationWindow = window.open(
108                                         url + ( ( url.indexOf( '?' ) !== -1 ) ? '&' : '?' ) + X_URL_objToParam(
109                                                 {
110                                                         'response_type' : 'code',
111                                                         'client_id'     : pair[ 'clientID' ],
112                                                         'redirect_uri'  : pair[ 'redirectURI' ],
113                                                         'scope'         : ( pair[ 'scopes' ] || []).join(' ')
114                                                 }
115                                         ), 'oauthauthorize',
116                                         'width=' + w
117                                         + ',height=' + h
118                                         + ',left=' + ( screen.width  - w ) / 2
119                                         + ',top='  + ( screen.height - h ) / 2
120                                         + ',menubar=no,toolbar=no');
121                                 
122                                 X_NET_OAUTH2_authorizationTimerID = X_Timer_add( 333, 0, this, X_Net_OAuth2_detectAuthPopup );
123                                 
124                                 pair.oauth2State = 1;
125                                 
126                                 this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Start to auth.' } );
127                         },
128                         
129                         /**
130                          * 認可プロセスのキャンセル。ポップアップを閉じて認可用の通信は中断する。
131                          */
132                         'cancelAuth' : function(){
133                                 var pair = X_Pair_get( this );
134                                 
135                                 if( pair.net ){
136                                         pair.net[ 'kill' ]();
137                                         delete pair.net;
138                                 };
139                                 
140                                 if( pair.oauth2State !== 1 ){
141                                         return;
142                                 };
143                                 
144                                 // http://kojikoji75.hatenablog.com/entry/2013/12/15/223839
145                                 X_NET_OAUTH2_authorizationWindow && X_NET_OAUTH2_authorizationWindow.open( 'about:blank', '_self' ).close();
146                                 X_NET_OAUTH2_authorizationWindow  = null;
147                                 
148                                 X_NET_OAUTH2_authorizationTimerID && X_Timer_remove( X_NET_OAUTH2_authorizationTimerID );
149                                 X_NET_OAUTH2_authorizationTimerID = 0;
150                                 
151                                 this[ 'asyncDispatch' ]( X_EVENT_CANCELED );
152                         },
153                         
154                         /**
155                          * アクセストークンのリフレッシュ。
156                          */
157                         'refreshToken' : function(){
158                                 var pair = X_Pair_get( this );
159                                 
160                                 if( pair.net ) return;
161                                 
162                                 if( pair.refreshTimerID ){
163                                         X_Timer_remove( pair.refreshTimerID );
164                                         delete pair.refreshTimerID;
165                                 };
166                                 
167                                 pair.oauth2State = 3;
168                                 
169                                 pair.net = X.Net( {
170                                         'xhr'      : pair[ 'tokenEndpoint' ],
171                                         'postdata' : X_URL_objToParam({
172                                                 'client_id'     : pair[ 'clientID' ],
173                                                 'client_secret' : pair[ 'clientSecret' ],
174                                                 'grant_type'    : 'refresh_token',
175                                                 'refresh_token' : _getRefreshToken( this )
176                                         }),
177                                         'dataType' : 'json',
178                                         'headers'  : {
179                                                                         'Accept'       : 'application/json',
180                                                                         'Content-Type' : 'application/x-www-form-urlencoded'
181                                                                 },
182                                         'test'     : 'gadget'
183                                 } ).listenOnce( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], this, X_Net_OAuth2_responceHandler );
184                                 
185                                 this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Start to refresh token.' } );
186                         }
187                 }
188         );
189
190 function X_NET_OAUTH2_handleEvent( e ){
191         var pair = X_Pair_get( this );
192         
193         switch( e.type ){
194                 case X_EVENT_KILL_INSTANCE :
195                         this[ 'cancelAuth' ]();
196                 
197                 case X_EVENT_ERROR :
198                 case X_EVENT_NEED_AUTH :
199                         pair.refreshTimerID && X_Timer_remove( pair.refreshTimerID );
200                         break;
201                         
202                 case X_EVENT_SUCCESS :
203                         pair.refreshTimerID && X_Timer_remove( pair.refreshTimerID );
204                         if( _getRefreshToken( this ) ){
205                                 // 自動リフレッシュ
206                                 pair.refreshTimerID = X_Timer_once( _getAccessTokenExpiry( this ) - X_Timer_now() - pair[ 'refreshMargin' ], this, this[ 'refreshToken' ] );
207                         };
208         };
209 };
210
211 function X_Net_OAuth2_detectAuthPopup(){
212         var closed, search, pair = X_Pair_get( this );
213         
214         if( X_NET_OAUTH2_authorizationWindow.closed ){
215                 pair.oauth2State = 0;
216                 closed = true;
217
218                 this[ 'asyncDispatch' ]( X_EVENT_CANCELED );
219         } else
220         if( search = X_NET_OAUTH2_detection( X_NET_OAUTH2_authorizationWindow ) ){
221                 pair      = X_Pair_get( this );
222                 pair.code = X_URL_ParamToObj( search.slice( 1 ) )[ 'code' ];
223
224                 X_NET_OAUTH2_authorizationWindow.open( 'about:blank', '_self' ).close();
225                 closed = true;
226
227                 X_Net_OAuth2_authorizationCode( this, pair );
228                 
229                 pair.oauth2State = 2;
230                 this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Get code success, then authorization code.' } );
231         };
232         
233         if( closed ){
234                 X_NET_OAUTH2_authorizationWindow  = null;
235                 X_NET_OAUTH2_authorizationTimerID = 0;
236                 
237                 return X_Callback_UN_LISTEN;    
238         };
239 };
240
241 function X_Net_OAuth2_authorizationCode( oauth2, pair ){        
242         pair.net = X.Net( {
243                 'xhr'      : pair[ 'tokenEndpoint' ],
244                 'postdata' : X_URL_objToParam({
245                         'client_id'     : pair[ 'clientID' ],
246                         'client_secret' : pair[ 'clientSecret' ],
247                         'grant_type'    : 'authorization_code',
248                         'code'          : pair.code,
249                         'redirect_uri'  : pair[ 'redirectURI' ]
250                 }),
251                 'dataType' : 'json',
252                 'headers'  : {
253                         'Accept'       : 'application/json',
254                         'Content-Type' : 'application/x-www-form-urlencoded'
255                 },
256                 'test'     : 'gadget'
257         } ).listenOnce( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], oauth2, X_Net_OAuth2_responceHandler );
258 };
259
260 function X_Net_OAuth2_responceHandler( e ){
261         var data = e.data,
262                 pair = X_Pair_get( this ),
263                 isRefresh = pair.oauth2State === 3;
264         
265         delete pair.net;
266         
267         switch( e.type ){
268                 case X_EVENT_SUCCESS :
269                         if( isRefresh && data.error ){
270                                 _removeRefreshToken( this );
271                                 pair.oauth2State = 0;
272                                 this[ 'asyncDispatch' ]( { type : X_EVENT_ERROR, message : 'Refresh access token error.' + data.error, data : data } );
273                                 this[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
274                                 return;
275                         } else
276                         if( data.error ){
277                                 pair.oauth2State = 0;
278                                 this[ 'asyncDispatch' ]( { type : X_EVENT_ERROR, message : 'Get new access token error.' + data.error, data : data } );
279                                 this[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
280                                 return;
281                         };
282                         
283                         _setAccessToken( this, data[ 'access_token' ] || '' );
284                         _setRefreshToken( this, data[ 'refresh_token' ] || '' );
285                         
286                         if( data[ 'expires_in' ] ){
287                                 _setAccessTokenExpiry( this, X_Timer_now() + data[ 'expires_in' ] * 1000 );
288                         } else
289                         if( _getAccessTokenExpiry( this ) ){
290                                 _removeAccessTokenExpiry( this );
291                         };
292                         
293                         pair.oauth2State = 4;
294                         this[ 'asyncDispatch' ]( { type : X_EVENT_SUCCESS, message : isRefresh ? 'Refresh access token success.' : 'Get new access token success.' } );
295                         break;
296                         
297                 case X_EVENT_ERROR :
298                         if( isRefresh ){
299                                 // other error, not auth
300                                 pair.oauth2State = 0;
301                                 this[ 'asyncDispatch' ]( { type : X_EVENT_ERROR, message : 'Refresh access token error.' } );
302                                 _removeRefreshToken( this );
303                                 this[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
304                         } else
305                         if( _getAuthMechanism( this ) === 'param' ){
306                                 pair.oauth2State = 0;
307                                 this[ 'asyncDispatch' ]( { type : X_EVENT_ERROR, message : 'network-error' } );
308                         } else {
309                                 pair.oauth2State = 0;
310                                 _setAuthMechanism( this, 'param' );
311                                 this[ 'asyncDispatch' ]( { type : X_EVENT_PROGRESS, message : 'Refresh access token failed. retry header -> param. ' } );
312                                 // retry
313                                 X_Net_OAuth2_authorizationCode( this, pair );
314                         };
315                         break;
316         };
317 };
318
319 function X_NET_OAUTH2_onXHR401Error( oauth2, e ){
320         var pair = this,
321                 headers = e[ 'headers' ],
322                 xhr, bearerParams, headersExposed = false;
323         
324         if( _getAuthMechanism( oauth2 ) !== 'param' ){
325                 xhr            = X_NET_currentWrapper[ '_rawObject' ];
326                 headersExposed = !X_Net_XHR_createXDR || !!headers; // this is a hack for Firefox and IE
327                 bearerParams   = headersExposed && ( headers[ 'WWW-Authenticate' ] || headers[ 'www-authenticate' ] );
328                 X_Type_isArray( bearerParams ) && ( bearerParams = bearerParams.join( '\n' ) );
329         };
330         
331         // http://d.hatena.ne.jp/ritou/20110402/1301679908
332         if ( bearerParams && bearerParams.indexOf( ' error=' ) === -1 ) {
333                 pair.oauth2State = 0;
334                 oauth2[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
335         } else
336         if ((( bearerParams && bearerParams.indexOf( 'invalid_token' ) !== -1 ) || !headersExposed) && _getRefreshToken( oauth2 ) ) {
337                 _removeAccessToken( oauth2 ); // It doesn't work any more.
338                 pair.oauth2State = 3;
339                 oauth2[ 'refreshToken' ]();
340         } else {
341         //if (!headersExposed && !_getRefreshToken( oauth2 )) {
342                 _removeAccessToken( oauth2 ); // It doesn't work any more.
343                 pair.oauth2State = 0;
344                 oauth2[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
345         };
346 };
347
348 function X_NET_OAUTH2_updateRequest( oauth2, request ){
349         var token     = _getAccessToken( oauth2 ),
350                 mechanism = _getAuthMechanism( oauth2 ),
351                 url       = request[ 'url' ],
352                 headers;
353
354         if( token && mechanism === 'param' ){
355                 request[ 'url' ] = url + ((url.indexOf('?') !== -1) ? '&' : '?') + 'bearer_token=' + encodeURIComponent( token );
356         };
357         
358         if( token && ( !mechanism || mechanism === 'header' ) ){
359                 headers = request[ 'headers' ] || ( request[ 'headers' ] = {} );
360                 headers[ 'Authorization' ] = 'Bearer ' + token;
361         };
362 };
363
364 function _getAccessToken( that ){ return updateLocalStorage( '', that, 'accessToken' ); }
365 function _getRefreshToken( that){ return updateLocalStorage( '', that, 'refreshToken' ); }
366 function _getAccessTokenExpiry( that ){ return parseInt( updateLocalStorage( '', that, 'tokenExpiry' ) ) || 0; }
367 function _getAuthMechanism( that ){
368                 // TODO use gadget | flash ...
369                 // IE's XDomainRequest doesn't support sending headers, so don't try.
370                 return X_Net_XHR_createXDR ? 'param' : updateLocalStorage( '', that, 'AuthMechanism' );
371         }
372 function _setAccessToken( that, value ){ updateLocalStorage( '+', that, 'accessToken' , value); }
373 function _setRefreshToken( that, value ){ updateLocalStorage( '+', that, 'refreshToken', value); }
374 function _setAccessTokenExpiry( that, value ){ updateLocalStorage( '+', that, 'tokenExpiry', value); }
375 function _setAuthMechanism( that, value ){ updateLocalStorage( '+', that, 'AuthMechanism', value); }
376
377 function _removeAccessToken( that ){ updateLocalStorage( '-', that, 'accessToken' ); }
378 function _removeRefreshToken( that ){ updateLocalStorage( '-', that, 'refreshToken' ); }
379 function _removeAccessTokenExpiry( that ){ updateLocalStorage( '-', that, 'tokenExpiry' ); }
380 function _removeAuthMechanism( that ){ updateLocalStorage( '-', that, 'AuthMechanism' ); }
381         
382 function updateLocalStorage( cmd, that, name, value ){
383         var action = cmd === '+' ? 'setItem' : cmd === '-' ? 'removeItem' : 'getItem',
384                 pair;
385         
386         if( window.localStorage ){
387                 return window.localStorage[ action ]( X_Pair_get( that )[ 'clientID' ] + name, value );
388         };
389         
390         pair = X_Pair_get( that );
391         switch( cmd ){
392                 case '+' :
393                         pair[ name ] = value;
394                         break;
395                 case '-' :
396                         if( pair[ name ] !== undefined ) delete pair[ name ];
397         };
398         return pair[ name ];
399 };
400