我搜索了很多東西,終於來到了這裏。通過SSO身份驗證訪問node.js中的用戶配置文件(BlueMix)
已有: 我在Bluemix上部署了一個簡單的node.js應用程序,該應用程序除了顯示Hello!在通過綁定到Bluemix上的應用程序的SSO SAML服務對用戶進行身份驗證之後。
極品: 我需要做的就是獲取用戶的個人資料(名字,姓氏,顯示名,EMAILID等),以便我可以將用戶存儲到我選擇的數據庫(說Cloudant)和進一步發展應用程序。
期: 我找不到方法(示例代碼)來檢索用戶配置文件。我已經讀過服務器返回的令牌需要被聲明/消費,但沒有一個例子可以找到如何做到這一點。
現有線程: 有計算器上,它類似於我的問題一個thread,但解決方案沒有奏效。我的代碼如下解釋。
我的代碼:
\t Strategy = new OpenIDConnectStrategy({
\t \t \t \t \t authorizationURL : authorization_url,
\t \t \t \t \t tokenURL : token_url,
\t \t \t \t \t clientID : client_id,
\t \t \t \t \t scope: 'openid',
\t \t \t \t \t response_type: 'code',
\t \t \t \t \t clientSecret : client_secret,
\t \t \t \t \t callbackURL : callback_url,
\t \t \t \t \t skipUserProfile: false,
\t \t \t \t \t issuer: issuer_id},
\t \t function(iss, sub, profile, accessToken, refreshToken, params, done) {
\t \t \t \t \t process.nextTick(function() {
\t \t \t profile.accessToken = accessToken;
\t \t \t profile.refreshToken = refreshToken;
\t \t \t done(null, profile);
\t \t \t \t })
\t });
\t passport.use(Strategy);
}
app.get('/',ensureAuthenticated, function(req, res){});
app.get('/login', passport.authenticate('openidconnect', {successRedirect: '/hello',failureRedirect: '/failure'}));
app.get('/hello', function(req, res) {
\t console.log("Pooshon1: ",JSON.stringify(req.user));
\t console.log("Pooshon3: ",JSON.stringify(req.profile));
\t res.send('Hello, ');
\t //res.send('Hello, '+ req.user.displayName + '!');
\t //res.send('Hello, '+ req.user['id'] + '!');
});
app.get('/failure', function(req, res) {
\t res.send('login failed');
});
我沒有把整個代碼,只是什麼是相關的。因此,護照返回done(null, profile)
,而我在互聯網上閱讀的內容是該配置文件對象由服務器返回並可在請求對象中找到。在我上面的代碼中,在app.get("/hello"
....兩個console.log語句打印"Pooshon: undefined"
,這意味着沒有什麼像req.user
或req.profile
因此最後兩行是註釋,因爲它會引發內部服務器錯誤(500)。
如果有人做過這樣的事情,請幫忙。
請注意, 'res.redirect( '/歡迎'); ' 只有在您評論res.send()語句後才能使用。 一旦你找到了你需要的用戶對象,那'res.send()'就沒有用了,那麼你需要實際重定向到一個合適的路由。如果您使用的是模板引擎,請將用戶對象或其所需的一些屬性傳遞給路由。 例如,如果您使用EJS: 'res.render(「/ welcome」,{user:req.session.passport.user});'' –