cube.js checkAuth 问题记录

默认cube.js 是基于jwt 进行认证的,当我们没有配置checkAuth 函数的时候,cube.js 的api server 会自动进行jwt 的解码
同时放到securityContext中,但是当我们自己配置了之后就不会自动处理securityContext了

解决方法

自己实现jwt 的解码,同时生成securityContext

  • 参考demo
 
// Cube.js configuration options: https://cube.dev/docs/config
const {MaterializeDriver,MaterializeQuery} = require("@dalongrong/materialize-driver")
const {DremioDriver,DremioQuery} = require("@dalongrong/mydremio-driver")
const MyS3FileRepository = require("@dalongrong/cube-s3repository")
const jwt = require('jsonwebtoken');
 
module.exports = {
    dialectFactory: (dataSource) => {
        // need config  datasource  for multitenant env
        return DremioQuery
    },
    checkAuth: async (req, auth) => {
            // 注意异常处理
           var secret = process.env.CUBEJS_API_SECRET
           console.log("secret:",secret)
           const decoded = jwt.decode(auth, { complete: true });
           console.log(decoded)
           req.securityContext = decoded.payload;
    },
    queryTransformer: (query, { securityContext }) => {
        console.log(securityContext)
        return query;
      },
    dbType: ({ dataSource } = {}) => {
        return "mydremio"
    },
    driverFactory: ({ dataSource } = {}) => {
        return new DremioDriver({})
    },
    repositoryFactory: ({securityContext})=>{
        return new MyS3FileRepository.S3FileRepository({
            objectPrefix:"appdemo/"
        })
    }
};

参考资料

https://cube.dev/docs/reference/environment-variables
https://cube.dev/docs/config#options-reference-check-auth

posted on 2021-04-08 20:21  荣锋亮  阅读(131)  评论(0编辑  收藏  举报

导航