2014-11-06 23:59:48 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2014-11-06 23:59:48 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2019-01-28 15:40:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @mixin @node-red/editor-api_auth
|
|
|
|
*/
|
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
var passport = require("passport");
|
|
|
|
var oauth2orize = require("oauth2orize");
|
|
|
|
|
|
|
|
var strategies = require("./strategies");
|
2014-12-10 15:16:07 +01:00
|
|
|
var Tokens = require("./tokens");
|
|
|
|
var Users = require("./users");
|
2015-02-06 00:43:35 +01:00
|
|
|
var permissions = require("./permissions");
|
2014-11-06 23:59:48 +01:00
|
|
|
|
2017-08-22 23:26:29 +02:00
|
|
|
var theme = require("../editor/theme");
|
2015-04-13 11:37:30 +02:00
|
|
|
|
2015-02-06 00:43:35 +01:00
|
|
|
var settings = null;
|
2018-08-04 23:23:06 +02:00
|
|
|
var log = require("@node-red/util").log; // TODO: separate module
|
2015-01-28 23:41:13 +01:00
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
|
|
|
|
passport.use(strategies.bearerStrategy.BearerStrategy);
|
|
|
|
passport.use(strategies.clientPasswordStrategy.ClientPasswordStrategy);
|
2014-12-10 15:16:07 +01:00
|
|
|
passport.use(strategies.anonymousStrategy);
|
2014-11-06 23:59:48 +01:00
|
|
|
|
|
|
|
var server = oauth2orize.createServer();
|
|
|
|
|
|
|
|
server.exchange(oauth2orize.exchange.password(strategies.passwordTokenExchange));
|
|
|
|
|
2018-04-19 12:23:08 +02:00
|
|
|
function init(_settings,storage) {
|
|
|
|
settings = _settings;
|
2015-01-28 23:41:13 +01:00
|
|
|
if (settings.adminAuth) {
|
2018-09-17 19:18:36 +02:00
|
|
|
var mergedAdminAuth = Object.assign({}, settings.adminAuth, settings.adminAuth.module);
|
2018-09-11 18:44:18 +02:00
|
|
|
Users.init(mergedAdminAuth);
|
2018-10-22 17:21:02 +02:00
|
|
|
Tokens.init(mergedAdminAuth,storage);
|
2015-01-28 23:41:13 +01:00
|
|
|
}
|
2014-12-10 15:16:07 +01:00
|
|
|
}
|
2019-01-28 15:40:24 +01:00
|
|
|
/**
|
|
|
|
* Returns an Express middleware function that ensures the user making a request
|
|
|
|
* has the necessary permission.
|
|
|
|
*
|
|
|
|
* @param {String} permission - the permission required for the request, such as `flows.write`
|
|
|
|
* @return {Function} - an Express middleware
|
|
|
|
* @memberof @node-red/editor-api_auth
|
|
|
|
*/
|
2015-02-06 00:43:35 +01:00
|
|
|
function needsPermission(permission) {
|
|
|
|
return function(req,res,next) {
|
2015-03-16 14:58:01 +01:00
|
|
|
if (settings && settings.adminAuth) {
|
2015-02-06 00:43:35 +01:00
|
|
|
return passport.authenticate(['bearer','anon'],{ session: false })(req,res,function() {
|
|
|
|
if (!req.user) {
|
|
|
|
return next();
|
|
|
|
}
|
2015-03-29 22:59:48 +02:00
|
|
|
if (permissions.hasPermission(req.authInfo.scope,permission)) {
|
2015-02-06 00:43:35 +01:00
|
|
|
return next();
|
|
|
|
}
|
2016-04-10 16:23:03 +02:00
|
|
|
log.audit({event: "permission.fail", permissions: permission},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
return res.status(401).end();
|
2015-02-06 00:43:35 +01:00
|
|
|
});
|
2014-11-06 23:59:48 +01:00
|
|
|
} else {
|
2015-02-06 00:43:35 +01:00
|
|
|
next();
|
2014-11-06 23:59:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureClientSecret(req,res,next) {
|
|
|
|
if (!req.body.client_secret) {
|
|
|
|
req.body.client_secret = 'not_available';
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
function authenticateClient(req,res,next) {
|
|
|
|
return passport.authenticate(['oauth2-client-password'], {session: false})(req,res,next);
|
|
|
|
}
|
|
|
|
function getToken(req,res,next) {
|
|
|
|
return server.token()(req,res,next);
|
|
|
|
}
|
|
|
|
|
2014-11-11 11:15:02 +01:00
|
|
|
function login(req,res) {
|
2015-03-25 21:32:40 +01:00
|
|
|
var response = {};
|
|
|
|
if (settings.adminAuth) {
|
2018-09-17 19:18:36 +02:00
|
|
|
var mergedAdminAuth = Object.assign({}, settings.adminAuth, settings.adminAuth.module);
|
2018-09-11 18:44:18 +02:00
|
|
|
if (mergedAdminAuth.type === "credentials") {
|
2017-04-12 11:09:03 +02:00
|
|
|
response = {
|
|
|
|
"type":"credentials",
|
2017-08-07 11:00:28 +02:00
|
|
|
"prompts":[{id:"username",type:"text",label:"user.username"},{id:"password",type:"password",label:"user.password"}]
|
2017-04-12 11:09:03 +02:00
|
|
|
}
|
2018-09-11 18:44:18 +02:00
|
|
|
} else if (mergedAdminAuth.type === "strategy") {
|
2017-12-04 22:13:07 +01:00
|
|
|
|
|
|
|
var urlPrefix = (settings.httpAdminRoot==='/')?"":settings.httpAdminRoot;
|
2017-04-12 11:09:03 +02:00
|
|
|
response = {
|
2017-04-21 22:54:48 +02:00
|
|
|
"type":"strategy",
|
2018-09-11 18:44:18 +02:00
|
|
|
"prompts":[{type:"button",label:mergedAdminAuth.strategy.label, url: urlPrefix + "auth/strategy"}]
|
2017-04-12 11:09:03 +02:00
|
|
|
}
|
2018-09-11 18:44:18 +02:00
|
|
|
if (mergedAdminAuth.strategy.icon) {
|
|
|
|
response.prompts[0].icon = mergedAdminAuth.strategy.icon;
|
2017-04-12 11:09:03 +02:00
|
|
|
}
|
2018-09-11 18:44:18 +02:00
|
|
|
if (mergedAdminAuth.strategy.image) {
|
|
|
|
response.prompts[0].image = theme.serveFile('/login/',mergedAdminAuth.strategy.image);
|
2017-04-12 11:09:03 +02:00
|
|
|
}
|
2015-03-25 21:32:40 +01:00
|
|
|
}
|
2015-04-13 11:37:30 +02:00
|
|
|
if (theme.context().login && theme.context().login.image) {
|
|
|
|
response.image = theme.context().login.image;
|
|
|
|
}
|
2014-11-11 11:15:02 +01:00
|
|
|
}
|
|
|
|
res.json(response);
|
|
|
|
}
|
2014-11-06 23:59:48 +01:00
|
|
|
|
2014-11-12 14:21:39 +01:00
|
|
|
function revoke(req,res) {
|
|
|
|
var token = req.body.token;
|
2015-01-28 23:41:13 +01:00
|
|
|
// TODO: audit log
|
2014-12-10 15:16:07 +01:00
|
|
|
Tokens.revoke(token).then(function() {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "auth.login.revoke"},req);
|
2017-04-12 22:40:30 +02:00
|
|
|
if (settings.editorTheme && settings.editorTheme.logout && settings.editorTheme.logout.redirect) {
|
|
|
|
res.json({redirect:settings.editorTheme.logout.redirect});
|
|
|
|
} else {
|
|
|
|
res.status(200).end();
|
|
|
|
}
|
2014-11-12 14:21:39 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:17:18 +02:00
|
|
|
function completeVerify(profile,done) {
|
|
|
|
Users.authenticate(profile).then(function(user) {
|
|
|
|
if (user) {
|
|
|
|
Tokens.create(user.username,"node-red-editor",user.permissions).then(function(tokens) {
|
|
|
|
log.audit({event: "auth.login",username:user.username,scope:user.permissions});
|
|
|
|
user.tokens = tokens;
|
|
|
|
done(null,user);
|
|
|
|
});
|
|
|
|
} else {
|
2017-04-21 22:54:48 +02:00
|
|
|
log.audit({event: "auth.login.fail.oauth",username:typeof profile === "string"?profile:profile.username});
|
2017-04-21 22:17:18 +02:00
|
|
|
done(null,false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-24 00:08:11 +01:00
|
|
|
|
|
|
|
function genericStrategy(adminApp,strategy) {
|
|
|
|
var crypto = require("crypto")
|
|
|
|
var session = require('express-session')
|
|
|
|
var MemoryStore = require('memorystore')(session)
|
|
|
|
|
|
|
|
adminApp.use(session({
|
|
|
|
// As the session is only used across the life-span of an auth
|
|
|
|
// hand-shake, we can use a instance specific random string
|
|
|
|
secret: crypto.randomBytes(20).toString('hex'),
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
|
|
|
store: new MemoryStore({
|
|
|
|
checkPeriod: 86400000 // prune expired entries every 24h
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
//TODO: all passport references ought to be in ./auth
|
|
|
|
adminApp.use(passport.initialize());
|
|
|
|
adminApp.use(passport.session());
|
|
|
|
|
|
|
|
var options = strategy.options;
|
|
|
|
|
|
|
|
passport.use(new strategy.strategy(options,
|
|
|
|
function() {
|
|
|
|
var originalDone = arguments[arguments.length-1];
|
|
|
|
if (options.verify) {
|
|
|
|
var args = Array.from(arguments);
|
|
|
|
args[args.length-1] = function(err,profile) {
|
|
|
|
if (err) {
|
|
|
|
return originalDone(err);
|
|
|
|
} else {
|
|
|
|
return completeVerify(profile,originalDone);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
options.verify.apply(null,args);
|
|
|
|
} else {
|
|
|
|
var profile = arguments[arguments.length - 2];
|
|
|
|
return completeVerify(profile,originalDone);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
adminApp.get('/auth/strategy',
|
|
|
|
passport.authenticate(strategy.name, {session:false, failureRedirect: settings.httpAdminRoot }),
|
|
|
|
completeGenerateStrategyAuth
|
|
|
|
);
|
2018-12-13 14:43:57 +01:00
|
|
|
|
|
|
|
var callbackMethodFunc = adminApp.get;
|
|
|
|
if (/^post$/i.test(options.callbackMethod)) {
|
|
|
|
callbackMethodFunc = adminApp.post;
|
|
|
|
}
|
2019-02-12 11:45:38 +01:00
|
|
|
callbackMethodFunc.call(adminApp,'/auth/strategy/callback',
|
2018-01-24 00:08:11 +01:00
|
|
|
passport.authenticate(strategy.name, {session:false, failureRedirect: settings.httpAdminRoot }),
|
|
|
|
completeGenerateStrategyAuth
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
function completeGenerateStrategyAuth(req,res) {
|
|
|
|
var tokens = req.user.tokens;
|
|
|
|
delete req.user.tokens;
|
|
|
|
// Successful authentication, redirect home.
|
|
|
|
res.redirect(settings.httpAdminRoot + '?access_token='+tokens.accessToken);
|
|
|
|
}
|
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
module.exports = {
|
2014-12-10 15:16:07 +01:00
|
|
|
init: init,
|
2015-02-06 00:43:35 +01:00
|
|
|
needsPermission: needsPermission,
|
2014-11-06 23:59:48 +01:00
|
|
|
ensureClientSecret: ensureClientSecret,
|
|
|
|
authenticateClient: authenticateClient,
|
|
|
|
getToken: getToken,
|
2015-01-28 23:41:13 +01:00
|
|
|
errorHandler: function(err,req,res,next) {
|
|
|
|
//TODO: audit log statment
|
2015-03-29 23:27:07 +02:00
|
|
|
//console.log(err.stack);
|
2015-01-28 23:41:13 +01:00
|
|
|
//log.log({level:"audit",type:"auth",msg:err.toString()});
|
|
|
|
return server.errorHandler()(err,req,res,next);
|
|
|
|
},
|
2014-11-12 14:21:39 +01:00
|
|
|
login: login,
|
2017-04-12 11:09:03 +02:00
|
|
|
revoke: revoke,
|
2018-01-24 00:08:11 +01:00
|
|
|
genericStrategy: genericStrategy
|
2014-11-06 23:59:48 +01:00
|
|
|
}
|