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:
parent
6104bb98f1
commit
50e2dcbcd5
36
red/api/auth/api-access-tokens.js
Normal file
36
red/api/auth/api-access-tokens.js
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,21 +31,27 @@ var log;
|
|||||||
|
|
||||||
var bearerStrategy = function (accessToken, done) {
|
var bearerStrategy = function (accessToken, done) {
|
||||||
// is this a valid token?
|
// is this a valid token?
|
||||||
Tokens.get(accessToken).then(function(token) {
|
apiAccessTokens.get(accessToken).then(function(tokenInfo) {
|
||||||
if (token) {
|
if (tokenInfo && tokenInfo.username && tokenInfo.scope) {
|
||||||
Users.get(token.user).then(function(user) {
|
done(null, tokenInfo.username,{scope:tokenInfo.scope});
|
||||||
if (user) {
|
} else {
|
||||||
done(null,user,{scope:token.scope});
|
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 {
|
} 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);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user