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 Tokens = require("./tokens");
var apiAccessTokens = require("./api-access-tokens");
var Users = require("./users");
var permissions = require("./permissions");
@ -42,6 +43,7 @@ function init(runtime) {
if (settings.adminAuth) {
Users.init(settings.adminAuth);
Tokens.init(settings.adminAuth,runtime.storage);
apiAccessTokens.init(settings.apiAccessTokens);
strategies.init(runtime);
}
}

View File

@ -22,6 +22,7 @@ var crypto = require("crypto");
var util = require("util");
var Tokens = require("./tokens");
var apiAccessTokens = require("./api-access-tokens");
var Users = require("./users");
var Clients = require("./clients");
var permissions = require("./permissions");
@ -30,21 +31,27 @@ var log;
var bearerStrategy = function (accessToken, done) {
// is this a valid token?
Tokens.get(accessToken).then(function(token) {
if (token) {
Users.get(token.user).then(function(user) {
if (user) {
done(null,user,{scope:token.scope});
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) {
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 {
log.audit({event: "auth.invalid-token"});
done(null,false);
}
});
} else {
log.audit({event: "auth.invalid-token"});
done(null,false);
}
});
})
}
bearerStrategy.BearerStrategy = new BearerStrategy(bearerStrategy);