Identity verification

Anyone can open the browser console on your app and call identify() with someone else's user id. With identity verification on, Neaten only accepts a user id your server has signed, so nobody can read or change another person's tour progress, or send events in their name.

Turn it on

  1. An Admin opens Settings โ†’ Security and copies the verification secret. Keep it on your server only, in an environment variable such as NEATEN_IDENTITY_SECRET. Never put it in browser code or commit it.
  2. Your server computes userHash = HMAC-SHA256(secret, userId) as lowercase hex (64 characters), over the exact user id string your page passes to identify, and sends it to the page with the signed-in user.
  3. The page passes it to identify.
  4. Deploy that, then switch on "Require identity verification" in Settings โ†’ Security. Switching it on first means Neaten refuses your users' events until the hashes are live.
(window.neaten = window.neaten || []).push([
  "identify",
  String(user.id),
  { plan: user.plan },
  { userHash: user.neatenHash }, // computed on your server
]);

// npm
identify(String(user.id), { plan: user.plan }, { userHash: user.neatenHash });

On your server

Each of these computes the same hash. They read the secret from NEATEN_IDENTITY_SECRET and stop with an error if it isn't set.

Node.js

// Node.js, on your server (never in browser code): the userHash that identify() sends when identity verification is on.
// NEATEN_IDENTITY_SECRET is the verification secret from Settings โ†’ Security, kept in your server's environment.
import { createHmac } from "node:crypto";

/** HMAC-SHA256 of the user id (the exact string you pass to identify), keyed with the secret, as lowercase hex. */
export function neatenUserHash(userId) {
  const secret = process.env.NEATEN_IDENTITY_SECRET;
  if (!secret) throw new Error("NEATEN_IDENTITY_SECRET is not set");
  return createHmac("sha256", secret).update(String(userId), "utf8").digest("hex");
}

Python

import hashlib
import hmac
import os


def neaten_user_hash(user_id: str) -> str:
    """HMAC-SHA256 of the user id (the exact string you pass to identify), keyed with the secret, as lowercase hex."""
    secret = os.environ["NEATEN_IDENTITY_SECRET"].encode("utf-8")
    return hmac.new(secret, str(user_id).encode("utf-8"), hashlib.sha256).hexdigest()

Ruby

require "openssl"

# HMAC-SHA256 of the user id (the exact string you pass to identify), keyed with the secret, as lowercase hex.
def neaten_user_hash(user_id)
  OpenSSL::HMAC.hexdigest("SHA256", ENV.fetch("NEATEN_IDENTITY_SECRET"), user_id.to_s)
end

PHP

<?php
// HMAC-SHA256 of the user id (the exact string you pass to identify), keyed with the secret, as lowercase hex.
function neaten_user_hash(string $userId): string
{
    $secret = getenv('NEATEN_IDENTITY_SECRET');
    if ($secret === false || $secret === '') {
        throw new RuntimeException('NEATEN_IDENTITY_SECRET is not set');
    }
    return hash_hmac('sha256', $userId, $secret);
}

Go

package neaten

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"errors"
	"os"
)

// UserHash is HMAC-SHA256 of the user id (the exact string you pass to identify), keyed with the secret, as
// lowercase hex.
func UserHash(userID string) (string, error) {
	secret := os.Getenv("NEATEN_IDENTITY_SECRET")
	if secret == "" {
		return "", errors.New("NEATEN_IDENTITY_SECRET is not set")
	}
	mac := hmac.New(sha256.New, []byte(secret))
	mac.Write([]byte(userID))
	return hex.EncodeToString(mac.Sum(nil)), nil
}

.NET (C#, .NET 6 or later)

using System;
using System.Security.Cryptography;
using System.Text;

public static class Neaten
{
    // HMAC-SHA256 of the user id (the exact string you pass to identify), keyed with the secret, as lowercase hex.
    public static string UserHash(string userId)
    {
        var secret = Environment.GetEnvironmentVariable("NEATEN_IDENTITY_SECRET")
            ?? throw new InvalidOperationException("NEATEN_IDENTITY_SECRET is not set");
        var hash = HMACSHA256.HashData(Encoding.UTF8.GetBytes(secret), Encoding.UTF8.GetBytes(userId));
        return Convert.ToHexString(hash).ToLowerInvariant();
    }
}

Check your code

With the secret test-secret and the user id user-42, the hash is:

86a7fc2883ace458e6fe1fb45d4672e7a35adc38383b0e1b0870cd6a42d1ddd8

What Neaten does with it