sjudson / paseto.js

PASETO: Platform-Agnostic Security Tokens
MIT License
271 stars 16 forks source link

ReferenceError: cb is not defined #12

Closed joseramongravity closed 5 years ago

joseramongravity commented 5 years ago

Error happens on macOs when trying to generate the token

ReferenceError: cb is not defined
    at encoder.encrypt (/Users/.../server/src/utils.js:556:16)
    at /Users/.../server/node_modules/paseto.js/lib/utils.js:239:39
    at sodium.ready.then (/Users/.../server/node_modules/paseto.js/lib/protocol/V2.js:146:14)

Edit: This happens when i try to use something that is not the callback function Error: Failed to construct 'TextDecoder': the 'fatal' option is unsupported.

sjudson commented 5 years ago

Can you give an example of the code you're using? You need to explicitly set footers to none if using callbacks, so that's my initial suspicion as to the cause.

joseramongravity commented 5 years ago
const signToken = async(input) => {
  const message = input
  const raw = Buffer.from('secret', 'hex')
  let sk = new Paseto.PrivateKey(new Paseto.V2())     // Make a new key with the App Secret
  await sk.inject(raw)
  const encoder = sk.protocol()
  const token = await encoder.encrypt(message, sk, null,(err, token) => {
    if (err) { return cb(err)}
    return token
  })    // Encrypt message

  return token
}

Its like this i think i dont have footers

sjudson commented 5 years ago

I'm unclear. Why are you using both async/await and callbacks? You should be able to just use await encode.encrypt(...) as described here: https://github.com/sjudson/paseto.js#callbacks-promises-and-asyncawait

The cb in if (err) { return cb(err)} is not defined in that snippet.

joseramongravity commented 5 years ago

I have tried the three different ways, but that one used to work like one week ago. But the other methods give me this error: Failed to construct 'TextDecoder': the 'fatal' option is unsupported. This method gives me the cb error:

const signToken = (input) => {
  const message = input
  const raw = Buffer.from('secret', 'hex')
  let sk = new Paseto.PrivateKey(new Paseto.V2())     // Make a new key with the App Secret
  sk.inject(raw)
  const encoder = sk.protocol()
  const token = encoder.encrypt(message, sk, null,(err, token) => {
    if (err) { return cb(err)}
    return token
  })    // Encrypt message

  return token
}

This one the Text Decoder

const signToken = (input) => {
  const message = input
  const raw = Buffer.from('secret', 'hex')
  let sk = new Paseto.PrivateKey(new Paseto.V2())     // Make a new key with the App Secret
  sk.inject(raw)
  const encoder = sk.protocol()
  const token = encoder.encrypt(message, sk).then(token => {
    console.log(token)
  })

  return token
}

This one also gives me the Text Decoder error

const signToken = async(input) => {
  const message = input
  const raw = Buffer.from('secret', 'hex')
  let sk = new Paseto.PrivateKey(new Paseto.V2())     // Make a new key with the App Secret
  await sk.inject(raw)
  const encoder = await sk.protocol()
  const token = await encoder.encrypt(message, sk)
  console.log('token')
  return token
}
sjudson commented 5 years ago

TextDecoder isn't part of this library. Additionally, for

const raw = Buffer.from('secret', 'hex')

'secret' isn't valid hex. When I try and run this last example, I get an invalid key length error...

joseramongravity commented 5 years ago

Okay i will check the TextDecoder btw i dont have secret as the key i just placed it there as an example it is not the real key Thanks

sjudson commented 5 years ago

I'll close for now, you're welcome to reopen if you can provide a reproducible example.