/** @lends OAuth2.prototype */
{
'Constructor' : function( obj ){
+ var expires_at;
+
obj = X_Object_clone( obj );
X_Pair_create( this, obj );
- if( _getAccessToken( this ) ){
- obj.oauth2State = 4;
- this[ 'asyncDispatch' ]( X_EVENT_SUCCESS );
+ obj.onAuthError = X_NET_OAUTH2_onXHR401Error;
+ obj.updateRequest = X_NET_OAUTH2_updateRequest;
+
+ if( _getAccessToken( this ) && ( expires_at = _getAccessTokenExpiry( this ) ) ){
+ if( expires_at < X_Timer_now() + 300000 ){ // 寿命が5分を切った
+ this[ 'refreshToken' ]();
+ } else {
+ obj.oauth2State = 4;
+ this[ 'asyncDispatch' ]( X_EVENT_SUCCESS );
+ };
} else {
this[ 'asyncDispatch' ]( X_EVENT_NEED_AUTH );
- }
-
- obj.onAuthError = X_NET_OAUTH2_onXHR401Error;
- obj.updateRequest = X_NET_OAUTH2_updateRequest;
+ };
// TODO canUse
// TODO kill の cancel
* 認可用 window をポップアップする。ポップアップブロックが働かないように必ず pointer event 内で呼ぶこと。
*/
'requestAuth' : function(){
+ var url, w, h;
+ // TODO pointer event 内か?チェック
// 二つ以上の popup を作らない
if( X_NET_OAUTH2_authorizationWindow ) return;
if( pair.net || pair.oauth2State ) return;
+ url = pair[ 'authorizeEndpoint' ];
+ w = pair[ 'authorizeWindowWidth' ] || 500;
+ h = pair[ 'authorizeWindowHeight' ] || 500;
+
X_NET_OAUTH2_authorizationWindow = window.open(
- pair[ 'authorizeEndpoint' ] + '?' + X_URL_objToParam(
+ url + ( ( url.indexOf( '?' ) !== -1 ) ? '&' : '?' ) + X_URL_objToParam(
{
'response_type' : 'code',
'client_id' : pair[ 'clientID' ],
'scope' : ( pair[ 'scopes' ] || []).join(' ')
}
), 'oauthauthorize',
- 'width=' + pair[ 'authorizeWindowWidth' ]
- + ',height=' + pair[ 'authorizeWindowHeight' ]
- + ',left=' + (screen.width - pair[ 'authorizeWindowWidth' ] ) / 2
- + ',top=' + (screen.height - pair[ 'authorizeWindowHeight' ] ) / 2
+ '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 );
* アクセストークンのリフレッシュ。
*/
'refreshToken' : function(){
- /* TODO 自動リフレッシュ
- * var expires_at = this._getAccessTokenExpiry();
- if (expires_at && Date.now() + millis > expires_at)
- this._refreshAccessToken({replay: false});
- */
-
- pair = X_Pair_get( this );
+ // TODO 自動リフレッシュ
+
+ var pair = X_Pair_get( this );
if( pair.net ) return;
function _getAccessToken( that ){ return updateLocalStorage( '', that, 'accessToken' ); }
function _getRefreshToken( that){ return updateLocalStorage( '', that, 'refreshToken' ); }
-function _getAccessTokenExpiry( that ){ return updateLocalStorage( '', that, 'tokenExpiry' ); }
+function _getAccessTokenExpiry( that ){ return parseInt( updateLocalStorage( '', that, 'tokenExpiry' ) ) || 0; }
function _getAuthMechanism( that ){
// TODO use gadget | flash ...
// IE's XDomainRequest doesn't support sending headers, so don't try.