cube.js 认证以及授权
cube.js 的认证以及授权是基于security context
的,以下是参考图
简单说明
cube.js 是基于jwt token 处理的,同时关于用户的信息是在存储在u 中的,同时可以通过USER_CONTEXT
访问
同时authInfo 变量可以在多租户中方便的使用
- 参考u 信息
{
"sub": "1234567890",
"iat": 1516239022,
"u": {
"user_id": 131
}
}
- 引用信息
cube(`Orders`, {
sql: `SELECT * FROM public.orders WHERE ${USER_CONTEXT.user_id.filter('user_id')}`,
measures: {
count: {
type: `count`,
},
},
});
- 自定义auth
const jwt = require('jsonwebtoken');
const fetch = require('node-fetch');
module.exports = {
checkAuth: async (req, auth) => {
// Replace `region` and `userPoolId` with your own
const jwks = await fetch(
'https://cognito-idp.{region}.amazonaws.com/{userPoolId}/.well-known/jwks.json'
).then((r) => r.json());
const decoded = jwt.decode(auth, { complete: true });
const jwk = _.find(jwks.keys, (x) => x.kid === decoded.header.kid);
const pem = jwkToPem(jwk);
req.authInfo = jwt.verify(auth, pem);
},
};
参考资料
https://cube.dev/docs/security
https://cube.dev/docs/config#options-reference-check-auth