https://github.com/dotcypress/tilde-token
Lightweight secure tokens
https://github.com/dotcypress/tilde-token
Last synced: about 1 month ago
JSON representation
Lightweight secure tokens
- Host: GitHub
- URL: https://github.com/dotcypress/tilde-token
- Owner: dotcypress
- Created: 2018-01-09T17:11:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-11-21T00:00:13.000Z (over 4 years ago)
- Last Synced: 2024-04-28T21:03:54.213Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 14
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# `~ token`
[](https://www.npmjs.com/package/tilde-token)
[](https://www.npmjs.com/package/tilde-token)
[](https://travis-ci.org/dotcypress/tilde-token)
[](http://standardjs.com/)> 🔐 Lightweight secure tokens
## Features
* Lightweight
* Secure
* Blazing fast
* Tamper Resistant
* Load balancer friendly## Installation
`$ npm install tilde-token`
or
`$ yarn add tilde-token`
## Token structure
Example token: `~qlHxEVZjv983RJcqQ/uMEHhdshyp7wp0Mwr/tVyKav3ijQA0XzwUxnnqAAXhgt5DDnQbmPnFxcPssBxgsz4sAgfoo`
```
┌────────────────┬────────────────────────────────┬───────────────┐
│ prefix │ signature │ payload │
├────────────────┼────────────────────────────────┼───────────────┤
│ ~ │ qlHxEVZjv3RJcqQ...xcPssBxz4sAg │ foo │
└────────────────┴────────────────────────────────┴───────────────┘
```* `prefix`(1 byte) - tilde itself;
* `signature`(86 bytes) - payload signature (ed25519, base64-encoded, without padding);
* `payload`(vary) - urlencoded/urlescaped data;## Usage
```js
const {
sign,
signer,
verify,
verifier,
decode,
loadKeyPair,
generateKeyPair
} = require('tilde-token')// Load keypair
const { privateKey, publicKey } = loadKeyPair(`
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIOAzzaE6rikNTr4ZbEz66rsGMxUfTutx2namfDJpmwD1
-----END PRIVATE KEY-----
`)// Generate keypair
const { privateKey, publicKey } = generateKeyPair()// Create token
const token = sign('foo', privateKey)// Decode token without signature verification
const { ok, data, signature } = decode(token)// Verify token
const { ok, data } = verify(token, privateKey)// Sign/Veryfy factories
const signToken = signer(privateKey)
const verifyToken = verifier(privateKey)// Verify token using public key
const { ok, data } = verify(token, publicKey)// Create token
const token = signToken({uid: '42', ssid: 'deadbeef'})// Decode and verify token
const { ok, data } = verifyToken(token)
console.log('result', ok, data)```