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.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var BearerStrategy = require('passport-http-bearer').Strategy;
|
|
|
|
var ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy;
|
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
var passport = require("passport");
|
2014-11-06 23:59:48 +01:00
|
|
|
var crypto = require("crypto");
|
2014-12-10 15:16:07 +01:00
|
|
|
var util = require("util");
|
2015-01-28 23:41:13 +01:00
|
|
|
|
2014-12-08 17:20:08 +01:00
|
|
|
var Tokens = require("./tokens");
|
|
|
|
var Users = require("./users");
|
|
|
|
var Clients = require("./clients");
|
2015-03-29 22:59:48 +02:00
|
|
|
var permissions = require("./permissions");
|
2014-11-06 23:59:48 +01:00
|
|
|
|
2018-08-04 23:23:06 +02:00
|
|
|
var log = require("@node-red/util").log; // TODO: separate module
|
2015-05-14 15:22:28 +02:00
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
var bearerStrategy = function (accessToken, done) {
|
|
|
|
// is this a valid token?
|
2014-12-08 17:20:08 +01:00
|
|
|
Tokens.get(accessToken).then(function(token) {
|
2014-11-06 23:59:48 +01:00
|
|
|
if (token) {
|
2014-12-08 17:20:08 +01:00
|
|
|
Users.get(token.user).then(function(user) {
|
2014-11-06 23:59:48 +01:00
|
|
|
if (user) {
|
2014-12-10 15:16:07 +01:00
|
|
|
done(null,user,{scope:token.scope});
|
2014-11-06 23:59:48 +01:00
|
|
|
} else {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "auth.invalid-token"});
|
2014-11-06 23:59:48 +01:00
|
|
|
done(null,false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "auth.invalid-token"});
|
2014-11-06 23:59:48 +01:00
|
|
|
done(null,false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
bearerStrategy.BearerStrategy = new BearerStrategy(bearerStrategy);
|
|
|
|
|
|
|
|
var clientPasswordStrategy = function(clientId, clientSecret, done) {
|
2014-12-08 17:20:08 +01:00
|
|
|
Clients.get(clientId).then(function(client) {
|
2014-11-06 23:59:48 +01:00
|
|
|
if (client && client.secret == clientSecret) {
|
|
|
|
done(null,client);
|
|
|
|
} else {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "auth.invalid-client",client:clientId});
|
2014-11-06 23:59:48 +01:00
|
|
|
done(null,false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
clientPasswordStrategy.ClientPasswordStrategy = new ClientPasswordStrategy(clientPasswordStrategy);
|
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
var loginAttempts = [];
|
2015-03-09 21:01:29 +01:00
|
|
|
var loginSignInWindow = 600000; // 10 minutes
|
2015-01-28 23:41:13 +01:00
|
|
|
|
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
var passwordTokenExchange = function(client, username, password, scope, done) {
|
2015-01-28 23:41:13 +01:00
|
|
|
var now = Date.now();
|
|
|
|
loginAttempts = loginAttempts.filter(function(logEntry) {
|
2015-03-09 21:01:29 +01:00
|
|
|
return logEntry.time + loginSignInWindow > now;
|
2015-01-28 23:41:13 +01:00
|
|
|
});
|
|
|
|
loginAttempts.push({time:now, user:username});
|
|
|
|
var attemptCount = 0;
|
|
|
|
loginAttempts.forEach(function(logEntry) {
|
2015-03-07 14:22:21 +01:00
|
|
|
/* istanbul ignore else */
|
2015-01-28 23:41:13 +01:00
|
|
|
if (logEntry.user == username) {
|
|
|
|
attemptCount++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (attemptCount > 5) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "auth.login.fail.too-many-attempts",username:username,client:client.id});
|
2015-01-28 23:41:13 +01:00
|
|
|
done(new Error("Too many login attempts. Wait 10 minutes and try again"),false);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-07 14:22:21 +01:00
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
Users.authenticate(username,password).then(function(user) {
|
|
|
|
if (user) {
|
2015-07-15 11:11:16 +02:00
|
|
|
if (scope === "") {
|
|
|
|
scope = user.permissions;
|
|
|
|
}
|
2015-03-29 23:27:07 +02:00
|
|
|
if (permissions.hasPermission(user.permissions,scope)) {
|
2015-03-29 22:59:48 +02:00
|
|
|
loginAttempts = loginAttempts.filter(function(logEntry) {
|
|
|
|
return logEntry.user !== username;
|
|
|
|
});
|
|
|
|
Tokens.create(username,client.id,scope).then(function(tokens) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "auth.login",username:username,client:client.id,scope:scope});
|
2015-03-30 15:14:32 +02:00
|
|
|
done(null,tokens.accessToken,null,{expires_in:tokens.expires_in});
|
2015-03-29 22:59:48 +02:00
|
|
|
});
|
|
|
|
} else {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "auth.login.fail.permissions",username:username,client:client.id,scope:scope});
|
2015-03-29 22:59:48 +02:00
|
|
|
done(null,false);
|
|
|
|
}
|
2015-01-28 23:41:13 +01:00
|
|
|
} else {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "auth.login.fail.credentials",username:username,client:client.id,scope:scope});
|
2015-01-28 23:41:13 +01:00
|
|
|
done(null,false);
|
|
|
|
}
|
|
|
|
});
|
2014-11-06 23:59:48 +01:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:07 +01:00
|
|
|
function AnonymousStrategy() {
|
|
|
|
passport.Strategy.call(this);
|
|
|
|
this.name = 'anon';
|
|
|
|
}
|
|
|
|
util.inherits(AnonymousStrategy, passport.Strategy);
|
|
|
|
AnonymousStrategy.prototype.authenticate = function(req) {
|
|
|
|
var self = this;
|
2015-01-28 23:41:13 +01:00
|
|
|
Users.default().then(function(anon) {
|
2014-12-10 15:16:07 +01:00
|
|
|
if (anon) {
|
2015-03-29 23:27:07 +02:00
|
|
|
self.success(anon,{scope:anon.permissions});
|
2014-12-10 15:16:07 +01:00
|
|
|
} else {
|
|
|
|
self.fail(401);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-26 04:59:40 +01:00
|
|
|
function TokensStrategy() {
|
|
|
|
passport.Strategy.call(this);
|
|
|
|
this.name = 'tokens';
|
|
|
|
}
|
|
|
|
util.inherits(TokensStrategy, passport.Strategy);
|
|
|
|
TokensStrategy.prototype.authenticate = function(req) {
|
|
|
|
var self = this;
|
|
|
|
var token = req.headers[Users.tokenHeader()];
|
|
|
|
if (token) {
|
|
|
|
Users.tokens(token).then(function(admin) {
|
|
|
|
if (admin) {
|
|
|
|
self.success(admin,{scope:admin.permissions});
|
|
|
|
} else {
|
|
|
|
self.fail(401);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.fail(401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
module.exports = {
|
|
|
|
bearerStrategy: bearerStrategy,
|
|
|
|
clientPasswordStrategy: clientPasswordStrategy,
|
2014-12-10 15:16:07 +01:00
|
|
|
passwordTokenExchange: passwordTokenExchange,
|
2020-02-26 04:59:40 +01:00
|
|
|
anonymousStrategy: new AnonymousStrategy(),
|
|
|
|
tokensStrategy: new TokensStrategy()
|
2014-11-06 23:59:48 +01:00
|
|
|
}
|