2014-03-31 53 views
0

首先,感謝您的回覆。 我嘗試使用nodeJS創建REST API,並且我認爲,oAuth2是根據我的系統對用戶進行身份驗證的最佳方式。REST API,OAuth2和認證錯誤

用戶存儲在LDAP中,API密鑰將保留在MySQL引擎上。客戶賬戶代碼也將存儲在MySQL中。 所有的過程是確定的,用戶進行身份驗證,API令牌已保存在MySQL的,但是當我嘗試使用此令牌,護照拒絕驗證了我的請求,並返回:

Error: Can't set headers after they are sent. 
    at ServerResponse.OutgoingMessage.setHeader (http.js:691:11) 
    at ServerResponse.res.setHeader (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/express/node_modules/connect/lib/patch.js:62:20) 
    at ServerResponse.res.header (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/express/lib/response.js:280:8) 
    at ServerResponse.res.json (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/express/lib/response.js:135:8) 
    at exports.info (/home/lolostates/Developpement/nodejs/oauth2/test/user.js:13:9) 
    at callbacks (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/express/lib/router/index.js:272:11) 
    at complete (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/passport/lib/passport/middleware/authenticate.js:218:13) 
    at /home/lolostates/Developpement/nodejs/oauth2/test/node_modules/passport/lib/passport/middleware/authenticate.js:200:15 
    at pass (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/passport/lib/passport/index.js:399:14) 
    at Passport.transformAuthInfo (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/passport/lib/passport/index.js:415:5) 

請求被捲曲發:

curl -H "Authorization: Bearer NoulKM889Aksf60rQONcUJwMuZHI3PDqzeXfkX3Li2BohsxNVsOrd2LLdvJAGZuE168IukUCPbviazhvBjt7VDfLFUMJRIY1fa95kGXQQKzE7etFhocsnYvbLSixbHRmCwXNx5FKj6v83Ci9f9xLqRinEKwaAUIjs03hhq8dCWIp7S0Cbi5jdkxlzwfpZxuShoAZYaFInlf4ymG5oyzQe0WJ2POXOaMarGLO7NkjyIMJXWh7s0Y" http://localhost:3000/api/userinfo 

我使用passport-http-bearer,oAuth2orize和OAuth2orize示例中提供的所有示例函數。

你能解釋我爲什麼嗎?

回答

0

看起來您已嘗試在回覆響應之後設置響應標頭。在嘗試設置標題之前,請確保您沒有撥打res.end()(或類似的方法)。

+0

我從來沒有叫res.end()或其他呼叫......我只能打電話與護照檢查: 'exports.info2 = [ passport.authenticate( '承載',{會話:假}), 功能( req,res){ res.json(req.user) } ]' 並且承載策略不允許我的請求,但策略在DB中查找用戶。 –

+0

我只是想爲你解釋錯誤信息。 exports.info(/home/lolostates/Developpement/nodejs/oauth2/test/user.js:13:9)發生了什麼?這似乎是你在事後設置標題的地方。 – AlexMA

+0

對不起,我沒有所有的單詞。 'VAR護照=要求( '護照') exports.info = [ passport.authenticate( '承載',{會話:假}), 函數(REQ,RES){ res.json({USER_ID: req.user.mail,name:req.user.mail,範圍:req.authInfo.scope}) } ]' 我只打電話給護照並返回一個json。即使他在數據庫中找到用戶,護照停止處理併發送401未授權。 –