mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Merge the logic for api access token to tokens.js so as not to change strategies.js
This commit is contained in:
parent
e982f5076f
commit
b14a0e0dde
@ -1,36 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
||||||
*
|
|
||||||
* 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 generatedTokens;
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
init: function(apiAccessTokensSettings) {
|
|
||||||
generatedTokens = {};
|
|
||||||
if ( Array.isArray(apiAccessTokensSettings) ) {
|
|
||||||
generatedTokens = apiAccessTokensSettings.reduce(function(prev, current) {
|
|
||||||
prev[current.token] = {
|
|
||||||
username: current.username,
|
|
||||||
scope: current.permissions
|
|
||||||
};
|
|
||||||
return prev;
|
|
||||||
}, {});
|
|
||||||
}
|
|
||||||
return Promise.resolve();
|
|
||||||
},
|
|
||||||
get: function(token) {
|
|
||||||
var info = generatedTokens[token] || null;
|
|
||||||
return Promise.resolve(info);
|
|
||||||
}
|
|
||||||
}
|
|
@ -19,7 +19,6 @@ var oauth2orize = require("oauth2orize");
|
|||||||
|
|
||||||
var strategies = require("./strategies");
|
var strategies = require("./strategies");
|
||||||
var Tokens = require("./tokens");
|
var Tokens = require("./tokens");
|
||||||
var apiAccessTokens = require("./api-access-tokens");
|
|
||||||
var Users = require("./users");
|
var Users = require("./users");
|
||||||
var permissions = require("./permissions");
|
var permissions = require("./permissions");
|
||||||
|
|
||||||
@ -41,9 +40,8 @@ function init(runtime) {
|
|||||||
settings = runtime.settings;
|
settings = runtime.settings;
|
||||||
log = runtime.log;
|
log = runtime.log;
|
||||||
if (settings.adminAuth) {
|
if (settings.adminAuth) {
|
||||||
Users.init(settings.adminAuth);
|
Users.init(settings.adminAuth,settings.apiAccessTokens);
|
||||||
Tokens.init(settings.adminAuth,runtime.storage);
|
Tokens.init(settings.adminAuth,runtime.storage,settings.apiAccessTokens);
|
||||||
apiAccessTokens.init(settings.apiAccessTokens);
|
|
||||||
strategies.init(runtime);
|
strategies.init(runtime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,6 @@ var crypto = require("crypto");
|
|||||||
var util = require("util");
|
var util = require("util");
|
||||||
|
|
||||||
var Tokens = require("./tokens");
|
var Tokens = require("./tokens");
|
||||||
var apiAccessTokens = require("./api-access-tokens");
|
|
||||||
var Users = require("./users");
|
var Users = require("./users");
|
||||||
var Clients = require("./clients");
|
var Clients = require("./clients");
|
||||||
var permissions = require("./permissions");
|
var permissions = require("./permissions");
|
||||||
@ -31,27 +30,21 @@ var log;
|
|||||||
|
|
||||||
var bearerStrategy = function (accessToken, done) {
|
var bearerStrategy = function (accessToken, done) {
|
||||||
// is this a valid token?
|
// is this a valid token?
|
||||||
apiAccessTokens.get(accessToken).then(function(tokenInfo) {
|
Tokens.get(accessToken).then(function(token) {
|
||||||
if (tokenInfo && tokenInfo.username && tokenInfo.scope) {
|
if (token) {
|
||||||
done(null, tokenInfo.username,{scope:tokenInfo.scope});
|
Users.get(token.user).then(function(user) {
|
||||||
} else {
|
if (user) {
|
||||||
Tokens.get(accessToken).then(function(token) {
|
done(null,user,{scope:token.scope});
|
||||||
if (token) {
|
|
||||||
Users.get(token.user).then(function(user) {
|
|
||||||
if (user) {
|
|
||||||
done(null,user,{scope:token.scope});
|
|
||||||
} else {
|
|
||||||
log.audit({event: "auth.invalid-token"});
|
|
||||||
done(null,false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
log.audit({event: "auth.invalid-token"});
|
log.audit({event: "auth.invalid-token"});
|
||||||
done(null,false);
|
done(null,false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
log.audit({event: "auth.invalid-token"});
|
||||||
|
done(null,false);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
bearerStrategy.BearerStrategy = new BearerStrategy(bearerStrategy);
|
bearerStrategy.BearerStrategy = new BearerStrategy(bearerStrategy);
|
||||||
|
|
||||||
|
@ -32,6 +32,8 @@ var sessions = {};
|
|||||||
|
|
||||||
var loadedSessions = null;
|
var loadedSessions = null;
|
||||||
|
|
||||||
|
var apiAccessTokens;
|
||||||
|
|
||||||
function expireSessions() {
|
function expireSessions() {
|
||||||
var now = Date.now();
|
var now = Date.now();
|
||||||
var modified = false;
|
var modified = false;
|
||||||
@ -61,22 +63,39 @@ function loadSessions() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init: function(adminAuthSettings, _storage) {
|
init: function(adminAuthSettings, _storage, apiAccessTokensSettings) {
|
||||||
storage = _storage;
|
storage = _storage;
|
||||||
sessionExpiryTime = adminAuthSettings.sessionExpiryTime || 604800; // 1 week in seconds
|
sessionExpiryTime = adminAuthSettings.sessionExpiryTime || 604800; // 1 week in seconds
|
||||||
// At this point, storage will not have been initialised, so defer loading
|
// At this point, storage will not have been initialised, so defer loading
|
||||||
// the sessions until there's a request for them.
|
// the sessions until there's a request for them.
|
||||||
loadedSessions = null;
|
loadedSessions = null;
|
||||||
|
|
||||||
|
apiAccessTokens = {};
|
||||||
|
if ( Array.isArray(apiAccessTokensSettings) ) {
|
||||||
|
apiAccessTokens = apiAccessTokensSettings.reduce(function(prev, current) {
|
||||||
|
prev[current.token] = {
|
||||||
|
user: current.username,
|
||||||
|
scope: current.permissions
|
||||||
|
};
|
||||||
|
return prev;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
get: function(token) {
|
get: function(token) {
|
||||||
return loadSessions().then(function() {
|
return loadSessions().then(function() {
|
||||||
if (sessions[token]) {
|
var info = apiAccessTokens[token] || null;
|
||||||
if (sessions[token].expires < Date.now()) {
|
|
||||||
return expireSessions().then(function() { return null });
|
if (info) {
|
||||||
|
return Promise.resolve(info);
|
||||||
|
} else {
|
||||||
|
if (sessions[token]) {
|
||||||
|
if (sessions[token].expires < Date.now()) {
|
||||||
|
return expireSessions().then(function() { return null });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return Promise.resolve(sessions[token]);
|
||||||
}
|
}
|
||||||
return Promise.resolve(sessions[token]);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
create: function(user,client,scope) {
|
create: function(user,client,scope) {
|
||||||
|
@ -57,18 +57,35 @@ function getDefaultUser() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var api = {
|
var api = {
|
||||||
get: get,
|
get: wrapperGetUserFromSettings(get),
|
||||||
authenticate: authenticate,
|
authenticate: authenticate,
|
||||||
default: getDefaultUser
|
default: getDefaultUser
|
||||||
}
|
}
|
||||||
|
|
||||||
function init(config) {
|
var apiAccessUsers = {};
|
||||||
|
function wrapperGetUserFromSettings (getFunc) {
|
||||||
|
return function (username) {
|
||||||
|
if (apiAccessUsers[username]) {
|
||||||
|
return Promise.resolve(apiAccessUsers[username]);
|
||||||
|
} else {
|
||||||
|
return getFunc(username);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function init(config, apiAccessTokensSettings) {
|
||||||
users = {};
|
users = {};
|
||||||
defaultUser = null;
|
defaultUser = null;
|
||||||
|
apiAccessUsers = apiAccessTokensSettings.reduce(function (prev, current) {
|
||||||
|
if (current.username) {
|
||||||
|
prev[current.username] = current.username;
|
||||||
|
}
|
||||||
|
return prev;
|
||||||
|
}, {});
|
||||||
if (config.type == "credentials" || config.type == "strategy") {
|
if (config.type == "credentials" || config.type == "strategy") {
|
||||||
if (config.users) {
|
if (config.users) {
|
||||||
if (typeof config.users === "function") {
|
if (typeof config.users === "function") {
|
||||||
api.get = config.users;
|
api.get = wrapperGetUserFromSettings(config.users);
|
||||||
} else {
|
} else {
|
||||||
var us = config.users;
|
var us = config.users;
|
||||||
/* istanbul ignore else */
|
/* istanbul ignore else */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user