快递和此代码有问题:
appToken.post('/getToken', function(req, res) {
console.log("Got one request to have a token");
console.log("cookie : "+req.headers.cookie);
wp_auth.checkAuth( req ).on( 'auth', function( auth_is_valid, user_id ) {
if(auth_is_valid){
//genToken();
console.log("[ACCEPTE] Création d'un token pour la connection de "+user_id)
var token = jwt.sign({ "userId" : user_id }, appToken.get("cert"), { "subject" : "ChatAuthentification"});
res.status(200).json({"token": token});
return;
}else{
console.log("[ERREUR] Utilisateur non valide");
res.status(401).json({error: "Connexion refusé. Vous êtes-vous connecté sur le site Wordpress ?"});
return;
}
});
});
在第一次连接时,everithing很好,他会发送回复等。 在第二个连接上,即使使用.json方法,谁必须关闭连接,我有以下问题:
"在发送标题后不能设置标题"
我怎么能通过这个?
HEre是wp_auth模块的一部分,由nightGunner5首先创建并且我已经修改以使其适用于wordpress 4.0(及其新的身份验证过程):
function WP_Auth( wpurl, logged_in_key, logged_in_salt,
mysql_host, mysql_user, mysql_pass, mysql_db,
wp_table_prefix ) {
var md5 = crypto.createHash( 'md5' );
md5.update( wpurl );
this.cookiename = 'wordpress_logged_in_' + md5.digest( 'hex' );
this.salt = logged_in_key + logged_in_salt;
this.db = require( 'mysql-native' ).createTCPClient( mysql_host );
this.db.auth( mysql_db, mysql_user, mysql_pass );
this.table_prefix = wp_table_prefix;
this.known_hashes = {};
this.known_hashes_timeout = {};
this.meta_cache = {};
this.meta_cache_timeout = {};
// Default cache time: 5 minutes
this.timeout = 300000;
}
WP_Auth.prototype.checkAuth = function( req ) {
var self = this, data = null;
if ( req.headers.cookie ) // s'il y a des cookies
req.headers.cookie.split( ';' ).forEach( function( cookie ) { // separation de chaque cookie
if ( cookie.split( '=' )[0].trim() == self.cookiename )
data = cookie.split( '=' )[1].trim().split( '\|' );
} );
else
return new Invalid_Auth();
if ( !data )
return new Invalid_Auth();
if ( parseInt( data[1] ) < new Date / 1000 )
return new Invalid_Auth();
return new Valid_Auth( data, this );
};
exports.create = function( wpurl, logged_in_key, logged_in_salt,
mysql_host, mysql_user, mysql_pass, mysql_db,
wp_table_prefix ) {
return new WP_Auth( wpurl, logged_in_key, logged_in_salt,
mysql_host, mysql_user, mysql_pass, mysql_db,
wp_table_prefix );
};
function Invalid_Auth() {}
Invalid_Auth.prototype.on = function( key, callback ) {
if ( key != 'auth' )
return this;
var self = this;
process.nextTick( function() {
callback.call( self, false, 0 );
} );
return this;
};
function Valid_Auth( data, auth ) {
var self = this, user_login = data[0], expiration = data[1], token = data[2], hash = data[3];
if ( user_login in auth.known_hashes_timeout && auth.known_hashes_timeout[user_login] < +new Date ) {
delete auth.known_hashes[user_login];
delete auth.known_hashes_timeout[user_login];
}
function parse( pass_frag, id ) {
var hmac1 = crypto.createHmac( 'md5', auth.salt );
hmac1.update( user_login +'|'+ pass_frag + '|'+ expiration + '|' + token);
var hmac2 = crypto.createHmac( 'sha256', hmac1.digest( 'hex' ));
hmac2.update( user_login + '|' + expiration + '|' + token );
if ( hash == hmac2.digest( 'hex' ) ) {
self.emit( 'auth', true, id );
} else {
self.emit( 'auth', false, 0 );
}
}
if ( user_login in auth.known_hashes )
process.nextTick(function() {
parse( auth.known_hashes[user_login].frag, auth.known_hashes[user_login].id );
} );
var found = false;
auth.db.query( 'select ID, user_pass from ' + auth.table_prefix + 'users where user_login = \'' + user_login.replace( /(\'|\\)/g, '\\$1' ) + '\'' ).on( 'row', function( data ) {
found = true;
auth.known_hashes[user_login] = {frag: data.user_pass.substr( 8, 4 ), id: data.ID};
auth.known_hashes_timeout[user_login] = +new Date + auth.timeout;
} ).on( 'end', function() {
if ( !found ) {
auth.known_hashes[user_login] = {frag: '__fail__', id: 0};
auth.known_hashes_timeout[user_login] = +new Date + auth.timeout;
}
parse( auth.known_hashes[user_login].frag, auth.known_hashes[user_login].id );
} );
}
答案 0 :(得分:0)
在我看来parse()
可以多次调用,这意味着
self.emit( 'auth', ...)
可以多次调用,这意味着您的请求处理程序将尝试多次发送响应,因为它为给定请求多次获取auth
事件。我想如果这个条件return
为真,你可能需要一个if ( user_login in auth.known_hashes )
语句,这样你才能进入下一个parse()
。
改变这个:
if ( user_login in auth.known_hashes )
process.nextTick(function() {
parse( auth.known_hashes[user_login].frag, auth.known_hashes[user_login].id );
});
到此;
if ( user_login in auth.known_hashes ) {
process.nextTick(function() {
parse( auth.known_hashes[user_login].frag, auth.known_hashes[user_login].id );
});
return;
}