Details
-
Type: Monitor
-
Status: Closed
-
Priority: Major
-
Resolution: Done
-
Affects Version/s: None
-
Fix Version/s: 2021
-
Component/s: FIWARE-TECH-HELP
-
HD-Chapter:Security
-
HD-Enabler:AuthZForce
Description
Created question in FIWARE Q/A platform on 28-04-2015 at 15:04
Please, ANSWER this question AT http://stackoverflow.com/questions/29921471/fiware-oauth2-authentication-in-node-js
Question:
FIWARE OAuth2 Authentication in Node JS
Description:
I'm trying to authenticate users in Fiware from Node JS.
I've created a server code that calls Oauth2 and when running it, it redirect you to fiware page, but when logging in Fiware shows nothing, and then when checking /user_info page it gives access token is null
here is my 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;
$
The oauth2.js file is the one from : https://github.com/ging/oauth2-example-client/blob/master/oauth2.js
The code that calls Oauth2 is the following:
$
/**
- 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',
);
});
app.get('/about', function (req, res)
);
}*/
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)
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)
);
});
// 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)
// Using the access token asks the IDM for the user info
oa.get(url, req.session.access_token, function (e, response)
);
});
// Handles logout requests to remove access_token from the session cookie
app.get('/logout', function(req, res){
req.session.access_token = undefined;
res.redirect('/');
});
$
when running it keeps giving access_token is null
2015-10-08 21:05|CREATED monitor | # answers= 2, accepted answer= True