This library implements split tokens.
A standard way to implement password reset tokens and similar is to generate a random token, send it to your use, usually in a URL, and store it in your database. When the user tries to use the token, you check that the token is in the database.
Split tokens improve upon this design. You still generate a random token and send it to your user, but you store it in the database in two parts called selector and verifier. Selector is stored as-is and verifier is hashed before saving it.
When the user tries to use a token, you split it and use the selecotr to look up the verifier hash from the database. Then you hash the user-supplied verifier and check that it matches the stored hash. This achieves two things:
This basically the same as why you hash users' passwords with a password hashing function before storing them a database, except that we have a single randomly-generated token instead of a username and a password.
Generating a token:
(require '[split-token.core :as split-token])
(split-token/generate)
;; {:selector "gEHOHXOFanTHp43CbFWdCw",
;; :verifier-hash "m0UYbYs2dhbeGHnsjCLY4w",
;; :token "gEHOHXOFanTHp43CbFWdC0yKajTVYk58FpXoCt9FyQY"}
Validating a token:
(let [token "gEHOHXOFanTHp43CbFWdC0yKajTVYk58FpXoCt9FyQY"]
(split-token/get-selector token))
;; "gEHOHXOFanTHp43CbFWdCw"
;; At this point you'd look up the verifier hash from the database based on the selector.
;; Then you can verify it:
(let [token "gEHOHXOFanTHp43CbFWdC0yKajTVYk58FpXoCt9FyQY"
verifier-hash "m0UYbYs2dhbeGHnsjCLY4w"]
(split-token/valid? token verifier-hash))
;; true
Run tests:
bin/kaocha
# Automatically run tests when files change
bin/kaocha --watch
Deployment:
export CLOJARS_USERNAME=...
export CLOJARS_PASSWORD=...
clj -T:build jar
clj -T:build deploy
Copyright 2021 Miikka Koskinen. Distributed under the terms of ISC license, see LICENSE
.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close