2015-04-28 77 views
2

我試圖從節點JS對Fiware中的用戶進行身份驗證。 我已經創建了一個調用的oauth2服務器代碼並運行它時,它重定向你fiware頁,但在Fiware登錄時顯示什麼,然後再辦理入住手續時/ USER_INFO頁它給訪問令牌是空節點JS中的FIWARE OAuth2身份驗證

這裏我config.js: $

var config=require('config'); 
config.idmURL = 'http://account.lab.fiware.org'; 
config.client_id = '2456'; 
config.client_secret = '12466'; 
config.callbackURL = 'http://localhost/login'; 

module.exports = config; 

$ 的oauth2.js文件是從一個:https://github.com/ging/oauth2-example-client/blob/master/oauth2.js

調用的oauth2的代碼如下: $

/** 
* Module dependencies. 
*/ 

var express = require('express') 
    , routes = require('./routes') 
    , user = require('./routes/user') 
    , http = require('http') 
    , path = require('path'); 
var OAuth2 = require('./oauth2').OAuth2; 
var config = require('./config'); 
var cookieparser= require('cookie-parser'); 
var expresssession= require('express-session'); 


var app = express(); 
app.use(cookieparser()); 
app.use(expresssession({ 
    secret: "257a57604cb5037dcfc2d42127e1104cb705f92344ff74aabadf14d0248cbe266d3e7d567bf7068645668add108a459b5d9af1917ddc6a47cae82a7a9798ae9d" 
})); 

app.configure(function(){ 
    app.set('port', process.env.PORT || 13299); 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.use(express.favicon()); 
    app.use(express.logger('dev')); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(app.router); 
    app.use(express.static(path.join(__dirname, 'public'))); 
}); 

//code I got from http://www.hanselman.com/blog/WebMatrixAndNodejsTheEasiestWayToGetStartedWithNodeOnWindows.aspx 
/*module.exports = function (app) 
{ 
    app.get('/', function (req, res) 
    { 
     res.render('index', 
     { 
      message: 'Welcome to my site!' 
     }); 
    }); 
    app.get('/about', function (req, res) 
    { 
     res.render('about'); 
    }); 
}*/ 
app.configure('development', function(){ 
    app.use(express.errorHandler()); 
}); 

//app.get('/', routes.index); 
app.get('/users', user.list); 

http.createServer(app).listen(app.get('port'), function(){ 
    console.log("Express server listening on port " + app.get('port')); 
}); 


// ..... 
// Creates oauth library object with the config data 
var oa = new OAuth2(config.client_id, 
        config.client_secret, 
        config.idmURL, 
        '/oauth2/authorize', 
        '/oauth2/token', 
        config.callbackURL); 

// Handles requests to the main page 
app.get('/', function (req, res) 
{ 

    // If auth_token is not stored in a session cookie it sends a button to redirect to IDM authentication portal 
    if (!req.session.access_token) 
    { 
     res.send("Oauth2 IDM Demo.<br><br><button onclick='window.location.href=\"/auth\"'>Log in with FI-WARE Account</button>"); 

     // If auth_token is stored in a session cookie it sends a button to get user info 
    } else 
    { 
     res.send("gghhhhhhhhhhhh"); 
     res.send("Successfully authenticated. <br><br> Your oauth access_token: " + /*req.session.access_token + */"<br><br><button onclick='window.location.href=\"/user_info\"'>Get my user info</button>"); 
    } 
}); 

// Handles requests from IDM with the access code 
app.get('/login', function (req, res) 
{ 

    res.end(req.query.code + "Hello Http, This is the server responding ............"); 

    // Using the access code goes again to the IDM to obtain the access_token 
    oa.getOAuthAccessToken(req.query.code, function (e, results) 
    { 

     // Stores the access_token in a session cookie 
     req.session.access_token = results.access_token; 
     res.end("Hello Http, This is the server responding"); 
     res.send("from inside /login code"); 
     res.redirect('/'); 

    }); 
}); 

// Redirection to IDM authentication portal 
app.get('/auth', function (req, res) 
{ 
    var path = oa.getAuthorizeUrl(); 
    res.redirect(path); 
}); 


// Ask IDM for user info 
app.get('/user_info', function (req, res) 
{ 
    var url = config.idmURL + '/user/'; 
    if (req.session.access_token == null) 
    { 
     res.send("access token is null"); 
    } 
    // Using the access token asks the IDM for the user info 
    oa.get(url, req.session.access_token, function (e, response) 
    { 
     //res.end("hiiiiiiiiiiii5555444"); 
     var user = JSON.parse(response); 
     res.send("Welcome " + user.displayName + "<br> Your email address is " + user.email + "<br><br><button onclick='window.location.href=\"/logout\"'>Log out</button>"); 
    }); 
}); 

// Handles logout requests to remove access_token from the session cookie 
app.get('/logout', function(req, res){ 

    req.session.access_token = undefined; 
    res.redirect('/'); 
}); 

運行時,它不斷給人爲的access_token $

回答

1

config.callbackURL = 'http://http://192.168.1.41/:10251/login'; 

回調URL是錯誤的。

編輯: 編輯後,回調仍然是錯誤的:

config.callbackURL = 'http://localhost/login'; 

IDM中應該是能夠調用回調URL。如果它在本地主機上,它會怎麼做?

+0

感謝您的回覆:我試過兩種: config.callbackURL ='http://192.168.1.41:app_port/login';或 config.callbackURL ='http:// public_ip:router_port/login'; 但仍然不起作用,並給我無效的授權請求 – user3260891

+0

現在我得到:「我們很抱歉,但出了問題。」登錄後 – user3260891

+0

被調用的最終網址是: https://account.lab.fiware.org/oauth2/authorize?response_type=code&client_id=2456&state=xyz&redirect_uri=213.123.216.45:10251//login 任何想法? – user3260891

1

我認爲你必須配置你的回調如先前表明:

config.callbackURL = 'public_ip:app_port/login'

而且你應該檢查你的虛擬機安全組,以確保您的app_port是開放的(如果你部署的服務器到雲)否則,如果你在本地機器上,請檢查你的路由器端口是否可以從外部訪問。

我看到你正在設置這個端口: app.set('port',process.env.PORT || 13299);