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

Example typo? #1

Open
m-onz opened this issue Mar 15, 2018 · 2 comments
Open

Example typo? #1

m-onz opened this issue Mar 15, 2018 · 2 comments

Comments

@m-onz
Copy link

m-onz commented Mar 15, 2018

Hello I'm having issues with the example and using the library...

`sessionToken=${secureToken.toString('base64')}`,
// secureToken.toString() returns... [object Object]

do you mean...

`sessionToken=${sessionToken.toString('base64')}`, ?

Would it be possible to add an example.js file (without a http server or anything) just to get a working example to go on?

Here is the code I'm running:

/*

  test for secure tokens...

*/

var secureToken = require ('secure-token')

var db = new Map ()

var session_token = secureToken.create()

//
db.set(secureToken.hash(session_token, 'session'), true)

var new_session_token = secureToken.hash(session_token).toString('base64')

// ---------------------------------------------------------------

var _session_token = Buffer.from(new_session_token, 'base64')
var _hash          = secureToken.hash(_session_token, 'session')

if (!db.get(_hash)) {
  return console.log('ALWAYS UNAUTHORIZED! but i thought this would work... ')
}

console.log('AUTHORIZATION SUCCESSFUL -- doesnt see me. : (')
@emilbayes
Copy link
Owner

emilbayes commented Mar 15, 2018

Ah yes, that is a typo, but I also realise now that there are a couple of other issues with the example, eg. Map checking by reference identity. Here's what a barebones example would look like (pardon the bad var names):

var secureToken = require('secure-token')

var sessionToken = secureToken.create()

// => This conversion to a string is important for our Map database
var saveToDb = secureToken.hash(sessionToken, 'session').toString('base64')
db.set(saveToDb, true)

var sendThisToClient = sessionToken.toString('base64')

// -----------------------------------------

var receivedToken = Buffer.from(sendThisToClient, 'base64')
var checkInDb = secureToken.hash(receivedToken, 'session').toString('base64')

if (db.has(checkInDb)) {
  console.log('Authorized')
} else {
  console.log('Unauthorized')
}

Thanks for reporting this!

@m-onz
Copy link
Author

m-onz commented Mar 15, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants