1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/red/api/auth/strategies.js

118 lines
3.6 KiB
JavaScript
Raw Normal View History

2014-11-06 23:59:48 +01:00
/**
2014-12-10 15:58:53 +01:00
* Copyright 2015 IBM Corp.
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;
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");
var Tokens = require("./tokens");
var Users = require("./users");
var Clients = require("./clients");
2014-11-06 23:59:48 +01:00
var bearerStrategy = function (accessToken, done) {
// is this a valid token?
Tokens.get(accessToken).then(function(token) {
2014-11-06 23:59:48 +01:00
if (token) {
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 {
done(null,false);
}
});
} else {
done(null,false);
}
});
}
bearerStrategy.BearerStrategy = new BearerStrategy(bearerStrategy);
var clientPasswordStrategy = function(clientId, clientSecret, done) {
Clients.get(clientId).then(function(client) {
2014-11-06 23:59:48 +01:00
if (client && client.secret == clientSecret) {
done(null,client);
} else {
done(null,false);
}
});
}
clientPasswordStrategy.ClientPasswordStrategy = new ClientPasswordStrategy(clientPasswordStrategy);
var loginAttempts = [];
2015-03-09 21:01:29 +01:00
var loginSignInWindow = 600000; // 10 minutes
2014-11-06 23:59:48 +01:00
var passwordTokenExchange = function(client, username, password, scope, done) {
var now = Date.now();
loginAttempts = loginAttempts.filter(function(logEntry) {
2015-03-09 21:01:29 +01:00
return logEntry.time + loginSignInWindow > now;
});
loginAttempts.push({time:now, user:username});
var attemptCount = 0;
loginAttempts.forEach(function(logEntry) {
/* istanbul ignore else */
if (logEntry.user == username) {
attemptCount++;
}
});
if (attemptCount > 5) {
// TODO: audit log
done(new Error("Too many login attempts. Wait 10 minutes and try again"),false);
return;
}
Users.authenticate(username,password).then(function(user) {
if (user) {
loginAttempts = loginAttempts.filter(function(logEntry) {
return logEntry.user !== username;
});
Tokens.create(username,client.id,scope).then(function(tokens) {
// TODO: audit log
done(null,tokens.accessToken);
});
} else {
// TODO: audit log
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;
Users.default().then(function(anon) {
2014-12-10 15:16:07 +01:00
if (anon) {
self.success(anon);
} 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,
anonymousStrategy: new AnonymousStrategy()
2014-11-06 23:59:48 +01:00
}