Oct*_*scu 0 node.js jwt keycloak keycloak-connect
我有一个 Keycloak 领域,其中一些用户作为 Nodejs + Typescript 项目的 IdP。
如果我按“全部注销”,它们就会从这里消失,但它们仍然有效。
例子:
1) I create a new session. I get its JWT token as a response
2) I do a GET req on one of the protected routes with the token attached. It works.
4) I logout all sessions by pressing the button in that photo
5) I do the same GET req on the same protected route. It still works.
I expect it NOT to work, because I previously logged out all sessions.
Run Code Online (Sandbox Code Playgroud)
这是我的 keycloak-config
import express, {Application} from 'express';
import { Keycloak as KeycloakType } from "keycloak-connect";
var session = require('express-session');
var Keycloak = require('keycloak-connect');
let _keycloak: KeycloakType;
var memoryStore = new session.MemoryStore();
let kcConfig = {
clientId: 'restapi',
bearerOnly: true,
serverUrl: 'http://localhost:8080/auth',
realm: 'supercatalog',
realmPublicKey: 'deleted'
};
function getKeycloak() {
if (_keycloak) {
return _keycloak;
}
console.log("Initializing Keycloak...");
_keycloak = new Keycloak({ store: memoryStore }, kcConfig);
return _keycloak;
}
export {getKeycloak, memoryStore};
Run Code Online (Sandbox Code Playgroud)
我的受保护路线
router.get('/', keycloak.protect(), async (req:Request, res:Response):Promise<void> => {
var bearerToken: string = await (await keycloak.getGrant(req, res)).toString() as string;
var decoded: any = jwtDecode(bearerToken);
console.log(decoded.resource_access.restapi.roles);
res.send("hello");
});
Run Code Online (Sandbox Code Playgroud)
我是否误解了代币流?
一般来说,会话独立于 JWT。JWT 的优点(和缺点)是,应用程序可以仅使用发行者公钥进行加密验证,该公钥可以是预共享的,也可以是动态查找和缓存的。JWT 是自编码访问令牌的一个示例,这意味着您可以验证它,而无需不断回调中央身份验证/授权服务。这使得它们成为分布式系统和零信任架构中身份验证的理想选择,只要您可以信任发行者公钥,您就可以验证和信任所提供的 JWT。
但这有一个缺点。JWT 包含一个到期时间,在此之前应用程序将认为它有效。正如已经指出的,JWT 的使用者(例如 REST API)不需要与发行服务进行对话来验证它。
因此,在您的特定情况下,您的应用程序会看到 JWT,以加密方式验证它并验证它是否尚未过期,从而接受它。
使用 JWT 的系统的去中心化性质意味着通常不会撤销 JWT 本身。这是因为它需要维护一个撤销列表,并且您必须有一个中央服务来做到这一点。这并不是说这在技术上是不可能的,但这样做首先会失去使用 JWT 的大部分好处。
由于会话的管理独立于 JWT,因此您可以使会话无效,同时为其颁发的 JWT 仍然有效。如果您需要有效的会话,那么您必须独立于基于 JWT 的身份验证来强制执行。
| 归档时间: |
|
| 查看次数: |
2696 次 |
| 最近记录: |