Skip to content

[V5] Socket.io Authentication #2051

Answered by M4gie
M4gie asked this question in Help
Dec 16, 2020 · 4 comments · 8 replies
Discussion options

You must be logged in to vote

I'm using API guard and I managed authentication with this

function urlDecode(encoded) {
  return Buffer.from(encoded, 'base64').toString('utf-8');
}

function generateHash(token) {
  return crypto.createHash('sha256').update(token).digest('hex');
}

function parseToken(token) {
  const parts = token.split('.');
  /**
   * Ensure the token has two parts
   */
  if (parts.length !== 2) {
    throw new Error('E_INVALID_API_TOKEN');
  }

  /**
   * Ensure the first part is a base64 encode id
   */
  const tokenId = urlDecode(parts[0]);

  if (!tokenId) {
    throw new Error('E_INVALID_API_TOKEN');
  }

  const parsedToken = generateHash(parts[1]);
  return {
    token: parsedToken,
    tokenId,

Replies: 4 comments 8 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@mwpb
Comment options

Answer selected by M4gie
Comment options

You must be logged in to vote
4 replies
@tnga
Comment options

@hlozancic
Comment options

@joris-gallot
Comment options

@sn0rcha
Comment options

Comment options

You must be logged in to vote
3 replies
@momar3632
Comment options

@taslangraham
Comment options

@momar3632
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
9 participants