Bug fix for channel registration

This commit is contained in:
斟酌 鵬兄 2016-07-04 15:04:46 +08:00
parent b0e52f3d6a
commit 321858f6ec
3 changed files with 51 additions and 36 deletions

@ -1 +1 @@
Subproject commit cd2cce7e985a00c685f9f0ad776af6a9f394d765 Subproject commit c73aa30c39942e2c49b2609d1a0cfa8b79fc9e21

View File

@ -36,35 +36,29 @@ class WNSAuth extends EventEmitter
if( this.__inAuth ) return; if( this.__inAuth ) return;
this.__inAuth = true; this.__inAuth = true;
var _self = this;
Model.Tokens.findOne({ name: AuthTokenName, date_created: { $gt: Date.now() - 83200 } }) Model.Tokens.findOne({ name: AuthTokenName, date_created: { $gt: Date.now() - 83200 } })
.exec( ( err, data ) => { .exec( ( err, data ) => {
if( err || !( data && data.token ) ) if( err || !( data && data.token ) )
{ {
Dragonfly.Info( "Database does not contain access token, authenticating" ); Dragonfly.Info( "Database does not contain access token, authenticating" );
_self.__authWNS(); this.__authWNS();
} }
else else
{ {
Dragonfly.Info( "Access token found in database, using it" ); Dragonfly.Info( "Access token found in database, using it" );
AuthToken = data.token; AuthToken = data.token;
_self.__emitAuthComplete(); this.__emitAuthComplete();
} }
} ); } );
} }
Register( uuid, ChannelUri, handler ) Register( uuid, ChannelUri, handler, retry )
{ {
var _self = this; if( retry == undefined ) retry = 0;
var VerifyChannel = () => var VerifyChannel = () =>
{ {
var N = new Notis({
id: "Null"
, title: "Channel Registration"
, message: "Registration success"
});
if( uuid ) if( uuid )
{ {
Dragonfly.Info( "Renewal request: " + uuid ); Dragonfly.Info( "Renewal request: " + uuid );
@ -72,26 +66,47 @@ class WNSAuth extends EventEmitter
return; return;
} }
uuid = Rand.uuid();
Dragonfly.Debug( "ChannelUri: " + ChannelUri ); Dragonfly.Debug( "ChannelUri: " + ChannelUri );
_self.__send( ChannelUri, N, ( sender, e ) => { var N = new Notis({
id: "Null"
, title: "Channel Registration"
, message: "Registration success"
});
this.__send( ChannelUri, N, ( sender, e ) => {
if( typeof( e ) == "string" ) if( typeof( e ) == "string" )
{ {
handler( _self, e ); handler( this, e );
return; return;
} }
if( e.statusCode == 200 ) switch( e.statusCode )
{ {
this.__updateToken( uuid, ChannelUri, handler ); case 200:
return; this.__updateToken( uuid || Rand.uuid(), ChannelUri, handler );
break;
default:
AuthToken = null;
Dragonfly.Info( "Perhaps access token is expired, retrying ..." );
if( retry < 2 )
{
this.once( "AuthComplete", () => {
this.Regster( uuid, ChannelUri, handler, retry + 1 );
});
}
else
{
Dragonfly.Debug( "WNSResponse: " + e.statusCode );
handler( this, e.statusCode + " Server Error: Unable to push message to channel" );
}
this.Authenticate();
} }
Dragonfly.Debug( e.statusCode + ": " + e.ResponseString );
handler( _self, e.statusCode + " Server Error: Unable to push message to channel" );
} ); } );
}; };
@ -119,7 +134,6 @@ class WNSAuth extends EventEmitter
Deliver( NotisQ ) Deliver( NotisQ )
{ {
var _self = this;
Model.Tokens Model.Tokens
.findOne({ name: NotisQ.id, expired: false }) .findOne({ name: NotisQ.id, expired: false })
.exec( ( err, data ) => { .exec( ( err, data ) => {
@ -152,9 +166,9 @@ class WNSAuth extends EventEmitter
if( NotisQ.Retry < 2 ) if( NotisQ.Retry < 2 )
{ {
_self.once( "AuthComplete", () => { this.once( "AuthComplete", () => {
NotisQ.Retry ++; NotisQ.Retry ++;
_self.Deliver( NotisQ ); this.Deliver( NotisQ );
}); });
} }
else else
@ -162,7 +176,7 @@ class WNSAuth extends EventEmitter
Dragonfly.Info( "Retrying exceeded the limit, dropping the message" ); Dragonfly.Info( "Retrying exceeded the limit, dropping the message" );
} }
_self.Authenticate(); this.Authenticate();
} }
} ); } );
} }
@ -175,7 +189,6 @@ class WNSAuth extends EventEmitter
__updateToken( uuid, ChannelUri, handler ) __updateToken( uuid, ChannelUri, handler )
{ {
var _self = this;
Model.Tokens.update( Model.Tokens.update(
{ name: uuid } { name: uuid }
, { , {
@ -191,12 +204,12 @@ class WNSAuth extends EventEmitter
if( err ) if( err )
{ {
Dragonfly.Error( err ); Dragonfly.Error( err );
handler( _self, "Server Error: Cannot save channel information" ); handler( this, "Server Error: Cannot save channel information" );
return; return;
} }
// Success // Success
handler( _self, uuid ); handler( this, uuid );
Dragonfly.Info( "Register: " + uuid ); Dragonfly.Info( "Register: " + uuid );
} ); } );
} }
@ -257,7 +270,6 @@ class WNSAuth extends EventEmitter
__requestComplete( sender, e ) __requestComplete( sender, e )
{ {
var _self = this;
let JResponse = JSON.parse( e.ResponseString ); let JResponse = JSON.parse( e.ResponseString );
if( JResponse && JResponse.access_token ) if( JResponse && JResponse.access_token )
@ -274,13 +286,13 @@ class WNSAuth extends EventEmitter
) )
.exec( ( err, data ) => { .exec( ( err, data ) => {
if( err ) Dragonfly.Error( err ); if( err ) Dragonfly.Error( err );
_self.__emitAuthComplete(); this.__emitAuthComplete();
}); });
} }
else else
{ {
Dragonfly.Error( "Unable to authenticate: " + e.ResponseString ); Dragonfly.Error( "Unable to authenticate: " + e.ResponseString );
_self.__emitAuthComplete(); this.__emitAuthComplete();
} }
} }

View File

@ -45,7 +45,6 @@ class App extends Base
{ {
e.Handled = true; e.Handled = true;
var _self = this;
var query = e.Data; var query = e.Data;
// Protected Actions // Protected Actions
@ -55,8 +54,11 @@ class App extends Base
case "remove": case "remove":
if(!( query.pass && query.pass == SrvAuth.Client )) if(!( query.pass && query.pass == SrvAuth.Client ))
{ {
Dragonfly.Debug( "Unauthorized Access" );
this.result = "Unauthorized Access"; this.result = "Unauthorized Access";
this.plantResult(); this.plantResult();
return; return;
} }
break; break;
@ -65,10 +67,11 @@ class App extends Base
switch( query.action ) switch( query.action )
{ {
case "register": case "register":
console.log( "HEiiiiRE" );
this.OAuth.Register( this.OAuth.Register(
query.id, query.uri, ( sender, mesg ) => { query.id, query.uri, ( sender, mesg ) => {
_self.result = mesg; this.result = mesg;
_self.plantResult(); this.plantResult();
} ); } );
break; break;
@ -88,8 +91,8 @@ class App extends Base
Dragonfly.Debug( "Removed " + query.id + ": " + data ); Dragonfly.Debug( "Removed " + query.id + ": " + data );
} }
_self.result = "OK"; this.result = "OK";
_self.plantResult(); this.plantResult();
} ); } );
break; break;
default: default: