Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Generate the token for Cube.js authentication using spring boot #2098

Closed
ImalshaRathnaweera opened this issue Feb 15, 2021 · 10 comments
Closed
Labels
question The issue is a question. Please use Stack Overflow for questions.

Comments

@ImalshaRathnaweera
Copy link

ImalshaRathnaweera commented Feb 15, 2021

I used the below code to generate the Jwt token in spring boot.

String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, Constants.API_SECRET_KEY)
                .setIssuedAt(new Date(timestamp))
                .setExpiration(new Date(timestamp + Constants.TOKEN_VALIDITY))
                .claim("user_id", user.getUserId())
                .compact();

A token was created, but not retrieve the correct data related to the cubejs scheme. how to make a Jwt token using spring boot for the cubejs authentication.

I generate a Jwt token using node backend.. then, it works for cubejs.

app.get('/auth/cubejs-token', (req, res) => {
    const CUBEJS_API_SECRET = "secret";
    const cubejsToken = jwt.sign({ user_id: 20 }, CUBEJS_API_SECRET, {
        expiresIn: '30d',
    });
    res.json({
        // Take note: cubejs expects the JWT payload to contain an object!
        token: cubejsToken
    });
});

I check node token and spring boot token using jwt.io. both results are equal. when applying the node token retrieve the correct data. but when applying spring boot token not retrieve the correct data.

@vasilev-alex
Copy link
Member

Hello @ImalshaRathnaweera! I can see that the payload you pass in is different.

Java version

userId

Node.js version

{ u: { user_id: 20 } }

May it be the issue?

@vasilev-alex vasilev-alex added the question The issue is a question. Please use Stack Overflow for questions. label Feb 15, 2021
@ImalshaRathnaweera
Copy link
Author

ImalshaRathnaweera commented Feb 15, 2021

@vasilev-alex I have fixed this issue. But not working. I updated my issue

@vasilev-alex
Copy link
Member

So, just to confirm, you don't receive the unauthenticated or any other error from the server?

If so, could you please share the related data schemas?

@ImalshaRathnaweera
Copy link
Author

cube(`Accounts`, {
  sql: `SELECT * FROM bank_schema.accounts WHERE ${SECURITY_CONTEXT.user_id.filter('user_id')}`,
  
  joins: {
    Branch: {
      sql: `${CUBE}.branch_id = ${Branch}.branch_id`,
      relationship: `belongsTo`
    },
    
    Customers: {
      sql: `${CUBE}.customer_id = ${Customers}.customer_id`,
      relationship: `belongsTo`
    },
    
    Sectors: {
      sql: `${CUBE}.sector_id = ${Sectors}.sector_id`,
      relationship: `belongsTo`
    }
  },
  
  measures: {
    count: {
      type: `count`,
      drillMembers: []
    },
  },
  
  dimensions: {
    accountBalance: {
      sql: `account_balance`,
      type: `string`
    },
    
    accountType: {
      sql: `account_type`,
      type: `string`
    },
    
    accountNumber:{
      sql: `account_no`,
      type: `number`,
      primaryKey: true
    }
    
  
  }
});

@ImalshaRathnaweera
Copy link
Author

ImalshaRathnaweera commented Feb 15, 2021

@vasilev-alex
I integrated the spring boot backend with nuxt js front end. then, become isAuthenticate: false. but login becomes successful and the token became store in the localstorage.

@vasilev-alex
Copy link
Member

@ImalshaRathnaweera how do you query the Cube.js API?

@ImalshaRathnaweera
Copy link
Author

const cubejsApi = cubejs(token, {
apiUrl: http://localhost:4000/cubejs-api/v1,
})

@vasilev-alex
Copy link
Member

@ImalshaRathnaweera Could you please generate 2 fresh tokens and share them here? One using Java and another using Node.js

@ImalshaRathnaweera
Copy link
Author

ImalshaRathnaweera commented Feb 16, 2021

@vasilev-alex
Node token
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjozMSwiaWF0IjoxNjEzNDcwOTE2LCJleHAiOjE2MTYwNjI5MTZ9.nYF6QOfjTfb07tav1wQdwbPJvujW88TkxkK9Tmnxj2k

Spring boot token
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MTM0NzEwMjksImV4cCI6MTYxMzQ3ODIyOSwidXNlcl9pZCI6MzF9.dWHHe22mjSKxB4mH7mh84RQSnrb9cSEsEXCRkvNt0d4

@vasilev-alex
Copy link
Member

hi @ImalshaRathnaweera! Looks like this should work for you auth0/node-jsonwebtoken#208 (comment)

The only thing, make sure to use HS256 (the code snippet from above uses HS512)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question The issue is a question. Please use Stack Overflow for questions.
Projects
None yet
Development

No branches or pull requests

2 participants