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

Add a processing to check specified API Access Tokens

This commit is contained in:
Hideki Nakamura 2018-07-05 18:58:02 -07:00
parent 6104bb98f1
commit 50e2dcbcd5
3 changed files with 54 additions and 9 deletions

View File

@ -0,0 +1,36 @@
/**
* 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);
}
}

View File

@ -19,6 +19,7 @@ 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");
@ -42,6 +43,7 @@ function init(runtime) {
if (settings.adminAuth) { if (settings.adminAuth) {
Users.init(settings.adminAuth); Users.init(settings.adminAuth);
Tokens.init(settings.adminAuth,runtime.storage); Tokens.init(settings.adminAuth,runtime.storage);
apiAccessTokens.init(settings.apiAccessTokens);
strategies.init(runtime); strategies.init(runtime);
} }
} }

View File

@ -22,6 +22,7 @@ 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");
@ -30,6 +31,10 @@ 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) {
if (tokenInfo && tokenInfo.username && tokenInfo.scope) {
done(null, tokenInfo.username,{scope:tokenInfo.scope});
} else {
Tokens.get(accessToken).then(function(token) { Tokens.get(accessToken).then(function(token) {
if (token) { if (token) {
Users.get(token.user).then(function(user) { Users.get(token.user).then(function(user) {
@ -46,6 +51,8 @@ var bearerStrategy = function (accessToken, done) {
} }
}); });
} }
})
}
bearerStrategy.BearerStrategy = new BearerStrategy(bearerStrategy); bearerStrategy.BearerStrategy = new BearerStrategy(bearerStrategy);
var clientPasswordStrategy = function(clientId, clientSecret, done) { var clientPasswordStrategy = function(clientId, clientSecret, done) {