PubTrivia sends product update emails to venues who opted in. Those emails need an unsubscribe link that works for a person who is not signed in, who may be reading in a mail client that will never show them a browser, and whose message has already been opened and refetched by two or three security scanners before they see it.
That is four constraints, and they decide the whole design. Here is what we ended up with, and the live endpoints you can poke at while you read.
The token
The authorisation is an HMAC of the user id, and nothing else:
function sign ( userId : string , key : string ) : string { return createHmac ( 'sha256' , key ) . update ( `unsubscribe:${userId}` ) . digest ( 'hex' ) }
Enter fullscreen mode
Exit fullscreen mode
64 hex characters.…