我有一个令牌,它是由...发出的。Identity Server
(IDP),然后有一个 NodeJS
应用程序,并且我想在NodeJS API中验证该token?
我正试图在NodeJS API中使用 何塞 (基于 这个)但我不知道如何使用它.是否可以做到这一点?
请注意。
在我的 ASP NET CORE API
在这里,是作为一个客户端,我只需要添加以下命令在 startup class
来验证我的 API
?
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
// base-address of your identityserver
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
// name of the API resource
options.ApiName = "api1";
// options.ApiSecret = "xxx";
});
在我的NodeJS api,是作为一个客户端,像上面的web api,我应该做什么?
更新。
我访问了这篇文章,但我没有帮助我!Identity Server 4 for NodeJS API
如果你只想验证你的token,你可以使用下面的方法。包裹:
npm install token-introspection --save
这个包配置了端点和客户端凭证,并返回一个函数。使用 token 和可选的 token_type_hint 调用该函数将返回一个 Promise。
const tokenIntrospection = require('token-introspection')({
endpoint: 'https://example.com/introspect',
client_id: '<Client ID>',
client_secret: '<Client Secret>',
});
tokenIntrospection(token).then(console.log).catch(console.warn);
例子......这是一个中间件,用于验证终端和客户端的证书,并返回一个函数。
这是一个验证token的中间件。
module.exports = (req, res, next) => {
const token = "wEvxS0y2TkvCjLpKP33oGTK0BcKUb6MHt1u3AeMu8h4"; // get your token from your request
const tokenIntrospection = require('token-introspection')({
endpoint: 'http://localhost:5000/connect/introspect',
client_id: 'api1',
client_secret: 'apisecret',
});
tokenIntrospection(token).then(result => {
console.log(result);
next();
}).catch(console.warn);
}
然后你可以像下面这样使用它。
const auth = require('./atuh')
app.get('/', auth, (req, res, next) => {
res.send("Hi");
})