【问题标题】:FIWARE OAuth2 Authentication in Node JSNode JS 中的 FIWARE OAuth2 身份验证
【发布时间】:2015-05-05 22:06:55
【问题描述】:

我正在尝试通过 Node JS 对 Fiware 中的用户进行身份验证。 我创建了一个调用 Oauth2 的服务器代码,在运行它时,它会将您重定向到 fiware 页面,但是当登录 Fiware 时什么都没有显示,然后在检查 /user_info 页面时它给出的访问令牌为 null

这是我的 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 为空

【问题讨论】:

    标签: node.js oauth-2.0 fiware


    【解决方案1】:

    这个

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

    回调 URL 错误。

    编辑: 修改后,你的回调还是错误的:

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

    IdM 应该能够调用回调 URL。如果在 localhost 会怎么做呢?

    【讨论】:

    • 感谢回复:我都试过了:config.callbackURL = '192.168.1.41:app_port/login';或 config.callbackURL = 'public_ip:router_port/login';但仍然不起作用并给我无效的授权请求
    • 现在我得到:“我们很抱歉,但出了点问题。”登录后
    • 最终被调用的 url 是:account.lab.fiware.org/oauth2/… 有什么想法吗?
    • 我很想知道你的意见@LeandroGuillen
    • 就像@GuilleJ 所说,可能是您的应用程序正在侦听的端口无法访问,无论它们是在本地机器中还是在具有公共 IP 的云环境中(例如)。
    【解决方案2】:

    我认为您必须按照前面所示配置回调:

    config.callbackURL = 'public_ip:app_port/login'

    并且您应该检查您的 VM 安全组以确保您的 app_port 已打开(如果您将服务器部署到云中),否则,如果您在本地计算机上,请检查您的路由器端口是否可以从外部访问。

    我看到您正在设置此端口: app.set('port', process.env.PORT || 13299);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-17
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多